d83c17d9a49ac2c9ac8b7611dd6aab195a7323ce
[sliver-openvswitch.git] / datapath / flow.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007, 2008, 2009, 2010, 2011 Nicira Networks.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #include "flow.h"
10 #include "datapath.h"
11 #include <asm/uaccess.h>
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>
18 #include <linux/jhash.h>
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>
24 #include <linux/if_arp.h>
25 #include <linux/if_ether.h>
26 #include <linux/ip.h>
27 #include <linux/tcp.h>
28 #include <linux/udp.h>
29 #include <linux/icmp.h>
30 #include <net/inet_ecn.h>
31 #include <net/ip.h>
32
33 static struct kmem_cache *flow_cache;
34 static unsigned int hash_seed __read_mostly;
35
36 static inline bool arphdr_ok(struct sk_buff *skb)
37 {
38         return skb->len >= skb_network_offset(skb) + sizeof(struct arp_eth_header);
39 }
40
41 static inline int check_iphdr(struct sk_buff *skb)
42 {
43         unsigned int nh_ofs = skb_network_offset(skb);
44         unsigned int ip_len;
45
46         if (skb->len < nh_ofs + sizeof(struct iphdr))
47                 return -EINVAL;
48
49         ip_len = ip_hdrlen(skb);
50         if (ip_len < sizeof(struct iphdr) || skb->len < nh_ofs + ip_len)
51                 return -EINVAL;
52
53         /*
54          * Pull enough header bytes to account for the IP header plus the
55          * longest transport header that we parse, currently 20 bytes for TCP.
56          */
57         if (!pskb_may_pull(skb, min(nh_ofs + ip_len + 20, skb->len)))
58                 return -ENOMEM;
59
60         skb_set_transport_header(skb, nh_ofs + ip_len);
61         return 0;
62 }
63
64 static inline bool tcphdr_ok(struct sk_buff *skb)
65 {
66         int th_ofs = skb_transport_offset(skb);
67         if (skb->len >= th_ofs + sizeof(struct tcphdr)) {
68                 int tcp_len = tcp_hdrlen(skb);
69                 return (tcp_len >= sizeof(struct tcphdr)
70                         && skb->len >= th_ofs + tcp_len);
71         }
72         return false;
73 }
74
75 static inline bool udphdr_ok(struct sk_buff *skb)
76 {
77         return skb->len >= skb_transport_offset(skb) + sizeof(struct udphdr);
78 }
79
80 static inline bool icmphdr_ok(struct sk_buff *skb)
81 {
82         return skb->len >= skb_transport_offset(skb) + sizeof(struct icmphdr);
83 }
84
85 u64 flow_used_time(unsigned long flow_jiffies)
86 {
87         struct timespec cur_ts;
88         u64 cur_ms, idle_ms;
89
90         ktime_get_ts(&cur_ts);
91         idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
92         cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
93                  cur_ts.tv_nsec / NSEC_PER_MSEC;
94
95         return cur_ms - idle_ms;
96 }
97
98
99 #define TCP_FLAGS_OFFSET 13
100 #define TCP_FLAG_MASK 0x3f
101
102 void flow_used(struct sw_flow *flow, struct sk_buff *skb)
103 {
104         u8 tcp_flags = 0;
105
106         if (flow->key.dl_type == htons(ETH_P_IP) &&
107             flow->key.nw_proto == IPPROTO_TCP) {
108                 u8 *tcp = (u8 *)tcp_hdr(skb);
109                 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
110         }
111
112         spin_lock_bh(&flow->lock);
113         flow->used = jiffies;
114         flow->packet_count++;
115         flow->byte_count += skb->len;
116         flow->tcp_flags |= tcp_flags;
117         spin_unlock_bh(&flow->lock);
118 }
119
120 struct sw_flow_actions *flow_actions_alloc(const struct nlattr *actions)
121 {
122         int actions_len = nla_len(actions);
123         struct sw_flow_actions *sfa;
124
125         /* At least DP_MAX_PORTS actions are required to be able to flood a
126          * packet to every port.  Factor of 2 allows for setting VLAN tags,
127          * etc. */
128         if (actions_len > 2 * DP_MAX_PORTS * nla_total_size(4))
129                 return ERR_PTR(-EINVAL);
130
131         sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL);
132         if (!sfa)
133                 return ERR_PTR(-ENOMEM);
134
135         sfa->actions_len = actions_len;
136         memcpy(sfa->actions, nla_data(actions), actions_len);
137         return sfa;
138 }
139
140 struct sw_flow *flow_alloc(void)
141 {
142         struct sw_flow *flow;
143
144         flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
145         if (!flow)
146                 return ERR_PTR(-ENOMEM);
147
148         spin_lock_init(&flow->lock);
149         atomic_set(&flow->refcnt, 1);
150         flow->dead = false;
151
152         return flow;
153 }
154
155 void flow_free_tbl(struct tbl_node *node)
156 {
157         struct sw_flow *flow = flow_cast(node);
158
159         flow->dead = true;
160         flow_put(flow);
161 }
162
163 /* RCU callback used by flow_deferred_free. */
164 static void rcu_free_flow_callback(struct rcu_head *rcu)
165 {
166         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
167
168         flow->dead = true;
169         flow_put(flow);
170 }
171
172 /* Schedules 'flow' to be freed after the next RCU grace period.
173  * The caller must hold rcu_read_lock for this to be sensible. */
174 void flow_deferred_free(struct sw_flow *flow)
175 {
176         call_rcu(&flow->rcu, rcu_free_flow_callback);
177 }
178
179 void flow_hold(struct sw_flow *flow)
180 {
181         atomic_inc(&flow->refcnt);
182 }
183
184 void flow_put(struct sw_flow *flow)
185 {
186         if (unlikely(!flow))
187                 return;
188
189         if (atomic_dec_and_test(&flow->refcnt)) {
190                 kfree((struct sf_flow_acts __force *)flow->sf_acts);
191                 kmem_cache_free(flow_cache, flow);
192         }
193 }
194
195 /* RCU callback used by flow_deferred_free_acts. */
196 static void rcu_free_acts_callback(struct rcu_head *rcu)
197 {
198         struct sw_flow_actions *sf_acts = container_of(rcu,
199                         struct sw_flow_actions, rcu);
200         kfree(sf_acts);
201 }
202
203 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
204  * The caller must hold rcu_read_lock for this to be sensible. */
205 void flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
206 {
207         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
208 }
209
210 static void parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
211 {
212         struct qtag_prefix {
213                 __be16 eth_type; /* ETH_P_8021Q */
214                 __be16 tci;
215         };
216         struct qtag_prefix *qp;
217
218         if (skb->len < sizeof(struct qtag_prefix) + sizeof(__be16))
219                 return;
220
221         qp = (struct qtag_prefix *) skb->data;
222         key->dl_tci = qp->tci | htons(VLAN_TAG_PRESENT);
223         __skb_pull(skb, sizeof(struct qtag_prefix));
224 }
225
226 static __be16 parse_ethertype(struct sk_buff *skb)
227 {
228         struct llc_snap_hdr {
229                 u8  dsap;  /* Always 0xAA */
230                 u8  ssap;  /* Always 0xAA */
231                 u8  ctrl;
232                 u8  oui[3];
233                 __be16 ethertype;
234         };
235         struct llc_snap_hdr *llc;
236         __be16 proto;
237
238         proto = *(__be16 *) skb->data;
239         __skb_pull(skb, sizeof(__be16));
240
241         if (ntohs(proto) >= 1536)
242                 return proto;
243
244         if (unlikely(skb->len < sizeof(struct llc_snap_hdr)))
245                 return htons(ETH_P_802_2);
246
247         llc = (struct llc_snap_hdr *) skb->data;
248         if (llc->dsap != LLC_SAP_SNAP ||
249             llc->ssap != LLC_SAP_SNAP ||
250             (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
251                 return htons(ETH_P_802_2);
252
253         __skb_pull(skb, sizeof(struct llc_snap_hdr));
254         return llc->ethertype;
255 }
256
257 /**
258  * flow_extract - extracts a flow key from an Ethernet frame.
259  * @skb: sk_buff that contains the frame, with skb->data pointing to the
260  * Ethernet header
261  * @in_port: port number on which @skb was received.
262  * @key: output flow key
263  * @is_frag: set to 1 if @skb contains an IPv4 fragment, or to 0 if @skb does
264  * not contain an IPv4 packet or if it is not a fragment.
265  *
266  * The caller must ensure that skb->len >= ETH_HLEN.
267  *
268  * Returns 0 if successful, otherwise a negative errno value.
269  *
270  * Initializes @skb header pointers as follows:
271  *
272  *    - skb->mac_header: the Ethernet header.
273  *
274  *    - skb->network_header: just past the Ethernet header, or just past the
275  *      VLAN header, to the first byte of the Ethernet payload.
276  *
277  *    - skb->transport_header: If key->dl_type is ETH_P_IP on output, then just
278  *      past the IPv4 header, if one is present and of a correct length,
279  *      otherwise the same as skb->network_header.  For other key->dl_type
280  *      values it is left untouched.
281  */
282 int flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
283                  bool *is_frag)
284 {
285         struct ethhdr *eth;
286
287         memset(key, 0, sizeof(*key));
288         key->tun_id = OVS_CB(skb)->tun_id;
289         key->in_port = in_port;
290         *is_frag = false;
291
292         /*
293          * We would really like to pull as many bytes as we could possibly
294          * want to parse into the linear data area.  Currently that is:
295          *
296          *    14     Ethernet header
297          *     4     VLAN header
298          *    60     max IP header with options
299          *    20     max TCP/UDP/ICMP header (don't care about options)
300          *    --
301          *    98
302          *
303          * But Xen only allocates 64 or 72 bytes for the linear data area in
304          * netback, which means that we would reallocate and copy the skb's
305          * linear data on every packet if we did that.  So instead just pull 64
306          * bytes, which is always sufficient without IP options, and then check
307          * whether we need to pull more later when we look at the IP header.
308          */
309         if (!pskb_may_pull(skb, min(skb->len, 64u)))
310                 return -ENOMEM;
311
312         skb_reset_mac_header(skb);
313
314         /* Link layer. */
315         eth = eth_hdr(skb);
316         memcpy(key->dl_src, eth->h_source, ETH_ALEN);
317         memcpy(key->dl_dst, eth->h_dest, ETH_ALEN);
318
319         /* dl_type, dl_vlan, dl_vlan_pcp. */
320         __skb_pull(skb, 2 * ETH_ALEN);
321         if (eth->h_proto == htons(ETH_P_8021Q))
322                 parse_vlan(skb, key);
323         key->dl_type = parse_ethertype(skb);
324         skb_reset_network_header(skb);
325         __skb_push(skb, skb->data - (unsigned char *)eth);
326
327         /* Network layer. */
328         if (key->dl_type == htons(ETH_P_IP)) {
329                 struct iphdr *nh;
330                 int error;
331
332                 error = check_iphdr(skb);
333                 if (unlikely(error)) {
334                         if (error == -EINVAL) {
335                                 skb->transport_header = skb->network_header;
336                                 return 0;
337                         }
338                         return error;
339                 }
340
341                 nh = ip_hdr(skb);
342                 key->nw_src = nh->saddr;
343                 key->nw_dst = nh->daddr;
344                 key->nw_tos = nh->tos & ~INET_ECN_MASK;
345                 key->nw_proto = nh->protocol;
346
347                 /* Transport layer. */
348                 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET)) &&
349                     !(skb_shinfo(skb)->gso_type & SKB_GSO_UDP)) {
350                         if (key->nw_proto == IPPROTO_TCP) {
351                                 if (tcphdr_ok(skb)) {
352                                         struct tcphdr *tcp = tcp_hdr(skb);
353                                         key->tp_src = tcp->source;
354                                         key->tp_dst = tcp->dest;
355                                 }
356                         } else if (key->nw_proto == IPPROTO_UDP) {
357                                 if (udphdr_ok(skb)) {
358                                         struct udphdr *udp = udp_hdr(skb);
359                                         key->tp_src = udp->source;
360                                         key->tp_dst = udp->dest;
361                                 }
362                         } else if (key->nw_proto == IPPROTO_ICMP) {
363                                 if (icmphdr_ok(skb)) {
364                                         struct icmphdr *icmp = icmp_hdr(skb);
365                                         /* The ICMP type and code fields use the 16-bit
366                                          * transport port fields, so we need to store them
367                                          * in 16-bit network byte order. */
368                                         key->tp_src = htons(icmp->type);
369                                         key->tp_dst = htons(icmp->code);
370                                 }
371                         }
372                 } else
373                         *is_frag = true;
374
375         } else if (key->dl_type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
376                 struct arp_eth_header *arp;
377
378                 arp = (struct arp_eth_header *)skb_network_header(skb);
379
380                 if (arp->ar_hrd == htons(ARPHRD_ETHER)
381                                 && arp->ar_pro == htons(ETH_P_IP)
382                                 && arp->ar_hln == ETH_ALEN
383                                 && arp->ar_pln == 4) {
384
385                         /* We only match on the lower 8 bits of the opcode. */
386                         if (ntohs(arp->ar_op) <= 0xff)
387                                 key->nw_proto = ntohs(arp->ar_op);
388
389                         if (key->nw_proto == ARPOP_REQUEST
390                                         || key->nw_proto == ARPOP_REPLY) {
391                                 memcpy(&key->nw_src, arp->ar_sip, sizeof(key->nw_src));
392                                 memcpy(&key->nw_dst, arp->ar_tip, sizeof(key->nw_dst));
393                                 memcpy(key->arp_sha, arp->ar_sha, ETH_ALEN);
394                                 memcpy(key->arp_tha, arp->ar_tha, ETH_ALEN);
395                         }
396                 }
397         }
398         return 0;
399 }
400
401 u32 flow_hash(const struct sw_flow_key *key)
402 {
403         return jhash2((u32*)key, sizeof(*key) / sizeof(u32), hash_seed);
404 }
405
406 int flow_cmp(const struct tbl_node *node, void *key2_)
407 {
408         const struct sw_flow_key *key1 = &flow_cast(node)->key;
409         const struct sw_flow_key *key2 = key2_;
410
411         return !memcmp(key1, key2, sizeof(struct sw_flow_key));
412 }
413
414 /**
415  * flow_from_nlattrs - parses Netlink attributes into a flow key.
416  * @swkey: receives the extracted flow key.
417  * @key: Netlink attribute holding nested %ODP_KEY_ATTR_* Netlink attribute
418  * sequence.
419  *
420  * This state machine accepts the following forms, with [] for optional
421  * elements and | for alternatives:
422  *
423  * [tun_id] in_port ethernet [8021q] [ethertype [IP [TCP|UDP|ICMP] | ARP]
424  */
425 int flow_from_nlattrs(struct sw_flow_key *swkey, const struct nlattr *attr)
426 {
427         const struct nlattr *nla;
428         u16 prev_type;
429         int rem;
430
431         memset(swkey, 0, sizeof(*swkey));
432         swkey->dl_type = htons(ETH_P_802_2);
433
434         prev_type = ODP_KEY_ATTR_UNSPEC;
435         nla_for_each_nested(nla, attr, rem) {
436                 static const u32 key_lens[ODP_KEY_ATTR_MAX + 1] = {
437                         [ODP_KEY_ATTR_TUN_ID] = 8,
438                         [ODP_KEY_ATTR_IN_PORT] = 4,
439                         [ODP_KEY_ATTR_ETHERNET] = sizeof(struct odp_key_ethernet),
440                         [ODP_KEY_ATTR_8021Q] = sizeof(struct odp_key_8021q),
441                         [ODP_KEY_ATTR_ETHERTYPE] = 2,
442                         [ODP_KEY_ATTR_IPV4] = sizeof(struct odp_key_ipv4),
443                         [ODP_KEY_ATTR_TCP] = sizeof(struct odp_key_tcp),
444                         [ODP_KEY_ATTR_UDP] = sizeof(struct odp_key_udp),
445                         [ODP_KEY_ATTR_ICMP] = sizeof(struct odp_key_icmp),
446                         [ODP_KEY_ATTR_ARP] = sizeof(struct odp_key_arp),
447                 };
448
449                 const struct odp_key_ethernet *eth_key;
450                 const struct odp_key_8021q *q_key;
451                 const struct odp_key_ipv4 *ipv4_key;
452                 const struct odp_key_tcp *tcp_key;
453                 const struct odp_key_udp *udp_key;
454                 const struct odp_key_icmp *icmp_key;
455                 const struct odp_key_arp *arp_key;
456
457                 int type = nla_type(nla);
458
459                 if (type > ODP_KEY_ATTR_MAX || nla_len(nla) != key_lens[type])
460                         return -EINVAL;
461
462 #define TRANSITION(PREV_TYPE, TYPE) (((PREV_TYPE) << 16) | (TYPE))
463                 switch (TRANSITION(prev_type, type)) {
464                 case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_TUN_ID):
465                         swkey->tun_id = nla_get_be64(nla);
466                         break;
467
468                 case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_IN_PORT):
469                 case TRANSITION(ODP_KEY_ATTR_TUN_ID, ODP_KEY_ATTR_IN_PORT):
470                         if (nla_get_u32(nla) >= DP_MAX_PORTS)
471                                 return -EINVAL;
472                         swkey->in_port = nla_get_u32(nla);
473                         break;
474
475                 case TRANSITION(ODP_KEY_ATTR_IN_PORT, ODP_KEY_ATTR_ETHERNET):
476                         eth_key = nla_data(nla);
477                         memcpy(swkey->dl_src, eth_key->eth_src, ETH_ALEN);
478                         memcpy(swkey->dl_dst, eth_key->eth_dst, ETH_ALEN);
479                         break;
480
481                 case TRANSITION(ODP_KEY_ATTR_ETHERNET, ODP_KEY_ATTR_8021Q):
482                         q_key = nla_data(nla);
483                         /* Only standard 0x8100 VLANs currently supported. */
484                         if (q_key->q_tpid != htons(ETH_P_8021Q))
485                                 return -EINVAL;
486                         if (q_key->q_tci & htons(VLAN_TAG_PRESENT))
487                                 return -EINVAL;
488                         swkey->dl_tci = q_key->q_tci | htons(VLAN_TAG_PRESENT);
489                         break;
490
491                 case TRANSITION(ODP_KEY_ATTR_8021Q, ODP_KEY_ATTR_ETHERTYPE):
492                 case TRANSITION(ODP_KEY_ATTR_ETHERNET, ODP_KEY_ATTR_ETHERTYPE):
493                         swkey->dl_type = nla_get_be16(nla);
494                         if (ntohs(swkey->dl_type) < 1536)
495                                 return -EINVAL;
496                         break;
497
498                 case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_IPV4):
499                         if (swkey->dl_type != htons(ETH_P_IP))
500                                 return -EINVAL;
501                         ipv4_key = nla_data(nla);
502                         swkey->nw_src = ipv4_key->ipv4_src;
503                         swkey->nw_dst = ipv4_key->ipv4_dst;
504                         swkey->nw_proto = ipv4_key->ipv4_proto;
505                         swkey->nw_tos = ipv4_key->ipv4_tos;
506                         if (swkey->nw_tos & INET_ECN_MASK)
507                                 return -EINVAL;
508                         break;
509
510                 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_TCP):
511                         if (swkey->nw_proto != IPPROTO_TCP)
512                                 return -EINVAL;
513                         tcp_key = nla_data(nla);
514                         swkey->tp_src = tcp_key->tcp_src;
515                         swkey->tp_dst = tcp_key->tcp_dst;
516                         break;
517
518                 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_UDP):
519                         if (swkey->nw_proto != IPPROTO_UDP)
520                                 return -EINVAL;
521                         udp_key = nla_data(nla);
522                         swkey->tp_src = udp_key->udp_src;
523                         swkey->tp_dst = udp_key->udp_dst;
524                         break;
525
526                 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_ICMP):
527                         if (swkey->nw_proto != IPPROTO_ICMP)
528                                 return -EINVAL;
529                         icmp_key = nla_data(nla);
530                         swkey->tp_src = htons(icmp_key->icmp_type);
531                         swkey->tp_dst = htons(icmp_key->icmp_code);
532                         break;
533
534                 case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_ARP):
535                         if (swkey->dl_type != htons(ETH_P_ARP))
536                                 return -EINVAL;
537                         arp_key = nla_data(nla);
538                         swkey->nw_src = arp_key->arp_sip;
539                         swkey->nw_dst = arp_key->arp_tip;
540                         if (arp_key->arp_op & htons(0xff00))
541                                 return -EINVAL;
542                         swkey->nw_proto = ntohs(arp_key->arp_op);
543                         memcpy(swkey->arp_sha, arp_key->arp_sha, ETH_ALEN);
544                         memcpy(swkey->arp_tha, arp_key->arp_tha, ETH_ALEN);
545                         break;
546
547                 default:
548                         return -EINVAL;
549                 }
550
551                 prev_type = type;
552         }
553         if (rem)
554                 return -EINVAL;
555
556         switch (prev_type) {
557         case ODP_KEY_ATTR_UNSPEC:
558                 return -EINVAL;
559
560         case ODP_KEY_ATTR_TUN_ID:
561         case ODP_KEY_ATTR_IN_PORT:
562                 return -EINVAL;
563
564         case ODP_KEY_ATTR_ETHERNET:
565         case ODP_KEY_ATTR_8021Q:
566                 return 0;
567
568         case ODP_KEY_ATTR_ETHERTYPE:
569                 if (swkey->dl_type == htons(ETH_P_IP) ||
570                     swkey->dl_type == htons(ETH_P_ARP))
571                         return -EINVAL;
572                 return 0;
573
574         case ODP_KEY_ATTR_IPV4:
575                 if (swkey->nw_proto == IPPROTO_TCP ||
576                     swkey->nw_proto == IPPROTO_UDP ||
577                     swkey->nw_proto == IPPROTO_ICMP)
578                         return -EINVAL;
579                 return 0;
580
581         case ODP_KEY_ATTR_TCP:
582         case ODP_KEY_ATTR_UDP:
583         case ODP_KEY_ATTR_ICMP:
584         case ODP_KEY_ATTR_ARP:
585                 return 0;
586         }
587
588         WARN_ON_ONCE(1);
589         return -EINVAL;
590 }
591
592 int flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
593 {
594         struct odp_key_ethernet *eth_key;
595         struct nlattr *nla;
596
597         if (swkey->tun_id != cpu_to_be64(0))
598                 NLA_PUT_BE64(skb, ODP_KEY_ATTR_TUN_ID, swkey->tun_id);
599
600         NLA_PUT_U32(skb, ODP_KEY_ATTR_IN_PORT, swkey->in_port);
601
602         nla = nla_reserve(skb, ODP_KEY_ATTR_ETHERNET, sizeof(*eth_key));
603         if (!nla)
604                 goto nla_put_failure;
605         eth_key = nla_data(nla);
606         memcpy(eth_key->eth_src, swkey->dl_src, ETH_ALEN);
607         memcpy(eth_key->eth_dst, swkey->dl_dst, ETH_ALEN);
608
609         if (swkey->dl_tci != htons(0)) {
610                 struct odp_key_8021q q_key;
611
612                 q_key.q_tpid = htons(ETH_P_8021Q);
613                 q_key.q_tci = swkey->dl_tci & ~htons(VLAN_TAG_PRESENT);
614                 NLA_PUT(skb, ODP_KEY_ATTR_8021Q, sizeof(q_key), &q_key);
615         }
616
617         if (swkey->dl_type == htons(ETH_P_802_2))
618                 return 0;
619
620         NLA_PUT_BE16(skb, ODP_KEY_ATTR_ETHERTYPE, swkey->dl_type);
621
622         if (swkey->dl_type == htons(ETH_P_IP)) {
623                 struct odp_key_ipv4 *ipv4_key;
624
625                 nla = nla_reserve(skb, ODP_KEY_ATTR_IPV4, sizeof(*ipv4_key));
626                 if (!nla)
627                         goto nla_put_failure;
628                 ipv4_key = nla_data(nla);
629                 ipv4_key->ipv4_src = swkey->nw_src;
630                 ipv4_key->ipv4_dst = swkey->nw_dst;
631                 ipv4_key->ipv4_proto = swkey->nw_proto;
632                 ipv4_key->ipv4_tos = swkey->nw_tos;
633
634                 if (swkey->nw_proto == IPPROTO_TCP) {
635                         struct odp_key_tcp *tcp_key;
636
637                         nla = nla_reserve(skb, ODP_KEY_ATTR_TCP, sizeof(*tcp_key));
638                         if (!nla)
639                                 goto nla_put_failure;
640                         tcp_key = nla_data(nla);
641                         tcp_key->tcp_src = swkey->tp_src;
642                         tcp_key->tcp_dst = swkey->tp_dst;
643                 } else if (swkey->nw_proto == IPPROTO_UDP) {
644                         struct odp_key_udp *udp_key;
645
646                         nla = nla_reserve(skb, ODP_KEY_ATTR_UDP, sizeof(*udp_key));
647                         if (!nla)
648                                 goto nla_put_failure;
649                         udp_key = nla_data(nla);
650                         udp_key->udp_src = swkey->tp_src;
651                         udp_key->udp_dst = swkey->tp_dst;
652                 } else if (swkey->nw_proto == IPPROTO_ICMP) {
653                         struct odp_key_icmp *icmp_key;
654
655                         nla = nla_reserve(skb, ODP_KEY_ATTR_ICMP, sizeof(*icmp_key));
656                         if (!nla)
657                                 goto nla_put_failure;
658                         icmp_key = nla_data(nla);
659                         icmp_key->icmp_type = ntohs(swkey->tp_src);
660                         icmp_key->icmp_code = ntohs(swkey->tp_dst);
661                 }
662         } else if (swkey->dl_type == htons(ETH_P_ARP)) {
663                 struct odp_key_arp *arp_key;
664
665                 nla = nla_reserve(skb, ODP_KEY_ATTR_ARP, sizeof(*arp_key));
666                 if (!nla)
667                         goto nla_put_failure;
668                 arp_key = nla_data(nla);
669                 arp_key->arp_sip = swkey->nw_src;
670                 arp_key->arp_tip = swkey->nw_dst;
671                 arp_key->arp_op = htons(swkey->nw_proto);
672                 memcpy(arp_key->arp_sha, swkey->arp_sha, ETH_ALEN);
673                 memcpy(arp_key->arp_tha, swkey->arp_tha, ETH_ALEN);
674         }
675
676         return 0;
677
678 nla_put_failure:
679         return -EMSGSIZE;
680 }
681
682 /* Initializes the flow module.
683  * Returns zero if successful or a negative error code. */
684 int flow_init(void)
685 {
686         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
687                                         0, NULL);
688         if (flow_cache == NULL)
689                 return -ENOMEM;
690
691         get_random_bytes(&hash_seed, sizeof(hash_seed));
692
693         return 0;
694 }
695
696 /* Uninitializes the flow module. */
697 void flow_exit(void)
698 {
699         kmem_cache_destroy(flow_cache);
700 }