]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/flow.c
bond: Fix error in bond_shift_load().
[mirror_ovs.git] / datapath / flow.c
CommitLineData
064af421 1/*
a1c564be 2 * Copyright (c) 2007-2013 Nicira, Inc.
a14bc59f 3 *
a9a29d22
JG
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
064af421
BP
17 */
18
19#include "flow.h"
f5e86186 20#include "datapath.h"
6455100f 21#include <linux/uaccess.h>
064af421
BP
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>
8d5ebd83 28#include <linux/jhash.h>
064af421
BP
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>
a26ef517 34#include <linux/if_arp.h>
064af421 35#include <linux/ip.h>
d31f1109 36#include <linux/ipv6.h>
10f72e3d 37#include <linux/sctp.h>
064af421
BP
38#include <linux/tcp.h>
39#include <linux/udp.h>
40#include <linux/icmp.h>
d31f1109 41#include <linux/icmpv6.h>
3544358a 42#include <linux/rculist.h>
064af421 43#include <net/ip.h>
d31f1109 44#include <net/ipv6.h>
685a51a5 45#include <net/ndisc.h>
064af421 46
6ce39213
JG
47#include "vlan.h"
48
33b38b63 49static struct kmem_cache *flow_cache;
064af421 50
a1c564be
AZ
51static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
52 struct sw_flow_key_range *range, u8 val);
53
54static void update_range__(struct sw_flow_match *match,
55 size_t offset, size_t size, bool is_mask)
56{
57 struct sw_flow_key_range *range = NULL;
58 size_t start = offset;
59 size_t end = offset + size;
60
61 if (!is_mask)
62 range = &match->range;
63 else if (match->mask)
64 range = &match->mask->range;
65
66 if (!range)
67 return;
68
69 if (range->start == range->end) {
70 range->start = start;
71 range->end = end;
72 return;
73 }
74
75 if (range->start > start)
76 range->start = start;
77
78 if (range->end < end)
79 range->end = end;
80}
81
82#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
83 do { \
84 update_range__(match, offsetof(struct sw_flow_key, field), \
85 sizeof((match)->key->field), is_mask); \
388c13d8
AZ
86 if (is_mask) { \
87 if ((match)->mask) \
88 (match)->mask->key.field = value; \
a1c564be
AZ
89 } else { \
90 (match)->key->field = value; \
91 } \
92 } while (0)
93
94#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
95 do { \
96 update_range__(match, offsetof(struct sw_flow_key, field), \
97 len, is_mask); \
388c13d8
AZ
98 if (is_mask) { \
99 if ((match)->mask) \
100 memcpy(&(match)->mask->key.field, value_p, len);\
a1c564be
AZ
101 } else { \
102 memcpy(&(match)->key->field, value_p, len); \
103 } \
104 } while (0)
105
106void ovs_match_init(struct sw_flow_match *match,
107 struct sw_flow_key *key,
108 struct sw_flow_mask *mask)
109{
110 memset(match, 0, sizeof(*match));
111 match->key = key;
112 match->mask = mask;
113
114 memset(key, 0, sizeof(*key));
115
116 if (mask) {
117 memset(&mask->key, 0, sizeof(mask->key));
118 mask->range.start = mask->range.end = 0;
119 }
120}
121
122static bool ovs_match_validate(const struct sw_flow_match *match,
123 u64 key_attrs, u64 mask_attrs)
124{
125 u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
126 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
127
128 /* The following mask attributes allowed only if they
129 * pass the validation tests. */
130 mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
131 | (1ULL << OVS_KEY_ATTR_IPV6)
132 | (1ULL << OVS_KEY_ATTR_TCP)
133 | (1ULL << OVS_KEY_ATTR_UDP)
10f72e3d 134 | (1ULL << OVS_KEY_ATTR_SCTP)
a1c564be
AZ
135 | (1ULL << OVS_KEY_ATTR_ICMP)
136 | (1ULL << OVS_KEY_ATTR_ICMPV6)
137 | (1ULL << OVS_KEY_ATTR_ARP)
138 | (1ULL << OVS_KEY_ATTR_ND));
139
202c1051
AZ
140 /* Always allowed mask fields. */
141 mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
33fd9f59 142 | (1ULL << OVS_KEY_ATTR_IN_PORT)
6273244a 143 | (1ULL << OVS_KEY_ATTR_ETHERTYPE));
a1c564be
AZ
144
145 /* Check key attributes. */
146 if (match->key->eth.type == htons(ETH_P_ARP)
147 || match->key->eth.type == htons(ETH_P_RARP)) {
148 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
149 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
150 mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
151 }
152
153 if (match->key->eth.type == htons(ETH_P_IP)) {
154 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
155 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
156 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
157
158 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
159 if (match->key->ip.proto == IPPROTO_UDP) {
160 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
161 if (match->mask && (match->mask->key.ip.proto == 0xff))
162 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
163 }
164
10f72e3d
JS
165 if (match->key->ip.proto == IPPROTO_SCTP) {
166 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
167 if (match->mask && (match->mask->key.ip.proto == 0xff))
168 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
169 }
170
a1c564be
AZ
171 if (match->key->ip.proto == IPPROTO_TCP) {
172 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
173 if (match->mask && (match->mask->key.ip.proto == 0xff))
174 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
175 }
176
177 if (match->key->ip.proto == IPPROTO_ICMP) {
178 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
179 if (match->mask && (match->mask->key.ip.proto == 0xff))
180 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
181 }
182 }
183 }
184
185 if (match->key->eth.type == htons(ETH_P_IPV6)) {
186 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
187 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
188 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
189
190 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
191 if (match->key->ip.proto == IPPROTO_UDP) {
192 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
193 if (match->mask && (match->mask->key.ip.proto == 0xff))
194 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
195 }
196
10f72e3d
JS
197 if (match->key->ip.proto == IPPROTO_SCTP) {
198 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
199 if (match->mask && (match->mask->key.ip.proto == 0xff))
200 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
201 }
202
a1c564be
AZ
203 if (match->key->ip.proto == IPPROTO_TCP) {
204 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
205 if (match->mask && (match->mask->key.ip.proto == 0xff))
206 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
207 }
208
209 if (match->key->ip.proto == IPPROTO_ICMPV6) {
210 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
211 if (match->mask && (match->mask->key.ip.proto == 0xff))
212 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
213
214 if (match->key->ipv6.tp.src ==
215 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
216 match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
217 key_expected |= 1ULL << OVS_KEY_ATTR_ND;
218 if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
219 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
220 }
221 }
222 }
223 }
224
1b936472 225 if ((key_attrs & key_expected) != key_expected) {
a1c564be 226 /* Key attributes check failed. */
1b936472
AZ
227 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
228 key_attrs, key_expected);
a1c564be 229 return false;
1b936472 230 }
a1c564be 231
1b936472 232 if ((mask_attrs & mask_allowed) != mask_attrs) {
a1c564be 233 /* Mask attributes check failed. */
1b936472
AZ
234 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
235 mask_attrs, mask_allowed);
a1c564be 236 return false;
1b936472 237 }
a1c564be
AZ
238
239 return true;
240}
241
2db65bf7
JG
242static int check_header(struct sk_buff *skb, int len)
243{
244 if (unlikely(skb->len < len))
245 return -EINVAL;
246 if (unlikely(!pskb_may_pull(skb, len)))
247 return -ENOMEM;
248 return 0;
249}
250
6455100f 251static bool arphdr_ok(struct sk_buff *skb)
a26ef517 252{
2db65bf7
JG
253 return pskb_may_pull(skb, skb_network_offset(skb) +
254 sizeof(struct arp_eth_header));
a26ef517
JP
255}
256
6455100f 257static int check_iphdr(struct sk_buff *skb)
064af421 258{
4c1ad233
BP
259 unsigned int nh_ofs = skb_network_offset(skb);
260 unsigned int ip_len;
2db65bf7 261 int err;
4c1ad233 262
2db65bf7
JG
263 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
264 if (unlikely(err))
265 return err;
4c1ad233
BP
266
267 ip_len = ip_hdrlen(skb);
2db65bf7
JG
268 if (unlikely(ip_len < sizeof(struct iphdr) ||
269 skb->len < nh_ofs + ip_len))
4c1ad233
BP
270 return -EINVAL;
271
4c1ad233
BP
272 skb_set_transport_header(skb, nh_ofs + ip_len);
273 return 0;
064af421
BP
274}
275
6455100f 276static bool tcphdr_ok(struct sk_buff *skb)
064af421
BP
277{
278 int th_ofs = skb_transport_offset(skb);
2db65bf7
JG
279 int tcp_len;
280
281 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
282 return false;
283
284 tcp_len = tcp_hdrlen(skb);
285 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
286 skb->len < th_ofs + tcp_len))
287 return false;
288
289 return true;
064af421
BP
290}
291
6455100f 292static bool udphdr_ok(struct sk_buff *skb)
064af421 293{
2db65bf7
JG
294 return pskb_may_pull(skb, skb_transport_offset(skb) +
295 sizeof(struct udphdr));
064af421
BP
296}
297
10f72e3d
JS
298static bool sctphdr_ok(struct sk_buff *skb)
299{
300 return pskb_may_pull(skb, skb_transport_offset(skb) +
301 sizeof(struct sctphdr));
302}
303
6455100f 304static bool icmphdr_ok(struct sk_buff *skb)
064af421 305{
2db65bf7
JG
306 return pskb_may_pull(skb, skb_transport_offset(skb) +
307 sizeof(struct icmphdr));
064af421
BP
308}
309
850b6b3b 310u64 ovs_flow_used_time(unsigned long flow_jiffies)
ec58547a
JG
311{
312 struct timespec cur_ts;
313 u64 cur_ms, idle_ms;
314
315 ktime_get_ts(&cur_ts);
316 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
317 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
318 cur_ts.tv_nsec / NSEC_PER_MSEC;
319
320 return cur_ms - idle_ms;
321}
322
a1c564be 323static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
d31f1109
JP
324{
325 unsigned int nh_ofs = skb_network_offset(skb);
326 unsigned int nh_len;
327 int payload_ofs;
d31f1109
JP
328 struct ipv6hdr *nh;
329 uint8_t nexthdr;
0fd0d083 330 __be16 frag_off;
2db65bf7 331 int err;
d31f1109 332
2db65bf7
JG
333 err = check_header(skb, nh_ofs + sizeof(*nh));
334 if (unlikely(err))
335 return err;
d31f1109
JP
336
337 nh = ipv6_hdr(skb);
338 nexthdr = nh->nexthdr;
339 payload_ofs = (u8 *)(nh + 1) - skb->data;
d31f1109 340
28bad473 341 key->ip.proto = NEXTHDR_NONE;
530180fd 342 key->ip.tos = ipv6_get_dsfield(nh);
a61680c6 343 key->ip.ttl = nh->hop_limit;
fa8223b7 344 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
858d1026
JG
345 key->ipv6.addr.src = nh->saddr;
346 key->ipv6.addr.dst = nh->daddr;
d31f1109 347
0fd0d083 348 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
bfef4717 349 if (unlikely(payload_ofs < 0))
d31f1109 350 return -EINVAL;
bfef4717 351
0fd0d083
JG
352 if (frag_off) {
353 if (frag_off & htons(~0x7))
354 key->ip.frag = OVS_FRAG_TYPE_LATER;
355 else
356 key->ip.frag = OVS_FRAG_TYPE_FIRST;
357 }
358
d31f1109 359 nh_len = payload_ofs - nh_ofs;
d31f1109 360 skb_set_transport_header(skb, nh_ofs + nh_len);
28bad473 361 key->ip.proto = nexthdr;
d31f1109
JP
362 return nh_len;
363}
364
365static bool icmp6hdr_ok(struct sk_buff *skb)
366{
2db65bf7
JG
367 return pskb_may_pull(skb, skb_transport_offset(skb) +
368 sizeof(struct icmp6hdr));
d31f1109 369}
ec58547a 370
529db635
JG
371void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src,
372 const struct sw_flow_mask *mask)
a1c564be
AZ
373{
374 u8 *m = (u8 *)&mask->key + mask->range.start;
375 u8 *s = (u8 *)src + mask->range.start;
376 u8 *d = (u8 *)dst + mask->range.start;
377 int i;
378
379 memset(dst, 0, sizeof(*dst));
380 for (i = 0; i < ovs_sw_flow_mask_size_roundup(mask); i++) {
381 *d = *s & *m;
382 d++, s++, m++;
383 }
384}
385
064af421
BP
386#define TCP_FLAGS_OFFSET 13
387#define TCP_FLAG_MASK 0x3f
388
850b6b3b 389void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
064af421 390{
064af421
BP
391 u8 tcp_flags = 0;
392
11460e23
JG
393 if ((flow->key.eth.type == htons(ETH_P_IP) ||
394 flow->key.eth.type == htons(ETH_P_IPV6)) &&
9c47b45a
JG
395 flow->key.ip.proto == IPPROTO_TCP &&
396 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
abfec865
BP
397 u8 *tcp = (u8 *)tcp_hdr(skb);
398 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
064af421
BP
399 }
400
e9141eec 401 spin_lock(&flow->lock);
6bfafa55 402 flow->used = jiffies;
064af421
BP
403 flow->packet_count++;
404 flow->byte_count += skb->len;
405 flow->tcp_flags |= tcp_flags;
e9141eec 406 spin_unlock(&flow->lock);
064af421
BP
407}
408
9b405f1a 409struct sw_flow_actions *ovs_flow_actions_alloc(int size)
064af421
BP
410{
411 struct sw_flow_actions *sfa;
412
9b405f1a 413 if (size > MAX_ACTIONS_BUFSIZE)
064af421
BP
414 return ERR_PTR(-EINVAL);
415
ba400435 416 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
064af421
BP
417 if (!sfa)
418 return ERR_PTR(-ENOMEM);
419
9b405f1a 420 sfa->actions_len = 0;
064af421
BP
421 return sfa;
422}
423
850b6b3b 424struct sw_flow *ovs_flow_alloc(void)
560e8022
JG
425{
426 struct sw_flow *flow;
427
428 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
429 if (!flow)
430 return ERR_PTR(-ENOMEM);
431
432 spin_lock_init(&flow->lock);
6f04e94a 433 flow->sf_acts = NULL;
a1c564be 434 flow->mask = NULL;
064af421 435
560e8022
JG
436 return flow;
437}
438
be2ba156 439static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
8d5ebd83 440{
acd051f1 441 hash = jhash_1word(hash, table->hash_seed);
3544358a
PS
442 return flex_array_get(table->buckets,
443 (hash & (table->n_buckets - 1)));
444}
445
be2ba156 446static struct flex_array *alloc_buckets(unsigned int n_buckets)
3544358a 447{
be2ba156 448 struct flex_array *buckets;
3544358a
PS
449 int i, err;
450
8e1a83d8 451 buckets = flex_array_alloc(sizeof(struct hlist_head),
3544358a
PS
452 n_buckets, GFP_KERNEL);
453 if (!buckets)
454 return NULL;
455
456 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
457 if (err) {
458 flex_array_free(buckets);
459 return NULL;
460 }
461
462 for (i = 0; i < n_buckets; i++)
463 INIT_HLIST_HEAD((struct hlist_head *)
464 flex_array_get(buckets, i));
465
466 return buckets;
467}
468
6455100f 469static void free_buckets(struct flex_array *buckets)
3544358a
PS
470{
471 flex_array_free(buckets);
472}
473
ede77a46 474static struct flow_table *__flow_tbl_alloc(int new_size)
3544358a
PS
475{
476 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
477
478 if (!table)
479 return NULL;
480
481 table->buckets = alloc_buckets(new_size);
482
483 if (!table->buckets) {
484 kfree(table);
485 return NULL;
486 }
487 table->n_buckets = new_size;
488 table->count = 0;
acd051f1
PS
489 table->node_ver = 0;
490 table->keep_flows = false;
491 get_random_bytes(&table->hash_seed, sizeof(u32));
ede77a46 492 table->mask_list = NULL;
3544358a
PS
493
494 return table;
495}
fb8c9347 496
a1c564be 497static void __flow_tbl_destroy(struct flow_table *table)
3544358a
PS
498{
499 int i;
500
acd051f1
PS
501 if (table->keep_flows)
502 goto skip_flows;
503
3544358a
PS
504 for (i = 0; i < table->n_buckets; i++) {
505 struct sw_flow *flow;
506 struct hlist_head *head = flex_array_get(table->buckets, i);
f8dfbcb7 507 struct hlist_node *n;
acd051f1 508 int ver = table->node_ver;
3544358a 509
f8dfbcb7 510 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
16c6b921 511 hlist_del(&flow->hash_node[ver]);
a1c564be 512 ovs_flow_free(flow, false);
3544358a
PS
513 }
514 }
515
ede77a46
AZ
516 BUG_ON(!list_empty(table->mask_list));
517 kfree(table->mask_list);
518
acd051f1 519skip_flows:
3544358a
PS
520 free_buckets(table->buckets);
521 kfree(table);
522}
523
ede77a46
AZ
524struct flow_table *ovs_flow_tbl_alloc(int new_size)
525{
526 struct flow_table *table = __flow_tbl_alloc(new_size);
527
528 if (!table)
529 return NULL;
530
531 table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
532 if (!table->mask_list) {
533 table->keep_flows = true;
534 __flow_tbl_destroy(table);
535 return NULL;
536 }
537 INIT_LIST_HEAD(table->mask_list);
538
539 return table;
540}
541
3544358a
PS
542static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
543{
544 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
545
a1c564be 546 __flow_tbl_destroy(table);
3544358a
PS
547}
548
a1c564be 549void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
3544358a 550{
6455100f
PS
551 if (!table)
552 return;
3544358a 553
a1c564be
AZ
554 if (deferred)
555 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
556 else
557 __flow_tbl_destroy(table);
3544358a
PS
558}
559
a1c564be 560struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last)
3544358a
PS
561{
562 struct sw_flow *flow;
563 struct hlist_head *head;
acd051f1 564 int ver;
3544358a
PS
565 int i;
566
acd051f1 567 ver = table->node_ver;
3544358a
PS
568 while (*bucket < table->n_buckets) {
569 i = 0;
570 head = flex_array_get(table->buckets, *bucket);
f8dfbcb7 571 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
3544358a
PS
572 if (i < *last) {
573 i++;
574 continue;
575 }
576 *last = i + 1;
577 return flow;
578 }
579 (*bucket)++;
580 *last = 0;
581 }
582
583 return NULL;
584}
585
a1c564be 586static void __tbl_insert(struct flow_table *table, struct sw_flow *flow)
13e24889
PS
587{
588 struct hlist_head *head;
a1c564be 589
13e24889
PS
590 head = find_bucket(table, flow->hash);
591 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
a1c564be 592
13e24889
PS
593 table->count++;
594}
595
acd051f1 596static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
3544358a 597{
acd051f1 598 int old_ver;
3544358a
PS
599 int i;
600
acd051f1
PS
601 old_ver = old->node_ver;
602 new->node_ver = !old_ver;
3544358a 603
acd051f1
PS
604 /* Insert in new table. */
605 for (i = 0; i < old->n_buckets; i++) {
3544358a
PS
606 struct sw_flow *flow;
607 struct hlist_head *head;
3544358a 608
acd051f1 609 head = flex_array_get(old->buckets, i);
3544358a 610
f8dfbcb7 611 hlist_for_each_entry(flow, head, hash_node[old_ver])
a1c564be 612 __tbl_insert(new, flow);
3544358a 613 }
a1c564be
AZ
614
615 new->mask_list = old->mask_list;
acd051f1
PS
616 old->keep_flows = true;
617}
618
619static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
620{
621 struct flow_table *new_table;
622
ede77a46 623 new_table = __flow_tbl_alloc(n_buckets);
acd051f1
PS
624 if (!new_table)
625 return ERR_PTR(-ENOMEM);
626
627 flow_table_copy_flows(table, new_table);
3544358a
PS
628
629 return new_table;
630}
631
acd051f1
PS
632struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
633{
634 return __flow_tbl_rehash(table, table->n_buckets);
635}
636
637struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
638{
639 return __flow_tbl_rehash(table, table->n_buckets * 2);
640}
641
a1c564be 642static void __flow_free(struct sw_flow *flow)
9321954a 643{
9321954a
JG
644 kfree((struct sf_flow_acts __force *)flow->sf_acts);
645 kmem_cache_free(flow_cache, flow);
646}
647
064af421
BP
648static void rcu_free_flow_callback(struct rcu_head *rcu)
649{
650 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
fb8c9347 651
a1c564be 652 __flow_free(flow);
064af421
BP
653}
654
a1c564be 655void ovs_flow_free(struct sw_flow *flow, bool deferred)
064af421 656{
a1c564be
AZ
657 if (!flow)
658 return;
659
3d097064 660 ovs_sw_flow_mask_del_ref(flow->mask, deferred);
a1c564be
AZ
661
662 if (deferred)
663 call_rcu(&flow->rcu, rcu_free_flow_callback);
664 else
665 __flow_free(flow);
064af421
BP
666}
667
850b6b3b 668/* RCU callback used by ovs_flow_deferred_free_acts. */
064af421
BP
669static void rcu_free_acts_callback(struct rcu_head *rcu)
670{
d295e8e9 671 struct sw_flow_actions *sf_acts = container_of(rcu,
064af421 672 struct sw_flow_actions, rcu);
ba400435 673 kfree(sf_acts);
064af421
BP
674}
675
676/* Schedules 'sf_acts' to be freed after the next RCU grace period.
677 * The caller must hold rcu_read_lock for this to be sensible. */
850b6b3b 678void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
064af421
BP
679{
680 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
681}
682
2db65bf7 683static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
064af421 684{
50f06e16
BP
685 struct qtag_prefix {
686 __be16 eth_type; /* ETH_P_8021Q */
687 __be16 tci;
688 };
689 struct qtag_prefix *qp;
690
8ddc056d
BP
691 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
692 return 0;
693
2db65bf7
JG
694 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
695 sizeof(__be16))))
696 return -ENOMEM;
50f06e16
BP
697
698 qp = (struct qtag_prefix *) skb->data;
76abe283 699 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
50f06e16 700 __skb_pull(skb, sizeof(struct qtag_prefix));
2db65bf7
JG
701
702 return 0;
50f06e16
BP
703}
704
705static __be16 parse_ethertype(struct sk_buff *skb)
064af421 706{
50f06e16
BP
707 struct llc_snap_hdr {
708 u8 dsap; /* Always 0xAA */
709 u8 ssap; /* Always 0xAA */
710 u8 ctrl;
711 u8 oui[3];
8dda8c9b 712 __be16 ethertype;
50f06e16
BP
713 };
714 struct llc_snap_hdr *llc;
715 __be16 proto;
716
717 proto = *(__be16 *) skb->data;
718 __skb_pull(skb, sizeof(__be16));
719
7cd46155 720 if (ntohs(proto) >= ETH_P_802_3_MIN)
50f06e16
BP
721 return proto;
722
2db65bf7 723 if (skb->len < sizeof(struct llc_snap_hdr))
36956a7d 724 return htons(ETH_P_802_2);
50f06e16 725
2db65bf7
JG
726 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
727 return htons(0);
728
50f06e16
BP
729 llc = (struct llc_snap_hdr *) skb->data;
730 if (llc->dsap != LLC_SAP_SNAP ||
731 llc->ssap != LLC_SAP_SNAP ||
732 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
36956a7d 733 return htons(ETH_P_802_2);
50f06e16
BP
734
735 __skb_pull(skb, sizeof(struct llc_snap_hdr));
9e69bc5f 736
7cd46155 737 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
9e69bc5f
RL
738 return llc->ethertype;
739
740 return htons(ETH_P_802_2);
064af421
BP
741}
742
685a51a5 743static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
a1c564be 744 int nh_len)
685a51a5 745{
685a51a5
JP
746 struct icmp6hdr *icmp = icmp6_hdr(skb);
747
748 /* The ICMPv6 type and code fields use the 16-bit transport port
bfef4717
JG
749 * fields, so we need to store them in 16-bit network byte order.
750 */
76abe283
AE
751 key->ipv6.tp.src = htons(icmp->icmp6_type);
752 key->ipv6.tp.dst = htons(icmp->icmp6_code);
685a51a5 753
bfef4717
JG
754 if (icmp->icmp6_code == 0 &&
755 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
756 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
e977ba19 757 int icmp_len = skb->len - skb_transport_offset(skb);
685a51a5
JP
758 struct nd_msg *nd;
759 int offset;
760
761 /* In order to process neighbor discovery options, we need the
bfef4717
JG
762 * entire packet.
763 */
764 if (unlikely(icmp_len < sizeof(*nd)))
a1c564be
AZ
765 return 0;
766
767 if (unlikely(skb_linearize(skb)))
768 return -ENOMEM;
685a51a5
JP
769
770 nd = (struct nd_msg *)skb_transport_header(skb);
858d1026 771 key->ipv6.nd.target = nd->target;
685a51a5
JP
772
773 icmp_len -= sizeof(*nd);
774 offset = 0;
775 while (icmp_len >= 8) {
6455100f
PS
776 struct nd_opt_hdr *nd_opt =
777 (struct nd_opt_hdr *)(nd->opt + offset);
685a51a5
JP
778 int opt_len = nd_opt->nd_opt_len * 8;
779
bfef4717 780 if (unlikely(!opt_len || opt_len > icmp_len))
a1c564be 781 return 0;
685a51a5 782
bfef4717
JG
783 /* Store the link layer address if the appropriate
784 * option is provided. It is considered an error if
785 * the same link layer option is specified twice.
786 */
685a51a5 787 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
bfef4717 788 && opt_len == 8) {
76abe283 789 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
685a51a5 790 goto invalid;
76abe283 791 memcpy(key->ipv6.nd.sll,
bfef4717 792 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
685a51a5 793 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
bfef4717 794 && opt_len == 8) {
76abe283 795 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
685a51a5 796 goto invalid;
76abe283 797 memcpy(key->ipv6.nd.tll,
bfef4717 798 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
685a51a5
JP
799 }
800
801 icmp_len -= opt_len;
802 offset += opt_len;
803 }
804 }
805
a1c564be 806 return 0;
685a51a5
JP
807
808invalid:
76abe283
AE
809 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
810 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
811 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
685a51a5 812
a1c564be 813 return 0;
685a51a5
JP
814}
815
a31e0e31 816/**
850b6b3b 817 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
a31e0e31
BP
818 * @skb: sk_buff that contains the frame, with skb->data pointing to the
819 * Ethernet header
820 * @in_port: port number on which @skb was received.
821 * @key: output flow key
822 *
823 * The caller must ensure that skb->len >= ETH_HLEN.
824 *
4c1ad233
BP
825 * Returns 0 if successful, otherwise a negative errno value.
826 *
59a18f80
BP
827 * Initializes @skb header pointers as follows:
828 *
829 * - skb->mac_header: the Ethernet header.
830 *
831 * - skb->network_header: just past the Ethernet header, or just past the
832 * VLAN header, to the first byte of the Ethernet payload.
833 *
ae9020cf 834 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
d31f1109
JP
835 * on output, then just past the IP header, if one is present and
836 * of a correct length, otherwise the same as skb->network_header.
ae9020cf 837 * For other key->eth.type values it is left untouched.
a31e0e31 838 */
a1c564be 839int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
064af421 840{
a1c564be 841 int error;
064af421 842 struct ethhdr *eth;
064af421 843
84c17d98 844 memset(key, 0, sizeof(*key));
abff858b
PS
845
846 key->phy.priority = skb->priority;
356af50b 847 if (OVS_CB(skb)->tun_key)
6694e498 848 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
abff858b 849 key->phy.in_port = in_port;
72e8bf28 850 key->phy.skb_mark = skb_get_mark(skb);
064af421 851
064af421 852 skb_reset_mac_header(skb);
064af421 853
2db65bf7
JG
854 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
855 * header in the linear data area.
856 */
50f06e16 857 eth = eth_hdr(skb);
76abe283
AE
858 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
859 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
860
50f06e16 861 __skb_pull(skb, 2 * ETH_ALEN);
3cfede14
PS
862 /* We are going to push all headers that we pull, so no need to
863 * update skb->csum here. */
6ce39213
JG
864
865 if (vlan_tx_tag_present(skb))
76abe283 866 key->eth.tci = htons(vlan_get_tci(skb));
6ce39213 867 else if (eth->h_proto == htons(ETH_P_8021Q))
2db65bf7
JG
868 if (unlikely(parse_vlan(skb, key)))
869 return -ENOMEM;
6ce39213 870
76abe283
AE
871 key->eth.type = parse_ethertype(skb);
872 if (unlikely(key->eth.type == htons(0)))
2db65bf7
JG
873 return -ENOMEM;
874
50f06e16 875 skb_reset_network_header(skb);
2db65bf7 876 __skb_push(skb, skb->data - skb_mac_header(skb));
064af421
BP
877
878 /* Network layer. */
76abe283 879 if (key->eth.type == htons(ETH_P_IP)) {
4c1ad233 880 struct iphdr *nh;
7257b535 881 __be16 offset;
76abe283 882
4c1ad233
BP
883 error = check_iphdr(skb);
884 if (unlikely(error)) {
885 if (error == -EINVAL) {
886 skb->transport_header = skb->network_header;
76abe283 887 error = 0;
4c1ad233 888 }
a1c564be 889 return error;
4c1ad233
BP
890 }
891
892 nh = ip_hdr(skb);
76abe283
AE
893 key->ipv4.addr.src = nh->saddr;
894 key->ipv4.addr.dst = nh->daddr;
7257b535 895
28bad473 896 key->ip.proto = nh->protocol;
530180fd 897 key->ip.tos = nh->tos;
a61680c6 898 key->ip.ttl = nh->ttl;
064af421 899
7257b535
BP
900 offset = nh->frag_off & htons(IP_OFFSET);
901 if (offset) {
9e44d715 902 key->ip.frag = OVS_FRAG_TYPE_LATER;
a1c564be 903 return 0;
7257b535
BP
904 }
905 if (nh->frag_off & htons(IP_MF) ||
906 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
9e44d715 907 key->ip.frag = OVS_FRAG_TYPE_FIRST;
b7a31ec1 908
7257b535 909 /* Transport layer. */
28bad473 910 if (key->ip.proto == IPPROTO_TCP) {
7257b535 911 if (tcphdr_ok(skb)) {
5f4d087c
JG
912 struct tcphdr *tcp = tcp_hdr(skb);
913 key->ipv4.tp.src = tcp->source;
914 key->ipv4.tp.dst = tcp->dest;
915 }
28bad473 916 } else if (key->ip.proto == IPPROTO_UDP) {
7257b535 917 if (udphdr_ok(skb)) {
5f4d087c
JG
918 struct udphdr *udp = udp_hdr(skb);
919 key->ipv4.tp.src = udp->source;
920 key->ipv4.tp.dst = udp->dest;
921 }
10f72e3d
JS
922 } else if (key->ip.proto == IPPROTO_SCTP) {
923 if (sctphdr_ok(skb)) {
924 struct sctphdr *sctp = sctp_hdr(skb);
925 key->ipv4.tp.src = sctp->source;
926 key->ipv4.tp.dst = sctp->dest;
927 }
28bad473 928 } else if (key->ip.proto == IPPROTO_ICMP) {
7257b535 929 if (icmphdr_ok(skb)) {
5f4d087c
JG
930 struct icmphdr *icmp = icmp_hdr(skb);
931 /* The ICMP type and code fields use the 16-bit
6455100f
PS
932 * transport port fields, so we need to store
933 * them in 16-bit network byte order. */
5f4d087c
JG
934 key->ipv4.tp.src = htons(icmp->type);
935 key->ipv4.tp.dst = htons(icmp->code);
936 }
937 }
938
8087f5ff
MM
939 } else if ((key->eth.type == htons(ETH_P_ARP) ||
940 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
a26ef517
JP
941 struct arp_eth_header *arp;
942
943 arp = (struct arp_eth_header *)skb_network_header(skb);
944
f5e86186 945 if (arp->ar_hrd == htons(ARPHRD_ETHER)
de3f65ea
JP
946 && arp->ar_pro == htons(ETH_P_IP)
947 && arp->ar_hln == ETH_ALEN
948 && arp->ar_pln == 4) {
949
950 /* We only match on the lower 8 bits of the opcode. */
b7a31ec1 951 if (ntohs(arp->ar_op) <= 0xff)
28bad473 952 key->ip.proto = ntohs(arp->ar_op);
a3d3ad0c
MM
953 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
954 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
955 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
956 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
de3f65ea 957 }
76abe283 958 } else if (key->eth.type == htons(ETH_P_IPV6)) {
d31f1109
JP
959 int nh_len; /* IPv6 Header + Extensions */
960
a1c564be 961 nh_len = parse_ipv6hdr(skb, key);
d31f1109 962 if (unlikely(nh_len < 0)) {
a1c564be 963 if (nh_len == -EINVAL) {
d31f1109 964 skb->transport_header = skb->network_header;
a1c564be
AZ
965 error = 0;
966 } else {
76abe283 967 error = nh_len;
a1c564be
AZ
968 }
969 return error;
d31f1109
JP
970 }
971
9e44d715 972 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
a1c564be 973 return 0;
7257b535 974 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
9e44d715 975 key->ip.frag = OVS_FRAG_TYPE_FIRST;
7257b535 976
d31f1109 977 /* Transport layer. */
28bad473 978 if (key->ip.proto == NEXTHDR_TCP) {
d31f1109
JP
979 if (tcphdr_ok(skb)) {
980 struct tcphdr *tcp = tcp_hdr(skb);
76abe283
AE
981 key->ipv6.tp.src = tcp->source;
982 key->ipv6.tp.dst = tcp->dest;
d31f1109 983 }
28bad473 984 } else if (key->ip.proto == NEXTHDR_UDP) {
d31f1109
JP
985 if (udphdr_ok(skb)) {
986 struct udphdr *udp = udp_hdr(skb);
76abe283
AE
987 key->ipv6.tp.src = udp->source;
988 key->ipv6.tp.dst = udp->dest;
d31f1109 989 }
10f72e3d
JS
990 } else if (key->ip.proto == NEXTHDR_SCTP) {
991 if (sctphdr_ok(skb)) {
992 struct sctphdr *sctp = sctp_hdr(skb);
993 key->ipv6.tp.src = sctp->source;
994 key->ipv6.tp.dst = sctp->dest;
995 }
28bad473 996 } else if (key->ip.proto == NEXTHDR_ICMP) {
d31f1109 997 if (icmp6hdr_ok(skb)) {
a1c564be
AZ
998 error = parse_icmpv6(skb, key, nh_len);
999 if (error)
1000 return error;
d31f1109
JP
1001 }
1002 }
064af421 1003 }
76abe283 1004
a1c564be 1005 return 0;
064af421
BP
1006}
1007
13e24889 1008static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start, int key_len)
8d5ebd83 1009{
13e24889
PS
1010 return jhash2((u32 *)((u8 *)key + key_start),
1011 DIV_ROUND_UP(key_len - key_start, sizeof(u32)), 0);
1012}
1013
a1c564be 1014static int flow_key_start(const struct sw_flow_key *key)
13e24889 1015{
6694e498 1016 if (key->tun_key.ipv4_dst)
13e24889
PS
1017 return 0;
1018 else
6694e498 1019 return offsetof(struct sw_flow_key, phy);
8d5ebd83
JG
1020}
1021
a1c564be
AZ
1022static bool __cmp_key(const struct sw_flow_key *key1,
1023 const struct sw_flow_key *key2, int key_start, int key_len)
1024{
1025 return !memcmp((u8 *)key1 + key_start,
1026 (u8 *)key2 + key_start, (key_len - key_start));
1027}
1028
1029static bool __flow_cmp_key(const struct sw_flow *flow,
1030 const struct sw_flow_key *key, int key_start, int key_len)
1031{
1032 return __cmp_key(&flow->key, key, key_start, key_len);
1033}
1034
1035static bool __flow_cmp_unmasked_key(const struct sw_flow *flow,
1036 const struct sw_flow_key *key, int key_start, int key_len)
1037{
1038 return __cmp_key(&flow->unmasked_key, key, key_start, key_len);
1039}
1040
1041bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
1042 const struct sw_flow_key *key, int key_len)
1043{
1044 int key_start;
1045 key_start = flow_key_start(key);
1046
1047 return __flow_cmp_unmasked_key(flow, key, key_start, key_len);
1048
1049}
1050
1051struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
1052 struct sw_flow_match *match)
1053{
1054 struct sw_flow_key *unmasked = match->key;
1055 int key_len = match->range.end;
1056 struct sw_flow *flow;
1057
1058 flow = ovs_flow_lookup(table, unmasked);
1059 if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_len)))
1060 flow = NULL;
1061
1062 return flow;
1063}
1064
1065static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
1066 const struct sw_flow_key *flow_key,
1067 struct sw_flow_mask *mask)
8d5ebd83 1068{
3544358a 1069 struct sw_flow *flow;
3544358a 1070 struct hlist_head *head;
a1c564be
AZ
1071 int key_start = mask->range.start;
1072 int key_len = mask->range.end;
3544358a 1073 u32 hash;
a1c564be 1074 struct sw_flow_key masked_key;
8d5ebd83 1075
529db635 1076 ovs_flow_key_mask(&masked_key, flow_key, mask);
a1c564be 1077 hash = ovs_flow_hash(&masked_key, key_start, key_len);
3544358a 1078 head = find_bucket(table, hash);
f8dfbcb7 1079 hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
a99c219c
AZ
1080 if (flow->mask == mask &&
1081 __flow_cmp_key(flow, &masked_key, key_start, key_len))
3544358a 1082 return flow;
3544358a
PS
1083 }
1084 return NULL;
1085}
1086
a1c564be
AZ
1087struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
1088 const struct sw_flow_key *key)
3544358a 1089{
a1c564be
AZ
1090 struct sw_flow *flow = NULL;
1091 struct sw_flow_mask *mask;
1092
ede77a46 1093 list_for_each_entry_rcu(mask, tbl->mask_list, list) {
a1c564be
AZ
1094 flow = ovs_masked_flow_lookup(tbl, key, mask);
1095 if (flow) /* Found */
1096 break;
1097 }
1098
1099 return flow;
3544358a
PS
1100}
1101
a1c564be 1102
529db635 1103void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow)
a1c564be 1104{
3d097064
AZ
1105 flow->hash = ovs_flow_hash(&flow->key, flow->mask->range.start,
1106 flow->mask->range.end);
a1c564be
AZ
1107 __tbl_insert(table, flow);
1108}
1109
1110void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow)
3544358a 1111{
49c7f80c 1112 BUG_ON(table->count == 0);
acd051f1
PS
1113 hlist_del_rcu(&flow->hash_node[table->node_ver]);
1114 table->count--;
36956a7d
BP
1115}
1116
df2c07f4 1117/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
4094406f
BP
1118const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
1119 [OVS_KEY_ATTR_ENCAP] = -1,
a1bf209f
BP
1120 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
1121 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
72e8bf28 1122 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
df2c07f4 1123 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
fea393b1 1124 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
a1bf209f 1125 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
df2c07f4
JP
1126 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
1127 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
1128 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
1129 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
10f72e3d 1130 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
df2c07f4
JP
1131 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
1132 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
1133 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
1134 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
9b405f1a 1135 [OVS_KEY_ATTR_TUNNEL] = -1,
80e5eed9
BP
1136};
1137
a1c564be 1138static bool is_all_zero(const u8 *fp, size_t size)
34118cae 1139{
a1c564be 1140 int i;
34118cae 1141
a1c564be
AZ
1142 if (!fp)
1143 return false;
34118cae 1144
a1c564be
AZ
1145 for (i = 0; i < size; i++)
1146 if (fp[i])
1147 return false;
34118cae 1148
a1c564be 1149 return true;
34118cae
BP
1150}
1151
a1c564be
AZ
1152static int __parse_flow_nlattrs(const struct nlattr *attr,
1153 const struct nlattr *a[],
1154 u64 *attrsp, bool nz)
fea393b1
BP
1155{
1156 const struct nlattr *nla;
1157 u64 attrs;
1158 int rem;
1159
a1c564be 1160 attrs = *attrsp;
fea393b1
BP
1161 nla_for_each_nested(nla, attr, rem) {
1162 u16 type = nla_type(nla);
4094406f 1163 int expected_len;
fea393b1 1164
1b936472
AZ
1165 if (type > OVS_KEY_ATTR_MAX) {
1166 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
1167 type, OVS_KEY_ATTR_MAX);
1168 }
4094406f 1169
1b936472
AZ
1170 if (attrs & (1ULL << type)) {
1171 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
4094406f 1172 return -EINVAL;
1b936472 1173 }
4094406f 1174
1b936472
AZ
1175 expected_len = ovs_key_lens[type];
1176 if (nla_len(nla) != expected_len && expected_len != -1) {
1177 OVS_NLERR("Key attribute has unexpected length (type=%d"
1178 ", length=%d, expected=%d).\n", type,
1179 nla_len(nla), expected_len);
a1c564be 1180 return -EINVAL;
1b936472 1181 }
a1c564be
AZ
1182
1183 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
1184 attrs |= 1ULL << type;
1185 a[type] = nla;
1186 }
fea393b1 1187 }
1b936472
AZ
1188 if (rem) {
1189 OVS_NLERR("Message has %d unknown bytes.\n", rem);
fea393b1 1190 return -EINVAL;
1b936472 1191 }
fea393b1
BP
1192
1193 *attrsp = attrs;
1194 return 0;
1195}
1196
a1c564be
AZ
1197static int parse_flow_mask_nlattrs(const struct nlattr *attr,
1198 const struct nlattr *a[], u64 *attrsp)
1199{
1200 return __parse_flow_nlattrs(attr, a, attrsp, true);
1201}
1202
1203static int parse_flow_nlattrs(const struct nlattr *attr,
1204 const struct nlattr *a[], u64 *attrsp)
1205{
1206 return __parse_flow_nlattrs(attr, a, attrsp, false);
1207}
1208
54cc3ec6
JG
1209int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr,
1210 struct sw_flow_match *match, bool is_mask)
9b405f1a
PS
1211{
1212 struct nlattr *a;
1213 int rem;
1214 bool ttl = false;
5ebaf571 1215 __be16 tun_flags = 0;
9b405f1a
PS
1216
1217 nla_for_each_nested(a, attr, rem) {
1218 int type = nla_type(a);
1219 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1220 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
1221 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
1222 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
1223 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
1224 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
1225 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
1226 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
1227 };
1228
1b936472 1229 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
17ce1965 1230 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
1b936472
AZ
1231 type, OVS_TUNNEL_KEY_ATTR_MAX);
1232 return -EINVAL;
1233 }
1234
1235 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
1236 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
e821558d 1237 " length (type=%d, length=%d, expected=%d).\n",
1b936472 1238 type, nla_len(a), ovs_tunnel_key_lens[type]);
9b405f1a 1239 return -EINVAL;
1b936472 1240 }
9b405f1a
PS
1241
1242 switch (type) {
1243 case OVS_TUNNEL_KEY_ATTR_ID:
a1c564be
AZ
1244 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
1245 nla_get_be64(a), is_mask);
5ebaf571 1246 tun_flags |= TUNNEL_KEY;
9b405f1a
PS
1247 break;
1248 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
a1c564be
AZ
1249 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
1250 nla_get_be32(a), is_mask);
9b405f1a
PS
1251 break;
1252 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
a1c564be
AZ
1253 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
1254 nla_get_be32(a), is_mask);
9b405f1a
PS
1255 break;
1256 case OVS_TUNNEL_KEY_ATTR_TOS:
a1c564be
AZ
1257 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
1258 nla_get_u8(a), is_mask);
9b405f1a
PS
1259 break;
1260 case OVS_TUNNEL_KEY_ATTR_TTL:
a1c564be
AZ
1261 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
1262 nla_get_u8(a), is_mask);
9b405f1a
PS
1263 ttl = true;
1264 break;
1265 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
5ebaf571 1266 tun_flags |= TUNNEL_DONT_FRAGMENT;
9b405f1a
PS
1267 break;
1268 case OVS_TUNNEL_KEY_ATTR_CSUM:
5ebaf571 1269 tun_flags |= TUNNEL_CSUM;
9b405f1a
PS
1270 break;
1271 default:
1272 return -EINVAL;
9b405f1a
PS
1273 }
1274 }
a1c564be
AZ
1275
1276 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
1277
1b936472
AZ
1278 if (rem > 0) {
1279 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
9b405f1a 1280 return -EINVAL;
1b936472 1281 }
9b405f1a 1282
01fcdfc6
JG
1283 if (!is_mask) {
1284 if (!match->key->tun_key.ipv4_dst) {
1285 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
1286 return -EINVAL;
1287 }
9b405f1a 1288
01fcdfc6
JG
1289 if (!ttl) {
1290 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
1291 return -EINVAL;
1292 }
1b936472 1293 }
9b405f1a
PS
1294
1295 return 0;
1296}
1297
54cc3ec6
JG
1298int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb,
1299 const struct ovs_key_ipv4_tunnel *tun_key,
1300 const struct ovs_key_ipv4_tunnel *output)
9b405f1a
PS
1301{
1302 struct nlattr *nla;
1303
1304 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
1305 if (!nla)
1306 return -EMSGSIZE;
1307
04b13268 1308 if (output->tun_flags & TUNNEL_KEY &&
a1c564be 1309 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
9b405f1a 1310 return -EMSGSIZE;
04b13268
AZ
1311 if (output->ipv4_src &&
1312 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
9b405f1a 1313 return -EMSGSIZE;
04b13268
AZ
1314 if (output->ipv4_dst &&
1315 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
9b405f1a 1316 return -EMSGSIZE;
04b13268
AZ
1317 if (output->ipv4_tos &&
1318 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
9b405f1a 1319 return -EMSGSIZE;
aa20ff9d 1320 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
9b405f1a 1321 return -EMSGSIZE;
04b13268 1322 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
9b405f1a
PS
1323 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
1324 return -EMSGSIZE;
04b13268 1325 if ((output->tun_flags & TUNNEL_CSUM) &&
9b405f1a
PS
1326 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
1327 return -EMSGSIZE;
1328
1329 nla_nest_end(skb, nla);
1330 return 0;
1331}
1332
36956a7d 1333
a1c564be
AZ
1334static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
1335 const struct nlattr **a, bool is_mask)
1336{
1337 if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
1338 SW_FLOW_KEY_PUT(match, phy.priority,
1339 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1340 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
34118cae 1341 }
a1c564be
AZ
1342
1343 if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
34118cae 1344 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
a1c564be 1345
202c1051
AZ
1346 if (is_mask)
1347 in_port = 0xffffffff; /* Always exact match in_port. */
1348 else if (in_port >= DP_MAX_PORTS)
34118cae 1349 return -EINVAL;
202c1051 1350
a1c564be
AZ
1351 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1352 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
861687d4
JG
1353 } else if (!is_mask) {
1354 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
34118cae 1355 }
a1c564be
AZ
1356
1357 if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
72e8bf28
AA
1358 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1359#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
1b936472
AZ
1360 if (!is_mask && mark != 0) {
1361 OVS_NLERR("skb->mark must be zero on this kernel (mark=%d).\n", mark);
72e8bf28 1362 return -EINVAL;
1b936472 1363 }
72e8bf28 1364#endif
a1c564be
AZ
1365 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1366 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
1367 }
1368 if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
54cc3ec6 1369 if (ovs_ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
a1c564be
AZ
1370 is_mask))
1371 return -EINVAL;
1372 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
72e8bf28 1373 }
a1c564be
AZ
1374 return 0;
1375}
36956a7d 1376
a1c564be
AZ
1377static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
1378 const struct nlattr **a, bool is_mask)
1379{
1380 int err;
828f5f64 1381 u64 orig_attrs = attrs;
5c7f5883 1382
a1c564be
AZ
1383 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
1384 if (err)
1385 return err;
76abe283 1386
a1c564be
AZ
1387 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
1388 const struct ovs_key_ethernet *eth_key;
36956a7d 1389
a1c564be
AZ
1390 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1391 SW_FLOW_KEY_MEMCPY(match, eth.src,
1392 eth_key->eth_src, ETH_ALEN, is_mask);
1393 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1394 eth_key->eth_dst, ETH_ALEN, is_mask);
1395 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
1396 }
76abe283 1397
a1c564be 1398 if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
8ddc056d
BP
1399 __be16 tci;
1400
a1c564be 1401 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
62f13eb2
AZ
1402 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1403 if (is_mask)
1404 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
1405 else
1b936472 1406 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
62f13eb2
AZ
1407
1408 return -EINVAL;
1409 }
36956a7d 1410
a1c564be
AZ
1411 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
1412 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
62f13eb2
AZ
1413 } else if (!is_mask)
1414 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
8ddc056d 1415
a1c564be
AZ
1416 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
1417 __be16 eth_type;
8ddc056d 1418
a1c564be 1419 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
33fd9f59
AZ
1420 if (is_mask) {
1421 /* Always exact match EtherType. */
1422 eth_type = htons(0xffff);
1423 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
e821558d 1424 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
1b936472 1425 ntohs(eth_type), ETH_P_802_3_MIN);
8ddc056d 1426 return -EINVAL;
1b936472 1427 }
a1c564be
AZ
1428
1429 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1430 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1431 } else if (!is_mask) {
1432 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
34118cae
BP
1433 }
1434
a1c564be
AZ
1435 if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1436 const struct ovs_key_ipv4 *ipv4_key;
1437
1438 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1b936472
AZ
1439 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
1440 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
1441 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
34118cae 1442 return -EINVAL;
1b936472 1443 }
a1c564be
AZ
1444 SW_FLOW_KEY_PUT(match, ip.proto,
1445 ipv4_key->ipv4_proto, is_mask);
1446 SW_FLOW_KEY_PUT(match, ip.tos,
1447 ipv4_key->ipv4_tos, is_mask);
1448 SW_FLOW_KEY_PUT(match, ip.ttl,
1449 ipv4_key->ipv4_ttl, is_mask);
1450 SW_FLOW_KEY_PUT(match, ip.frag,
1451 ipv4_key->ipv4_frag, is_mask);
1452 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1453 ipv4_key->ipv4_src, is_mask);
1454 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1455 ipv4_key->ipv4_dst, is_mask);
1456 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
34118cae
BP
1457 }
1458
a1c564be
AZ
1459 if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
1460 const struct ovs_key_ipv6 *ipv6_key;
685a51a5 1461
a1c564be 1462 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1b936472
AZ
1463 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
1464 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
1465 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
34118cae 1466 return -EINVAL;
1b936472 1467 }
a1c564be
AZ
1468 SW_FLOW_KEY_PUT(match, ipv6.label,
1469 ipv6_key->ipv6_label, is_mask);
1470 SW_FLOW_KEY_PUT(match, ip.proto,
1471 ipv6_key->ipv6_proto, is_mask);
1472 SW_FLOW_KEY_PUT(match, ip.tos,
1473 ipv6_key->ipv6_tclass, is_mask);
1474 SW_FLOW_KEY_PUT(match, ip.ttl,
1475 ipv6_key->ipv6_hlimit, is_mask);
1476 SW_FLOW_KEY_PUT(match, ip.frag,
1477 ipv6_key->ipv6_frag, is_mask);
1478 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1479 ipv6_key->ipv6_src,
1480 sizeof(match->key->ipv6.addr.src),
1481 is_mask);
1482 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1483 ipv6_key->ipv6_dst,
1484 sizeof(match->key->ipv6.addr.dst),
1485 is_mask);
1486
1487 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
1488 }
34118cae 1489
a1c564be
AZ
1490 if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
1491 const struct ovs_key_arp *arp_key;
1492
1493 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1b936472
AZ
1494 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
1495 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
1496 arp_key->arp_op);
34118cae 1497 return -EINVAL;
1b936472 1498 }
a1c564be
AZ
1499
1500 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1501 arp_key->arp_sip, is_mask);
1502 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1503 arp_key->arp_tip, is_mask);
1504 SW_FLOW_KEY_PUT(match, ip.proto,
1505 ntohs(arp_key->arp_op), is_mask);
1506 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1507 arp_key->arp_sha, ETH_ALEN, is_mask);
1508 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1509 arp_key->arp_tha, ETH_ALEN, is_mask);
1510
1511 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
1512 }
1513
1514 if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
1515 const struct ovs_key_tcp *tcp_key;
1516
1517 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
828f5f64
JP
1518 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1519 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1520 tcp_key->tcp_src, is_mask);
1521 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1522 tcp_key->tcp_dst, is_mask);
1523 } else {
1524 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1525 tcp_key->tcp_src, is_mask);
1526 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1527 tcp_key->tcp_dst, is_mask);
1528 }
a1c564be
AZ
1529 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
1530 }
1531
1532 if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
1533 const struct ovs_key_udp *udp_key;
1534
1535 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
828f5f64
JP
1536 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1537 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1538 udp_key->udp_src, is_mask);
1539 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1540 udp_key->udp_dst, is_mask);
1541 } else {
1542 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1543 udp_key->udp_src, is_mask);
1544 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1545 udp_key->udp_dst, is_mask);
1546 }
a1c564be
AZ
1547 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
1548 }
1549
10f72e3d
JS
1550 if (attrs & (1ULL << OVS_KEY_ATTR_SCTP)) {
1551 const struct ovs_key_sctp *sctp_key;
1552
1553 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1554 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1555 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1556 sctp_key->sctp_src, is_mask);
1557 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1558 sctp_key->sctp_dst, is_mask);
1559 } else {
1560 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1561 sctp_key->sctp_src, is_mask);
1562 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1563 sctp_key->sctp_dst, is_mask);
1564 }
1565 attrs &= ~(1ULL << OVS_KEY_ATTR_SCTP);
1566 }
1567
a1c564be
AZ
1568 if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
1569 const struct ovs_key_icmp *icmp_key;
1570
1571 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1572 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1573 htons(icmp_key->icmp_type), is_mask);
1574 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1575 htons(icmp_key->icmp_code), is_mask);
1576 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
1577 }
1578
1579 if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
1580 const struct ovs_key_icmpv6 *icmpv6_key;
1581
1582 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1583 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1584 htons(icmpv6_key->icmpv6_type), is_mask);
1585 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1586 htons(icmpv6_key->icmpv6_code), is_mask);
1587 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
1588 }
1589
1590 if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
1591 const struct ovs_key_nd *nd_key;
1592
1593 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1594 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1595 nd_key->nd_target,
1596 sizeof(match->key->ipv6.nd.target),
1597 is_mask);
1598 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1599 nd_key->nd_sll, ETH_ALEN, is_mask);
1600 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1601 nd_key->nd_tll, ETH_ALEN, is_mask);
1602 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
1603 }
1604
1605 if (attrs != 0)
1606 return -EINVAL;
1607
1608 return 0;
1609}
1610
1611/**
1612 * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
1613 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1614 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1615 * does not include any don't care bit.
1616 * @match: receives the extracted flow match information.
1617 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1618 * sequence. The fields should of the packet that triggered the creation
1619 * of this flow.
1620 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1621 * attribute specifies the mask field of the wildcarded flow.
1622 */
1623int ovs_match_from_nlattrs(struct sw_flow_match *match,
1624 const struct nlattr *key,
1625 const struct nlattr *mask)
1626{
1627 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
a1c564be
AZ
1628 const struct nlattr *encap;
1629 u64 key_attrs = 0;
1630 u64 mask_attrs = 0;
1631 bool encap_valid = false;
1632 int err;
1633
1634 err = parse_flow_nlattrs(key, a, &key_attrs);
1635 if (err)
1636 return err;
1637
1638 if (key_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1639 encap = a[OVS_KEY_ATTR_ENCAP];
1640 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1641 if (nla_len(encap)) {
1642 __be16 eth_type = 0; /* ETH_P_8021Q */
1643
1644 if (a[OVS_KEY_ATTR_ETHERTYPE])
1645 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1646
1647 if ((eth_type == htons(ETH_P_8021Q)) && (a[OVS_KEY_ATTR_VLAN])) {
1648 encap_valid = true;
1649 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1650 err = parse_flow_nlattrs(encap, a, &key_attrs);
1b936472
AZ
1651 } else {
1652 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
a1c564be 1653 err = -EINVAL;
1b936472 1654 }
a1c564be 1655
34118cae
BP
1656 if (err)
1657 return err;
36956a7d 1658 }
a1c564be 1659 }
36956a7d 1660
a1c564be
AZ
1661 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
1662 if (err)
1663 return err;
1664
1665 if (mask) {
1a02b768 1666 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
a1c564be
AZ
1667 if (err)
1668 return err;
1669
1670 if ((mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) && encap_valid) {
1671 __be16 eth_type = 0;
1672
063236f1 1673 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1a02b768
JG
1674 if (a[OVS_KEY_ATTR_ETHERTYPE])
1675 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
a1c564be
AZ
1676 if (eth_type == htons(0xffff)) {
1677 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1a02b768
JG
1678 encap = a[OVS_KEY_ATTR_ENCAP];
1679 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
1b936472
AZ
1680 } else {
1681 OVS_NLERR("VLAN frames must have an exact match"
1682 " on the TPID (mask=%x).\n",
1683 ntohs(eth_type));
a1c564be 1684 err = -EINVAL;
1b936472 1685 }
34118cae 1686
34118cae
BP
1687 if (err)
1688 return err;
1689 }
36956a7d 1690
1a02b768 1691 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
a1c564be
AZ
1692 if (err)
1693 return err;
1694 } else {
1695 /* Populate exact match flow's key mask. */
1696 if (match->mask)
1697 ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
34118cae 1698 }
76abe283 1699
a1c564be 1700 if (!ovs_match_validate(match, key_attrs, mask_attrs))
34118cae 1701 return -EINVAL;
34118cae
BP
1702
1703 return 0;
36956a7d
BP
1704}
1705
80e5eed9 1706/**
850b6b3b 1707 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
c3b36027 1708 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
c3b36027 1709 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
80e5eed9
BP
1710 * sequence.
1711 *
1712 * This parses a series of Netlink attributes that form a flow key, which must
1713 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1714 * get the metadata, that is, the parts of the flow key that cannot be
1715 * extracted from the packet itself.
1716 */
13e24889 1717
a1c564be
AZ
1718int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
1719 const struct nlattr *attr)
80e5eed9 1720{
6694e498 1721 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
a1c564be
AZ
1722 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1723 u64 attrs = 0;
1724 int err;
1725 struct sw_flow_match match;
80e5eed9 1726
13e24889
PS
1727 flow->key.phy.in_port = DP_MAX_PORTS;
1728 flow->key.phy.priority = 0;
72e8bf28 1729 flow->key.phy.skb_mark = 0;
6694e498 1730 memset(tun_key, 0, sizeof(flow->key.tun_key));
80e5eed9 1731
a1c564be
AZ
1732 err = parse_flow_nlattrs(attr, a, &attrs);
1733 if (err)
80e5eed9 1734 return -EINVAL;
13e24889 1735
46d4c97f
JG
1736 memset(&match, 0, sizeof(match));
1737 match.key = &flow->key;
a1c564be
AZ
1738
1739 err = metadata_from_nlattrs(&match, &attrs, a, false);
1740 if (err)
1741 return err;
13e24889 1742
80e5eed9
BP
1743 return 0;
1744}
1745
a1c564be
AZ
1746int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
1747 const struct sw_flow_key *output, struct sk_buff *skb)
36956a7d 1748{
df2c07f4 1749 struct ovs_key_ethernet *eth_key;
fea393b1 1750 struct nlattr *nla, *encap;
a583e032 1751 bool is_mask = (swkey != output);
36956a7d 1752
dd62b991 1753 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
c3cc8c03 1754 goto nla_put_failure;
18c43631 1755
01fcdfc6 1756 if ((swkey->tun_key.ipv4_dst || is_mask) &&
54cc3ec6 1757 ovs_ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
9b405f1a
PS
1758 goto nla_put_failure;
1759
04b13268 1760 if (swkey->phy.in_port == DP_MAX_PORTS) {
a583e032 1761 if (is_mask && (output->phy.in_port == 0xffff))
04b13268
AZ
1762 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1763 goto nla_put_failure;
1764 } else {
a1c564be 1765 u16 upper_u16;
a583e032 1766 upper_u16 = !is_mask ? 0 : 0xffff;
a1c564be
AZ
1767
1768 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
04b13268 1769 (upper_u16 << 16) | output->phy.in_port))
a1c564be
AZ
1770 goto nla_put_failure;
1771 }
36956a7d 1772
dd62b991 1773 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
72e8bf28
AA
1774 goto nla_put_failure;
1775
df2c07f4 1776 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
d6569377
BP
1777 if (!nla)
1778 goto nla_put_failure;
a1c564be 1779
d6569377 1780 eth_key = nla_data(nla);
a1c564be
AZ
1781 memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
1782 memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
36956a7d 1783
8ddc056d 1784 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
a1c564be 1785 __be16 eth_type;
a583e032 1786 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
a1c564be
AZ
1787 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1788 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
c3cc8c03 1789 goto nla_put_failure;
fea393b1 1790 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
8ddc056d
BP
1791 if (!swkey->eth.tci)
1792 goto unencap;
a1c564be 1793 } else
fea393b1 1794 encap = NULL;
36956a7d 1795
04b13268
AZ
1796 if (swkey->eth.type == htons(ETH_P_802_2)) {
1797 /*
1798 * Ethertype 802.2 is represented in the netlink with omitted
1799 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1800 * 0xffff in the mask attribute. Ethertype can also
1801 * be wildcarded.
1802 */
a583e032 1803 if (is_mask && output->eth.type)
04b13268
AZ
1804 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1805 output->eth.type))
1806 goto nla_put_failure;
fea393b1 1807 goto unencap;
04b13268 1808 }
36956a7d 1809
04b13268
AZ
1810 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1811 goto nla_put_failure;
36956a7d 1812
76abe283 1813 if (swkey->eth.type == htons(ETH_P_IP)) {
df2c07f4 1814 struct ovs_key_ipv4 *ipv4_key;
36956a7d 1815
df2c07f4 1816 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
d6569377
BP
1817 if (!nla)
1818 goto nla_put_failure;
1819 ipv4_key = nla_data(nla);
a1c564be
AZ
1820 ipv4_key->ipv4_src = output->ipv4.addr.src;
1821 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1822 ipv4_key->ipv4_proto = output->ip.proto;
1823 ipv4_key->ipv4_tos = output->ip.tos;
1824 ipv4_key->ipv4_ttl = output->ip.ttl;
1825 ipv4_key->ipv4_frag = output->ip.frag;
76abe283 1826 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
df2c07f4 1827 struct ovs_key_ipv6 *ipv6_key;
d31f1109 1828
df2c07f4 1829 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
d31f1109
JP
1830 if (!nla)
1831 goto nla_put_failure;
1832 ipv6_key = nla_data(nla);
a1c564be 1833 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
d31f1109 1834 sizeof(ipv6_key->ipv6_src));
a1c564be 1835 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
d31f1109 1836 sizeof(ipv6_key->ipv6_dst));
a1c564be
AZ
1837 ipv6_key->ipv6_label = output->ipv6.label;
1838 ipv6_key->ipv6_proto = output->ip.proto;
1839 ipv6_key->ipv6_tclass = output->ip.tos;
1840 ipv6_key->ipv6_hlimit = output->ip.ttl;
1841 ipv6_key->ipv6_frag = output->ip.frag;
8087f5ff
MM
1842 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1843 swkey->eth.type == htons(ETH_P_RARP)) {
df2c07f4 1844 struct ovs_key_arp *arp_key;
d31f1109 1845
df2c07f4 1846 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
d31f1109
JP
1847 if (!nla)
1848 goto nla_put_failure;
1849 arp_key = nla_data(nla);
df2c07f4 1850 memset(arp_key, 0, sizeof(struct ovs_key_arp));
a1c564be
AZ
1851 arp_key->arp_sip = output->ipv4.addr.src;
1852 arp_key->arp_tip = output->ipv4.addr.dst;
1853 arp_key->arp_op = htons(output->ip.proto);
1854 memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
1855 memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
d31f1109
JP
1856 }
1857
7257b535
BP
1858 if ((swkey->eth.type == htons(ETH_P_IP) ||
1859 swkey->eth.type == htons(ETH_P_IPV6)) &&
9e44d715 1860 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
36956a7d 1861
28bad473 1862 if (swkey->ip.proto == IPPROTO_TCP) {
df2c07f4 1863 struct ovs_key_tcp *tcp_key;
36956a7d 1864
df2c07f4 1865 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
d6569377
BP
1866 if (!nla)
1867 goto nla_put_failure;
1868 tcp_key = nla_data(nla);
76abe283 1869 if (swkey->eth.type == htons(ETH_P_IP)) {
a1c564be
AZ
1870 tcp_key->tcp_src = output->ipv4.tp.src;
1871 tcp_key->tcp_dst = output->ipv4.tp.dst;
76abe283 1872 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
a1c564be
AZ
1873 tcp_key->tcp_src = output->ipv6.tp.src;
1874 tcp_key->tcp_dst = output->ipv6.tp.dst;
76abe283 1875 }
28bad473 1876 } else if (swkey->ip.proto == IPPROTO_UDP) {
df2c07f4 1877 struct ovs_key_udp *udp_key;
36956a7d 1878
df2c07f4 1879 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
d6569377
BP
1880 if (!nla)
1881 goto nla_put_failure;
1882 udp_key = nla_data(nla);
76abe283 1883 if (swkey->eth.type == htons(ETH_P_IP)) {
a1c564be
AZ
1884 udp_key->udp_src = output->ipv4.tp.src;
1885 udp_key->udp_dst = output->ipv4.tp.dst;
76abe283 1886 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
a1c564be
AZ
1887 udp_key->udp_src = output->ipv6.tp.src;
1888 udp_key->udp_dst = output->ipv6.tp.dst;
76abe283 1889 }
10f72e3d
JS
1890 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1891 struct ovs_key_sctp *sctp_key;
1892
1893 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1894 if (!nla)
1895 goto nla_put_failure;
1896 sctp_key = nla_data(nla);
1897 if (swkey->eth.type == htons(ETH_P_IP)) {
1898 sctp_key->sctp_src = swkey->ipv4.tp.src;
1899 sctp_key->sctp_dst = swkey->ipv4.tp.dst;
1900 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1901 sctp_key->sctp_src = swkey->ipv6.tp.src;
1902 sctp_key->sctp_dst = swkey->ipv6.tp.dst;
1903 }
76abe283 1904 } else if (swkey->eth.type == htons(ETH_P_IP) &&
28bad473 1905 swkey->ip.proto == IPPROTO_ICMP) {
df2c07f4 1906 struct ovs_key_icmp *icmp_key;
36956a7d 1907
df2c07f4 1908 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
d6569377
BP
1909 if (!nla)
1910 goto nla_put_failure;
1911 icmp_key = nla_data(nla);
a1c564be
AZ
1912 icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
1913 icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
76abe283 1914 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
28bad473 1915 swkey->ip.proto == IPPROTO_ICMPV6) {
df2c07f4 1916 struct ovs_key_icmpv6 *icmpv6_key;
36956a7d 1917
df2c07f4 1918 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
bfef4717 1919 sizeof(*icmpv6_key));
d31f1109
JP
1920 if (!nla)
1921 goto nla_put_failure;
1922 icmpv6_key = nla_data(nla);
a1c564be
AZ
1923 icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
1924 icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
685a51a5 1925
bfef4717
JG
1926 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1927 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
df2c07f4 1928 struct ovs_key_nd *nd_key;
685a51a5 1929
df2c07f4 1930 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
685a51a5
JP
1931 if (!nla)
1932 goto nla_put_failure;
1933 nd_key = nla_data(nla);
a1c564be 1934 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
685a51a5 1935 sizeof(nd_key->nd_target));
a1c564be
AZ
1936 memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
1937 memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
685a51a5 1938 }
d31f1109 1939 }
36956a7d
BP
1940 }
1941
fea393b1
BP
1942unencap:
1943 if (encap)
1944 nla_nest_end(skb, encap);
1945
d6569377 1946 return 0;
36956a7d 1947
d6569377
BP
1948nla_put_failure:
1949 return -EMSGSIZE;
8d5ebd83
JG
1950}
1951
064af421
BP
1952/* Initializes the flow module.
1953 * Returns zero if successful or a negative error code. */
850b6b3b 1954int ovs_flow_init(void)
064af421
BP
1955{
1956 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1957 0, NULL);
1958 if (flow_cache == NULL)
1959 return -ENOMEM;
1960
1961 return 0;
1962}
1963
1964/* Uninitializes the flow module. */
850b6b3b 1965void ovs_flow_exit(void)
064af421
BP
1966{
1967 kmem_cache_destroy(flow_cache);
1968}
a1c564be
AZ
1969
1970struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
1971{
1972 struct sw_flow_mask *mask;
1973
1974 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
1975 if (mask)
1976 mask->ref_count = 0;
1977
1978 return mask;
1979}
1980
1981void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
1982{
1983 mask->ref_count++;
1984}
1985
1986static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu)
1987{
1988 struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu);
1989
1990 kfree(mask);
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 call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
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
ede77a46 2026 list_for_each(ml, tbl->mask_list) {
a1c564be
AZ
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{
ede77a46 2043 list_add_rcu(&mask->list, tbl->mask_list);
a1c564be
AZ
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}