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