2 * Distributed under the terms of the GNU GPL version 2.
3 * Copyright (c) 2007, 2008, 2009, 2010, 2011 Nicira Networks.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
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>
23 #include <linux/rcupdate.h>
24 #include <linux/if_arp.h>
25 #include <linux/if_ether.h>
27 #include <linux/ipv6.h>
28 #include <linux/tcp.h>
29 #include <linux/udp.h>
30 #include <linux/icmp.h>
31 #include <linux/icmpv6.h>
32 #include <net/inet_ecn.h>
35 #include <net/ndisc.h>
39 static struct kmem_cache *flow_cache;
40 static unsigned int hash_seed __read_mostly;
42 static int check_header(struct sk_buff *skb, int len)
44 if (unlikely(skb->len < len))
46 if (unlikely(!pskb_may_pull(skb, len)))
51 static inline bool arphdr_ok(struct sk_buff *skb)
53 return pskb_may_pull(skb, skb_network_offset(skb) +
54 sizeof(struct arp_eth_header));
57 static inline int check_iphdr(struct sk_buff *skb)
59 unsigned int nh_ofs = skb_network_offset(skb);
63 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
67 ip_len = ip_hdrlen(skb);
68 if (unlikely(ip_len < sizeof(struct iphdr) ||
69 skb->len < nh_ofs + ip_len))
72 skb_set_transport_header(skb, nh_ofs + ip_len);
76 static inline bool tcphdr_ok(struct sk_buff *skb)
78 int th_ofs = skb_transport_offset(skb);
81 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
84 tcp_len = tcp_hdrlen(skb);
85 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
86 skb->len < th_ofs + tcp_len))
92 static inline bool udphdr_ok(struct sk_buff *skb)
94 return pskb_may_pull(skb, skb_transport_offset(skb) +
95 sizeof(struct udphdr));
98 static inline bool icmphdr_ok(struct sk_buff *skb)
100 return pskb_may_pull(skb, skb_transport_offset(skb) +
101 sizeof(struct icmphdr));
104 u64 flow_used_time(unsigned long flow_jiffies)
106 struct timespec cur_ts;
109 ktime_get_ts(&cur_ts);
110 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
111 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
112 cur_ts.tv_nsec / NSEC_PER_MSEC;
114 return cur_ms - idle_ms;
117 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
119 unsigned int nh_ofs = skb_network_offset(skb);
126 err = check_header(skb, nh_ofs + sizeof(*nh));
131 nexthdr = nh->nexthdr;
132 payload_ofs = (u8 *)(nh + 1) - skb->data;
134 ipv6_addr_copy(&key->ipv6_src, &nh->saddr);
135 ipv6_addr_copy(&key->ipv6_dst, &nh->daddr);
136 key->nw_tos = ipv6_get_dsfield(nh) & ~INET_ECN_MASK;
137 key->nw_proto = NEXTHDR_NONE;
139 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr);
140 if (unlikely(payload_ofs < 0))
143 nh_len = payload_ofs - nh_ofs;
144 skb_set_transport_header(skb, nh_ofs + nh_len);
145 key->nw_proto = nexthdr;
149 static bool icmp6hdr_ok(struct sk_buff *skb)
151 return pskb_may_pull(skb, skb_transport_offset(skb) +
152 sizeof(struct icmp6hdr));
155 #define TCP_FLAGS_OFFSET 13
156 #define TCP_FLAG_MASK 0x3f
158 void flow_used(struct sw_flow *flow, struct sk_buff *skb)
162 if (flow->key.dl_type == htons(ETH_P_IP) &&
163 flow->key.nw_proto == IPPROTO_TCP) {
164 u8 *tcp = (u8 *)tcp_hdr(skb);
165 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
168 spin_lock_bh(&flow->lock);
169 flow->used = jiffies;
170 flow->packet_count++;
171 flow->byte_count += skb->len;
172 flow->tcp_flags |= tcp_flags;
173 spin_unlock_bh(&flow->lock);
176 struct sw_flow_actions *flow_actions_alloc(const struct nlattr *actions)
178 int actions_len = nla_len(actions);
179 struct sw_flow_actions *sfa;
181 /* At least DP_MAX_PORTS actions are required to be able to flood a
182 * packet to every port. Factor of 2 allows for setting VLAN tags,
184 if (actions_len > 2 * DP_MAX_PORTS * nla_total_size(4))
185 return ERR_PTR(-EINVAL);
187 sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL);
189 return ERR_PTR(-ENOMEM);
191 sfa->actions_len = actions_len;
192 memcpy(sfa->actions, nla_data(actions), actions_len);
196 struct sw_flow *flow_alloc(void)
198 struct sw_flow *flow;
200 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
202 return ERR_PTR(-ENOMEM);
204 spin_lock_init(&flow->lock);
205 atomic_set(&flow->refcnt, 1);
206 flow->sf_acts = NULL;
212 void flow_free_tbl(struct tbl_node *node)
214 struct sw_flow *flow = flow_cast(node);
220 /* RCU callback used by flow_deferred_free. */
221 static void rcu_free_flow_callback(struct rcu_head *rcu)
223 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
229 /* Schedules 'flow' to be freed after the next RCU grace period.
230 * The caller must hold rcu_read_lock for this to be sensible. */
231 void flow_deferred_free(struct sw_flow *flow)
233 call_rcu(&flow->rcu, rcu_free_flow_callback);
236 void flow_hold(struct sw_flow *flow)
238 atomic_inc(&flow->refcnt);
241 void flow_put(struct sw_flow *flow)
246 if (atomic_dec_and_test(&flow->refcnt)) {
247 kfree((struct sf_flow_acts __force *)flow->sf_acts);
248 kmem_cache_free(flow_cache, flow);
252 /* RCU callback used by flow_deferred_free_acts. */
253 static void rcu_free_acts_callback(struct rcu_head *rcu)
255 struct sw_flow_actions *sf_acts = container_of(rcu,
256 struct sw_flow_actions, rcu);
260 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
261 * The caller must hold rcu_read_lock for this to be sensible. */
262 void flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
264 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
267 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
270 __be16 eth_type; /* ETH_P_8021Q */
273 struct qtag_prefix *qp;
275 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
279 qp = (struct qtag_prefix *) skb->data;
280 key->dl_tci = qp->tci | htons(VLAN_TAG_PRESENT);
281 __skb_pull(skb, sizeof(struct qtag_prefix));
286 static __be16 parse_ethertype(struct sk_buff *skb)
288 struct llc_snap_hdr {
289 u8 dsap; /* Always 0xAA */
290 u8 ssap; /* Always 0xAA */
295 struct llc_snap_hdr *llc;
298 proto = *(__be16 *) skb->data;
299 __skb_pull(skb, sizeof(__be16));
301 if (ntohs(proto) >= 1536)
304 if (skb->len < sizeof(struct llc_snap_hdr))
305 return htons(ETH_P_802_2);
307 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
310 llc = (struct llc_snap_hdr *) skb->data;
311 if (llc->dsap != LLC_SAP_SNAP ||
312 llc->ssap != LLC_SAP_SNAP ||
313 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
314 return htons(ETH_P_802_2);
316 __skb_pull(skb, sizeof(struct llc_snap_hdr));
317 return llc->ethertype;
320 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
323 struct icmp6hdr *icmp = icmp6_hdr(skb);
325 /* The ICMPv6 type and code fields use the 16-bit transport port
326 * fields, so we need to store them in 16-bit network byte order.
328 key->tp_src = htons(icmp->icmp6_type);
329 key->tp_dst = htons(icmp->icmp6_code);
331 if (icmp->icmp6_code == 0 &&
332 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
333 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
334 int icmp_len = skb->len - skb_transport_offset(skb);
338 /* In order to process neighbor discovery options, we need the
341 if (unlikely(icmp_len < sizeof(*nd)))
343 if (unlikely(skb_linearize(skb)))
346 nd = (struct nd_msg *)skb_transport_header(skb);
347 ipv6_addr_copy(&key->nd_target, &nd->target);
349 icmp_len -= sizeof(*nd);
351 while (icmp_len >= 8) {
352 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd->opt + offset);
353 int opt_len = nd_opt->nd_opt_len * 8;
355 if (unlikely(!opt_len || opt_len > icmp_len))
358 /* Store the link layer address if the appropriate
359 * option is provided. It is considered an error if
360 * the same link layer option is specified twice.
362 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
364 if (unlikely(!is_zero_ether_addr(key->arp_sha)))
367 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
368 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
370 if (unlikely(!is_zero_ether_addr(key->arp_tha)))
373 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
384 memset(&key->nd_target, 0, sizeof(key->nd_target));
385 memset(key->arp_sha, 0, sizeof(key->arp_sha));
386 memset(key->arp_tha, 0, sizeof(key->arp_tha));
392 * flow_extract - extracts a flow key from an Ethernet frame.
393 * @skb: sk_buff that contains the frame, with skb->data pointing to the
395 * @in_port: port number on which @skb was received.
396 * @key: output flow key
397 * @is_frag: set to 1 if @skb contains an IPv4 fragment, or to 0 if @skb does
398 * not contain an IPv4 packet or if it is not a fragment.
400 * The caller must ensure that skb->len >= ETH_HLEN.
402 * Returns 0 if successful, otherwise a negative errno value.
404 * Initializes @skb header pointers as follows:
406 * - skb->mac_header: the Ethernet header.
408 * - skb->network_header: just past the Ethernet header, or just past the
409 * VLAN header, to the first byte of the Ethernet payload.
411 * - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
412 * on output, then just past the IP header, if one is present and
413 * of a correct length, otherwise the same as skb->network_header.
414 * For other key->dl_type values it is left untouched.
416 int flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
421 memset(key, 0, sizeof(*key));
422 key->tun_id = OVS_CB(skb)->tun_id;
423 key->in_port = in_port;
426 skb_reset_mac_header(skb);
428 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
429 * header in the linear data area.
432 memcpy(key->dl_src, eth->h_source, ETH_ALEN);
433 memcpy(key->dl_dst, eth->h_dest, ETH_ALEN);
434 __skb_pull(skb, 2 * ETH_ALEN);
436 if (vlan_tx_tag_present(skb))
437 key->dl_tci = htons(vlan_get_tci(skb));
438 else if (eth->h_proto == htons(ETH_P_8021Q))
439 if (unlikely(parse_vlan(skb, key)))
442 key->dl_type = parse_ethertype(skb);
443 if (unlikely(key->dl_type == htons(0)))
446 skb_reset_network_header(skb);
447 __skb_push(skb, skb->data - skb_mac_header(skb));
450 if (key->dl_type == htons(ETH_P_IP)) {
454 error = check_iphdr(skb);
455 if (unlikely(error)) {
456 if (error == -EINVAL) {
457 skb->transport_header = skb->network_header;
464 key->ipv4_src = nh->saddr;
465 key->ipv4_dst = nh->daddr;
466 key->nw_tos = nh->tos & ~INET_ECN_MASK;
467 key->nw_proto = nh->protocol;
469 /* Transport layer. */
470 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET)) &&
471 !(skb_shinfo(skb)->gso_type & SKB_GSO_UDP)) {
472 if (key->nw_proto == IPPROTO_TCP) {
473 if (tcphdr_ok(skb)) {
474 struct tcphdr *tcp = tcp_hdr(skb);
475 key->tp_src = tcp->source;
476 key->tp_dst = tcp->dest;
478 } else if (key->nw_proto == IPPROTO_UDP) {
479 if (udphdr_ok(skb)) {
480 struct udphdr *udp = udp_hdr(skb);
481 key->tp_src = udp->source;
482 key->tp_dst = udp->dest;
484 } else if (key->nw_proto == IPPROTO_ICMP) {
485 if (icmphdr_ok(skb)) {
486 struct icmphdr *icmp = icmp_hdr(skb);
487 /* The ICMP type and code fields use the 16-bit
488 * transport port fields, so we need to store them
489 * in 16-bit network byte order. */
490 key->tp_src = htons(icmp->type);
491 key->tp_dst = htons(icmp->code);
497 } else if (key->dl_type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
498 struct arp_eth_header *arp;
500 arp = (struct arp_eth_header *)skb_network_header(skb);
502 if (arp->ar_hrd == htons(ARPHRD_ETHER)
503 && arp->ar_pro == htons(ETH_P_IP)
504 && arp->ar_hln == ETH_ALEN
505 && arp->ar_pln == 4) {
507 /* We only match on the lower 8 bits of the opcode. */
508 if (ntohs(arp->ar_op) <= 0xff)
509 key->nw_proto = ntohs(arp->ar_op);
511 if (key->nw_proto == ARPOP_REQUEST
512 || key->nw_proto == ARPOP_REPLY) {
513 memcpy(&key->ipv4_src, arp->ar_sip, sizeof(key->ipv4_src));
514 memcpy(&key->ipv4_dst, arp->ar_tip, sizeof(key->ipv4_dst));
515 memcpy(key->arp_sha, arp->ar_sha, ETH_ALEN);
516 memcpy(key->arp_tha, arp->ar_tha, ETH_ALEN);
519 } else if (key->dl_type == htons(ETH_P_IPV6)) {
520 int nh_len; /* IPv6 Header + Extensions */
522 nh_len = parse_ipv6hdr(skb, key);
523 if (unlikely(nh_len < 0)) {
524 if (nh_len == -EINVAL) {
525 skb->transport_header = skb->network_header;
531 /* Transport layer. */
532 if (key->nw_proto == NEXTHDR_TCP) {
533 if (tcphdr_ok(skb)) {
534 struct tcphdr *tcp = tcp_hdr(skb);
535 key->tp_src = tcp->source;
536 key->tp_dst = tcp->dest;
538 } else if (key->nw_proto == NEXTHDR_UDP) {
539 if (udphdr_ok(skb)) {
540 struct udphdr *udp = udp_hdr(skb);
541 key->tp_src = udp->source;
542 key->tp_dst = udp->dest;
544 } else if (key->nw_proto == NEXTHDR_ICMP) {
545 if (icmp6hdr_ok(skb)) {
546 int error = parse_icmpv6(skb, key, nh_len);
555 u32 flow_hash(const struct sw_flow_key *key)
557 return jhash2((u32*)key, sizeof(*key) / sizeof(u32), hash_seed);
560 int flow_cmp(const struct tbl_node *node, void *key2_)
562 const struct sw_flow_key *key1 = &flow_cast(node)->key;
563 const struct sw_flow_key *key2 = key2_;
565 return !memcmp(key1, key2, sizeof(struct sw_flow_key));
569 * flow_from_nlattrs - parses Netlink attributes into a flow key.
570 * @swkey: receives the extracted flow key.
571 * @key: Netlink attribute holding nested %ODP_KEY_ATTR_* Netlink attribute
574 * This state machine accepts the following forms, with [] for optional
575 * elements and | for alternatives:
577 * [tun_id] in_port ethernet [8021q] [ethertype \
578 * [IPv4 [TCP|UDP|ICMP] | IPv6 [TCP|UDP|ICMPv6 [ND]] | ARP]]
580 int flow_from_nlattrs(struct sw_flow_key *swkey, const struct nlattr *attr)
582 const struct nlattr *nla;
586 memset(swkey, 0, sizeof(*swkey));
587 swkey->dl_type = htons(ETH_P_802_2);
589 prev_type = ODP_KEY_ATTR_UNSPEC;
590 nla_for_each_nested(nla, attr, rem) {
591 static const u32 key_lens[ODP_KEY_ATTR_MAX + 1] = {
592 [ODP_KEY_ATTR_TUN_ID] = 8,
593 [ODP_KEY_ATTR_IN_PORT] = 4,
594 [ODP_KEY_ATTR_ETHERNET] = sizeof(struct odp_key_ethernet),
595 [ODP_KEY_ATTR_8021Q] = sizeof(struct odp_key_8021q),
596 [ODP_KEY_ATTR_ETHERTYPE] = 2,
597 [ODP_KEY_ATTR_IPV4] = sizeof(struct odp_key_ipv4),
598 [ODP_KEY_ATTR_IPV6] = sizeof(struct odp_key_ipv6),
599 [ODP_KEY_ATTR_TCP] = sizeof(struct odp_key_tcp),
600 [ODP_KEY_ATTR_UDP] = sizeof(struct odp_key_udp),
601 [ODP_KEY_ATTR_ICMP] = sizeof(struct odp_key_icmp),
602 [ODP_KEY_ATTR_ICMPV6] = sizeof(struct odp_key_icmpv6),
603 [ODP_KEY_ATTR_ARP] = sizeof(struct odp_key_arp),
604 [ODP_KEY_ATTR_ND] = sizeof(struct odp_key_nd),
607 const struct odp_key_ethernet *eth_key;
608 const struct odp_key_8021q *q_key;
609 const struct odp_key_ipv4 *ipv4_key;
610 const struct odp_key_ipv6 *ipv6_key;
611 const struct odp_key_tcp *tcp_key;
612 const struct odp_key_udp *udp_key;
613 const struct odp_key_icmp *icmp_key;
614 const struct odp_key_icmpv6 *icmpv6_key;
615 const struct odp_key_arp *arp_key;
616 const struct odp_key_nd *nd_key;
618 int type = nla_type(nla);
620 if (type > ODP_KEY_ATTR_MAX || nla_len(nla) != key_lens[type])
623 #define TRANSITION(PREV_TYPE, TYPE) (((PREV_TYPE) << 16) | (TYPE))
624 switch (TRANSITION(prev_type, type)) {
625 case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_TUN_ID):
626 swkey->tun_id = nla_get_be64(nla);
629 case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_IN_PORT):
630 case TRANSITION(ODP_KEY_ATTR_TUN_ID, ODP_KEY_ATTR_IN_PORT):
631 if (nla_get_u32(nla) >= DP_MAX_PORTS)
633 swkey->in_port = nla_get_u32(nla);
636 case TRANSITION(ODP_KEY_ATTR_IN_PORT, ODP_KEY_ATTR_ETHERNET):
637 eth_key = nla_data(nla);
638 memcpy(swkey->dl_src, eth_key->eth_src, ETH_ALEN);
639 memcpy(swkey->dl_dst, eth_key->eth_dst, ETH_ALEN);
642 case TRANSITION(ODP_KEY_ATTR_ETHERNET, ODP_KEY_ATTR_8021Q):
643 q_key = nla_data(nla);
644 /* Only standard 0x8100 VLANs currently supported. */
645 if (q_key->q_tpid != htons(ETH_P_8021Q))
647 if (q_key->q_tci & htons(VLAN_TAG_PRESENT))
649 swkey->dl_tci = q_key->q_tci | htons(VLAN_TAG_PRESENT);
652 case TRANSITION(ODP_KEY_ATTR_8021Q, ODP_KEY_ATTR_ETHERTYPE):
653 case TRANSITION(ODP_KEY_ATTR_ETHERNET, ODP_KEY_ATTR_ETHERTYPE):
654 swkey->dl_type = nla_get_be16(nla);
655 if (ntohs(swkey->dl_type) < 1536)
659 case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_IPV4):
660 if (swkey->dl_type != htons(ETH_P_IP))
662 ipv4_key = nla_data(nla);
663 swkey->ipv4_src = ipv4_key->ipv4_src;
664 swkey->ipv4_dst = ipv4_key->ipv4_dst;
665 swkey->nw_proto = ipv4_key->ipv4_proto;
666 swkey->nw_tos = ipv4_key->ipv4_tos;
667 if (swkey->nw_tos & INET_ECN_MASK)
671 case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_IPV6):
672 if (swkey->dl_type != htons(ETH_P_IPV6))
674 ipv6_key = nla_data(nla);
675 memcpy(&swkey->ipv6_src, ipv6_key->ipv6_src,
676 sizeof(swkey->ipv6_src));
677 memcpy(&swkey->ipv6_dst, ipv6_key->ipv6_dst,
678 sizeof(swkey->ipv6_dst));
679 swkey->nw_proto = ipv6_key->ipv6_proto;
680 swkey->nw_tos = ipv6_key->ipv6_tos;
681 if (swkey->nw_tos & INET_ECN_MASK)
685 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_TCP):
686 case TRANSITION(ODP_KEY_ATTR_IPV6, ODP_KEY_ATTR_TCP):
687 if (swkey->nw_proto != IPPROTO_TCP)
689 tcp_key = nla_data(nla);
690 swkey->tp_src = tcp_key->tcp_src;
691 swkey->tp_dst = tcp_key->tcp_dst;
694 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_UDP):
695 case TRANSITION(ODP_KEY_ATTR_IPV6, ODP_KEY_ATTR_UDP):
696 if (swkey->nw_proto != IPPROTO_UDP)
698 udp_key = nla_data(nla);
699 swkey->tp_src = udp_key->udp_src;
700 swkey->tp_dst = udp_key->udp_dst;
703 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_ICMP):
704 if (swkey->nw_proto != IPPROTO_ICMP)
706 icmp_key = nla_data(nla);
707 swkey->tp_src = htons(icmp_key->icmp_type);
708 swkey->tp_dst = htons(icmp_key->icmp_code);
711 case TRANSITION(ODP_KEY_ATTR_IPV6, ODP_KEY_ATTR_ICMPV6):
712 if (swkey->nw_proto != IPPROTO_ICMPV6)
714 icmpv6_key = nla_data(nla);
715 swkey->tp_src = htons(icmpv6_key->icmpv6_type);
716 swkey->tp_dst = htons(icmpv6_key->icmpv6_code);
719 case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_ARP):
720 if (swkey->dl_type != htons(ETH_P_ARP))
722 arp_key = nla_data(nla);
723 swkey->ipv4_src = arp_key->arp_sip;
724 swkey->ipv4_dst = arp_key->arp_tip;
725 if (arp_key->arp_op & htons(0xff00))
727 swkey->nw_proto = ntohs(arp_key->arp_op);
728 memcpy(swkey->arp_sha, arp_key->arp_sha, ETH_ALEN);
729 memcpy(swkey->arp_tha, arp_key->arp_tha, ETH_ALEN);
732 case TRANSITION(ODP_KEY_ATTR_ICMPV6, ODP_KEY_ATTR_ND):
733 if (swkey->tp_src != htons(NDISC_NEIGHBOUR_SOLICITATION)
734 && swkey->tp_src != htons(NDISC_NEIGHBOUR_ADVERTISEMENT))
736 nd_key = nla_data(nla);
737 memcpy(&swkey->nd_target, nd_key->nd_target,
738 sizeof(swkey->nd_target));
739 memcpy(swkey->arp_sha, nd_key->nd_sll, ETH_ALEN);
740 memcpy(swkey->arp_tha, nd_key->nd_tll, ETH_ALEN);
753 case ODP_KEY_ATTR_UNSPEC:
756 case ODP_KEY_ATTR_TUN_ID:
757 case ODP_KEY_ATTR_IN_PORT:
760 case ODP_KEY_ATTR_ETHERNET:
761 case ODP_KEY_ATTR_8021Q:
764 case ODP_KEY_ATTR_ETHERTYPE:
765 if (swkey->dl_type == htons(ETH_P_IP) ||
766 swkey->dl_type == htons(ETH_P_ARP))
770 case ODP_KEY_ATTR_IPV4:
771 if (swkey->nw_proto == IPPROTO_TCP ||
772 swkey->nw_proto == IPPROTO_UDP ||
773 swkey->nw_proto == IPPROTO_ICMP)
777 case ODP_KEY_ATTR_IPV6:
778 if (swkey->nw_proto == IPPROTO_TCP ||
779 swkey->nw_proto == IPPROTO_UDP ||
780 swkey->nw_proto == IPPROTO_ICMPV6)
784 case ODP_KEY_ATTR_ICMPV6:
785 if (swkey->tp_src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
786 swkey->tp_src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT))
790 case ODP_KEY_ATTR_TCP:
791 case ODP_KEY_ATTR_UDP:
792 case ODP_KEY_ATTR_ICMP:
793 case ODP_KEY_ATTR_ARP:
794 case ODP_KEY_ATTR_ND:
802 int flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
804 struct odp_key_ethernet *eth_key;
807 /* This is an imperfect sanity-check that FLOW_BUFSIZE doesn't need
808 * to be updated, but will at least raise awareness when new ODP key
809 * types are added. */
810 BUILD_BUG_ON(__ODP_KEY_ATTR_MAX != 14);
812 if (swkey->tun_id != cpu_to_be64(0))
813 NLA_PUT_BE64(skb, ODP_KEY_ATTR_TUN_ID, swkey->tun_id);
815 NLA_PUT_U32(skb, ODP_KEY_ATTR_IN_PORT, swkey->in_port);
817 nla = nla_reserve(skb, ODP_KEY_ATTR_ETHERNET, sizeof(*eth_key));
819 goto nla_put_failure;
820 eth_key = nla_data(nla);
821 memcpy(eth_key->eth_src, swkey->dl_src, ETH_ALEN);
822 memcpy(eth_key->eth_dst, swkey->dl_dst, ETH_ALEN);
824 if (swkey->dl_tci != htons(0)) {
825 struct odp_key_8021q q_key;
827 q_key.q_tpid = htons(ETH_P_8021Q);
828 q_key.q_tci = swkey->dl_tci & ~htons(VLAN_TAG_PRESENT);
829 NLA_PUT(skb, ODP_KEY_ATTR_8021Q, sizeof(q_key), &q_key);
832 if (swkey->dl_type == htons(ETH_P_802_2))
835 NLA_PUT_BE16(skb, ODP_KEY_ATTR_ETHERTYPE, swkey->dl_type);
837 if (swkey->dl_type == htons(ETH_P_IP)) {
838 struct odp_key_ipv4 *ipv4_key;
840 nla = nla_reserve(skb, ODP_KEY_ATTR_IPV4, sizeof(*ipv4_key));
842 goto nla_put_failure;
843 ipv4_key = nla_data(nla);
844 memset(ipv4_key, 0, sizeof(struct odp_key_ipv4));
845 ipv4_key->ipv4_src = swkey->ipv4_src;
846 ipv4_key->ipv4_dst = swkey->ipv4_dst;
847 ipv4_key->ipv4_proto = swkey->nw_proto;
848 ipv4_key->ipv4_tos = swkey->nw_tos;
849 } else if (swkey->dl_type == htons(ETH_P_IPV6)) {
850 struct odp_key_ipv6 *ipv6_key;
852 nla = nla_reserve(skb, ODP_KEY_ATTR_IPV6, sizeof(*ipv6_key));
854 goto nla_put_failure;
855 ipv6_key = nla_data(nla);
856 memset(ipv6_key, 0, sizeof(struct odp_key_ipv6));
857 memcpy(ipv6_key->ipv6_src, &swkey->ipv6_src,
858 sizeof(ipv6_key->ipv6_src));
859 memcpy(ipv6_key->ipv6_dst, &swkey->ipv6_dst,
860 sizeof(ipv6_key->ipv6_dst));
861 ipv6_key->ipv6_proto = swkey->nw_proto;
862 ipv6_key->ipv6_tos = swkey->nw_tos;
863 } else if (swkey->dl_type == htons(ETH_P_ARP)) {
864 struct odp_key_arp *arp_key;
866 nla = nla_reserve(skb, ODP_KEY_ATTR_ARP, sizeof(*arp_key));
868 goto nla_put_failure;
869 arp_key = nla_data(nla);
870 memset(arp_key, 0, sizeof(struct odp_key_arp));
871 arp_key->arp_sip = swkey->ipv4_src;
872 arp_key->arp_tip = swkey->ipv4_dst;
873 arp_key->arp_op = htons(swkey->nw_proto);
874 memcpy(arp_key->arp_sha, swkey->arp_sha, ETH_ALEN);
875 memcpy(arp_key->arp_tha, swkey->arp_tha, ETH_ALEN);
878 if (swkey->dl_type == htons(ETH_P_IP) ||
879 swkey->dl_type == htons(ETH_P_IPV6)) {
881 if (swkey->nw_proto == IPPROTO_TCP) {
882 struct odp_key_tcp *tcp_key;
884 nla = nla_reserve(skb, ODP_KEY_ATTR_TCP, sizeof(*tcp_key));
886 goto nla_put_failure;
887 tcp_key = nla_data(nla);
888 tcp_key->tcp_src = swkey->tp_src;
889 tcp_key->tcp_dst = swkey->tp_dst;
890 } else if (swkey->nw_proto == IPPROTO_UDP) {
891 struct odp_key_udp *udp_key;
893 nla = nla_reserve(skb, ODP_KEY_ATTR_UDP, sizeof(*udp_key));
895 goto nla_put_failure;
896 udp_key = nla_data(nla);
897 udp_key->udp_src = swkey->tp_src;
898 udp_key->udp_dst = swkey->tp_dst;
899 } else if (swkey->dl_type == htons(ETH_P_IP) &&
900 swkey->nw_proto == IPPROTO_ICMP) {
901 struct odp_key_icmp *icmp_key;
903 nla = nla_reserve(skb, ODP_KEY_ATTR_ICMP, sizeof(*icmp_key));
905 goto nla_put_failure;
906 icmp_key = nla_data(nla);
907 icmp_key->icmp_type = ntohs(swkey->tp_src);
908 icmp_key->icmp_code = ntohs(swkey->tp_dst);
909 } else if (swkey->dl_type == htons(ETH_P_IPV6) &&
910 swkey->nw_proto == IPPROTO_ICMPV6) {
911 struct odp_key_icmpv6 *icmpv6_key;
913 nla = nla_reserve(skb, ODP_KEY_ATTR_ICMPV6,
914 sizeof(*icmpv6_key));
916 goto nla_put_failure;
917 icmpv6_key = nla_data(nla);
918 icmpv6_key->icmpv6_type = ntohs(swkey->tp_src);
919 icmpv6_key->icmpv6_code = ntohs(swkey->tp_dst);
921 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
922 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
923 struct odp_key_nd *nd_key;
925 nla = nla_reserve(skb, ODP_KEY_ATTR_ND, sizeof(*nd_key));
927 goto nla_put_failure;
928 nd_key = nla_data(nla);
929 memcpy(nd_key->nd_target, &swkey->nd_target,
930 sizeof(nd_key->nd_target));
931 memcpy(nd_key->nd_sll, swkey->arp_sha, ETH_ALEN);
932 memcpy(nd_key->nd_tll, swkey->arp_tha, ETH_ALEN);
943 /* Initializes the flow module.
944 * Returns zero if successful or a negative error code. */
947 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
949 if (flow_cache == NULL)
952 get_random_bytes(&hash_seed, sizeof(hash_seed));
957 /* Uninitializes the flow module. */
960 kmem_cache_destroy(flow_cache);