blob: 2b4785590b56ce43ff09f62a47708fd5e712dcaf [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Andy Zhou03f0d912013-08-07 20:01:00 -07002 * Copyright (c) 2007-2013 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#include "flow.h"
20#include "datapath.h"
21#include <linux/uaccess.h>
22#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
24#include <linux/if_ether.h>
25#include <linux/if_vlan.h>
26#include <net/llc_pdu.h>
27#include <linux/kernel.h>
28#include <linux/jhash.h>
29#include <linux/jiffies.h>
30#include <linux/llc.h>
31#include <linux/module.h>
32#include <linux/in.h>
33#include <linux/rcupdate.h>
34#include <linux/if_arp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070035#include <linux/ip.h>
36#include <linux/ipv6.h>
Joe Stringera175a722013-08-22 12:30:48 -070037#include <linux/sctp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070038#include <linux/tcp.h>
39#include <linux/udp.h>
40#include <linux/icmp.h>
41#include <linux/icmpv6.h>
42#include <linux/rculist.h>
43#include <net/ip.h>
Pravin B Shelar7d5437c2013-06-17 17:50:18 -070044#include <net/ip_tunnels.h>
Jesse Grossccb13522011-10-25 19:26:31 -070045#include <net/ipv6.h>
46#include <net/ndisc.h>
47
48static struct kmem_cache *flow_cache;
49
Andy Zhou03f0d912013-08-07 20:01:00 -070050static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
51 struct sw_flow_key_range *range, u8 val);
52
53static void update_range__(struct sw_flow_match *match,
54 size_t offset, size_t size, bool is_mask)
55{
56 struct sw_flow_key_range *range = NULL;
57 size_t start = offset;
58 size_t end = offset + size;
59
60 if (!is_mask)
61 range = &match->range;
62 else if (match->mask)
63 range = &match->mask->range;
64
65 if (!range)
66 return;
67
68 if (range->start == range->end) {
69 range->start = start;
70 range->end = end;
71 return;
72 }
73
74 if (range->start > start)
75 range->start = start;
76
77 if (range->end < end)
78 range->end = end;
79}
80
81#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
82 do { \
83 update_range__(match, offsetof(struct sw_flow_key, field), \
84 sizeof((match)->key->field), is_mask); \
85 if (is_mask) { \
86 if ((match)->mask) \
87 (match)->mask->key.field = value; \
88 } else { \
89 (match)->key->field = value; \
90 } \
91 } while (0)
92
93#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
94 do { \
95 update_range__(match, offsetof(struct sw_flow_key, field), \
96 len, is_mask); \
97 if (is_mask) { \
98 if ((match)->mask) \
99 memcpy(&(match)->mask->key.field, value_p, len);\
100 } else { \
101 memcpy(&(match)->key->field, value_p, len); \
102 } \
103 } while (0)
104
105void ovs_match_init(struct sw_flow_match *match,
106 struct sw_flow_key *key,
107 struct sw_flow_mask *mask)
108{
109 memset(match, 0, sizeof(*match));
110 match->key = key;
111 match->mask = mask;
112
113 memset(key, 0, sizeof(*key));
114
115 if (mask) {
116 memset(&mask->key, 0, sizeof(mask->key));
117 mask->range.start = mask->range.end = 0;
118 }
119}
120
121static bool ovs_match_validate(const struct sw_flow_match *match,
122 u64 key_attrs, u64 mask_attrs)
123{
124 u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
125 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
126
127 /* The following mask attributes allowed only if they
128 * pass the validation tests. */
129 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
130 | (1 << OVS_KEY_ATTR_IPV6)
131 | (1 << OVS_KEY_ATTR_TCP)
132 | (1 << OVS_KEY_ATTR_UDP)
Joe Stringera175a722013-08-22 12:30:48 -0700133 | (1 << OVS_KEY_ATTR_SCTP)
Andy Zhou03f0d912013-08-07 20:01:00 -0700134 | (1 << OVS_KEY_ATTR_ICMP)
135 | (1 << OVS_KEY_ATTR_ICMPV6)
136 | (1 << OVS_KEY_ATTR_ARP)
137 | (1 << OVS_KEY_ATTR_ND));
138
139 /* Always allowed mask fields. */
140 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
141 | (1 << OVS_KEY_ATTR_IN_PORT)
142 | (1 << OVS_KEY_ATTR_ETHERTYPE));
143
144 /* Check key attributes. */
145 if (match->key->eth.type == htons(ETH_P_ARP)
146 || match->key->eth.type == htons(ETH_P_RARP)) {
147 key_expected |= 1 << OVS_KEY_ATTR_ARP;
148 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
149 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
150 }
151
152 if (match->key->eth.type == htons(ETH_P_IP)) {
153 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
154 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
155 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
156
157 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
158 if (match->key->ip.proto == IPPROTO_UDP) {
159 key_expected |= 1 << OVS_KEY_ATTR_UDP;
160 if (match->mask && (match->mask->key.ip.proto == 0xff))
161 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
162 }
163
Joe Stringera175a722013-08-22 12:30:48 -0700164 if (match->key->ip.proto == IPPROTO_SCTP) {
165 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
166 if (match->mask && (match->mask->key.ip.proto == 0xff))
167 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
168 }
169
Andy Zhou03f0d912013-08-07 20:01:00 -0700170 if (match->key->ip.proto == IPPROTO_TCP) {
171 key_expected |= 1 << OVS_KEY_ATTR_TCP;
172 if (match->mask && (match->mask->key.ip.proto == 0xff))
173 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
174 }
175
176 if (match->key->ip.proto == IPPROTO_ICMP) {
177 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
178 if (match->mask && (match->mask->key.ip.proto == 0xff))
179 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
180 }
181 }
182 }
183
184 if (match->key->eth.type == htons(ETH_P_IPV6)) {
185 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
186 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
187 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
188
189 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
190 if (match->key->ip.proto == IPPROTO_UDP) {
191 key_expected |= 1 << OVS_KEY_ATTR_UDP;
192 if (match->mask && (match->mask->key.ip.proto == 0xff))
193 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
194 }
195
Joe Stringera175a722013-08-22 12:30:48 -0700196 if (match->key->ip.proto == IPPROTO_SCTP) {
197 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
198 if (match->mask && (match->mask->key.ip.proto == 0xff))
199 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
200 }
201
Andy Zhou03f0d912013-08-07 20:01:00 -0700202 if (match->key->ip.proto == IPPROTO_TCP) {
203 key_expected |= 1 << OVS_KEY_ATTR_TCP;
204 if (match->mask && (match->mask->key.ip.proto == 0xff))
205 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
206 }
207
208 if (match->key->ip.proto == IPPROTO_ICMPV6) {
209 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
210 if (match->mask && (match->mask->key.ip.proto == 0xff))
211 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
212
213 if (match->key->ipv6.tp.src ==
214 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
215 match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
216 key_expected |= 1 << OVS_KEY_ATTR_ND;
217 if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
218 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
219 }
220 }
221 }
222 }
223
224 if ((key_attrs & key_expected) != key_expected) {
225 /* Key attributes check failed. */
226 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
227 key_attrs, key_expected);
228 return false;
229 }
230
231 if ((mask_attrs & mask_allowed) != mask_attrs) {
232 /* Mask attributes check failed. */
233 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
234 mask_attrs, mask_allowed);
235 return false;
236 }
237
238 return true;
239}
240
Jesse Grossccb13522011-10-25 19:26:31 -0700241static int check_header(struct sk_buff *skb, int len)
242{
243 if (unlikely(skb->len < len))
244 return -EINVAL;
245 if (unlikely(!pskb_may_pull(skb, len)))
246 return -ENOMEM;
247 return 0;
248}
249
250static bool arphdr_ok(struct sk_buff *skb)
251{
252 return pskb_may_pull(skb, skb_network_offset(skb) +
253 sizeof(struct arp_eth_header));
254}
255
256static int check_iphdr(struct sk_buff *skb)
257{
258 unsigned int nh_ofs = skb_network_offset(skb);
259 unsigned int ip_len;
260 int err;
261
262 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
263 if (unlikely(err))
264 return err;
265
266 ip_len = ip_hdrlen(skb);
267 if (unlikely(ip_len < sizeof(struct iphdr) ||
268 skb->len < nh_ofs + ip_len))
269 return -EINVAL;
270
271 skb_set_transport_header(skb, nh_ofs + ip_len);
272 return 0;
273}
274
275static bool tcphdr_ok(struct sk_buff *skb)
276{
277 int th_ofs = skb_transport_offset(skb);
278 int tcp_len;
279
280 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
281 return false;
282
283 tcp_len = tcp_hdrlen(skb);
284 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
285 skb->len < th_ofs + tcp_len))
286 return false;
287
288 return true;
289}
290
291static bool udphdr_ok(struct sk_buff *skb)
292{
293 return pskb_may_pull(skb, skb_transport_offset(skb) +
294 sizeof(struct udphdr));
295}
296
Joe Stringera175a722013-08-22 12:30:48 -0700297static bool sctphdr_ok(struct sk_buff *skb)
298{
299 return pskb_may_pull(skb, skb_transport_offset(skb) +
300 sizeof(struct sctphdr));
301}
302
Jesse Grossccb13522011-10-25 19:26:31 -0700303static bool icmphdr_ok(struct sk_buff *skb)
304{
305 return pskb_may_pull(skb, skb_transport_offset(skb) +
306 sizeof(struct icmphdr));
307}
308
309u64 ovs_flow_used_time(unsigned long flow_jiffies)
310{
311 struct timespec cur_ts;
312 u64 cur_ms, idle_ms;
313
314 ktime_get_ts(&cur_ts);
315 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
316 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
317 cur_ts.tv_nsec / NSEC_PER_MSEC;
318
319 return cur_ms - idle_ms;
320}
321
Andy Zhou03f0d912013-08-07 20:01:00 -0700322static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700323{
324 unsigned int nh_ofs = skb_network_offset(skb);
325 unsigned int nh_len;
326 int payload_ofs;
327 struct ipv6hdr *nh;
328 uint8_t nexthdr;
329 __be16 frag_off;
330 int err;
331
Jesse Grossccb13522011-10-25 19:26:31 -0700332 err = check_header(skb, nh_ofs + sizeof(*nh));
333 if (unlikely(err))
334 return err;
335
336 nh = ipv6_hdr(skb);
337 nexthdr = nh->nexthdr;
338 payload_ofs = (u8 *)(nh + 1) - skb->data;
339
340 key->ip.proto = NEXTHDR_NONE;
341 key->ip.tos = ipv6_get_dsfield(nh);
342 key->ip.ttl = nh->hop_limit;
343 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
344 key->ipv6.addr.src = nh->saddr;
345 key->ipv6.addr.dst = nh->daddr;
346
347 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
348 if (unlikely(payload_ofs < 0))
349 return -EINVAL;
350
351 if (frag_off) {
352 if (frag_off & htons(~0x7))
353 key->ip.frag = OVS_FRAG_TYPE_LATER;
354 else
355 key->ip.frag = OVS_FRAG_TYPE_FIRST;
356 }
357
358 nh_len = payload_ofs - nh_ofs;
359 skb_set_transport_header(skb, nh_ofs + nh_len);
360 key->ip.proto = nexthdr;
361 return nh_len;
362}
363
364static bool icmp6hdr_ok(struct sk_buff *skb)
365{
366 return pskb_may_pull(skb, skb_transport_offset(skb) +
367 sizeof(struct icmp6hdr));
368}
369
Andy Zhou03f0d912013-08-07 20:01:00 -0700370void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src,
371 const struct sw_flow_mask *mask)
372{
373 u8 *m = (u8 *)&mask->key + mask->range.start;
374 u8 *s = (u8 *)src + mask->range.start;
375 u8 *d = (u8 *)dst + mask->range.start;
376 int i;
377
378 memset(dst, 0, sizeof(*dst));
379 for (i = 0; i < ovs_sw_flow_mask_size_roundup(mask); i++) {
380 *d = *s & *m;
381 d++, s++, m++;
382 }
383}
384
Jesse Grossccb13522011-10-25 19:26:31 -0700385#define TCP_FLAGS_OFFSET 13
386#define TCP_FLAG_MASK 0x3f
387
388void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
389{
390 u8 tcp_flags = 0;
391
Jesse Grossc55177e2012-04-02 15:13:36 -0700392 if ((flow->key.eth.type == htons(ETH_P_IP) ||
393 flow->key.eth.type == htons(ETH_P_IPV6)) &&
Jesse Grossbf32fec2012-04-02 14:26:27 -0700394 flow->key.ip.proto == IPPROTO_TCP &&
395 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
Jesse Grossccb13522011-10-25 19:26:31 -0700396 u8 *tcp = (u8 *)tcp_hdr(skb);
397 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
398 }
399
400 spin_lock(&flow->lock);
401 flow->used = jiffies;
402 flow->packet_count++;
403 flow->byte_count += skb->len;
404 flow->tcp_flags |= tcp_flags;
405 spin_unlock(&flow->lock);
406}
407
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700408struct sw_flow_actions *ovs_flow_actions_alloc(int size)
Jesse Grossccb13522011-10-25 19:26:31 -0700409{
Jesse Grossccb13522011-10-25 19:26:31 -0700410 struct sw_flow_actions *sfa;
411
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700412 if (size > MAX_ACTIONS_BUFSIZE)
Jesse Grossccb13522011-10-25 19:26:31 -0700413 return ERR_PTR(-EINVAL);
414
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700415 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -0700416 if (!sfa)
417 return ERR_PTR(-ENOMEM);
418
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700419 sfa->actions_len = 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700420 return sfa;
421}
422
423struct sw_flow *ovs_flow_alloc(void)
424{
425 struct sw_flow *flow;
426
427 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
428 if (!flow)
429 return ERR_PTR(-ENOMEM);
430
431 spin_lock_init(&flow->lock);
432 flow->sf_acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700433 flow->mask = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700434
435 return flow;
436}
437
438static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
439{
440 hash = jhash_1word(hash, table->hash_seed);
441 return flex_array_get(table->buckets,
442 (hash & (table->n_buckets - 1)));
443}
444
445static struct flex_array *alloc_buckets(unsigned int n_buckets)
446{
447 struct flex_array *buckets;
448 int i, err;
449
Pravin B Shelar42415c92013-07-30 15:44:14 -0700450 buckets = flex_array_alloc(sizeof(struct hlist_head),
Jesse Grossccb13522011-10-25 19:26:31 -0700451 n_buckets, GFP_KERNEL);
452 if (!buckets)
453 return NULL;
454
455 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
456 if (err) {
457 flex_array_free(buckets);
458 return NULL;
459 }
460
461 for (i = 0; i < n_buckets; i++)
462 INIT_HLIST_HEAD((struct hlist_head *)
463 flex_array_get(buckets, i));
464
465 return buckets;
466}
467
468static void free_buckets(struct flex_array *buckets)
469{
470 flex_array_free(buckets);
471}
472
Andy Zhou03f0d912013-08-07 20:01:00 -0700473static struct flow_table *__flow_tbl_alloc(int new_size)
Jesse Grossccb13522011-10-25 19:26:31 -0700474{
475 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
476
477 if (!table)
478 return NULL;
479
480 table->buckets = alloc_buckets(new_size);
481
482 if (!table->buckets) {
483 kfree(table);
484 return NULL;
485 }
486 table->n_buckets = new_size;
487 table->count = 0;
488 table->node_ver = 0;
489 table->keep_flows = false;
490 get_random_bytes(&table->hash_seed, sizeof(u32));
Andy Zhou03f0d912013-08-07 20:01:00 -0700491 table->mask_list = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700492
493 return table;
494}
495
Andy Zhou03f0d912013-08-07 20:01:00 -0700496static void __flow_tbl_destroy(struct flow_table *table)
Jesse Grossccb13522011-10-25 19:26:31 -0700497{
498 int i;
499
Jesse Grossccb13522011-10-25 19:26:31 -0700500 if (table->keep_flows)
501 goto skip_flows;
502
503 for (i = 0; i < table->n_buckets; i++) {
504 struct sw_flow *flow;
505 struct hlist_head *head = flex_array_get(table->buckets, i);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800506 struct hlist_node *n;
Jesse Grossccb13522011-10-25 19:26:31 -0700507 int ver = table->node_ver;
508
Sasha Levinb67bfe02013-02-27 17:06:00 -0800509 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
Pravin B Shelar76a66c72013-07-30 15:45:59 -0700510 hlist_del(&flow->hash_node[ver]);
Andy Zhou03f0d912013-08-07 20:01:00 -0700511 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700512 }
513 }
514
Andy Zhou03f0d912013-08-07 20:01:00 -0700515 BUG_ON(!list_empty(table->mask_list));
516 kfree(table->mask_list);
517
Jesse Grossccb13522011-10-25 19:26:31 -0700518skip_flows:
519 free_buckets(table->buckets);
520 kfree(table);
521}
522
Andy Zhou03f0d912013-08-07 20:01:00 -0700523struct flow_table *ovs_flow_tbl_alloc(int new_size)
524{
525 struct flow_table *table = __flow_tbl_alloc(new_size);
526
527 if (!table)
528 return NULL;
529
530 table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
531 if (!table->mask_list) {
532 table->keep_flows = true;
533 __flow_tbl_destroy(table);
534 return NULL;
535 }
536 INIT_LIST_HEAD(table->mask_list);
537
538 return table;
539}
540
Jesse Grossccb13522011-10-25 19:26:31 -0700541static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
542{
543 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
544
Andy Zhou03f0d912013-08-07 20:01:00 -0700545 __flow_tbl_destroy(table);
Jesse Grossccb13522011-10-25 19:26:31 -0700546}
547
Andy Zhou03f0d912013-08-07 20:01:00 -0700548void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
Jesse Grossccb13522011-10-25 19:26:31 -0700549{
550 if (!table)
551 return;
552
Andy Zhou03f0d912013-08-07 20:01:00 -0700553 if (deferred)
554 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
555 else
556 __flow_tbl_destroy(table);
Jesse Grossccb13522011-10-25 19:26:31 -0700557}
558
Andy Zhou03f0d912013-08-07 20:01:00 -0700559struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last)
Jesse Grossccb13522011-10-25 19:26:31 -0700560{
561 struct sw_flow *flow;
562 struct hlist_head *head;
Jesse Grossccb13522011-10-25 19:26:31 -0700563 int ver;
564 int i;
565
566 ver = table->node_ver;
567 while (*bucket < table->n_buckets) {
568 i = 0;
569 head = flex_array_get(table->buckets, *bucket);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800570 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
Jesse Grossccb13522011-10-25 19:26:31 -0700571 if (i < *last) {
572 i++;
573 continue;
574 }
575 *last = i + 1;
576 return flow;
577 }
578 (*bucket)++;
579 *last = 0;
580 }
581
582 return NULL;
583}
584
Andy Zhou03f0d912013-08-07 20:01:00 -0700585static void __tbl_insert(struct flow_table *table, struct sw_flow *flow)
Pravin B Shelara3e82992013-06-17 17:50:28 -0700586{
587 struct hlist_head *head;
Andy Zhou03f0d912013-08-07 20:01:00 -0700588
Pravin B Shelara3e82992013-06-17 17:50:28 -0700589 head = find_bucket(table, flow->hash);
590 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
Andy Zhou03f0d912013-08-07 20:01:00 -0700591
Pravin B Shelara3e82992013-06-17 17:50:28 -0700592 table->count++;
593}
594
Jesse Grossccb13522011-10-25 19:26:31 -0700595static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
596{
597 int old_ver;
598 int i;
599
600 old_ver = old->node_ver;
601 new->node_ver = !old_ver;
602
603 /* Insert in new table. */
604 for (i = 0; i < old->n_buckets; i++) {
605 struct sw_flow *flow;
606 struct hlist_head *head;
Jesse Grossccb13522011-10-25 19:26:31 -0700607
608 head = flex_array_get(old->buckets, i);
609
Sasha Levinb67bfe02013-02-27 17:06:00 -0800610 hlist_for_each_entry(flow, head, hash_node[old_ver])
Andy Zhou03f0d912013-08-07 20:01:00 -0700611 __tbl_insert(new, flow);
Jesse Grossccb13522011-10-25 19:26:31 -0700612 }
Andy Zhou03f0d912013-08-07 20:01:00 -0700613
614 new->mask_list = old->mask_list;
Jesse Grossccb13522011-10-25 19:26:31 -0700615 old->keep_flows = true;
616}
617
618static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
619{
620 struct flow_table *new_table;
621
Andy Zhou03f0d912013-08-07 20:01:00 -0700622 new_table = __flow_tbl_alloc(n_buckets);
Jesse Grossccb13522011-10-25 19:26:31 -0700623 if (!new_table)
624 return ERR_PTR(-ENOMEM);
625
626 flow_table_copy_flows(table, new_table);
627
628 return new_table;
629}
630
631struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
632{
633 return __flow_tbl_rehash(table, table->n_buckets);
634}
635
636struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
637{
638 return __flow_tbl_rehash(table, table->n_buckets * 2);
639}
640
Andy Zhou03f0d912013-08-07 20:01:00 -0700641static void __flow_free(struct sw_flow *flow)
Jesse Grossccb13522011-10-25 19:26:31 -0700642{
Jesse Grossccb13522011-10-25 19:26:31 -0700643 kfree((struct sf_flow_acts __force *)flow->sf_acts);
644 kmem_cache_free(flow_cache, flow);
645}
646
Jesse Grossccb13522011-10-25 19:26:31 -0700647static void rcu_free_flow_callback(struct rcu_head *rcu)
648{
649 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
650
Andy Zhou03f0d912013-08-07 20:01:00 -0700651 __flow_free(flow);
Jesse Grossccb13522011-10-25 19:26:31 -0700652}
653
Andy Zhou03f0d912013-08-07 20:01:00 -0700654void ovs_flow_free(struct sw_flow *flow, bool deferred)
Jesse Grossccb13522011-10-25 19:26:31 -0700655{
Andy Zhou03f0d912013-08-07 20:01:00 -0700656 if (!flow)
657 return;
658
659 ovs_sw_flow_mask_del_ref(flow->mask, deferred);
660
661 if (deferred)
662 call_rcu(&flow->rcu, rcu_free_flow_callback);
663 else
664 __flow_free(flow);
Jesse Grossccb13522011-10-25 19:26:31 -0700665}
666
Jesse Grossccb13522011-10-25 19:26:31 -0700667/* Schedules 'sf_acts' to be freed after the next RCU grace period.
668 * The caller must hold rcu_read_lock for this to be sensible. */
669void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
670{
Wei Yongjun80f0fd82012-08-26 18:20:45 +0000671 kfree_rcu(sf_acts, rcu);
Jesse Grossccb13522011-10-25 19:26:31 -0700672}
673
674static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
675{
676 struct qtag_prefix {
677 __be16 eth_type; /* ETH_P_8021Q */
678 __be16 tci;
679 };
680 struct qtag_prefix *qp;
681
682 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
683 return 0;
684
685 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
686 sizeof(__be16))))
687 return -ENOMEM;
688
689 qp = (struct qtag_prefix *) skb->data;
690 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
691 __skb_pull(skb, sizeof(struct qtag_prefix));
692
693 return 0;
694}
695
696static __be16 parse_ethertype(struct sk_buff *skb)
697{
698 struct llc_snap_hdr {
699 u8 dsap; /* Always 0xAA */
700 u8 ssap; /* Always 0xAA */
701 u8 ctrl;
702 u8 oui[3];
703 __be16 ethertype;
704 };
705 struct llc_snap_hdr *llc;
706 __be16 proto;
707
708 proto = *(__be16 *) skb->data;
709 __skb_pull(skb, sizeof(__be16));
710
Simon Hormane5c5d222013-03-28 13:38:25 +0900711 if (ntohs(proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700712 return proto;
713
714 if (skb->len < sizeof(struct llc_snap_hdr))
715 return htons(ETH_P_802_2);
716
717 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
718 return htons(0);
719
720 llc = (struct llc_snap_hdr *) skb->data;
721 if (llc->dsap != LLC_SAP_SNAP ||
722 llc->ssap != LLC_SAP_SNAP ||
723 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
724 return htons(ETH_P_802_2);
725
726 __skb_pull(skb, sizeof(struct llc_snap_hdr));
Rich Lane17b682a2013-02-19 11:10:30 -0800727
Simon Hormane5c5d222013-03-28 13:38:25 +0900728 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
Rich Lane17b682a2013-02-19 11:10:30 -0800729 return llc->ethertype;
730
731 return htons(ETH_P_802_2);
Jesse Grossccb13522011-10-25 19:26:31 -0700732}
733
734static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
Andy Zhou03f0d912013-08-07 20:01:00 -0700735 int nh_len)
Jesse Grossccb13522011-10-25 19:26:31 -0700736{
737 struct icmp6hdr *icmp = icmp6_hdr(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700738
739 /* The ICMPv6 type and code fields use the 16-bit transport port
740 * fields, so we need to store them in 16-bit network byte order.
741 */
742 key->ipv6.tp.src = htons(icmp->icmp6_type);
743 key->ipv6.tp.dst = htons(icmp->icmp6_code);
Jesse Grossccb13522011-10-25 19:26:31 -0700744
745 if (icmp->icmp6_code == 0 &&
746 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
747 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
748 int icmp_len = skb->len - skb_transport_offset(skb);
749 struct nd_msg *nd;
750 int offset;
751
Jesse Grossccb13522011-10-25 19:26:31 -0700752 /* In order to process neighbor discovery options, we need the
753 * entire packet.
754 */
755 if (unlikely(icmp_len < sizeof(*nd)))
Andy Zhou03f0d912013-08-07 20:01:00 -0700756 return 0;
757
758 if (unlikely(skb_linearize(skb)))
759 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -0700760
761 nd = (struct nd_msg *)skb_transport_header(skb);
762 key->ipv6.nd.target = nd->target;
Jesse Grossccb13522011-10-25 19:26:31 -0700763
764 icmp_len -= sizeof(*nd);
765 offset = 0;
766 while (icmp_len >= 8) {
767 struct nd_opt_hdr *nd_opt =
768 (struct nd_opt_hdr *)(nd->opt + offset);
769 int opt_len = nd_opt->nd_opt_len * 8;
770
771 if (unlikely(!opt_len || opt_len > icmp_len))
Andy Zhou03f0d912013-08-07 20:01:00 -0700772 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700773
774 /* Store the link layer address if the appropriate
775 * option is provided. It is considered an error if
776 * the same link layer option is specified twice.
777 */
778 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
779 && opt_len == 8) {
780 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
781 goto invalid;
782 memcpy(key->ipv6.nd.sll,
783 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
784 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
785 && opt_len == 8) {
786 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
787 goto invalid;
788 memcpy(key->ipv6.nd.tll,
789 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
790 }
791
792 icmp_len -= opt_len;
793 offset += opt_len;
794 }
795 }
796
Andy Zhou03f0d912013-08-07 20:01:00 -0700797 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700798
799invalid:
800 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
801 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
802 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
803
Andy Zhou03f0d912013-08-07 20:01:00 -0700804 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700805}
806
807/**
808 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
809 * @skb: sk_buff that contains the frame, with skb->data pointing to the
810 * Ethernet header
811 * @in_port: port number on which @skb was received.
812 * @key: output flow key
Jesse Grossccb13522011-10-25 19:26:31 -0700813 *
814 * The caller must ensure that skb->len >= ETH_HLEN.
815 *
816 * Returns 0 if successful, otherwise a negative errno value.
817 *
818 * Initializes @skb header pointers as follows:
819 *
820 * - skb->mac_header: the Ethernet header.
821 *
822 * - skb->network_header: just past the Ethernet header, or just past the
823 * VLAN header, to the first byte of the Ethernet payload.
824 *
Lorand Jakab34d94f22013-06-03 10:01:14 -0700825 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
Jesse Grossccb13522011-10-25 19:26:31 -0700826 * on output, then just past the IP header, if one is present and
827 * of a correct length, otherwise the same as skb->network_header.
Lorand Jakab34d94f22013-06-03 10:01:14 -0700828 * For other key->eth.type values it is left untouched.
Jesse Grossccb13522011-10-25 19:26:31 -0700829 */
Andy Zhou03f0d912013-08-07 20:01:00 -0700830int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700831{
Andy Zhou03f0d912013-08-07 20:01:00 -0700832 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700833 struct ethhdr *eth;
834
835 memset(key, 0, sizeof(*key));
836
837 key->phy.priority = skb->priority;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700838 if (OVS_CB(skb)->tun_key)
839 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
Jesse Grossccb13522011-10-25 19:26:31 -0700840 key->phy.in_port = in_port;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800841 key->phy.skb_mark = skb->mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700842
843 skb_reset_mac_header(skb);
844
845 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
846 * header in the linear data area.
847 */
848 eth = eth_hdr(skb);
849 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
850 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
851
852 __skb_pull(skb, 2 * ETH_ALEN);
Pravin B Shelarb34df5e2013-06-13 11:11:44 -0700853 /* We are going to push all headers that we pull, so no need to
854 * update skb->csum here.
855 */
Jesse Grossccb13522011-10-25 19:26:31 -0700856
857 if (vlan_tx_tag_present(skb))
858 key->eth.tci = htons(skb->vlan_tci);
859 else if (eth->h_proto == htons(ETH_P_8021Q))
860 if (unlikely(parse_vlan(skb, key)))
861 return -ENOMEM;
862
863 key->eth.type = parse_ethertype(skb);
864 if (unlikely(key->eth.type == htons(0)))
865 return -ENOMEM;
866
867 skb_reset_network_header(skb);
868 __skb_push(skb, skb->data - skb_mac_header(skb));
869
870 /* Network layer. */
871 if (key->eth.type == htons(ETH_P_IP)) {
872 struct iphdr *nh;
873 __be16 offset;
874
Jesse Grossccb13522011-10-25 19:26:31 -0700875 error = check_iphdr(skb);
876 if (unlikely(error)) {
877 if (error == -EINVAL) {
878 skb->transport_header = skb->network_header;
879 error = 0;
880 }
Andy Zhou03f0d912013-08-07 20:01:00 -0700881 return error;
Jesse Grossccb13522011-10-25 19:26:31 -0700882 }
883
884 nh = ip_hdr(skb);
885 key->ipv4.addr.src = nh->saddr;
886 key->ipv4.addr.dst = nh->daddr;
887
888 key->ip.proto = nh->protocol;
889 key->ip.tos = nh->tos;
890 key->ip.ttl = nh->ttl;
891
892 offset = nh->frag_off & htons(IP_OFFSET);
893 if (offset) {
894 key->ip.frag = OVS_FRAG_TYPE_LATER;
Andy Zhou03f0d912013-08-07 20:01:00 -0700895 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700896 }
897 if (nh->frag_off & htons(IP_MF) ||
898 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
899 key->ip.frag = OVS_FRAG_TYPE_FIRST;
900
901 /* Transport layer. */
902 if (key->ip.proto == IPPROTO_TCP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700903 if (tcphdr_ok(skb)) {
904 struct tcphdr *tcp = tcp_hdr(skb);
905 key->ipv4.tp.src = tcp->source;
906 key->ipv4.tp.dst = tcp->dest;
907 }
908 } else if (key->ip.proto == IPPROTO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700909 if (udphdr_ok(skb)) {
910 struct udphdr *udp = udp_hdr(skb);
911 key->ipv4.tp.src = udp->source;
912 key->ipv4.tp.dst = udp->dest;
913 }
Joe Stringera175a722013-08-22 12:30:48 -0700914 } else if (key->ip.proto == IPPROTO_SCTP) {
915 if (sctphdr_ok(skb)) {
916 struct sctphdr *sctp = sctp_hdr(skb);
917 key->ipv4.tp.src = sctp->source;
918 key->ipv4.tp.dst = sctp->dest;
919 }
Jesse Grossccb13522011-10-25 19:26:31 -0700920 } else if (key->ip.proto == IPPROTO_ICMP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700921 if (icmphdr_ok(skb)) {
922 struct icmphdr *icmp = icmp_hdr(skb);
923 /* The ICMP type and code fields use the 16-bit
924 * transport port fields, so we need to store
925 * them in 16-bit network byte order. */
926 key->ipv4.tp.src = htons(icmp->type);
927 key->ipv4.tp.dst = htons(icmp->code);
928 }
929 }
930
Mehak Mahajanc0618532012-11-02 14:14:31 -0700931 } else if ((key->eth.type == htons(ETH_P_ARP) ||
932 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
Jesse Grossccb13522011-10-25 19:26:31 -0700933 struct arp_eth_header *arp;
934
935 arp = (struct arp_eth_header *)skb_network_header(skb);
936
937 if (arp->ar_hrd == htons(ARPHRD_ETHER)
938 && arp->ar_pro == htons(ETH_P_IP)
939 && arp->ar_hln == ETH_ALEN
940 && arp->ar_pln == 4) {
941
942 /* We only match on the lower 8 bits of the opcode. */
943 if (ntohs(arp->ar_op) <= 0xff)
944 key->ip.proto = ntohs(arp->ar_op);
Mehak Mahajand04d3822012-10-30 15:50:28 -0700945 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
946 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
947 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
948 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
Jesse Grossccb13522011-10-25 19:26:31 -0700949 }
950 } else if (key->eth.type == htons(ETH_P_IPV6)) {
951 int nh_len; /* IPv6 Header + Extensions */
952
Andy Zhou03f0d912013-08-07 20:01:00 -0700953 nh_len = parse_ipv6hdr(skb, key);
Jesse Grossccb13522011-10-25 19:26:31 -0700954 if (unlikely(nh_len < 0)) {
Andy Zhou03f0d912013-08-07 20:01:00 -0700955 if (nh_len == -EINVAL) {
Jesse Grossccb13522011-10-25 19:26:31 -0700956 skb->transport_header = skb->network_header;
Andy Zhou03f0d912013-08-07 20:01:00 -0700957 error = 0;
958 } else {
Jesse Grossccb13522011-10-25 19:26:31 -0700959 error = nh_len;
Andy Zhou03f0d912013-08-07 20:01:00 -0700960 }
961 return error;
Jesse Grossccb13522011-10-25 19:26:31 -0700962 }
963
964 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
Andy Zhou03f0d912013-08-07 20:01:00 -0700965 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700966 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
967 key->ip.frag = OVS_FRAG_TYPE_FIRST;
968
969 /* Transport layer. */
970 if (key->ip.proto == NEXTHDR_TCP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700971 if (tcphdr_ok(skb)) {
972 struct tcphdr *tcp = tcp_hdr(skb);
973 key->ipv6.tp.src = tcp->source;
974 key->ipv6.tp.dst = tcp->dest;
975 }
976 } else if (key->ip.proto == NEXTHDR_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700977 if (udphdr_ok(skb)) {
978 struct udphdr *udp = udp_hdr(skb);
979 key->ipv6.tp.src = udp->source;
980 key->ipv6.tp.dst = udp->dest;
981 }
Joe Stringera175a722013-08-22 12:30:48 -0700982 } else if (key->ip.proto == NEXTHDR_SCTP) {
983 if (sctphdr_ok(skb)) {
984 struct sctphdr *sctp = sctp_hdr(skb);
985 key->ipv6.tp.src = sctp->source;
986 key->ipv6.tp.dst = sctp->dest;
987 }
Jesse Grossccb13522011-10-25 19:26:31 -0700988 } else if (key->ip.proto == NEXTHDR_ICMP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700989 if (icmp6hdr_ok(skb)) {
Andy Zhou03f0d912013-08-07 20:01:00 -0700990 error = parse_icmpv6(skb, key, nh_len);
991 if (error)
992 return error;
Jesse Grossccb13522011-10-25 19:26:31 -0700993 }
994 }
995 }
996
Andy Zhou03f0d912013-08-07 20:01:00 -0700997 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700998}
999
Pravin B Shelara3e82992013-06-17 17:50:28 -07001000static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start, int key_len)
Jesse Grossccb13522011-10-25 19:26:31 -07001001{
Pravin B Shelara3e82992013-06-17 17:50:28 -07001002 return jhash2((u32 *)((u8 *)key + key_start),
1003 DIV_ROUND_UP(key_len - key_start, sizeof(u32)), 0);
1004}
1005
Andy Zhou03f0d912013-08-07 20:01:00 -07001006static int flow_key_start(const struct sw_flow_key *key)
Pravin B Shelara3e82992013-06-17 17:50:28 -07001007{
1008 if (key->tun_key.ipv4_dst)
1009 return 0;
1010 else
1011 return offsetof(struct sw_flow_key, phy);
Jesse Grossccb13522011-10-25 19:26:31 -07001012}
1013
Andy Zhou03f0d912013-08-07 20:01:00 -07001014static bool __cmp_key(const struct sw_flow_key *key1,
1015 const struct sw_flow_key *key2, int key_start, int key_len)
1016{
1017 return !memcmp((u8 *)key1 + key_start,
1018 (u8 *)key2 + key_start, (key_len - key_start));
1019}
1020
1021static bool __flow_cmp_key(const struct sw_flow *flow,
1022 const struct sw_flow_key *key, int key_start, int key_len)
1023{
1024 return __cmp_key(&flow->key, key, key_start, key_len);
1025}
1026
1027static bool __flow_cmp_unmasked_key(const struct sw_flow *flow,
1028 const struct sw_flow_key *key, int key_start, int key_len)
1029{
1030 return __cmp_key(&flow->unmasked_key, key, key_start, key_len);
1031}
1032
1033bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
1034 const struct sw_flow_key *key, int key_len)
1035{
1036 int key_start;
1037 key_start = flow_key_start(key);
1038
1039 return __flow_cmp_unmasked_key(flow, key, key_start, key_len);
1040
1041}
1042
1043struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
1044 struct sw_flow_match *match)
1045{
1046 struct sw_flow_key *unmasked = match->key;
1047 int key_len = match->range.end;
1048 struct sw_flow *flow;
1049
1050 flow = ovs_flow_lookup(table, unmasked);
1051 if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_len)))
1052 flow = NULL;
1053
1054 return flow;
1055}
1056
1057static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
1058 const struct sw_flow_key *flow_key,
1059 struct sw_flow_mask *mask)
Jesse Grossccb13522011-10-25 19:26:31 -07001060{
1061 struct sw_flow *flow;
Jesse Grossccb13522011-10-25 19:26:31 -07001062 struct hlist_head *head;
Andy Zhou03f0d912013-08-07 20:01:00 -07001063 int key_start = mask->range.start;
1064 int key_len = mask->range.end;
Jesse Grossccb13522011-10-25 19:26:31 -07001065 u32 hash;
Andy Zhou03f0d912013-08-07 20:01:00 -07001066 struct sw_flow_key masked_key;
Jesse Grossccb13522011-10-25 19:26:31 -07001067
Andy Zhou03f0d912013-08-07 20:01:00 -07001068 ovs_flow_key_mask(&masked_key, flow_key, mask);
1069 hash = ovs_flow_hash(&masked_key, key_start, key_len);
Jesse Grossccb13522011-10-25 19:26:31 -07001070 head = find_bucket(table, hash);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001071 hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
Andy Zhou03f0d912013-08-07 20:01:00 -07001072 if (flow->mask == mask &&
1073 __flow_cmp_key(flow, &masked_key, key_start, key_len))
Jesse Grossccb13522011-10-25 19:26:31 -07001074 return flow;
Jesse Grossccb13522011-10-25 19:26:31 -07001075 }
1076 return NULL;
1077}
1078
Andy Zhou03f0d912013-08-07 20:01:00 -07001079struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
1080 const struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -07001081{
Andy Zhou03f0d912013-08-07 20:01:00 -07001082 struct sw_flow *flow = NULL;
1083 struct sw_flow_mask *mask;
1084
1085 list_for_each_entry_rcu(mask, tbl->mask_list, list) {
1086 flow = ovs_masked_flow_lookup(tbl, key, mask);
1087 if (flow) /* Found */
1088 break;
1089 }
1090
1091 return flow;
Jesse Grossccb13522011-10-25 19:26:31 -07001092}
1093
Andy Zhou03f0d912013-08-07 20:01:00 -07001094
1095void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow)
1096{
1097 flow->hash = ovs_flow_hash(&flow->key, flow->mask->range.start,
1098 flow->mask->range.end);
1099 __tbl_insert(table, flow);
1100}
1101
1102void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow)
Jesse Grossccb13522011-10-25 19:26:31 -07001103{
Hong Zhiguod3e11012013-03-27 20:41:17 +08001104 BUG_ON(table->count == 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001105 hlist_del_rcu(&flow->hash_node[table->node_ver]);
1106 table->count--;
Jesse Grossccb13522011-10-25 19:26:31 -07001107}
1108
1109/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
1110const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
1111 [OVS_KEY_ATTR_ENCAP] = -1,
1112 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
1113 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
Ansis Atteka39c7caeb2012-11-26 11:24:11 -08001114 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
Jesse Grossccb13522011-10-25 19:26:31 -07001115 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
1116 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
1117 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
1118 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
1119 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
1120 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
1121 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
Joe Stringera175a722013-08-22 12:30:48 -07001122 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
Jesse Grossccb13522011-10-25 19:26:31 -07001123 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
1124 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
1125 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
1126 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001127 [OVS_KEY_ATTR_TUNNEL] = -1,
Jesse Grossccb13522011-10-25 19:26:31 -07001128};
1129
Andy Zhou03f0d912013-08-07 20:01:00 -07001130static bool is_all_zero(const u8 *fp, size_t size)
Jesse Grossccb13522011-10-25 19:26:31 -07001131{
Andy Zhou03f0d912013-08-07 20:01:00 -07001132 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001133
Andy Zhou03f0d912013-08-07 20:01:00 -07001134 if (!fp)
1135 return false;
Jesse Grossccb13522011-10-25 19:26:31 -07001136
Andy Zhou03f0d912013-08-07 20:01:00 -07001137 for (i = 0; i < size; i++)
1138 if (fp[i])
1139 return false;
Jesse Grossccb13522011-10-25 19:26:31 -07001140
Andy Zhou03f0d912013-08-07 20:01:00 -07001141 return true;
Jesse Grossccb13522011-10-25 19:26:31 -07001142}
1143
Andy Zhou03f0d912013-08-07 20:01:00 -07001144static int __parse_flow_nlattrs(const struct nlattr *attr,
1145 const struct nlattr *a[],
1146 u64 *attrsp, bool nz)
Jesse Grossccb13522011-10-25 19:26:31 -07001147{
1148 const struct nlattr *nla;
1149 u32 attrs;
1150 int rem;
1151
Andy Zhou03f0d912013-08-07 20:01:00 -07001152 attrs = *attrsp;
Jesse Grossccb13522011-10-25 19:26:31 -07001153 nla_for_each_nested(nla, attr, rem) {
1154 u16 type = nla_type(nla);
1155 int expected_len;
1156
Andy Zhou03f0d912013-08-07 20:01:00 -07001157 if (type > OVS_KEY_ATTR_MAX) {
1158 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
1159 type, OVS_KEY_ATTR_MAX);
1160 }
1161
1162 if (attrs & (1 << type)) {
1163 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
Jesse Grossccb13522011-10-25 19:26:31 -07001164 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001165 }
Jesse Grossccb13522011-10-25 19:26:31 -07001166
1167 expected_len = ovs_key_lens[type];
Andy Zhou03f0d912013-08-07 20:01:00 -07001168 if (nla_len(nla) != expected_len && expected_len != -1) {
1169 OVS_NLERR("Key attribute has unexpected length (type=%d"
1170 ", length=%d, expected=%d).\n", type,
1171 nla_len(nla), expected_len);
Jesse Grossccb13522011-10-25 19:26:31 -07001172 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001173 }
Jesse Grossccb13522011-10-25 19:26:31 -07001174
Andy Zhou03f0d912013-08-07 20:01:00 -07001175 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
1176 attrs |= 1 << type;
1177 a[type] = nla;
1178 }
Jesse Grossccb13522011-10-25 19:26:31 -07001179 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001180 if (rem) {
1181 OVS_NLERR("Message has %d unknown bytes.\n", rem);
Jesse Grossccb13522011-10-25 19:26:31 -07001182 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001183 }
Jesse Grossccb13522011-10-25 19:26:31 -07001184
1185 *attrsp = attrs;
1186 return 0;
1187}
1188
Andy Zhou03f0d912013-08-07 20:01:00 -07001189static int parse_flow_mask_nlattrs(const struct nlattr *attr,
1190 const struct nlattr *a[], u64 *attrsp)
1191{
1192 return __parse_flow_nlattrs(attr, a, attrsp, true);
1193}
1194
1195static int parse_flow_nlattrs(const struct nlattr *attr,
1196 const struct nlattr *a[], u64 *attrsp)
1197{
1198 return __parse_flow_nlattrs(attr, a, attrsp, false);
1199}
1200
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001201int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr,
Andy Zhou03f0d912013-08-07 20:01:00 -07001202 struct sw_flow_match *match, bool is_mask)
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001203{
1204 struct nlattr *a;
1205 int rem;
1206 bool ttl = false;
Andy Zhou03f0d912013-08-07 20:01:00 -07001207 __be16 tun_flags = 0;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001208
1209 nla_for_each_nested(a, attr, rem) {
1210 int type = nla_type(a);
1211 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1212 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
1213 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
1214 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
1215 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
1216 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
1217 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
1218 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
1219 };
1220
Andy Zhou03f0d912013-08-07 20:01:00 -07001221 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
1222 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
1223 type, OVS_TUNNEL_KEY_ATTR_MAX);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001224 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001225 }
1226
1227 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
1228 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
1229 " length (type=%d, length=%d, expected=%d).\n",
1230 type, nla_len(a), ovs_tunnel_key_lens[type]);
1231 return -EINVAL;
1232 }
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001233
1234 switch (type) {
1235 case OVS_TUNNEL_KEY_ATTR_ID:
Andy Zhou03f0d912013-08-07 20:01:00 -07001236 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
1237 nla_get_be64(a), is_mask);
1238 tun_flags |= TUNNEL_KEY;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001239 break;
1240 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
Andy Zhou03f0d912013-08-07 20:01:00 -07001241 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
1242 nla_get_be32(a), is_mask);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001243 break;
1244 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
Andy Zhou03f0d912013-08-07 20:01:00 -07001245 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
1246 nla_get_be32(a), is_mask);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001247 break;
1248 case OVS_TUNNEL_KEY_ATTR_TOS:
Andy Zhou03f0d912013-08-07 20:01:00 -07001249 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
1250 nla_get_u8(a), is_mask);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001251 break;
1252 case OVS_TUNNEL_KEY_ATTR_TTL:
Andy Zhou03f0d912013-08-07 20:01:00 -07001253 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
1254 nla_get_u8(a), is_mask);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001255 ttl = true;
1256 break;
1257 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
Andy Zhou03f0d912013-08-07 20:01:00 -07001258 tun_flags |= TUNNEL_DONT_FRAGMENT;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001259 break;
1260 case OVS_TUNNEL_KEY_ATTR_CSUM:
Andy Zhou03f0d912013-08-07 20:01:00 -07001261 tun_flags |= TUNNEL_CSUM;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001262 break;
1263 default:
1264 return -EINVAL;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001265 }
1266 }
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001267
Andy Zhou03f0d912013-08-07 20:01:00 -07001268 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001269
Andy Zhou03f0d912013-08-07 20:01:00 -07001270 if (rem > 0) {
1271 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001272 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001273 }
1274
1275 if (!is_mask) {
1276 if (!match->key->tun_key.ipv4_dst) {
1277 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
1278 return -EINVAL;
1279 }
1280
1281 if (!ttl) {
1282 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
1283 return -EINVAL;
1284 }
1285 }
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001286
1287 return 0;
1288}
1289
1290int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb,
Andy Zhou03f0d912013-08-07 20:01:00 -07001291 const struct ovs_key_ipv4_tunnel *tun_key,
1292 const struct ovs_key_ipv4_tunnel *output)
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001293{
1294 struct nlattr *nla;
1295
1296 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
1297 if (!nla)
1298 return -EMSGSIZE;
1299
Andy Zhou03f0d912013-08-07 20:01:00 -07001300 if (output->tun_flags & TUNNEL_KEY &&
1301 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001302 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -07001303 if (output->ipv4_src &&
1304 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001305 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -07001306 if (output->ipv4_dst &&
1307 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001308 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -07001309 if (output->ipv4_tos &&
1310 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001311 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -07001312 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001313 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -07001314 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001315 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
1316 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -07001317 if ((output->tun_flags & TUNNEL_CSUM) &&
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001318 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
1319 return -EMSGSIZE;
1320
1321 nla_nest_end(skb, nla);
1322 return 0;
1323}
1324
Andy Zhou03f0d912013-08-07 20:01:00 -07001325static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
1326 const struct nlattr **a, bool is_mask)
Jesse Grossccb13522011-10-25 19:26:31 -07001327{
Andy Zhou03f0d912013-08-07 20:01:00 -07001328 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1329 SW_FLOW_KEY_PUT(match, phy.priority,
1330 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1331 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1332 }
1333
1334 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1335 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1336
1337 if (is_mask)
1338 in_port = 0xffffffff; /* Always exact match in_port. */
1339 else if (in_port >= DP_MAX_PORTS)
1340 return -EINVAL;
1341
1342 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1343 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1344 } else if (!is_mask) {
1345 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1346 }
1347
1348 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1349 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1350
1351 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1352 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1353 }
1354 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
1355 if (ovs_ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1356 is_mask))
1357 return -EINVAL;
1358 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1359 }
1360 return 0;
1361}
1362
1363static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
1364 const struct nlattr **a, bool is_mask)
1365{
Jesse Grossccb13522011-10-25 19:26:31 -07001366 int err;
Andy Zhou03f0d912013-08-07 20:01:00 -07001367 u64 orig_attrs = attrs;
Jesse Grossccb13522011-10-25 19:26:31 -07001368
Andy Zhou03f0d912013-08-07 20:01:00 -07001369 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
Jesse Grossccb13522011-10-25 19:26:31 -07001370 if (err)
1371 return err;
1372
Andy Zhou03f0d912013-08-07 20:01:00 -07001373 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
1374 const struct ovs_key_ethernet *eth_key;
1375
1376 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1377 SW_FLOW_KEY_MEMCPY(match, eth.src,
1378 eth_key->eth_src, ETH_ALEN, is_mask);
1379 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1380 eth_key->eth_dst, ETH_ALEN, is_mask);
1381 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
Ansis Atteka39c7caeb2012-11-26 11:24:11 -08001382 }
Jesse Grossccb13522011-10-25 19:26:31 -07001383
Andy Zhou03f0d912013-08-07 20:01:00 -07001384 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
Jesse Grossccb13522011-10-25 19:26:31 -07001385 __be16 tci;
1386
Jesse Grossccb13522011-10-25 19:26:31 -07001387 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
Andy Zhou03f0d912013-08-07 20:01:00 -07001388 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1389 if (is_mask)
1390 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
1391 else
1392 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001393
Andy Zhou03f0d912013-08-07 20:01:00 -07001394 return -EINVAL;
1395 }
1396
1397 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
1398 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
1399 } else if (!is_mask)
1400 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1401
1402 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1403 __be16 eth_type;
1404
1405 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1406 if (is_mask) {
1407 /* Always exact match EtherType. */
1408 eth_type = htons(0xffff);
1409 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
1410 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
1411 ntohs(eth_type), ETH_P_802_3_MIN);
1412 return -EINVAL;
1413 }
1414
1415 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1416 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1417 } else if (!is_mask) {
1418 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1419 }
1420
1421 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1422 const struct ovs_key_ipv4 *ipv4_key;
1423
1424 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1425 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
1426 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
1427 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
1428 return -EINVAL;
1429 }
1430 SW_FLOW_KEY_PUT(match, ip.proto,
1431 ipv4_key->ipv4_proto, is_mask);
1432 SW_FLOW_KEY_PUT(match, ip.tos,
1433 ipv4_key->ipv4_tos, is_mask);
1434 SW_FLOW_KEY_PUT(match, ip.ttl,
1435 ipv4_key->ipv4_ttl, is_mask);
1436 SW_FLOW_KEY_PUT(match, ip.frag,
1437 ipv4_key->ipv4_frag, is_mask);
1438 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1439 ipv4_key->ipv4_src, is_mask);
1440 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1441 ipv4_key->ipv4_dst, is_mask);
1442 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1443 }
1444
1445 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
1446 const struct ovs_key_ipv6 *ipv6_key;
1447
1448 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1449 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
1450 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
1451 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
1452 return -EINVAL;
1453 }
1454 SW_FLOW_KEY_PUT(match, ipv6.label,
1455 ipv6_key->ipv6_label, is_mask);
1456 SW_FLOW_KEY_PUT(match, ip.proto,
1457 ipv6_key->ipv6_proto, is_mask);
1458 SW_FLOW_KEY_PUT(match, ip.tos,
1459 ipv6_key->ipv6_tclass, is_mask);
1460 SW_FLOW_KEY_PUT(match, ip.ttl,
1461 ipv6_key->ipv6_hlimit, is_mask);
1462 SW_FLOW_KEY_PUT(match, ip.frag,
1463 ipv6_key->ipv6_frag, is_mask);
1464 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1465 ipv6_key->ipv6_src,
1466 sizeof(match->key->ipv6.addr.src),
1467 is_mask);
1468 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1469 ipv6_key->ipv6_dst,
1470 sizeof(match->key->ipv6.addr.dst),
1471 is_mask);
1472
1473 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1474 }
1475
1476 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
1477 const struct ovs_key_arp *arp_key;
1478
1479 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1480 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
1481 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
1482 arp_key->arp_op);
1483 return -EINVAL;
1484 }
1485
1486 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1487 arp_key->arp_sip, is_mask);
1488 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1489 arp_key->arp_tip, is_mask);
1490 SW_FLOW_KEY_PUT(match, ip.proto,
1491 ntohs(arp_key->arp_op), is_mask);
1492 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1493 arp_key->arp_sha, ETH_ALEN, is_mask);
1494 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1495 arp_key->arp_tha, ETH_ALEN, is_mask);
1496
1497 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1498 }
1499
1500 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1501 const struct ovs_key_tcp *tcp_key;
1502
1503 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1504 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1505 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1506 tcp_key->tcp_src, is_mask);
1507 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1508 tcp_key->tcp_dst, is_mask);
1509 } else {
1510 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1511 tcp_key->tcp_src, is_mask);
1512 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1513 tcp_key->tcp_dst, is_mask);
1514 }
1515 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1516 }
1517
1518 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1519 const struct ovs_key_udp *udp_key;
1520
1521 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1522 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1523 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1524 udp_key->udp_src, is_mask);
1525 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1526 udp_key->udp_dst, is_mask);
1527 } else {
1528 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1529 udp_key->udp_src, is_mask);
1530 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1531 udp_key->udp_dst, is_mask);
1532 }
1533 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1534 }
1535
Joe Stringera175a722013-08-22 12:30:48 -07001536 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1537 const struct ovs_key_sctp *sctp_key;
1538
1539 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1540 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1541 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1542 sctp_key->sctp_src, is_mask);
1543 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1544 sctp_key->sctp_dst, is_mask);
1545 } else {
1546 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1547 sctp_key->sctp_src, is_mask);
1548 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1549 sctp_key->sctp_dst, is_mask);
1550 }
1551 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1552 }
1553
Andy Zhou03f0d912013-08-07 20:01:00 -07001554 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1555 const struct ovs_key_icmp *icmp_key;
1556
1557 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1558 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1559 htons(icmp_key->icmp_type), is_mask);
1560 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1561 htons(icmp_key->icmp_code), is_mask);
1562 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1563 }
1564
1565 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1566 const struct ovs_key_icmpv6 *icmpv6_key;
1567
1568 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1569 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1570 htons(icmpv6_key->icmpv6_type), is_mask);
1571 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1572 htons(icmpv6_key->icmpv6_code), is_mask);
1573 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1574 }
1575
1576 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1577 const struct ovs_key_nd *nd_key;
1578
1579 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1580 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1581 nd_key->nd_target,
1582 sizeof(match->key->ipv6.nd.target),
1583 is_mask);
1584 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1585 nd_key->nd_sll, ETH_ALEN, is_mask);
1586 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1587 nd_key->nd_tll, ETH_ALEN, is_mask);
1588 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1589 }
1590
1591 if (attrs != 0)
1592 return -EINVAL;
1593
1594 return 0;
1595}
1596
1597/**
1598 * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
1599 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1600 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1601 * does not include any don't care bit.
1602 * @match: receives the extracted flow match information.
1603 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1604 * sequence. The fields should of the packet that triggered the creation
1605 * of this flow.
1606 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1607 * attribute specifies the mask field of the wildcarded flow.
1608 */
1609int ovs_match_from_nlattrs(struct sw_flow_match *match,
1610 const struct nlattr *key,
1611 const struct nlattr *mask)
1612{
1613 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1614 const struct nlattr *encap;
1615 u64 key_attrs = 0;
1616 u64 mask_attrs = 0;
1617 bool encap_valid = false;
1618 int err;
1619
1620 err = parse_flow_nlattrs(key, a, &key_attrs);
1621 if (err)
1622 return err;
1623
1624 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
1625 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
1626 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
1627 __be16 tci;
1628
1629 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
1630 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
1631 OVS_NLERR("Invalid Vlan frame.\n");
1632 return -EINVAL;
1633 }
1634
1635 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1636 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1637 encap = a[OVS_KEY_ATTR_ENCAP];
1638 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1639 encap_valid = true;
1640
1641 if (tci & htons(VLAN_TAG_PRESENT)) {
1642 err = parse_flow_nlattrs(encap, a, &key_attrs);
Jesse Grossccb13522011-10-25 19:26:31 -07001643 if (err)
1644 return err;
1645 } else if (!tci) {
1646 /* Corner case for truncated 802.1Q header. */
Andy Zhou03f0d912013-08-07 20:01:00 -07001647 if (nla_len(encap)) {
1648 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001649 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001650 }
Jesse Grossccb13522011-10-25 19:26:31 -07001651 } else {
Andy Zhou03f0d912013-08-07 20:01:00 -07001652 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
1653 return -EINVAL;
Jesse Grossccb13522011-10-25 19:26:31 -07001654 }
1655 }
1656
Andy Zhou03f0d912013-08-07 20:01:00 -07001657 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
1658 if (err)
1659 return err;
1660
1661 if (mask) {
1662 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
1663 if (err)
1664 return err;
1665
1666 if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1667 __be16 eth_type = 0;
1668 __be16 tci = 0;
1669
1670 if (!encap_valid) {
1671 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
1672 return -EINVAL;
1673 }
1674
1675 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1676 if (a[OVS_KEY_ATTR_ETHERTYPE])
1677 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1678
1679 if (eth_type == htons(0xffff)) {
1680 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1681 encap = a[OVS_KEY_ATTR_ENCAP];
1682 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
1683 } else {
1684 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
1685 ntohs(eth_type));
1686 return -EINVAL;
1687 }
1688
1689 if (a[OVS_KEY_ATTR_VLAN])
1690 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1691
1692 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1693 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
1694 return -EINVAL;
1695 }
1696 }
1697
1698 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
1699 if (err)
1700 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001701 } else {
Andy Zhou03f0d912013-08-07 20:01:00 -07001702 /* Populate exact match flow's key mask. */
1703 if (match->mask)
1704 ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
Jesse Grossccb13522011-10-25 19:26:31 -07001705 }
1706
Andy Zhou03f0d912013-08-07 20:01:00 -07001707 if (!ovs_match_validate(match, key_attrs, mask_attrs))
Jesse Grossccb13522011-10-25 19:26:31 -07001708 return -EINVAL;
Jesse Grossccb13522011-10-25 19:26:31 -07001709
1710 return 0;
1711}
1712
1713/**
1714 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001715 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
1716 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
Jesse Grossccb13522011-10-25 19:26:31 -07001717 * sequence.
1718 *
1719 * This parses a series of Netlink attributes that form a flow key, which must
1720 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1721 * get the metadata, that is, the parts of the flow key that cannot be
1722 * extracted from the packet itself.
1723 */
Andy Zhou03f0d912013-08-07 20:01:00 -07001724
1725int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
1726 const struct nlattr *attr)
Jesse Grossccb13522011-10-25 19:26:31 -07001727{
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001728 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
Andy Zhou03f0d912013-08-07 20:01:00 -07001729 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1730 u64 attrs = 0;
1731 int err;
1732 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001733
Pravin B Shelar93d8fd12013-06-13 11:11:32 -07001734 flow->key.phy.in_port = DP_MAX_PORTS;
1735 flow->key.phy.priority = 0;
1736 flow->key.phy.skb_mark = 0;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001737 memset(tun_key, 0, sizeof(flow->key.tun_key));
Jesse Grossccb13522011-10-25 19:26:31 -07001738
Andy Zhou03f0d912013-08-07 20:01:00 -07001739 err = parse_flow_nlattrs(attr, a, &attrs);
1740 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001741 return -EINVAL;
Pravin B Shelara3e82992013-06-17 17:50:28 -07001742
Andy Zhou03f0d912013-08-07 20:01:00 -07001743 memset(&match, 0, sizeof(match));
1744 match.key = &flow->key;
1745
1746 err = metadata_from_nlattrs(&match, &attrs, a, false);
1747 if (err)
1748 return err;
Pravin B Shelara3e82992013-06-17 17:50:28 -07001749
Jesse Grossccb13522011-10-25 19:26:31 -07001750 return 0;
1751}
1752
Andy Zhou03f0d912013-08-07 20:01:00 -07001753int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
1754 const struct sw_flow_key *output, struct sk_buff *skb)
Jesse Grossccb13522011-10-25 19:26:31 -07001755{
1756 struct ovs_key_ethernet *eth_key;
1757 struct nlattr *nla, *encap;
Andy Zhou03f0d912013-08-07 20:01:00 -07001758 bool is_mask = (swkey != output);
Jesse Grossccb13522011-10-25 19:26:31 -07001759
Andy Zhou03f0d912013-08-07 20:01:00 -07001760 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
David S. Miller028d6a62012-03-29 23:20:48 -04001761 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001762
Andy Zhou03f0d912013-08-07 20:01:00 -07001763 if ((swkey->tun_key.ipv4_dst || is_mask) &&
1764 ovs_ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001765 goto nla_put_failure;
1766
Andy Zhou03f0d912013-08-07 20:01:00 -07001767 if (swkey->phy.in_port == DP_MAX_PORTS) {
1768 if (is_mask && (output->phy.in_port == 0xffff))
1769 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1770 goto nla_put_failure;
1771 } else {
1772 u16 upper_u16;
1773 upper_u16 = !is_mask ? 0 : 0xffff;
Jesse Grossccb13522011-10-25 19:26:31 -07001774
Andy Zhou03f0d912013-08-07 20:01:00 -07001775 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1776 (upper_u16 << 16) | output->phy.in_port))
1777 goto nla_put_failure;
1778 }
1779
1780 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
Ansis Atteka39c7caeb2012-11-26 11:24:11 -08001781 goto nla_put_failure;
1782
Jesse Grossccb13522011-10-25 19:26:31 -07001783 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1784 if (!nla)
1785 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -07001786
Jesse Grossccb13522011-10-25 19:26:31 -07001787 eth_key = nla_data(nla);
Andy Zhou03f0d912013-08-07 20:01:00 -07001788 memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
1789 memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
Jesse Grossccb13522011-10-25 19:26:31 -07001790
1791 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
Andy Zhou03f0d912013-08-07 20:01:00 -07001792 __be16 eth_type;
1793 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1794 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1795 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
David S. Miller028d6a62012-03-29 23:20:48 -04001796 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001797 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1798 if (!swkey->eth.tci)
1799 goto unencap;
Andy Zhou03f0d912013-08-07 20:01:00 -07001800 } else
Jesse Grossccb13522011-10-25 19:26:31 -07001801 encap = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001802
1803 if (swkey->eth.type == htons(ETH_P_802_2)) {
1804 /*
1805 * Ethertype 802.2 is represented in the netlink with omitted
1806 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1807 * 0xffff in the mask attribute. Ethertype can also
1808 * be wildcarded.
1809 */
1810 if (is_mask && output->eth.type)
1811 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1812 output->eth.type))
1813 goto nla_put_failure;
1814 goto unencap;
Jesse Grossccb13522011-10-25 19:26:31 -07001815 }
1816
Andy Zhou03f0d912013-08-07 20:01:00 -07001817 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
David S. Miller028d6a62012-03-29 23:20:48 -04001818 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001819
1820 if (swkey->eth.type == htons(ETH_P_IP)) {
1821 struct ovs_key_ipv4 *ipv4_key;
1822
1823 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1824 if (!nla)
1825 goto nla_put_failure;
1826 ipv4_key = nla_data(nla);
Andy Zhou03f0d912013-08-07 20:01:00 -07001827 ipv4_key->ipv4_src = output->ipv4.addr.src;
1828 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1829 ipv4_key->ipv4_proto = output->ip.proto;
1830 ipv4_key->ipv4_tos = output->ip.tos;
1831 ipv4_key->ipv4_ttl = output->ip.ttl;
1832 ipv4_key->ipv4_frag = output->ip.frag;
Jesse Grossccb13522011-10-25 19:26:31 -07001833 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1834 struct ovs_key_ipv6 *ipv6_key;
1835
1836 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1837 if (!nla)
1838 goto nla_put_failure;
1839 ipv6_key = nla_data(nla);
Andy Zhou03f0d912013-08-07 20:01:00 -07001840 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
Jesse Grossccb13522011-10-25 19:26:31 -07001841 sizeof(ipv6_key->ipv6_src));
Andy Zhou03f0d912013-08-07 20:01:00 -07001842 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
Jesse Grossccb13522011-10-25 19:26:31 -07001843 sizeof(ipv6_key->ipv6_dst));
Andy Zhou03f0d912013-08-07 20:01:00 -07001844 ipv6_key->ipv6_label = output->ipv6.label;
1845 ipv6_key->ipv6_proto = output->ip.proto;
1846 ipv6_key->ipv6_tclass = output->ip.tos;
1847 ipv6_key->ipv6_hlimit = output->ip.ttl;
1848 ipv6_key->ipv6_frag = output->ip.frag;
Mehak Mahajanc0618532012-11-02 14:14:31 -07001849 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1850 swkey->eth.type == htons(ETH_P_RARP)) {
Jesse Grossccb13522011-10-25 19:26:31 -07001851 struct ovs_key_arp *arp_key;
1852
1853 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1854 if (!nla)
1855 goto nla_put_failure;
1856 arp_key = nla_data(nla);
1857 memset(arp_key, 0, sizeof(struct ovs_key_arp));
Andy Zhou03f0d912013-08-07 20:01:00 -07001858 arp_key->arp_sip = output->ipv4.addr.src;
1859 arp_key->arp_tip = output->ipv4.addr.dst;
1860 arp_key->arp_op = htons(output->ip.proto);
1861 memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
1862 memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
Jesse Grossccb13522011-10-25 19:26:31 -07001863 }
1864
1865 if ((swkey->eth.type == htons(ETH_P_IP) ||
1866 swkey->eth.type == htons(ETH_P_IPV6)) &&
1867 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1868
1869 if (swkey->ip.proto == IPPROTO_TCP) {
1870 struct ovs_key_tcp *tcp_key;
1871
1872 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1873 if (!nla)
1874 goto nla_put_failure;
1875 tcp_key = nla_data(nla);
1876 if (swkey->eth.type == htons(ETH_P_IP)) {
Andy Zhou03f0d912013-08-07 20:01:00 -07001877 tcp_key->tcp_src = output->ipv4.tp.src;
1878 tcp_key->tcp_dst = output->ipv4.tp.dst;
Jesse Grossccb13522011-10-25 19:26:31 -07001879 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
Andy Zhou03f0d912013-08-07 20:01:00 -07001880 tcp_key->tcp_src = output->ipv6.tp.src;
1881 tcp_key->tcp_dst = output->ipv6.tp.dst;
Jesse Grossccb13522011-10-25 19:26:31 -07001882 }
1883 } else if (swkey->ip.proto == IPPROTO_UDP) {
1884 struct ovs_key_udp *udp_key;
1885
1886 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1887 if (!nla)
1888 goto nla_put_failure;
1889 udp_key = nla_data(nla);
1890 if (swkey->eth.type == htons(ETH_P_IP)) {
Andy Zhou03f0d912013-08-07 20:01:00 -07001891 udp_key->udp_src = output->ipv4.tp.src;
1892 udp_key->udp_dst = output->ipv4.tp.dst;
Jesse Grossccb13522011-10-25 19:26:31 -07001893 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
Andy Zhou03f0d912013-08-07 20:01:00 -07001894 udp_key->udp_src = output->ipv6.tp.src;
1895 udp_key->udp_dst = output->ipv6.tp.dst;
Jesse Grossccb13522011-10-25 19:26:31 -07001896 }
Joe Stringera175a722013-08-22 12:30:48 -07001897 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1898 struct ovs_key_sctp *sctp_key;
1899
1900 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1901 if (!nla)
1902 goto nla_put_failure;
1903 sctp_key = nla_data(nla);
1904 if (swkey->eth.type == htons(ETH_P_IP)) {
1905 sctp_key->sctp_src = swkey->ipv4.tp.src;
1906 sctp_key->sctp_dst = swkey->ipv4.tp.dst;
1907 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1908 sctp_key->sctp_src = swkey->ipv6.tp.src;
1909 sctp_key->sctp_dst = swkey->ipv6.tp.dst;
1910 }
Jesse Grossccb13522011-10-25 19:26:31 -07001911 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1912 swkey->ip.proto == IPPROTO_ICMP) {
1913 struct ovs_key_icmp *icmp_key;
1914
1915 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1916 if (!nla)
1917 goto nla_put_failure;
1918 icmp_key = nla_data(nla);
Andy Zhou03f0d912013-08-07 20:01:00 -07001919 icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
1920 icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
Jesse Grossccb13522011-10-25 19:26:31 -07001921 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1922 swkey->ip.proto == IPPROTO_ICMPV6) {
1923 struct ovs_key_icmpv6 *icmpv6_key;
1924
1925 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1926 sizeof(*icmpv6_key));
1927 if (!nla)
1928 goto nla_put_failure;
1929 icmpv6_key = nla_data(nla);
Andy Zhou03f0d912013-08-07 20:01:00 -07001930 icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
1931 icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
Jesse Grossccb13522011-10-25 19:26:31 -07001932
1933 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1934 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1935 struct ovs_key_nd *nd_key;
1936
1937 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1938 if (!nla)
1939 goto nla_put_failure;
1940 nd_key = nla_data(nla);
Andy Zhou03f0d912013-08-07 20:01:00 -07001941 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
Jesse Grossccb13522011-10-25 19:26:31 -07001942 sizeof(nd_key->nd_target));
Andy Zhou03f0d912013-08-07 20:01:00 -07001943 memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
1944 memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
Jesse Grossccb13522011-10-25 19:26:31 -07001945 }
1946 }
1947 }
1948
1949unencap:
1950 if (encap)
1951 nla_nest_end(skb, encap);
1952
1953 return 0;
1954
1955nla_put_failure:
1956 return -EMSGSIZE;
1957}
1958
1959/* Initializes the flow module.
1960 * Returns zero if successful or a negative error code. */
1961int ovs_flow_init(void)
1962{
1963 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1964 0, NULL);
1965 if (flow_cache == NULL)
1966 return -ENOMEM;
1967
1968 return 0;
1969}
1970
1971/* Uninitializes the flow module. */
1972void ovs_flow_exit(void)
1973{
1974 kmem_cache_destroy(flow_cache);
1975}
Andy Zhou03f0d912013-08-07 20:01:00 -07001976
1977struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
1978{
1979 struct sw_flow_mask *mask;
1980
1981 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
1982 if (mask)
1983 mask->ref_count = 0;
1984
1985 return mask;
1986}
1987
1988void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
1989{
1990 mask->ref_count++;
1991}
1992
1993void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
1994{
1995 if (!mask)
1996 return;
1997
1998 BUG_ON(!mask->ref_count);
1999 mask->ref_count--;
2000
2001 if (!mask->ref_count) {
2002 list_del_rcu(&mask->list);
2003 if (deferred)
2004 kfree_rcu(mask, rcu);
2005 else
2006 kfree(mask);
2007 }
2008}
2009
2010static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a,
2011 const struct sw_flow_mask *b)
2012{
2013 u8 *a_ = (u8 *)&a->key + a->range.start;
2014 u8 *b_ = (u8 *)&b->key + b->range.start;
2015
2016 return (a->range.end == b->range.end)
2017 && (a->range.start == b->range.start)
2018 && (memcmp(a_, b_, ovs_sw_flow_mask_actual_size(a)) == 0);
2019}
2020
2021struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
2022 const struct sw_flow_mask *mask)
2023{
2024 struct list_head *ml;
2025
2026 list_for_each(ml, tbl->mask_list) {
2027 struct sw_flow_mask *m;
2028 m = container_of(ml, struct sw_flow_mask, list);
2029 if (ovs_sw_flow_mask_equal(mask, m))
2030 return m;
2031 }
2032
2033 return NULL;
2034}
2035
2036/**
2037 * add a new mask into the mask list.
2038 * The caller needs to make sure that 'mask' is not the same
2039 * as any masks that are already on the list.
2040 */
2041void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
2042{
2043 list_add_rcu(&mask->list, tbl->mask_list);
2044}
2045
2046/**
2047 * Set 'range' fields in the mask to the value of 'val'.
2048 */
2049static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
2050 struct sw_flow_key_range *range, u8 val)
2051{
2052 u8 *m = (u8 *)&mask->key + range->start;
2053
2054 mask->range = *range;
2055 memset(m, val, ovs_sw_flow_mask_size_roundup(mask));
2056}