2 * Copyright (c) 2007-2011 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <linux/uaccess.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <net/llc_pdu.h>
27 #include <linux/kernel.h>
28 #include <linux/jhash.h>
29 #include <linux/jiffies.h>
30 #include <linux/llc.h>
31 #include <linux/module.h>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.h>
36 #include <linux/ipv6.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/icmp.h>
40 #include <linux/icmpv6.h>
41 #include <linux/rculist.h>
44 #include <net/ndisc.h>
46 static struct kmem_cache *flow_cache;
48 static int check_header(struct sk_buff *skb, int len)
50 if (unlikely(skb->len < len))
52 if (unlikely(!pskb_may_pull(skb, len)))
57 static bool arphdr_ok(struct sk_buff *skb)
59 return pskb_may_pull(skb, skb_network_offset(skb) +
60 sizeof(struct arp_eth_header));
63 static int check_iphdr(struct sk_buff *skb)
65 unsigned int nh_ofs = skb_network_offset(skb);
69 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
73 ip_len = ip_hdrlen(skb);
74 if (unlikely(ip_len < sizeof(struct iphdr) ||
75 skb->len < nh_ofs + ip_len))
78 skb_set_transport_header(skb, nh_ofs + ip_len);
82 static bool tcphdr_ok(struct sk_buff *skb)
84 int th_ofs = skb_transport_offset(skb);
87 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
90 tcp_len = tcp_hdrlen(skb);
91 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
92 skb->len < th_ofs + tcp_len))
98 static bool udphdr_ok(struct sk_buff *skb)
100 return pskb_may_pull(skb, skb_transport_offset(skb) +
101 sizeof(struct udphdr));
104 static bool icmphdr_ok(struct sk_buff *skb)
106 return pskb_may_pull(skb, skb_transport_offset(skb) +
107 sizeof(struct icmphdr));
110 u64 ovs_flow_used_time(unsigned long flow_jiffies)
112 struct timespec cur_ts;
115 ktime_get_ts(&cur_ts);
116 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
117 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
118 cur_ts.tv_nsec / NSEC_PER_MSEC;
120 return cur_ms - idle_ms;
123 #define SW_FLOW_KEY_OFFSET(field) \
124 (offsetof(struct sw_flow_key, field) + \
125 FIELD_SIZEOF(struct sw_flow_key, field))
127 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key,
130 unsigned int nh_ofs = skb_network_offset(skb);
138 *key_lenp = SW_FLOW_KEY_OFFSET(ipv6.label);
140 err = check_header(skb, nh_ofs + sizeof(*nh));
145 nexthdr = nh->nexthdr;
146 payload_ofs = (u8 *)(nh + 1) - skb->data;
148 key->ip.proto = NEXTHDR_NONE;
149 key->ip.tos = ipv6_get_dsfield(nh);
150 key->ip.ttl = nh->hop_limit;
151 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
152 key->ipv6.addr.src = nh->saddr;
153 key->ipv6.addr.dst = nh->daddr;
155 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
156 if (unlikely(payload_ofs < 0))
160 if (frag_off & htons(~0x7))
161 key->ip.frag = OVS_FRAG_TYPE_LATER;
163 key->ip.frag = OVS_FRAG_TYPE_FIRST;
166 nh_len = payload_ofs - nh_ofs;
167 skb_set_transport_header(skb, nh_ofs + nh_len);
168 key->ip.proto = nexthdr;
172 static bool icmp6hdr_ok(struct sk_buff *skb)
174 return pskb_may_pull(skb, skb_transport_offset(skb) +
175 sizeof(struct icmp6hdr));
178 #define TCP_FLAGS_OFFSET 13
179 #define TCP_FLAG_MASK 0x3f
181 void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
185 if ((flow->key.eth.type == htons(ETH_P_IP) ||
186 flow->key.eth.type == htons(ETH_P_IPV6)) &&
187 flow->key.ip.proto == IPPROTO_TCP &&
188 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
189 u8 *tcp = (u8 *)tcp_hdr(skb);
190 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
193 spin_lock(&flow->lock);
194 flow->used = jiffies;
195 flow->packet_count++;
196 flow->byte_count += skb->len;
197 flow->tcp_flags |= tcp_flags;
198 spin_unlock(&flow->lock);
201 struct sw_flow_actions *ovs_flow_actions_alloc(const struct nlattr *actions)
203 int actions_len = nla_len(actions);
204 struct sw_flow_actions *sfa;
206 if (actions_len > MAX_ACTIONS_BUFSIZE)
207 return ERR_PTR(-EINVAL);
209 sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL);
211 return ERR_PTR(-ENOMEM);
213 sfa->actions_len = actions_len;
214 nla_memcpy(sfa->actions, actions, actions_len);
218 struct sw_flow *ovs_flow_alloc(void)
220 struct sw_flow *flow;
222 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
224 return ERR_PTR(-ENOMEM);
226 spin_lock_init(&flow->lock);
227 flow->sf_acts = NULL;
232 static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
234 hash = jhash_1word(hash, table->hash_seed);
235 return flex_array_get(table->buckets,
236 (hash & (table->n_buckets - 1)));
239 static struct flex_array *alloc_buckets(unsigned int n_buckets)
241 struct flex_array *buckets;
244 buckets = flex_array_alloc(sizeof(struct hlist_head *),
245 n_buckets, GFP_KERNEL);
249 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
251 flex_array_free(buckets);
255 for (i = 0; i < n_buckets; i++)
256 INIT_HLIST_HEAD((struct hlist_head *)
257 flex_array_get(buckets, i));
262 static void free_buckets(struct flex_array *buckets)
264 flex_array_free(buckets);
267 struct flow_table *ovs_flow_tbl_alloc(int new_size)
269 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
274 table->buckets = alloc_buckets(new_size);
276 if (!table->buckets) {
280 table->n_buckets = new_size;
283 table->keep_flows = false;
284 get_random_bytes(&table->hash_seed, sizeof(u32));
289 void ovs_flow_tbl_destroy(struct flow_table *table)
296 if (table->keep_flows)
299 for (i = 0; i < table->n_buckets; i++) {
300 struct sw_flow *flow;
301 struct hlist_head *head = flex_array_get(table->buckets, i);
302 struct hlist_node *n;
303 int ver = table->node_ver;
305 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
306 hlist_del_rcu(&flow->hash_node[ver]);
312 free_buckets(table->buckets);
316 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
318 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
320 ovs_flow_tbl_destroy(table);
323 void ovs_flow_tbl_deferred_destroy(struct flow_table *table)
328 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
331 struct sw_flow *ovs_flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last)
333 struct sw_flow *flow;
334 struct hlist_head *head;
338 ver = table->node_ver;
339 while (*bucket < table->n_buckets) {
341 head = flex_array_get(table->buckets, *bucket);
342 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
357 static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
362 old_ver = old->node_ver;
363 new->node_ver = !old_ver;
365 /* Insert in new table. */
366 for (i = 0; i < old->n_buckets; i++) {
367 struct sw_flow *flow;
368 struct hlist_head *head;
370 head = flex_array_get(old->buckets, i);
372 hlist_for_each_entry(flow, head, hash_node[old_ver])
373 ovs_flow_tbl_insert(new, flow);
375 old->keep_flows = true;
378 static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
380 struct flow_table *new_table;
382 new_table = ovs_flow_tbl_alloc(n_buckets);
384 return ERR_PTR(-ENOMEM);
386 flow_table_copy_flows(table, new_table);
391 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
393 return __flow_tbl_rehash(table, table->n_buckets);
396 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
398 return __flow_tbl_rehash(table, table->n_buckets * 2);
401 void ovs_flow_free(struct sw_flow *flow)
406 kfree((struct sf_flow_acts __force *)flow->sf_acts);
407 kmem_cache_free(flow_cache, flow);
410 /* RCU callback used by ovs_flow_deferred_free. */
411 static void rcu_free_flow_callback(struct rcu_head *rcu)
413 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
418 /* Schedules 'flow' to be freed after the next RCU grace period.
419 * The caller must hold rcu_read_lock for this to be sensible. */
420 void ovs_flow_deferred_free(struct sw_flow *flow)
422 call_rcu(&flow->rcu, rcu_free_flow_callback);
425 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
426 * The caller must hold rcu_read_lock for this to be sensible. */
427 void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
429 kfree_rcu(sf_acts, rcu);
432 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
435 __be16 eth_type; /* ETH_P_8021Q */
438 struct qtag_prefix *qp;
440 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
443 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
447 qp = (struct qtag_prefix *) skb->data;
448 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
449 __skb_pull(skb, sizeof(struct qtag_prefix));
454 static __be16 parse_ethertype(struct sk_buff *skb)
456 struct llc_snap_hdr {
457 u8 dsap; /* Always 0xAA */
458 u8 ssap; /* Always 0xAA */
463 struct llc_snap_hdr *llc;
466 proto = *(__be16 *) skb->data;
467 __skb_pull(skb, sizeof(__be16));
469 if (ntohs(proto) >= ETH_P_802_3_MIN)
472 if (skb->len < sizeof(struct llc_snap_hdr))
473 return htons(ETH_P_802_2);
475 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
478 llc = (struct llc_snap_hdr *) skb->data;
479 if (llc->dsap != LLC_SAP_SNAP ||
480 llc->ssap != LLC_SAP_SNAP ||
481 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
482 return htons(ETH_P_802_2);
484 __skb_pull(skb, sizeof(struct llc_snap_hdr));
486 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
487 return llc->ethertype;
489 return htons(ETH_P_802_2);
492 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
493 int *key_lenp, int nh_len)
495 struct icmp6hdr *icmp = icmp6_hdr(skb);
499 /* The ICMPv6 type and code fields use the 16-bit transport port
500 * fields, so we need to store them in 16-bit network byte order.
502 key->ipv6.tp.src = htons(icmp->icmp6_type);
503 key->ipv6.tp.dst = htons(icmp->icmp6_code);
504 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
506 if (icmp->icmp6_code == 0 &&
507 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
508 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
509 int icmp_len = skb->len - skb_transport_offset(skb);
513 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
515 /* In order to process neighbor discovery options, we need the
518 if (unlikely(icmp_len < sizeof(*nd)))
520 if (unlikely(skb_linearize(skb))) {
525 nd = (struct nd_msg *)skb_transport_header(skb);
526 key->ipv6.nd.target = nd->target;
527 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
529 icmp_len -= sizeof(*nd);
531 while (icmp_len >= 8) {
532 struct nd_opt_hdr *nd_opt =
533 (struct nd_opt_hdr *)(nd->opt + offset);
534 int opt_len = nd_opt->nd_opt_len * 8;
536 if (unlikely(!opt_len || opt_len > icmp_len))
539 /* Store the link layer address if the appropriate
540 * option is provided. It is considered an error if
541 * the same link layer option is specified twice.
543 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
545 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
547 memcpy(key->ipv6.nd.sll,
548 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
549 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
551 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
553 memcpy(key->ipv6.nd.tll,
554 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
565 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
566 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
567 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
575 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
576 * @skb: sk_buff that contains the frame, with skb->data pointing to the
578 * @in_port: port number on which @skb was received.
579 * @key: output flow key
580 * @key_lenp: length of output flow key
582 * The caller must ensure that skb->len >= ETH_HLEN.
584 * Returns 0 if successful, otherwise a negative errno value.
586 * Initializes @skb header pointers as follows:
588 * - skb->mac_header: the Ethernet header.
590 * - skb->network_header: just past the Ethernet header, or just past the
591 * VLAN header, to the first byte of the Ethernet payload.
593 * - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
594 * on output, then just past the IP header, if one is present and
595 * of a correct length, otherwise the same as skb->network_header.
596 * For other key->dl_type values it is left untouched.
598 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
602 int key_len = SW_FLOW_KEY_OFFSET(eth);
605 memset(key, 0, sizeof(*key));
607 key->phy.priority = skb->priority;
608 key->phy.in_port = in_port;
609 key->phy.skb_mark = skb->mark;
611 skb_reset_mac_header(skb);
613 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
614 * header in the linear data area.
617 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
618 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
620 __skb_pull(skb, 2 * ETH_ALEN);
622 if (vlan_tx_tag_present(skb))
623 key->eth.tci = htons(skb->vlan_tci);
624 else if (eth->h_proto == htons(ETH_P_8021Q))
625 if (unlikely(parse_vlan(skb, key)))
628 key->eth.type = parse_ethertype(skb);
629 if (unlikely(key->eth.type == htons(0)))
632 skb_reset_network_header(skb);
633 __skb_push(skb, skb->data - skb_mac_header(skb));
636 if (key->eth.type == htons(ETH_P_IP)) {
640 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
642 error = check_iphdr(skb);
643 if (unlikely(error)) {
644 if (error == -EINVAL) {
645 skb->transport_header = skb->network_header;
652 key->ipv4.addr.src = nh->saddr;
653 key->ipv4.addr.dst = nh->daddr;
655 key->ip.proto = nh->protocol;
656 key->ip.tos = nh->tos;
657 key->ip.ttl = nh->ttl;
659 offset = nh->frag_off & htons(IP_OFFSET);
661 key->ip.frag = OVS_FRAG_TYPE_LATER;
664 if (nh->frag_off & htons(IP_MF) ||
665 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
666 key->ip.frag = OVS_FRAG_TYPE_FIRST;
668 /* Transport layer. */
669 if (key->ip.proto == IPPROTO_TCP) {
670 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
671 if (tcphdr_ok(skb)) {
672 struct tcphdr *tcp = tcp_hdr(skb);
673 key->ipv4.tp.src = tcp->source;
674 key->ipv4.tp.dst = tcp->dest;
676 } else if (key->ip.proto == IPPROTO_UDP) {
677 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
678 if (udphdr_ok(skb)) {
679 struct udphdr *udp = udp_hdr(skb);
680 key->ipv4.tp.src = udp->source;
681 key->ipv4.tp.dst = udp->dest;
683 } else if (key->ip.proto == IPPROTO_ICMP) {
684 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
685 if (icmphdr_ok(skb)) {
686 struct icmphdr *icmp = icmp_hdr(skb);
687 /* The ICMP type and code fields use the 16-bit
688 * transport port fields, so we need to store
689 * them in 16-bit network byte order. */
690 key->ipv4.tp.src = htons(icmp->type);
691 key->ipv4.tp.dst = htons(icmp->code);
695 } else if ((key->eth.type == htons(ETH_P_ARP) ||
696 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
697 struct arp_eth_header *arp;
699 arp = (struct arp_eth_header *)skb_network_header(skb);
701 if (arp->ar_hrd == htons(ARPHRD_ETHER)
702 && arp->ar_pro == htons(ETH_P_IP)
703 && arp->ar_hln == ETH_ALEN
704 && arp->ar_pln == 4) {
706 /* We only match on the lower 8 bits of the opcode. */
707 if (ntohs(arp->ar_op) <= 0xff)
708 key->ip.proto = ntohs(arp->ar_op);
709 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
710 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
711 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
712 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
713 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
715 } else if (key->eth.type == htons(ETH_P_IPV6)) {
716 int nh_len; /* IPv6 Header + Extensions */
718 nh_len = parse_ipv6hdr(skb, key, &key_len);
719 if (unlikely(nh_len < 0)) {
720 if (nh_len == -EINVAL)
721 skb->transport_header = skb->network_header;
727 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
729 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
730 key->ip.frag = OVS_FRAG_TYPE_FIRST;
732 /* Transport layer. */
733 if (key->ip.proto == NEXTHDR_TCP) {
734 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
735 if (tcphdr_ok(skb)) {
736 struct tcphdr *tcp = tcp_hdr(skb);
737 key->ipv6.tp.src = tcp->source;
738 key->ipv6.tp.dst = tcp->dest;
740 } else if (key->ip.proto == NEXTHDR_UDP) {
741 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
742 if (udphdr_ok(skb)) {
743 struct udphdr *udp = udp_hdr(skb);
744 key->ipv6.tp.src = udp->source;
745 key->ipv6.tp.dst = udp->dest;
747 } else if (key->ip.proto == NEXTHDR_ICMP) {
748 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
749 if (icmp6hdr_ok(skb)) {
750 error = parse_icmpv6(skb, key, &key_len, nh_len);
762 u32 ovs_flow_hash(const struct sw_flow_key *key, int key_len)
764 return jhash2((u32 *)key, DIV_ROUND_UP(key_len, sizeof(u32)), 0);
767 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
768 struct sw_flow_key *key, int key_len)
770 struct sw_flow *flow;
771 struct hlist_head *head;
774 hash = ovs_flow_hash(key, key_len);
776 head = find_bucket(table, hash);
777 hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
779 if (flow->hash == hash &&
780 !memcmp(&flow->key, key, key_len)) {
787 void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
789 struct hlist_head *head;
791 head = find_bucket(table, flow->hash);
792 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
796 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
798 BUG_ON(table->count == 0);
799 hlist_del_rcu(&flow->hash_node[table->node_ver]);
803 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
804 const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
805 [OVS_KEY_ATTR_ENCAP] = -1,
806 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
807 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
808 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
809 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
810 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
811 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
812 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
813 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
814 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
815 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
816 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
817 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
818 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
819 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
822 static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
823 const struct nlattr *a[], u32 *attrs)
825 const struct ovs_key_icmp *icmp_key;
826 const struct ovs_key_tcp *tcp_key;
827 const struct ovs_key_udp *udp_key;
829 switch (swkey->ip.proto) {
831 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
833 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
835 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
836 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
837 swkey->ipv4.tp.src = tcp_key->tcp_src;
838 swkey->ipv4.tp.dst = tcp_key->tcp_dst;
842 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
844 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
846 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
847 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
848 swkey->ipv4.tp.src = udp_key->udp_src;
849 swkey->ipv4.tp.dst = udp_key->udp_dst;
853 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
855 *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
857 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
858 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
859 swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
860 swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
867 static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
868 const struct nlattr *a[], u32 *attrs)
870 const struct ovs_key_icmpv6 *icmpv6_key;
871 const struct ovs_key_tcp *tcp_key;
872 const struct ovs_key_udp *udp_key;
874 switch (swkey->ip.proto) {
876 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
878 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
880 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
881 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
882 swkey->ipv6.tp.src = tcp_key->tcp_src;
883 swkey->ipv6.tp.dst = tcp_key->tcp_dst;
887 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
889 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
891 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
892 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
893 swkey->ipv6.tp.src = udp_key->udp_src;
894 swkey->ipv6.tp.dst = udp_key->udp_dst;
898 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
900 *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
902 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
903 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
904 swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
905 swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
907 if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
908 swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
909 const struct ovs_key_nd *nd_key;
911 if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
913 *attrs &= ~(1 << OVS_KEY_ATTR_ND);
915 *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
916 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
917 memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
918 sizeof(swkey->ipv6.nd.target));
919 memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
920 memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
928 static int parse_flow_nlattrs(const struct nlattr *attr,
929 const struct nlattr *a[], u32 *attrsp)
931 const struct nlattr *nla;
936 nla_for_each_nested(nla, attr, rem) {
937 u16 type = nla_type(nla);
940 if (type > OVS_KEY_ATTR_MAX || attrs & (1 << type))
943 expected_len = ovs_key_lens[type];
944 if (nla_len(nla) != expected_len && expected_len != -1)
958 * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
959 * @swkey: receives the extracted flow key.
960 * @key_lenp: number of bytes used in @swkey.
961 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
964 int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
965 const struct nlattr *attr)
967 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
968 const struct ovs_key_ethernet *eth_key;
973 memset(swkey, 0, sizeof(struct sw_flow_key));
974 key_len = SW_FLOW_KEY_OFFSET(eth);
976 err = parse_flow_nlattrs(attr, a, &attrs);
980 /* Metadata attributes. */
981 if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
982 swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
983 attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
985 if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
986 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
987 if (in_port >= DP_MAX_PORTS)
989 swkey->phy.in_port = in_port;
990 attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
992 swkey->phy.in_port = DP_MAX_PORTS;
994 if (attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
995 swkey->phy.skb_mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
996 attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
999 /* Data attributes. */
1000 if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
1002 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1004 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1005 memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
1006 memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
1008 if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
1009 nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
1010 const struct nlattr *encap;
1013 if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
1014 (1 << OVS_KEY_ATTR_ETHERTYPE) |
1015 (1 << OVS_KEY_ATTR_ENCAP)))
1018 encap = a[OVS_KEY_ATTR_ENCAP];
1019 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1020 if (tci & htons(VLAN_TAG_PRESENT)) {
1021 swkey->eth.tci = tci;
1023 err = parse_flow_nlattrs(encap, a, &attrs);
1027 /* Corner case for truncated 802.1Q header. */
1031 swkey->eth.type = htons(ETH_P_8021Q);
1032 *key_lenp = key_len;
1039 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1040 swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1041 if (ntohs(swkey->eth.type) < ETH_P_802_3_MIN)
1043 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1045 swkey->eth.type = htons(ETH_P_802_2);
1048 if (swkey->eth.type == htons(ETH_P_IP)) {
1049 const struct ovs_key_ipv4 *ipv4_key;
1051 if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
1053 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1055 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
1056 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1057 if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
1059 swkey->ip.proto = ipv4_key->ipv4_proto;
1060 swkey->ip.tos = ipv4_key->ipv4_tos;
1061 swkey->ip.ttl = ipv4_key->ipv4_ttl;
1062 swkey->ip.frag = ipv4_key->ipv4_frag;
1063 swkey->ipv4.addr.src = ipv4_key->ipv4_src;
1064 swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
1066 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1067 err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1071 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1072 const struct ovs_key_ipv6 *ipv6_key;
1074 if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
1076 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1078 key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
1079 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1080 if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
1082 swkey->ipv6.label = ipv6_key->ipv6_label;
1083 swkey->ip.proto = ipv6_key->ipv6_proto;
1084 swkey->ip.tos = ipv6_key->ipv6_tclass;
1085 swkey->ip.ttl = ipv6_key->ipv6_hlimit;
1086 swkey->ip.frag = ipv6_key->ipv6_frag;
1087 memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
1088 sizeof(swkey->ipv6.addr.src));
1089 memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
1090 sizeof(swkey->ipv6.addr.dst));
1092 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1093 err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1097 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1098 swkey->eth.type == htons(ETH_P_RARP)) {
1099 const struct ovs_key_arp *arp_key;
1101 if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
1103 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1105 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
1106 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1107 swkey->ipv4.addr.src = arp_key->arp_sip;
1108 swkey->ipv4.addr.dst = arp_key->arp_tip;
1109 if (arp_key->arp_op & htons(0xff00))
1111 swkey->ip.proto = ntohs(arp_key->arp_op);
1112 memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
1113 memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
1118 *key_lenp = key_len;
1124 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1125 * @priority: receives the skb priority
1126 * @mark: receives the skb mark
1127 * @in_port: receives the extracted input port.
1128 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1131 * This parses a series of Netlink attributes that form a flow key, which must
1132 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1133 * get the metadata, that is, the parts of the flow key that cannot be
1134 * extracted from the packet itself.
1136 int ovs_flow_metadata_from_nlattrs(u32 *priority, u32 *mark, u16 *in_port,
1137 const struct nlattr *attr)
1139 const struct nlattr *nla;
1142 *in_port = DP_MAX_PORTS;
1146 nla_for_each_nested(nla, attr, rem) {
1147 int type = nla_type(nla);
1149 if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
1150 if (nla_len(nla) != ovs_key_lens[type])
1154 case OVS_KEY_ATTR_PRIORITY:
1155 *priority = nla_get_u32(nla);
1158 case OVS_KEY_ATTR_IN_PORT:
1159 if (nla_get_u32(nla) >= DP_MAX_PORTS)
1161 *in_port = nla_get_u32(nla);
1164 case OVS_KEY_ATTR_SKB_MARK:
1165 *mark = nla_get_u32(nla);
1175 int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
1177 struct ovs_key_ethernet *eth_key;
1178 struct nlattr *nla, *encap;
1180 if (swkey->phy.priority &&
1181 nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority))
1182 goto nla_put_failure;
1184 if (swkey->phy.in_port != DP_MAX_PORTS &&
1185 nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port))
1186 goto nla_put_failure;
1188 if (swkey->phy.skb_mark &&
1189 nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, swkey->phy.skb_mark))
1190 goto nla_put_failure;
1192 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1194 goto nla_put_failure;
1195 eth_key = nla_data(nla);
1196 memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
1197 memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
1199 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1200 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) ||
1201 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci))
1202 goto nla_put_failure;
1203 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1204 if (!swkey->eth.tci)
1210 if (swkey->eth.type == htons(ETH_P_802_2))
1213 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type))
1214 goto nla_put_failure;
1216 if (swkey->eth.type == htons(ETH_P_IP)) {
1217 struct ovs_key_ipv4 *ipv4_key;
1219 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1221 goto nla_put_failure;
1222 ipv4_key = nla_data(nla);
1223 ipv4_key->ipv4_src = swkey->ipv4.addr.src;
1224 ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
1225 ipv4_key->ipv4_proto = swkey->ip.proto;
1226 ipv4_key->ipv4_tos = swkey->ip.tos;
1227 ipv4_key->ipv4_ttl = swkey->ip.ttl;
1228 ipv4_key->ipv4_frag = swkey->ip.frag;
1229 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1230 struct ovs_key_ipv6 *ipv6_key;
1232 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1234 goto nla_put_failure;
1235 ipv6_key = nla_data(nla);
1236 memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
1237 sizeof(ipv6_key->ipv6_src));
1238 memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
1239 sizeof(ipv6_key->ipv6_dst));
1240 ipv6_key->ipv6_label = swkey->ipv6.label;
1241 ipv6_key->ipv6_proto = swkey->ip.proto;
1242 ipv6_key->ipv6_tclass = swkey->ip.tos;
1243 ipv6_key->ipv6_hlimit = swkey->ip.ttl;
1244 ipv6_key->ipv6_frag = swkey->ip.frag;
1245 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1246 swkey->eth.type == htons(ETH_P_RARP)) {
1247 struct ovs_key_arp *arp_key;
1249 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1251 goto nla_put_failure;
1252 arp_key = nla_data(nla);
1253 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1254 arp_key->arp_sip = swkey->ipv4.addr.src;
1255 arp_key->arp_tip = swkey->ipv4.addr.dst;
1256 arp_key->arp_op = htons(swkey->ip.proto);
1257 memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
1258 memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
1261 if ((swkey->eth.type == htons(ETH_P_IP) ||
1262 swkey->eth.type == htons(ETH_P_IPV6)) &&
1263 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1265 if (swkey->ip.proto == IPPROTO_TCP) {
1266 struct ovs_key_tcp *tcp_key;
1268 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1270 goto nla_put_failure;
1271 tcp_key = nla_data(nla);
1272 if (swkey->eth.type == htons(ETH_P_IP)) {
1273 tcp_key->tcp_src = swkey->ipv4.tp.src;
1274 tcp_key->tcp_dst = swkey->ipv4.tp.dst;
1275 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1276 tcp_key->tcp_src = swkey->ipv6.tp.src;
1277 tcp_key->tcp_dst = swkey->ipv6.tp.dst;
1279 } else if (swkey->ip.proto == IPPROTO_UDP) {
1280 struct ovs_key_udp *udp_key;
1282 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1284 goto nla_put_failure;
1285 udp_key = nla_data(nla);
1286 if (swkey->eth.type == htons(ETH_P_IP)) {
1287 udp_key->udp_src = swkey->ipv4.tp.src;
1288 udp_key->udp_dst = swkey->ipv4.tp.dst;
1289 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1290 udp_key->udp_src = swkey->ipv6.tp.src;
1291 udp_key->udp_dst = swkey->ipv6.tp.dst;
1293 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1294 swkey->ip.proto == IPPROTO_ICMP) {
1295 struct ovs_key_icmp *icmp_key;
1297 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1299 goto nla_put_failure;
1300 icmp_key = nla_data(nla);
1301 icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
1302 icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
1303 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1304 swkey->ip.proto == IPPROTO_ICMPV6) {
1305 struct ovs_key_icmpv6 *icmpv6_key;
1307 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1308 sizeof(*icmpv6_key));
1310 goto nla_put_failure;
1311 icmpv6_key = nla_data(nla);
1312 icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
1313 icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
1315 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1316 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1317 struct ovs_key_nd *nd_key;
1319 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1321 goto nla_put_failure;
1322 nd_key = nla_data(nla);
1323 memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
1324 sizeof(nd_key->nd_target));
1325 memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
1326 memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
1333 nla_nest_end(skb, encap);
1341 /* Initializes the flow module.
1342 * Returns zero if successful or a negative error code. */
1343 int ovs_flow_init(void)
1345 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1347 if (flow_cache == NULL)
1353 /* Uninitializes the flow module. */
1354 void ovs_flow_exit(void)
1356 kmem_cache_destroy(flow_cache);