]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/flow.c
datapath: Fix recv path for CONFIG_PREEMPT_RCU.
[mirror_ovs.git] / datapath / flow.c
CommitLineData
064af421
BP
1/*
2 * Distributed under the terms of the GNU GPL version 2.
f632c8fc 3 * Copyright (c) 2007, 2008, 2009, 2010, 2011 Nicira Networks.
a14bc59f
BP
4 *
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
064af421
BP
7 */
8
9#include "flow.h"
f5e86186 10#include "datapath.h"
36956a7d 11#include <asm/uaccess.h>
064af421
BP
12#include <linux/netdevice.h>
13#include <linux/etherdevice.h>
14#include <linux/if_ether.h>
15#include <linux/if_vlan.h>
16#include <net/llc_pdu.h>
17#include <linux/kernel.h>
8d5ebd83 18#include <linux/jhash.h>
064af421
BP
19#include <linux/jiffies.h>
20#include <linux/llc.h>
21#include <linux/module.h>
22#include <linux/in.h>
23#include <linux/rcupdate.h>
a26ef517 24#include <linux/if_arp.h>
064af421
BP
25#include <linux/if_ether.h>
26#include <linux/ip.h>
d31f1109 27#include <linux/ipv6.h>
064af421
BP
28#include <linux/tcp.h>
29#include <linux/udp.h>
30#include <linux/icmp.h>
d31f1109 31#include <linux/icmpv6.h>
3544358a 32#include <linux/rculist.h>
3c5f6de3 33#include <net/inet_ecn.h>
064af421 34#include <net/ip.h>
d31f1109 35#include <net/ipv6.h>
685a51a5 36#include <net/ndisc.h>
064af421 37
6ce39213
JG
38#include "vlan.h"
39
33b38b63 40static struct kmem_cache *flow_cache;
83e3e75b 41static unsigned int hash_seed __read_mostly;
064af421 42
2db65bf7
JG
43static int check_header(struct sk_buff *skb, int len)
44{
45 if (unlikely(skb->len < len))
46 return -EINVAL;
47 if (unlikely(!pskb_may_pull(skb, len)))
48 return -ENOMEM;
49 return 0;
50}
51
e819fb47 52static inline bool arphdr_ok(struct sk_buff *skb)
a26ef517 53{
2db65bf7
JG
54 return pskb_may_pull(skb, skb_network_offset(skb) +
55 sizeof(struct arp_eth_header));
a26ef517
JP
56}
57
4c1ad233 58static inline int check_iphdr(struct sk_buff *skb)
064af421 59{
4c1ad233
BP
60 unsigned int nh_ofs = skb_network_offset(skb);
61 unsigned int ip_len;
2db65bf7 62 int err;
4c1ad233 63
2db65bf7
JG
64 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
65 if (unlikely(err))
66 return err;
4c1ad233
BP
67
68 ip_len = ip_hdrlen(skb);
2db65bf7
JG
69 if (unlikely(ip_len < sizeof(struct iphdr) ||
70 skb->len < nh_ofs + ip_len))
4c1ad233
BP
71 return -EINVAL;
72
4c1ad233
BP
73 skb_set_transport_header(skb, nh_ofs + ip_len);
74 return 0;
064af421
BP
75}
76
e819fb47 77static inline bool tcphdr_ok(struct sk_buff *skb)
064af421
BP
78{
79 int th_ofs = skb_transport_offset(skb);
2db65bf7
JG
80 int tcp_len;
81
82 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
83 return false;
84
85 tcp_len = tcp_hdrlen(skb);
86 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
87 skb->len < th_ofs + tcp_len))
88 return false;
89
90 return true;
064af421
BP
91}
92
e819fb47 93static inline bool udphdr_ok(struct sk_buff *skb)
064af421 94{
2db65bf7
JG
95 return pskb_may_pull(skb, skb_transport_offset(skb) +
96 sizeof(struct udphdr));
064af421
BP
97}
98
e819fb47 99static inline bool icmphdr_ok(struct sk_buff *skb)
064af421 100{
2db65bf7
JG
101 return pskb_may_pull(skb, skb_transport_offset(skb) +
102 sizeof(struct icmphdr));
064af421
BP
103}
104
ec58547a
JG
105u64 flow_used_time(unsigned long flow_jiffies)
106{
107 struct timespec cur_ts;
108 u64 cur_ms, idle_ms;
109
110 ktime_get_ts(&cur_ts);
111 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
112 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
113 cur_ts.tv_nsec / NSEC_PER_MSEC;
114
115 return cur_ms - idle_ms;
116}
117
76abe283
AE
118#define SW_FLOW_KEY_OFFSET(field) \
119 offsetof(struct sw_flow_key, field) + \
120 FIELD_SIZEOF(struct sw_flow_key, field)
121
122static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key,
123 int *key_lenp)
d31f1109
JP
124{
125 unsigned int nh_ofs = skb_network_offset(skb);
126 unsigned int nh_len;
127 int payload_ofs;
d31f1109
JP
128 struct ipv6hdr *nh;
129 uint8_t nexthdr;
2db65bf7 130 int err;
d31f1109 131
76abe283
AE
132 *key_lenp = SW_FLOW_KEY_OFFSET(ipv6.addr);
133
2db65bf7
JG
134 err = check_header(skb, nh_ofs + sizeof(*nh));
135 if (unlikely(err))
136 return err;
d31f1109
JP
137
138 nh = ipv6_hdr(skb);
139 nexthdr = nh->nexthdr;
140 payload_ofs = (u8 *)(nh + 1) - skb->data;
d31f1109 141
28bad473
JG
142 key->ip.proto = NEXTHDR_NONE;
143 key->ip.tos = ipv6_get_dsfield(nh) & ~INET_ECN_MASK;
76abe283
AE
144 ipv6_addr_copy(&key->ipv6.addr.src, &nh->saddr);
145 ipv6_addr_copy(&key->ipv6.addr.dst, &nh->daddr);
d31f1109 146
d31f1109 147 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr);
bfef4717 148 if (unlikely(payload_ofs < 0))
d31f1109 149 return -EINVAL;
bfef4717 150
d31f1109 151 nh_len = payload_ofs - nh_ofs;
d31f1109 152 skb_set_transport_header(skb, nh_ofs + nh_len);
28bad473 153 key->ip.proto = nexthdr;
d31f1109
JP
154 return nh_len;
155}
156
157static bool icmp6hdr_ok(struct sk_buff *skb)
158{
2db65bf7
JG
159 return pskb_may_pull(skb, skb_transport_offset(skb) +
160 sizeof(struct icmp6hdr));
d31f1109 161}
ec58547a 162
064af421
BP
163#define TCP_FLAGS_OFFSET 13
164#define TCP_FLAG_MASK 0x3f
165
064af421
BP
166void flow_used(struct sw_flow *flow, struct sk_buff *skb)
167{
064af421
BP
168 u8 tcp_flags = 0;
169
76abe283 170 if (flow->key.eth.type == htons(ETH_P_IP) &&
28bad473 171 flow->key.ip.proto == IPPROTO_TCP) {
abfec865
BP
172 u8 *tcp = (u8 *)tcp_hdr(skb);
173 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
064af421
BP
174 }
175
f2459fe7 176 spin_lock_bh(&flow->lock);
6bfafa55 177 flow->used = jiffies;
064af421
BP
178 flow->packet_count++;
179 flow->byte_count += skb->len;
180 flow->tcp_flags |= tcp_flags;
f2459fe7 181 spin_unlock_bh(&flow->lock);
064af421
BP
182}
183
37a1300c 184struct sw_flow_actions *flow_actions_alloc(const struct nlattr *actions)
064af421 185{
37a1300c 186 int actions_len = nla_len(actions);
064af421
BP
187 struct sw_flow_actions *sfa;
188
722d19c5
BP
189 /* At least DP_MAX_PORTS actions are required to be able to flood a
190 * packet to every port. Factor of 2 allows for setting VLAN tags,
191 * etc. */
cdee00fd 192 if (actions_len > 2 * DP_MAX_PORTS * nla_total_size(4))
064af421
BP
193 return ERR_PTR(-EINVAL);
194
84c17d98 195 sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL);
064af421
BP
196 if (!sfa)
197 return ERR_PTR(-ENOMEM);
198
cdee00fd 199 sfa->actions_len = actions_len;
37a1300c 200 memcpy(sfa->actions, nla_data(actions), actions_len);
064af421
BP
201 return sfa;
202}
203
560e8022
JG
204struct sw_flow *flow_alloc(void)
205{
206 struct sw_flow *flow;
207
208 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
209 if (!flow)
210 return ERR_PTR(-ENOMEM);
211
212 spin_lock_init(&flow->lock);
fb8c9347 213 atomic_set(&flow->refcnt, 1);
6f04e94a 214 flow->sf_acts = NULL;
fb8c9347 215 flow->dead = false;
064af421 216
560e8022
JG
217 return flow;
218}
219
3544358a 220static struct hlist_head __rcu *find_bucket(struct flow_table * table, u32 hash)
8d5ebd83 221{
3544358a
PS
222 return flex_array_get(table->buckets,
223 (hash & (table->n_buckets - 1)));
224}
225
226static struct flex_array __rcu *alloc_buckets(unsigned int n_buckets)
227{
228 struct flex_array __rcu * buckets;
229 int i, err;
230
231 buckets = flex_array_alloc(sizeof(struct hlist_head *),
232 n_buckets, GFP_KERNEL);
233 if (!buckets)
234 return NULL;
235
236 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
237 if (err) {
238 flex_array_free(buckets);
239 return NULL;
240 }
241
242 for (i = 0; i < n_buckets; i++)
243 INIT_HLIST_HEAD((struct hlist_head *)
244 flex_array_get(buckets, i));
245
246 return buckets;
247}
248
249static void free_buckets(struct flex_array * buckets)
250{
251 flex_array_free(buckets);
252}
253
254struct flow_table *flow_tbl_alloc(int new_size)
255{
256 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
257
258 if (!table)
259 return NULL;
260
261 table->buckets = alloc_buckets(new_size);
262
263 if (!table->buckets) {
264 kfree(table);
265 return NULL;
266 }
267 table->n_buckets = new_size;
268 table->count = 0;
269
270 return table;
271}
fb8c9347 272
3544358a
PS
273static void flow_free(struct sw_flow *flow)
274{
fb8c9347
JG
275 flow->dead = true;
276 flow_put(flow);
8d5ebd83
JG
277}
278
3544358a
PS
279void flow_tbl_destroy(struct flow_table *table)
280{
281 int i;
282
283 if (!table)
284 return;
285
286 for (i = 0; i < table->n_buckets; i++) {
287 struct sw_flow *flow;
288 struct hlist_head *head = flex_array_get(table->buckets, i);
289 struct hlist_node *node, *n;
290
291 hlist_for_each_entry_safe(flow, node, n, head, hash_node) {
292 hlist_del_init_rcu(&flow->hash_node);
293 flow_free(flow);
294 }
295 }
296
297 free_buckets(table->buckets);
298 kfree(table);
299}
300
301static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
302{
303 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
304
305 flow_tbl_destroy(table);
306}
307
308void flow_tbl_deferred_destroy(struct flow_table *table)
309{
310 if (!table)
311 return;
312
313 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
314}
315
316struct sw_flow *flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last)
317{
318 struct sw_flow *flow;
319 struct hlist_head *head;
320 struct hlist_node *n;
321 int i;
322
323 while (*bucket < table->n_buckets) {
324 i = 0;
325 head = flex_array_get(table->buckets, *bucket);
326 hlist_for_each_entry_rcu(flow, n, head, hash_node) {
327 if (i < *last) {
328 i++;
329 continue;
330 }
331 *last = i + 1;
332 return flow;
333 }
334 (*bucket)++;
335 *last = 0;
336 }
337
338 return NULL;
339}
340
341struct flow_table *flow_tbl_expand(struct flow_table *table)
342{
343 struct flow_table *new_table;
344 int n_buckets = table->n_buckets * 2;
345 int i;
346
347 new_table = flow_tbl_alloc(n_buckets);
348 if (!new_table)
349 return ERR_PTR(-ENOMEM);
350
351 for (i = 0; i < table->n_buckets; i++) {
352 struct sw_flow *flow;
353 struct hlist_head *head;
354 struct hlist_node *n, *pos;
355
356 head = flex_array_get(table->buckets, i);
357
358 hlist_for_each_entry_safe(flow, n, pos, head, hash_node) {
359 hlist_del_init_rcu(&flow->hash_node);
360 flow_tbl_insert(new_table, flow);
361 }
362 }
363
364 return new_table;
365}
366
064af421
BP
367/* RCU callback used by flow_deferred_free. */
368static void rcu_free_flow_callback(struct rcu_head *rcu)
369{
370 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
fb8c9347
JG
371
372 flow->dead = true;
373 flow_put(flow);
064af421
BP
374}
375
376/* Schedules 'flow' to be freed after the next RCU grace period.
377 * The caller must hold rcu_read_lock for this to be sensible. */
378void flow_deferred_free(struct sw_flow *flow)
379{
380 call_rcu(&flow->rcu, rcu_free_flow_callback);
381}
382
fb8c9347
JG
383void flow_hold(struct sw_flow *flow)
384{
385 atomic_inc(&flow->refcnt);
386}
387
388void flow_put(struct sw_flow *flow)
389{
390 if (unlikely(!flow))
391 return;
392
393 if (atomic_dec_and_test(&flow->refcnt)) {
39872c70 394 kfree((struct sf_flow_acts __force *)flow->sf_acts);
fb8c9347
JG
395 kmem_cache_free(flow_cache, flow);
396 }
397}
398
064af421
BP
399/* RCU callback used by flow_deferred_free_acts. */
400static void rcu_free_acts_callback(struct rcu_head *rcu)
401{
d295e8e9 402 struct sw_flow_actions *sf_acts = container_of(rcu,
064af421
BP
403 struct sw_flow_actions, rcu);
404 kfree(sf_acts);
405}
406
407/* Schedules 'sf_acts' to be freed after the next RCU grace period.
408 * The caller must hold rcu_read_lock for this to be sensible. */
409void flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
410{
411 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
412}
413
2db65bf7 414static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
064af421 415{
50f06e16
BP
416 struct qtag_prefix {
417 __be16 eth_type; /* ETH_P_8021Q */
418 __be16 tci;
419 };
420 struct qtag_prefix *qp;
421
2db65bf7
JG
422 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
423 sizeof(__be16))))
424 return -ENOMEM;
50f06e16
BP
425
426 qp = (struct qtag_prefix *) skb->data;
76abe283 427 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
50f06e16 428 __skb_pull(skb, sizeof(struct qtag_prefix));
2db65bf7
JG
429
430 return 0;
50f06e16
BP
431}
432
433static __be16 parse_ethertype(struct sk_buff *skb)
064af421 434{
50f06e16
BP
435 struct llc_snap_hdr {
436 u8 dsap; /* Always 0xAA */
437 u8 ssap; /* Always 0xAA */
438 u8 ctrl;
439 u8 oui[3];
8dda8c9b 440 __be16 ethertype;
50f06e16
BP
441 };
442 struct llc_snap_hdr *llc;
443 __be16 proto;
444
445 proto = *(__be16 *) skb->data;
446 __skb_pull(skb, sizeof(__be16));
447
36956a7d 448 if (ntohs(proto) >= 1536)
50f06e16
BP
449 return proto;
450
2db65bf7 451 if (skb->len < sizeof(struct llc_snap_hdr))
36956a7d 452 return htons(ETH_P_802_2);
50f06e16 453
2db65bf7
JG
454 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
455 return htons(0);
456
50f06e16
BP
457 llc = (struct llc_snap_hdr *) skb->data;
458 if (llc->dsap != LLC_SAP_SNAP ||
459 llc->ssap != LLC_SAP_SNAP ||
460 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
36956a7d 461 return htons(ETH_P_802_2);
50f06e16
BP
462
463 __skb_pull(skb, sizeof(struct llc_snap_hdr));
464 return llc->ethertype;
064af421
BP
465}
466
685a51a5 467static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
76abe283 468 int *key_lenp, int nh_len)
685a51a5 469{
685a51a5 470 struct icmp6hdr *icmp = icmp6_hdr(skb);
76abe283
AE
471 int error = 0;
472 int key_len;
685a51a5
JP
473
474 /* The ICMPv6 type and code fields use the 16-bit transport port
bfef4717
JG
475 * fields, so we need to store them in 16-bit network byte order.
476 */
76abe283
AE
477 key->ipv6.tp.src = htons(icmp->icmp6_type);
478 key->ipv6.tp.dst = htons(icmp->icmp6_code);
479 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
685a51a5 480
bfef4717
JG
481 if (icmp->icmp6_code == 0 &&
482 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
483 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
e977ba19 484 int icmp_len = skb->len - skb_transport_offset(skb);
685a51a5
JP
485 struct nd_msg *nd;
486 int offset;
487
76abe283
AE
488 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
489
685a51a5 490 /* In order to process neighbor discovery options, we need the
bfef4717
JG
491 * entire packet.
492 */
493 if (unlikely(icmp_len < sizeof(*nd)))
76abe283
AE
494 goto out;
495 if (unlikely(skb_linearize(skb))) {
496 error = -ENOMEM;
497 goto out;
498 }
685a51a5
JP
499
500 nd = (struct nd_msg *)skb_transport_header(skb);
76abe283
AE
501 ipv6_addr_copy(&key->ipv6.nd.target, &nd->target);
502 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
685a51a5
JP
503
504 icmp_len -= sizeof(*nd);
505 offset = 0;
506 while (icmp_len >= 8) {
507 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd->opt + offset);
508 int opt_len = nd_opt->nd_opt_len * 8;
509
bfef4717 510 if (unlikely(!opt_len || opt_len > icmp_len))
685a51a5
JP
511 goto invalid;
512
bfef4717
JG
513 /* Store the link layer address if the appropriate
514 * option is provided. It is considered an error if
515 * the same link layer option is specified twice.
516 */
685a51a5 517 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
bfef4717 518 && opt_len == 8) {
76abe283 519 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
685a51a5 520 goto invalid;
76abe283 521 memcpy(key->ipv6.nd.sll,
bfef4717 522 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
685a51a5 523 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
bfef4717 524 && opt_len == 8) {
76abe283 525 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
685a51a5 526 goto invalid;
76abe283 527 memcpy(key->ipv6.nd.tll,
bfef4717 528 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
685a51a5
JP
529 }
530
531 icmp_len -= opt_len;
532 offset += opt_len;
533 }
534 }
535
76abe283 536 goto out;
685a51a5
JP
537
538invalid:
76abe283
AE
539 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
540 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
541 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
685a51a5 542
76abe283
AE
543out:
544 *key_lenp = key_len;
545 return error;
685a51a5
JP
546}
547
a31e0e31
BP
548/**
549 * flow_extract - extracts a flow key from an Ethernet frame.
550 * @skb: sk_buff that contains the frame, with skb->data pointing to the
551 * Ethernet header
552 * @in_port: port number on which @skb was received.
553 * @key: output flow key
76abe283 554 * @key_lenp: length of output flow key
26233bb4
BP
555 * @is_frag: set to 1 if @skb contains an IPv4 fragment, or to 0 if @skb does
556 * not contain an IPv4 packet or if it is not a fragment.
a31e0e31
BP
557 *
558 * The caller must ensure that skb->len >= ETH_HLEN.
559 *
4c1ad233
BP
560 * Returns 0 if successful, otherwise a negative errno value.
561 *
59a18f80
BP
562 * Initializes @skb header pointers as follows:
563 *
564 * - skb->mac_header: the Ethernet header.
565 *
566 * - skb->network_header: just past the Ethernet header, or just past the
567 * VLAN header, to the first byte of the Ethernet payload.
568 *
d31f1109
JP
569 * - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
570 * on output, then just past the IP header, if one is present and
571 * of a correct length, otherwise the same as skb->network_header.
572 * For other key->dl_type values it is left untouched.
a31e0e31 573 */
36956a7d 574int flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
76abe283 575 int *key_lenp, bool *is_frag)
064af421 576{
76abe283
AE
577 int error = 0;
578 int key_len = SW_FLOW_KEY_OFFSET(eth);
064af421 579 struct ethhdr *eth;
064af421 580
84c17d98 581 memset(key, 0, sizeof(*key));
76abe283
AE
582 key->eth.tun_id = OVS_CB(skb)->tun_id;
583 key->eth.in_port = in_port;
b7a31ec1 584 *is_frag = false;
064af421 585
064af421 586 skb_reset_mac_header(skb);
064af421 587
2db65bf7
JG
588 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
589 * header in the linear data area.
590 */
50f06e16 591 eth = eth_hdr(skb);
76abe283
AE
592 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
593 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
594
50f06e16 595 __skb_pull(skb, 2 * ETH_ALEN);
6ce39213
JG
596
597 if (vlan_tx_tag_present(skb))
76abe283 598 key->eth.tci = htons(vlan_get_tci(skb));
6ce39213 599 else if (eth->h_proto == htons(ETH_P_8021Q))
2db65bf7
JG
600 if (unlikely(parse_vlan(skb, key)))
601 return -ENOMEM;
6ce39213 602
76abe283
AE
603 key->eth.type = parse_ethertype(skb);
604 if (unlikely(key->eth.type == htons(0)))
2db65bf7
JG
605 return -ENOMEM;
606
50f06e16 607 skb_reset_network_header(skb);
2db65bf7 608 __skb_push(skb, skb->data - skb_mac_header(skb));
064af421
BP
609
610 /* Network layer. */
76abe283 611 if (key->eth.type == htons(ETH_P_IP)) {
4c1ad233 612 struct iphdr *nh;
76abe283
AE
613
614 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
4c1ad233
BP
615
616 error = check_iphdr(skb);
617 if (unlikely(error)) {
618 if (error == -EINVAL) {
619 skb->transport_header = skb->network_header;
76abe283 620 error = 0;
4c1ad233 621 }
76abe283 622 goto out;
4c1ad233
BP
623 }
624
625 nh = ip_hdr(skb);
76abe283
AE
626 key->ipv4.addr.src = nh->saddr;
627 key->ipv4.addr.dst = nh->daddr;
28bad473
JG
628 key->ip.tos = nh->tos & ~INET_ECN_MASK;
629 key->ip.proto = nh->protocol;
064af421
BP
630
631 /* Transport layer. */
5f4d087c
JG
632 if ((nh->frag_off & htons(IP_MF | IP_OFFSET)) ||
633 (skb_shinfo(skb)->gso_type & SKB_GSO_UDP))
b7a31ec1
JG
634 *is_frag = true;
635
28bad473 636 if (key->ip.proto == IPPROTO_TCP) {
5f4d087c
JG
637 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
638 if (!*is_frag && tcphdr_ok(skb)) {
639 struct tcphdr *tcp = tcp_hdr(skb);
640 key->ipv4.tp.src = tcp->source;
641 key->ipv4.tp.dst = tcp->dest;
642 }
28bad473 643 } else if (key->ip.proto == IPPROTO_UDP) {
5f4d087c
JG
644 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
645 if (!*is_frag && udphdr_ok(skb)) {
646 struct udphdr *udp = udp_hdr(skb);
647 key->ipv4.tp.src = udp->source;
648 key->ipv4.tp.dst = udp->dest;
649 }
28bad473 650 } else if (key->ip.proto == IPPROTO_ICMP) {
5f4d087c
JG
651 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
652 if (!*is_frag && icmphdr_ok(skb)) {
653 struct icmphdr *icmp = icmp_hdr(skb);
654 /* The ICMP type and code fields use the 16-bit
655 * transport port fields, so we need to store them
656 * in 16-bit network byte order. */
657 key->ipv4.tp.src = htons(icmp->type);
658 key->ipv4.tp.dst = htons(icmp->code);
659 }
660 }
661
76abe283 662 } else if (key->eth.type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
a26ef517
JP
663 struct arp_eth_header *arp;
664
665 arp = (struct arp_eth_header *)skb_network_header(skb);
666
f5e86186 667 if (arp->ar_hrd == htons(ARPHRD_ETHER)
de3f65ea
JP
668 && arp->ar_pro == htons(ETH_P_IP)
669 && arp->ar_hln == ETH_ALEN
670 && arp->ar_pln == 4) {
671
672 /* We only match on the lower 8 bits of the opcode. */
b7a31ec1 673 if (ntohs(arp->ar_op) <= 0xff)
28bad473 674 key->ip.proto = ntohs(arp->ar_op);
76abe283 675
28bad473
JG
676 if (key->ip.proto == ARPOP_REQUEST
677 || key->ip.proto == ARPOP_REPLY) {
76abe283
AE
678 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
679 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
680 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
681 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
682 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
de3f65ea
JP
683 }
684 }
76abe283 685 } else if (key->eth.type == htons(ETH_P_IPV6)) {
d31f1109
JP
686 int nh_len; /* IPv6 Header + Extensions */
687
76abe283 688 nh_len = parse_ipv6hdr(skb, key, &key_len);
d31f1109 689 if (unlikely(nh_len < 0)) {
76abe283 690 if (nh_len == -EINVAL)
d31f1109 691 skb->transport_header = skb->network_header;
76abe283
AE
692 else
693 error = nh_len;
694 goto out;
d31f1109
JP
695 }
696
697 /* Transport layer. */
28bad473 698 if (key->ip.proto == NEXTHDR_TCP) {
76abe283 699 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
d31f1109
JP
700 if (tcphdr_ok(skb)) {
701 struct tcphdr *tcp = tcp_hdr(skb);
76abe283
AE
702 key->ipv6.tp.src = tcp->source;
703 key->ipv6.tp.dst = tcp->dest;
d31f1109 704 }
28bad473 705 } else if (key->ip.proto == NEXTHDR_UDP) {
76abe283 706 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
d31f1109
JP
707 if (udphdr_ok(skb)) {
708 struct udphdr *udp = udp_hdr(skb);
76abe283
AE
709 key->ipv6.tp.src = udp->source;
710 key->ipv6.tp.dst = udp->dest;
d31f1109 711 }
28bad473 712 } else if (key->ip.proto == NEXTHDR_ICMP) {
76abe283 713 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
d31f1109 714 if (icmp6hdr_ok(skb)) {
76abe283 715 error = parse_icmpv6(skb, key, &key_len, nh_len);
685a51a5 716 if (error < 0)
76abe283 717 goto out;
d31f1109
JP
718 }
719 }
064af421 720 }
76abe283
AE
721
722out:
723 *key_lenp = key_len;
724 return error;
064af421
BP
725}
726
76abe283 727u32 flow_hash(const struct sw_flow_key *key, int key_len)
8d5ebd83 728{
76abe283 729 return jhash2((u32*)key, DIV_ROUND_UP(key_len, sizeof(u32)), hash_seed);
8d5ebd83
JG
730}
731
3544358a
PS
732struct sw_flow * flow_tbl_lookup(struct flow_table *table,
733 struct sw_flow_key *key, int key_len)
8d5ebd83 734{
3544358a
PS
735 struct sw_flow *flow;
736 struct hlist_node *n;
737 struct hlist_head *head;
738 u32 hash;
8d5ebd83 739
3544358a
PS
740 hash = flow_hash(key, key_len);
741
742 head = find_bucket(table, hash);
743 hlist_for_each_entry_rcu(flow, n, head, hash_node) {
744
745 if (flow->hash == hash &&
746 !memcmp(&flow->key, key, key_len)) {
747 return flow;
748 }
749 }
750 return NULL;
751}
752
753void flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
754{
755 struct hlist_head *head;
756
757 head = find_bucket(table, flow->hash);
758 hlist_add_head_rcu(&flow->hash_node, head);
759 table->count++;
760}
761
762void flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
763{
764 if (!hlist_unhashed(&flow->hash_node)) {
765 hlist_del_init_rcu(&flow->hash_node);
766 table->count--;
767 BUG_ON(table->count < 0);
768 }
36956a7d
BP
769}
770
df2c07f4
JP
771/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
772static const u32 key_lens[OVS_KEY_ATTR_MAX + 1] = {
773 [OVS_KEY_ATTR_TUN_ID] = 8,
774 [OVS_KEY_ATTR_IN_PORT] = 4,
775 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
776 [OVS_KEY_ATTR_8021Q] = sizeof(struct ovs_key_8021q),
777 [OVS_KEY_ATTR_ETHERTYPE] = 2,
778 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
779 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
780 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
781 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
782 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
783 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
784 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
785 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
80e5eed9
BP
786};
787
36956a7d
BP
788/**
789 * flow_from_nlattrs - parses Netlink attributes into a flow key.
790 * @swkey: receives the extracted flow key.
76abe283 791 * @key_lenp: number of bytes used in @swkey.
df2c07f4 792 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
d6569377 793 * sequence.
36956a7d
BP
794 *
795 * This state machine accepts the following forms, with [] for optional
796 * elements and | for alternatives:
797 *
18886b60 798 * [tun_id] [in_port] ethernet [8021q] [ethertype \
685a51a5 799 * [IPv4 [TCP|UDP|ICMP] | IPv6 [TCP|UDP|ICMPv6 [ND]] | ARP]]
36956a7d 800 */
76abe283
AE
801int flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
802 const struct nlattr *attr)
36956a7d 803{
76abe283 804 int error = 0;
36956a7d
BP
805 const struct nlattr *nla;
806 u16 prev_type;
807 int rem;
76abe283 808 int key_len;
36956a7d
BP
809
810 memset(swkey, 0, sizeof(*swkey));
18886b60 811 swkey->eth.in_port = USHRT_MAX;
76abe283
AE
812 swkey->eth.type = htons(ETH_P_802_2);
813 key_len = SW_FLOW_KEY_OFFSET(eth);
36956a7d 814
df2c07f4 815 prev_type = OVS_KEY_ATTR_UNSPEC;
d6569377 816 nla_for_each_nested(nla, attr, rem) {
df2c07f4
JP
817 const struct ovs_key_ethernet *eth_key;
818 const struct ovs_key_8021q *q_key;
819 const struct ovs_key_ipv4 *ipv4_key;
820 const struct ovs_key_ipv6 *ipv6_key;
821 const struct ovs_key_tcp *tcp_key;
822 const struct ovs_key_udp *udp_key;
823 const struct ovs_key_icmp *icmp_key;
824 const struct ovs_key_icmpv6 *icmpv6_key;
825 const struct ovs_key_arp *arp_key;
826 const struct ovs_key_nd *nd_key;
36956a7d
BP
827
828 int type = nla_type(nla);
829
df2c07f4 830 if (type > OVS_KEY_ATTR_MAX || nla_len(nla) != key_lens[type])
76abe283 831 goto invalid;
36956a7d
BP
832
833#define TRANSITION(PREV_TYPE, TYPE) (((PREV_TYPE) << 16) | (TYPE))
834 switch (TRANSITION(prev_type, type)) {
df2c07f4 835 case TRANSITION(OVS_KEY_ATTR_UNSPEC, OVS_KEY_ATTR_TUN_ID):
76abe283 836 swkey->eth.tun_id = nla_get_be64(nla);
36956a7d
BP
837 break;
838
df2c07f4
JP
839 case TRANSITION(OVS_KEY_ATTR_UNSPEC, OVS_KEY_ATTR_IN_PORT):
840 case TRANSITION(OVS_KEY_ATTR_TUN_ID, OVS_KEY_ATTR_IN_PORT):
36956a7d 841 if (nla_get_u32(nla) >= DP_MAX_PORTS)
76abe283
AE
842 goto invalid;
843 swkey->eth.in_port = nla_get_u32(nla);
36956a7d
BP
844 break;
845
18886b60
BP
846 case TRANSITION(OVS_KEY_ATTR_UNSPEC, OVS_KEY_ATTR_ETHERNET):
847 case TRANSITION(OVS_KEY_ATTR_TUN_ID, OVS_KEY_ATTR_ETHERNET):
df2c07f4 848 case TRANSITION(OVS_KEY_ATTR_IN_PORT, OVS_KEY_ATTR_ETHERNET):
36956a7d 849 eth_key = nla_data(nla);
76abe283
AE
850 memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
851 memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
36956a7d
BP
852 break;
853
df2c07f4 854 case TRANSITION(OVS_KEY_ATTR_ETHERNET, OVS_KEY_ATTR_8021Q):
36956a7d
BP
855 q_key = nla_data(nla);
856 /* Only standard 0x8100 VLANs currently supported. */
857 if (q_key->q_tpid != htons(ETH_P_8021Q))
76abe283 858 goto invalid;
36956a7d 859 if (q_key->q_tci & htons(VLAN_TAG_PRESENT))
76abe283
AE
860 goto invalid;
861 swkey->eth.tci = q_key->q_tci | htons(VLAN_TAG_PRESENT);
36956a7d
BP
862 break;
863
df2c07f4
JP
864 case TRANSITION(OVS_KEY_ATTR_8021Q, OVS_KEY_ATTR_ETHERTYPE):
865 case TRANSITION(OVS_KEY_ATTR_ETHERNET, OVS_KEY_ATTR_ETHERTYPE):
76abe283
AE
866 swkey->eth.type = nla_get_be16(nla);
867 if (ntohs(swkey->eth.type) < 1536)
868 goto invalid;
36956a7d
BP
869 break;
870
df2c07f4 871 case TRANSITION(OVS_KEY_ATTR_ETHERTYPE, OVS_KEY_ATTR_IPV4):
76abe283
AE
872 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
873 if (swkey->eth.type != htons(ETH_P_IP))
874 goto invalid;
36956a7d 875 ipv4_key = nla_data(nla);
28bad473
JG
876 swkey->ip.proto = ipv4_key->ipv4_proto;
877 swkey->ip.tos = ipv4_key->ipv4_tos;
76abe283
AE
878 swkey->ipv4.addr.src = ipv4_key->ipv4_src;
879 swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
28bad473 880 if (swkey->ip.tos & INET_ECN_MASK)
76abe283 881 goto invalid;
36956a7d
BP
882 break;
883
df2c07f4 884 case TRANSITION(OVS_KEY_ATTR_ETHERTYPE, OVS_KEY_ATTR_IPV6):
76abe283
AE
885 key_len = SW_FLOW_KEY_OFFSET(ipv6.addr);
886 if (swkey->eth.type != htons(ETH_P_IPV6))
887 goto invalid;
d31f1109 888 ipv6_key = nla_data(nla);
28bad473
JG
889 swkey->ip.proto = ipv6_key->ipv6_proto;
890 swkey->ip.tos = ipv6_key->ipv6_tos;
76abe283
AE
891 memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
892 sizeof(swkey->ipv6.addr.src));
893 memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
894 sizeof(swkey->ipv6.addr.dst));
28bad473 895 if (swkey->ip.tos & INET_ECN_MASK)
76abe283 896 goto invalid;
d31f1109
JP
897 break;
898
df2c07f4 899 case TRANSITION(OVS_KEY_ATTR_IPV4, OVS_KEY_ATTR_TCP):
76abe283 900 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
28bad473 901 if (swkey->ip.proto != IPPROTO_TCP)
76abe283
AE
902 goto invalid;
903 tcp_key = nla_data(nla);
904 swkey->ipv4.tp.src = tcp_key->tcp_src;
905 swkey->ipv4.tp.dst = tcp_key->tcp_dst;
906 break;
907
df2c07f4 908 case TRANSITION(OVS_KEY_ATTR_IPV6, OVS_KEY_ATTR_TCP):
76abe283 909 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
28bad473 910 if (swkey->ip.proto != IPPROTO_TCP)
76abe283 911 goto invalid;
36956a7d 912 tcp_key = nla_data(nla);
76abe283
AE
913 swkey->ipv6.tp.src = tcp_key->tcp_src;
914 swkey->ipv6.tp.dst = tcp_key->tcp_dst;
36956a7d
BP
915 break;
916
df2c07f4 917 case TRANSITION(OVS_KEY_ATTR_IPV4, OVS_KEY_ATTR_UDP):
76abe283 918 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
28bad473 919 if (swkey->ip.proto != IPPROTO_UDP)
76abe283
AE
920 goto invalid;
921 udp_key = nla_data(nla);
922 swkey->ipv4.tp.src = udp_key->udp_src;
923 swkey->ipv4.tp.dst = udp_key->udp_dst;
924 break;
925
df2c07f4 926 case TRANSITION(OVS_KEY_ATTR_IPV6, OVS_KEY_ATTR_UDP):
76abe283 927 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
28bad473 928 if (swkey->ip.proto != IPPROTO_UDP)
76abe283 929 goto invalid;
36956a7d 930 udp_key = nla_data(nla);
76abe283
AE
931 swkey->ipv6.tp.src = udp_key->udp_src;
932 swkey->ipv6.tp.dst = udp_key->udp_dst;
36956a7d
BP
933 break;
934
df2c07f4 935 case TRANSITION(OVS_KEY_ATTR_IPV4, OVS_KEY_ATTR_ICMP):
76abe283 936 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
28bad473 937 if (swkey->ip.proto != IPPROTO_ICMP)
76abe283 938 goto invalid;
36956a7d 939 icmp_key = nla_data(nla);
76abe283
AE
940 swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
941 swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
36956a7d
BP
942 break;
943
df2c07f4 944 case TRANSITION(OVS_KEY_ATTR_IPV6, OVS_KEY_ATTR_ICMPV6):
76abe283 945 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
28bad473 946 if (swkey->ip.proto != IPPROTO_ICMPV6)
76abe283 947 goto invalid;
d31f1109 948 icmpv6_key = nla_data(nla);
76abe283
AE
949 swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
950 swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
d31f1109
JP
951 break;
952
df2c07f4 953 case TRANSITION(OVS_KEY_ATTR_ETHERTYPE, OVS_KEY_ATTR_ARP):
76abe283
AE
954 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
955 if (swkey->eth.type != htons(ETH_P_ARP))
956 goto invalid;
36956a7d 957 arp_key = nla_data(nla);
76abe283
AE
958 swkey->ipv4.addr.src = arp_key->arp_sip;
959 swkey->ipv4.addr.dst = arp_key->arp_tip;
36956a7d 960 if (arp_key->arp_op & htons(0xff00))
76abe283 961 goto invalid;
28bad473 962 swkey->ip.proto = ntohs(arp_key->arp_op);
76abe283
AE
963 memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
964 memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
36956a7d
BP
965 break;
966
df2c07f4 967 case TRANSITION(OVS_KEY_ATTR_ICMPV6, OVS_KEY_ATTR_ND):
76abe283
AE
968 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
969 if (swkey->ipv6.tp.src != htons(NDISC_NEIGHBOUR_SOLICITATION)
970 && swkey->ipv6.tp.src != htons(NDISC_NEIGHBOUR_ADVERTISEMENT))
971 goto invalid;
685a51a5 972 nd_key = nla_data(nla);
76abe283
AE
973 memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
974 sizeof(swkey->ipv6.nd.target));
975 memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
976 memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
685a51a5
JP
977 break;
978
36956a7d 979 default:
76abe283 980 goto invalid;
36956a7d
BP
981 }
982
983 prev_type = type;
984 }
985 if (rem)
76abe283 986 goto invalid;
36956a7d
BP
987
988 switch (prev_type) {
df2c07f4 989 case OVS_KEY_ATTR_UNSPEC:
76abe283 990 goto invalid;
36956a7d 991
df2c07f4
JP
992 case OVS_KEY_ATTR_TUN_ID:
993 case OVS_KEY_ATTR_IN_PORT:
76abe283 994 goto invalid;
36956a7d 995
df2c07f4
JP
996 case OVS_KEY_ATTR_ETHERNET:
997 case OVS_KEY_ATTR_8021Q:
76abe283 998 goto ok;
36956a7d 999
df2c07f4 1000 case OVS_KEY_ATTR_ETHERTYPE:
76abe283
AE
1001 if (swkey->eth.type == htons(ETH_P_IP) ||
1002 swkey->eth.type == htons(ETH_P_ARP))
1003 goto invalid;
1004 goto ok;
36956a7d 1005
df2c07f4 1006 case OVS_KEY_ATTR_IPV4:
28bad473
JG
1007 if (swkey->ip.proto == IPPROTO_TCP ||
1008 swkey->ip.proto == IPPROTO_UDP ||
1009 swkey->ip.proto == IPPROTO_ICMP)
76abe283
AE
1010 goto invalid;
1011 goto ok;
36956a7d 1012
df2c07f4 1013 case OVS_KEY_ATTR_IPV6:
28bad473
JG
1014 if (swkey->ip.proto == IPPROTO_TCP ||
1015 swkey->ip.proto == IPPROTO_UDP ||
1016 swkey->ip.proto == IPPROTO_ICMPV6)
76abe283
AE
1017 goto invalid;
1018 goto ok;
d31f1109 1019
df2c07f4 1020 case OVS_KEY_ATTR_ICMPV6:
76abe283
AE
1021 if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
1022 swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT))
1023 goto invalid;
1024 goto ok;
685a51a5 1025
df2c07f4
JP
1026 case OVS_KEY_ATTR_TCP:
1027 case OVS_KEY_ATTR_UDP:
1028 case OVS_KEY_ATTR_ICMP:
1029 case OVS_KEY_ATTR_ARP:
1030 case OVS_KEY_ATTR_ND:
76abe283
AE
1031 goto ok;
1032
1033 default:
1034 WARN_ON_ONCE(1);
36956a7d
BP
1035 }
1036
76abe283
AE
1037invalid:
1038 error = -EINVAL;
1039
1040ok:
1041 WARN_ON_ONCE(!key_len && !error);
1042 *key_lenp = key_len;
1043 return error;
36956a7d
BP
1044}
1045
80e5eed9
BP
1046/**
1047 * flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1048 * @in_port: receives the extracted input port.
1049 * @tun_id: receives the extracted tunnel ID.
df2c07f4 1050 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
80e5eed9
BP
1051 * sequence.
1052 *
1053 * This parses a series of Netlink attributes that form a flow key, which must
1054 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1055 * get the metadata, that is, the parts of the flow key that cannot be
1056 * extracted from the packet itself.
1057 */
1058int flow_metadata_from_nlattrs(u16 *in_port, __be64 *tun_id,
1059 const struct nlattr *attr)
1060{
1061 const struct nlattr *nla;
1062 u16 prev_type;
1063 int rem;
1064
18886b60 1065 *in_port = USHRT_MAX;
80e5eed9
BP
1066 *tun_id = 0;
1067
df2c07f4 1068 prev_type = OVS_KEY_ATTR_UNSPEC;
80e5eed9
BP
1069 nla_for_each_nested(nla, attr, rem) {
1070 int type = nla_type(nla);
1071
df2c07f4 1072 if (type > OVS_KEY_ATTR_MAX || nla_len(nla) != key_lens[type])
80e5eed9
BP
1073 return -EINVAL;
1074
1075 switch (TRANSITION(prev_type, type)) {
df2c07f4 1076 case TRANSITION(OVS_KEY_ATTR_UNSPEC, OVS_KEY_ATTR_TUN_ID):
80e5eed9
BP
1077 *tun_id = nla_get_be64(nla);
1078 break;
1079
df2c07f4
JP
1080 case TRANSITION(OVS_KEY_ATTR_UNSPEC, OVS_KEY_ATTR_IN_PORT):
1081 case TRANSITION(OVS_KEY_ATTR_TUN_ID, OVS_KEY_ATTR_IN_PORT):
80e5eed9
BP
1082 if (nla_get_u32(nla) >= DP_MAX_PORTS)
1083 return -EINVAL;
1084 *in_port = nla_get_u32(nla);
1085 break;
1086
1087 default:
18886b60 1088 return 0;
80e5eed9
BP
1089 }
1090
1091 prev_type = type;
1092 }
1093 if (rem)
1094 return -EINVAL;
80e5eed9
BP
1095 return 0;
1096}
1097
d6569377 1098int flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
36956a7d 1099{
df2c07f4 1100 struct ovs_key_ethernet *eth_key;
d6569377 1101 struct nlattr *nla;
36956a7d 1102
18c43631 1103 /* This is an imperfect sanity-check that FLOW_BUFSIZE doesn't need
df2c07f4
JP
1104 * to be updated, but will at least raise awareness when new
1105 * datapath key types are added. */
1106 BUILD_BUG_ON(__OVS_KEY_ATTR_MAX != 14);
18c43631 1107
76abe283 1108 if (swkey->eth.tun_id != cpu_to_be64(0))
df2c07f4 1109 NLA_PUT_BE64(skb, OVS_KEY_ATTR_TUN_ID, swkey->eth.tun_id);
36956a7d 1110
18886b60
BP
1111 if (swkey->eth.in_port != USHRT_MAX)
1112 NLA_PUT_U32(skb, OVS_KEY_ATTR_IN_PORT, swkey->eth.in_port);
36956a7d 1113
df2c07f4 1114 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
d6569377
BP
1115 if (!nla)
1116 goto nla_put_failure;
1117 eth_key = nla_data(nla);
76abe283
AE
1118 memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
1119 memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
36956a7d 1120
76abe283 1121 if (swkey->eth.tci != htons(0)) {
df2c07f4 1122 struct ovs_key_8021q q_key;
36956a7d 1123
d6569377 1124 q_key.q_tpid = htons(ETH_P_8021Q);
76abe283 1125 q_key.q_tci = swkey->eth.tci & ~htons(VLAN_TAG_PRESENT);
df2c07f4 1126 NLA_PUT(skb, OVS_KEY_ATTR_8021Q, sizeof(q_key), &q_key);
36956a7d
BP
1127 }
1128
76abe283 1129 if (swkey->eth.type == htons(ETH_P_802_2))
d6569377 1130 return 0;
36956a7d 1131
df2c07f4 1132 NLA_PUT_BE16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type);
36956a7d 1133
76abe283 1134 if (swkey->eth.type == htons(ETH_P_IP)) {
df2c07f4 1135 struct ovs_key_ipv4 *ipv4_key;
36956a7d 1136
df2c07f4 1137 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
d6569377
BP
1138 if (!nla)
1139 goto nla_put_failure;
1140 ipv4_key = nla_data(nla);
df2c07f4 1141 memset(ipv4_key, 0, sizeof(struct ovs_key_ipv4));
76abe283
AE
1142 ipv4_key->ipv4_src = swkey->ipv4.addr.src;
1143 ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
28bad473
JG
1144 ipv4_key->ipv4_proto = swkey->ip.proto;
1145 ipv4_key->ipv4_tos = swkey->ip.tos;
76abe283 1146 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
df2c07f4 1147 struct ovs_key_ipv6 *ipv6_key;
d31f1109 1148
df2c07f4 1149 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
d31f1109
JP
1150 if (!nla)
1151 goto nla_put_failure;
1152 ipv6_key = nla_data(nla);
df2c07f4 1153 memset(ipv6_key, 0, sizeof(struct ovs_key_ipv6));
76abe283 1154 memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
d31f1109 1155 sizeof(ipv6_key->ipv6_src));
76abe283 1156 memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
d31f1109 1157 sizeof(ipv6_key->ipv6_dst));
28bad473
JG
1158 ipv6_key->ipv6_proto = swkey->ip.proto;
1159 ipv6_key->ipv6_tos = swkey->ip.tos;
76abe283 1160 } else if (swkey->eth.type == htons(ETH_P_ARP)) {
df2c07f4 1161 struct ovs_key_arp *arp_key;
d31f1109 1162
df2c07f4 1163 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
d31f1109
JP
1164 if (!nla)
1165 goto nla_put_failure;
1166 arp_key = nla_data(nla);
df2c07f4 1167 memset(arp_key, 0, sizeof(struct ovs_key_arp));
76abe283
AE
1168 arp_key->arp_sip = swkey->ipv4.addr.src;
1169 arp_key->arp_tip = swkey->ipv4.addr.dst;
28bad473 1170 arp_key->arp_op = htons(swkey->ip.proto);
76abe283
AE
1171 memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
1172 memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
d31f1109
JP
1173 }
1174
76abe283
AE
1175 if (swkey->eth.type == htons(ETH_P_IP) ||
1176 swkey->eth.type == htons(ETH_P_IPV6)) {
36956a7d 1177
28bad473 1178 if (swkey->ip.proto == IPPROTO_TCP) {
df2c07f4 1179 struct ovs_key_tcp *tcp_key;
36956a7d 1180
df2c07f4 1181 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
d6569377
BP
1182 if (!nla)
1183 goto nla_put_failure;
1184 tcp_key = nla_data(nla);
76abe283
AE
1185 if (swkey->eth.type == htons(ETH_P_IP)) {
1186 tcp_key->tcp_src = swkey->ipv4.tp.src;
1187 tcp_key->tcp_dst = swkey->ipv4.tp.dst;
1188 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1189 tcp_key->tcp_src = swkey->ipv6.tp.src;
1190 tcp_key->tcp_dst = swkey->ipv6.tp.dst;
1191 }
28bad473 1192 } else if (swkey->ip.proto == IPPROTO_UDP) {
df2c07f4 1193 struct ovs_key_udp *udp_key;
36956a7d 1194
df2c07f4 1195 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
d6569377
BP
1196 if (!nla)
1197 goto nla_put_failure;
1198 udp_key = nla_data(nla);
76abe283
AE
1199 if (swkey->eth.type == htons(ETH_P_IP)) {
1200 udp_key->udp_src = swkey->ipv4.tp.src;
1201 udp_key->udp_dst = swkey->ipv4.tp.dst;
1202 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1203 udp_key->udp_src = swkey->ipv6.tp.src;
1204 udp_key->udp_dst = swkey->ipv6.tp.dst;
1205 }
1206 } else if (swkey->eth.type == htons(ETH_P_IP) &&
28bad473 1207 swkey->ip.proto == IPPROTO_ICMP) {
df2c07f4 1208 struct ovs_key_icmp *icmp_key;
36956a7d 1209
df2c07f4 1210 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
d6569377
BP
1211 if (!nla)
1212 goto nla_put_failure;
1213 icmp_key = nla_data(nla);
76abe283
AE
1214 icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
1215 icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
1216 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
28bad473 1217 swkey->ip.proto == IPPROTO_ICMPV6) {
df2c07f4 1218 struct ovs_key_icmpv6 *icmpv6_key;
36956a7d 1219
df2c07f4 1220 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
bfef4717 1221 sizeof(*icmpv6_key));
d31f1109
JP
1222 if (!nla)
1223 goto nla_put_failure;
1224 icmpv6_key = nla_data(nla);
76abe283
AE
1225 icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
1226 icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
685a51a5 1227
bfef4717
JG
1228 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1229 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
df2c07f4 1230 struct ovs_key_nd *nd_key;
685a51a5 1231
df2c07f4 1232 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
685a51a5
JP
1233 if (!nla)
1234 goto nla_put_failure;
1235 nd_key = nla_data(nla);
76abe283 1236 memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
685a51a5 1237 sizeof(nd_key->nd_target));
76abe283
AE
1238 memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
1239 memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
685a51a5 1240 }
d31f1109 1241 }
36956a7d
BP
1242 }
1243
d6569377 1244 return 0;
36956a7d 1245
d6569377
BP
1246nla_put_failure:
1247 return -EMSGSIZE;
8d5ebd83
JG
1248}
1249
064af421
BP
1250/* Initializes the flow module.
1251 * Returns zero if successful or a negative error code. */
1252int flow_init(void)
1253{
1254 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1255 0, NULL);
1256 if (flow_cache == NULL)
1257 return -ENOMEM;
1258
84c17d98 1259 get_random_bytes(&hash_seed, sizeof(hash_seed));
8d5ebd83 1260
064af421
BP
1261 return 0;
1262}
1263
1264/* Uninitializes the flow module. */
1265void flow_exit(void)
1266{
1267 kmem_cache_destroy(flow_cache);
1268}