2 * Copyright (c) 2007-2013 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>
48 static struct kmem_cache *flow_cache;
50 static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
51 struct sw_flow_key_range *range, u8 val);
53 static void update_range__(struct sw_flow_match *match,
54 size_t offset, size_t size, bool is_mask)
56 struct sw_flow_key_range *range = NULL;
57 size_t start = offset;
58 size_t end = offset + size;
61 range = &match->range;
63 range = &match->mask->range;
68 if (range->start == range->end) {
74 if (range->start > start)
81 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
83 update_range__(match, offsetof(struct sw_flow_key, field), \
84 sizeof((match)->key->field), is_mask); \
87 (match)->mask->key.field = value; \
89 (match)->key->field = value; \
93 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
95 update_range__(match, offsetof(struct sw_flow_key, field), \
99 memcpy(&(match)->mask->key.field, value_p, len);\
101 memcpy(&(match)->key->field, value_p, len); \
105 void ovs_match_init(struct sw_flow_match *match,
106 struct sw_flow_key *key,
107 struct sw_flow_mask *mask)
109 memset(match, 0, sizeof(*match));
113 memset(key, 0, sizeof(*key));
116 memset(&mask->key, 0, sizeof(mask->key));
117 mask->range.start = mask->range.end = 0;
121 static bool ovs_match_validate(const struct sw_flow_match *match,
122 u64 key_attrs, u64 mask_attrs)
124 u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
125 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
127 /* The following mask attributes allowed only if they
128 * pass the validation tests. */
129 mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
130 | (1ULL << OVS_KEY_ATTR_IPV6)
131 | (1ULL << OVS_KEY_ATTR_TCP)
132 | (1ULL << OVS_KEY_ATTR_UDP)
133 | (1ULL << OVS_KEY_ATTR_ICMP)
134 | (1ULL << OVS_KEY_ATTR_ICMPV6)
135 | (1ULL << OVS_KEY_ATTR_ARP)
136 | (1ULL << OVS_KEY_ATTR_ND));
138 /* Always allowed mask fields. */
139 mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
140 | (1ULL << OVS_KEY_ATTR_IN_PORT)
141 | (11ULL << OVS_KEY_ATTR_ETHERTYPE));
143 /* Check key attributes. */
144 if (match->key->eth.type == htons(ETH_P_ARP)
145 || match->key->eth.type == htons(ETH_P_RARP)) {
146 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
147 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
148 mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
151 if (match->key->eth.type == htons(ETH_P_IP)) {
152 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
153 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
154 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
156 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
157 if (match->key->ip.proto == IPPROTO_UDP) {
158 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
159 if (match->mask && (match->mask->key.ip.proto == 0xff))
160 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
163 if (match->key->ip.proto == IPPROTO_TCP) {
164 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
165 if (match->mask && (match->mask->key.ip.proto == 0xff))
166 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
169 if (match->key->ip.proto == IPPROTO_ICMP) {
170 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
171 if (match->mask && (match->mask->key.ip.proto == 0xff))
172 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
177 if (match->key->eth.type == htons(ETH_P_IPV6)) {
178 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
179 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
180 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
182 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
183 if (match->key->ip.proto == IPPROTO_UDP) {
184 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
185 if (match->mask && (match->mask->key.ip.proto == 0xff))
186 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
189 if (match->key->ip.proto == IPPROTO_TCP) {
190 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
191 if (match->mask && (match->mask->key.ip.proto == 0xff))
192 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
195 if (match->key->ip.proto == IPPROTO_ICMPV6) {
196 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
197 if (match->mask && (match->mask->key.ip.proto == 0xff))
198 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
200 if (match->key->ipv6.tp.src ==
201 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
202 match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
203 key_expected |= 1ULL << OVS_KEY_ATTR_ND;
204 if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
205 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
211 if ((key_attrs & key_expected) != key_expected) {
212 /* Key attributes check failed. */
213 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
214 key_attrs, key_expected);
218 if ((mask_attrs & mask_allowed) != mask_attrs) {
219 /* Mask attributes check failed. */
220 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
221 mask_attrs, mask_allowed);
228 static int check_header(struct sk_buff *skb, int len)
230 if (unlikely(skb->len < len))
232 if (unlikely(!pskb_may_pull(skb, len)))
237 static bool arphdr_ok(struct sk_buff *skb)
239 return pskb_may_pull(skb, skb_network_offset(skb) +
240 sizeof(struct arp_eth_header));
243 static int check_iphdr(struct sk_buff *skb)
245 unsigned int nh_ofs = skb_network_offset(skb);
249 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
253 ip_len = ip_hdrlen(skb);
254 if (unlikely(ip_len < sizeof(struct iphdr) ||
255 skb->len < nh_ofs + ip_len))
258 skb_set_transport_header(skb, nh_ofs + ip_len);
262 static bool tcphdr_ok(struct sk_buff *skb)
264 int th_ofs = skb_transport_offset(skb);
267 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
270 tcp_len = tcp_hdrlen(skb);
271 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
272 skb->len < th_ofs + tcp_len))
278 static bool udphdr_ok(struct sk_buff *skb)
280 return pskb_may_pull(skb, skb_transport_offset(skb) +
281 sizeof(struct udphdr));
284 static bool icmphdr_ok(struct sk_buff *skb)
286 return pskb_may_pull(skb, skb_transport_offset(skb) +
287 sizeof(struct icmphdr));
290 u64 ovs_flow_used_time(unsigned long flow_jiffies)
292 struct timespec cur_ts;
295 ktime_get_ts(&cur_ts);
296 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
297 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
298 cur_ts.tv_nsec / NSEC_PER_MSEC;
300 return cur_ms - idle_ms;
303 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
305 unsigned int nh_ofs = skb_network_offset(skb);
313 err = check_header(skb, nh_ofs + sizeof(*nh));
318 nexthdr = nh->nexthdr;
319 payload_ofs = (u8 *)(nh + 1) - skb->data;
321 key->ip.proto = NEXTHDR_NONE;
322 key->ip.tos = ipv6_get_dsfield(nh);
323 key->ip.ttl = nh->hop_limit;
324 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
325 key->ipv6.addr.src = nh->saddr;
326 key->ipv6.addr.dst = nh->daddr;
328 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
329 if (unlikely(payload_ofs < 0))
333 if (frag_off & htons(~0x7))
334 key->ip.frag = OVS_FRAG_TYPE_LATER;
336 key->ip.frag = OVS_FRAG_TYPE_FIRST;
339 nh_len = payload_ofs - nh_ofs;
340 skb_set_transport_header(skb, nh_ofs + nh_len);
341 key->ip.proto = nexthdr;
345 static bool icmp6hdr_ok(struct sk_buff *skb)
347 return pskb_may_pull(skb, skb_transport_offset(skb) +
348 sizeof(struct icmp6hdr));
351 void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src,
352 const struct sw_flow_mask *mask)
354 u8 *m = (u8 *)&mask->key + mask->range.start;
355 u8 *s = (u8 *)src + mask->range.start;
356 u8 *d = (u8 *)dst + mask->range.start;
359 memset(dst, 0, sizeof(*dst));
360 for (i = 0; i < ovs_sw_flow_mask_size_roundup(mask); i++) {
366 #define TCP_FLAGS_OFFSET 13
367 #define TCP_FLAG_MASK 0x3f
369 void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
373 if ((flow->key.eth.type == htons(ETH_P_IP) ||
374 flow->key.eth.type == htons(ETH_P_IPV6)) &&
375 flow->key.ip.proto == IPPROTO_TCP &&
376 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
377 u8 *tcp = (u8 *)tcp_hdr(skb);
378 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
381 spin_lock(&flow->lock);
382 flow->used = jiffies;
383 flow->packet_count++;
384 flow->byte_count += skb->len;
385 flow->tcp_flags |= tcp_flags;
386 spin_unlock(&flow->lock);
389 struct sw_flow_actions *ovs_flow_actions_alloc(int size)
391 struct sw_flow_actions *sfa;
393 if (size > MAX_ACTIONS_BUFSIZE)
394 return ERR_PTR(-EINVAL);
396 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
398 return ERR_PTR(-ENOMEM);
400 sfa->actions_len = 0;
404 struct sw_flow *ovs_flow_alloc(void)
406 struct sw_flow *flow;
408 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
410 return ERR_PTR(-ENOMEM);
412 spin_lock_init(&flow->lock);
413 flow->sf_acts = NULL;
419 static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
421 hash = jhash_1word(hash, table->hash_seed);
422 return flex_array_get(table->buckets,
423 (hash & (table->n_buckets - 1)));
426 static struct flex_array *alloc_buckets(unsigned int n_buckets)
428 struct flex_array *buckets;
431 buckets = flex_array_alloc(sizeof(struct hlist_head),
432 n_buckets, GFP_KERNEL);
436 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
438 flex_array_free(buckets);
442 for (i = 0; i < n_buckets; i++)
443 INIT_HLIST_HEAD((struct hlist_head *)
444 flex_array_get(buckets, i));
449 static void free_buckets(struct flex_array *buckets)
451 flex_array_free(buckets);
454 static struct flow_table *__flow_tbl_alloc(int new_size)
456 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
461 table->buckets = alloc_buckets(new_size);
463 if (!table->buckets) {
467 table->n_buckets = new_size;
470 table->keep_flows = false;
471 get_random_bytes(&table->hash_seed, sizeof(u32));
472 table->mask_list = NULL;
477 static void __flow_tbl_destroy(struct flow_table *table)
481 if (table->keep_flows)
484 for (i = 0; i < table->n_buckets; i++) {
485 struct sw_flow *flow;
486 struct hlist_head *head = flex_array_get(table->buckets, i);
487 struct hlist_node *n;
488 int ver = table->node_ver;
490 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
491 hlist_del(&flow->hash_node[ver]);
492 ovs_flow_free(flow, false);
496 BUG_ON(!list_empty(table->mask_list));
497 kfree(table->mask_list);
500 free_buckets(table->buckets);
504 struct flow_table *ovs_flow_tbl_alloc(int new_size)
506 struct flow_table *table = __flow_tbl_alloc(new_size);
511 table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
512 if (!table->mask_list) {
513 table->keep_flows = true;
514 __flow_tbl_destroy(table);
517 INIT_LIST_HEAD(table->mask_list);
522 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
524 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
526 __flow_tbl_destroy(table);
529 void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
535 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
537 __flow_tbl_destroy(table);
540 struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last)
542 struct sw_flow *flow;
543 struct hlist_head *head;
547 ver = table->node_ver;
548 while (*bucket < table->n_buckets) {
550 head = flex_array_get(table->buckets, *bucket);
551 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
566 static void __tbl_insert(struct flow_table *table, struct sw_flow *flow)
568 struct hlist_head *head;
570 head = find_bucket(table, flow->hash);
571 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
576 static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
581 old_ver = old->node_ver;
582 new->node_ver = !old_ver;
584 /* Insert in new table. */
585 for (i = 0; i < old->n_buckets; i++) {
586 struct sw_flow *flow;
587 struct hlist_head *head;
589 head = flex_array_get(old->buckets, i);
591 hlist_for_each_entry(flow, head, hash_node[old_ver])
592 __tbl_insert(new, flow);
595 new->mask_list = old->mask_list;
596 old->keep_flows = true;
599 static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
601 struct flow_table *new_table;
603 new_table = __flow_tbl_alloc(n_buckets);
605 return ERR_PTR(-ENOMEM);
607 flow_table_copy_flows(table, new_table);
612 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
614 return __flow_tbl_rehash(table, table->n_buckets);
617 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
619 return __flow_tbl_rehash(table, table->n_buckets * 2);
622 static void __flow_free(struct sw_flow *flow)
624 kfree((struct sf_flow_acts __force *)flow->sf_acts);
625 kmem_cache_free(flow_cache, flow);
628 static void rcu_free_flow_callback(struct rcu_head *rcu)
630 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
635 void ovs_flow_free(struct sw_flow *flow, bool deferred)
640 ovs_sw_flow_mask_del_ref(flow->mask, deferred);
643 call_rcu(&flow->rcu, rcu_free_flow_callback);
648 /* RCU callback used by ovs_flow_deferred_free_acts. */
649 static void rcu_free_acts_callback(struct rcu_head *rcu)
651 struct sw_flow_actions *sf_acts = container_of(rcu,
652 struct sw_flow_actions, rcu);
656 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
657 * The caller must hold rcu_read_lock for this to be sensible. */
658 void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
660 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
663 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
666 __be16 eth_type; /* ETH_P_8021Q */
669 struct qtag_prefix *qp;
671 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
674 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
678 qp = (struct qtag_prefix *) skb->data;
679 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
680 __skb_pull(skb, sizeof(struct qtag_prefix));
685 static __be16 parse_ethertype(struct sk_buff *skb)
687 struct llc_snap_hdr {
688 u8 dsap; /* Always 0xAA */
689 u8 ssap; /* Always 0xAA */
694 struct llc_snap_hdr *llc;
697 proto = *(__be16 *) skb->data;
698 __skb_pull(skb, sizeof(__be16));
700 if (ntohs(proto) >= ETH_P_802_3_MIN)
703 if (skb->len < sizeof(struct llc_snap_hdr))
704 return htons(ETH_P_802_2);
706 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
709 llc = (struct llc_snap_hdr *) skb->data;
710 if (llc->dsap != LLC_SAP_SNAP ||
711 llc->ssap != LLC_SAP_SNAP ||
712 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
713 return htons(ETH_P_802_2);
715 __skb_pull(skb, sizeof(struct llc_snap_hdr));
717 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
718 return llc->ethertype;
720 return htons(ETH_P_802_2);
723 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
726 struct icmp6hdr *icmp = icmp6_hdr(skb);
728 /* The ICMPv6 type and code fields use the 16-bit transport port
729 * fields, so we need to store them in 16-bit network byte order.
731 key->ipv6.tp.src = htons(icmp->icmp6_type);
732 key->ipv6.tp.dst = htons(icmp->icmp6_code);
734 if (icmp->icmp6_code == 0 &&
735 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
736 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
737 int icmp_len = skb->len - skb_transport_offset(skb);
741 /* In order to process neighbor discovery options, we need the
744 if (unlikely(icmp_len < sizeof(*nd)))
747 if (unlikely(skb_linearize(skb)))
750 nd = (struct nd_msg *)skb_transport_header(skb);
751 key->ipv6.nd.target = nd->target;
753 icmp_len -= sizeof(*nd);
755 while (icmp_len >= 8) {
756 struct nd_opt_hdr *nd_opt =
757 (struct nd_opt_hdr *)(nd->opt + offset);
758 int opt_len = nd_opt->nd_opt_len * 8;
760 if (unlikely(!opt_len || opt_len > icmp_len))
763 /* Store the link layer address if the appropriate
764 * option is provided. It is considered an error if
765 * the same link layer option is specified twice.
767 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
769 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
771 memcpy(key->ipv6.nd.sll,
772 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
773 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
775 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
777 memcpy(key->ipv6.nd.tll,
778 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
789 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
790 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
791 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
797 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
798 * @skb: sk_buff that contains the frame, with skb->data pointing to the
800 * @in_port: port number on which @skb was received.
801 * @key: output flow key
802 * @key_lenp: length of output flow key
804 * The caller must ensure that skb->len >= ETH_HLEN.
806 * Returns 0 if successful, otherwise a negative errno value.
808 * Initializes @skb header pointers as follows:
810 * - skb->mac_header: the Ethernet header.
812 * - skb->network_header: just past the Ethernet header, or just past the
813 * VLAN header, to the first byte of the Ethernet payload.
815 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
816 * on output, then just past the IP header, if one is present and
817 * of a correct length, otherwise the same as skb->network_header.
818 * For other key->eth.type values it is left untouched.
820 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
825 memset(key, 0, sizeof(*key));
827 key->phy.priority = skb->priority;
828 if (OVS_CB(skb)->tun_key)
829 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
830 key->phy.in_port = in_port;
831 key->phy.skb_mark = skb_get_mark(skb);
833 skb_reset_mac_header(skb);
835 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
836 * header in the linear data area.
839 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
840 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
842 __skb_pull(skb, 2 * ETH_ALEN);
843 /* We are going to push all headers that we pull, so no need to
844 * update skb->csum here. */
846 if (vlan_tx_tag_present(skb))
847 key->eth.tci = htons(vlan_get_tci(skb));
848 else if (eth->h_proto == htons(ETH_P_8021Q))
849 if (unlikely(parse_vlan(skb, key)))
852 key->eth.type = parse_ethertype(skb);
853 if (unlikely(key->eth.type == htons(0)))
856 skb_reset_network_header(skb);
857 __skb_push(skb, skb->data - skb_mac_header(skb));
860 if (key->eth.type == htons(ETH_P_IP)) {
864 error = check_iphdr(skb);
865 if (unlikely(error)) {
866 if (error == -EINVAL) {
867 skb->transport_header = skb->network_header;
874 key->ipv4.addr.src = nh->saddr;
875 key->ipv4.addr.dst = nh->daddr;
877 key->ip.proto = nh->protocol;
878 key->ip.tos = nh->tos;
879 key->ip.ttl = nh->ttl;
881 offset = nh->frag_off & htons(IP_OFFSET);
883 key->ip.frag = OVS_FRAG_TYPE_LATER;
886 if (nh->frag_off & htons(IP_MF) ||
887 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
888 key->ip.frag = OVS_FRAG_TYPE_FIRST;
890 /* Transport layer. */
891 if (key->ip.proto == IPPROTO_TCP) {
892 if (tcphdr_ok(skb)) {
893 struct tcphdr *tcp = tcp_hdr(skb);
894 key->ipv4.tp.src = tcp->source;
895 key->ipv4.tp.dst = tcp->dest;
897 } else if (key->ip.proto == IPPROTO_UDP) {
898 if (udphdr_ok(skb)) {
899 struct udphdr *udp = udp_hdr(skb);
900 key->ipv4.tp.src = udp->source;
901 key->ipv4.tp.dst = udp->dest;
903 } else if (key->ip.proto == IPPROTO_ICMP) {
904 if (icmphdr_ok(skb)) {
905 struct icmphdr *icmp = icmp_hdr(skb);
906 /* The ICMP type and code fields use the 16-bit
907 * transport port fields, so we need to store
908 * them in 16-bit network byte order. */
909 key->ipv4.tp.src = htons(icmp->type);
910 key->ipv4.tp.dst = htons(icmp->code);
914 } else if ((key->eth.type == htons(ETH_P_ARP) ||
915 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
916 struct arp_eth_header *arp;
918 arp = (struct arp_eth_header *)skb_network_header(skb);
920 if (arp->ar_hrd == htons(ARPHRD_ETHER)
921 && arp->ar_pro == htons(ETH_P_IP)
922 && arp->ar_hln == ETH_ALEN
923 && arp->ar_pln == 4) {
925 /* We only match on the lower 8 bits of the opcode. */
926 if (ntohs(arp->ar_op) <= 0xff)
927 key->ip.proto = ntohs(arp->ar_op);
928 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
929 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
930 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
931 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
933 } else if (key->eth.type == htons(ETH_P_IPV6)) {
934 int nh_len; /* IPv6 Header + Extensions */
936 nh_len = parse_ipv6hdr(skb, key);
937 if (unlikely(nh_len < 0)) {
938 if (nh_len == -EINVAL) {
939 skb->transport_header = skb->network_header;
947 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
949 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
950 key->ip.frag = OVS_FRAG_TYPE_FIRST;
952 /* Transport layer. */
953 if (key->ip.proto == NEXTHDR_TCP) {
954 if (tcphdr_ok(skb)) {
955 struct tcphdr *tcp = tcp_hdr(skb);
956 key->ipv6.tp.src = tcp->source;
957 key->ipv6.tp.dst = tcp->dest;
959 } else if (key->ip.proto == NEXTHDR_UDP) {
960 if (udphdr_ok(skb)) {
961 struct udphdr *udp = udp_hdr(skb);
962 key->ipv6.tp.src = udp->source;
963 key->ipv6.tp.dst = udp->dest;
965 } else if (key->ip.proto == NEXTHDR_ICMP) {
966 if (icmp6hdr_ok(skb)) {
967 error = parse_icmpv6(skb, key, nh_len);
977 static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start, int key_len)
979 return jhash2((u32 *)((u8 *)key + key_start),
980 DIV_ROUND_UP(key_len - key_start, sizeof(u32)), 0);
983 static int flow_key_start(const struct sw_flow_key *key)
985 if (key->tun_key.ipv4_dst)
988 return offsetof(struct sw_flow_key, phy);
991 static bool __cmp_key(const struct sw_flow_key *key1,
992 const struct sw_flow_key *key2, int key_start, int key_len)
994 return !memcmp((u8 *)key1 + key_start,
995 (u8 *)key2 + key_start, (key_len - key_start));
998 static bool __flow_cmp_key(const struct sw_flow *flow,
999 const struct sw_flow_key *key, int key_start, int key_len)
1001 return __cmp_key(&flow->key, key, key_start, key_len);
1004 static bool __flow_cmp_unmasked_key(const struct sw_flow *flow,
1005 const struct sw_flow_key *key, int key_start, int key_len)
1007 return __cmp_key(&flow->unmasked_key, key, key_start, key_len);
1010 bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
1011 const struct sw_flow_key *key, int key_len)
1014 key_start = flow_key_start(key);
1016 return __flow_cmp_unmasked_key(flow, key, key_start, key_len);
1020 struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
1021 struct sw_flow_match *match)
1023 struct sw_flow_key *unmasked = match->key;
1024 int key_len = match->range.end;
1025 struct sw_flow *flow;
1027 flow = ovs_flow_lookup(table, unmasked);
1028 if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_len)))
1034 static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
1035 const struct sw_flow_key *flow_key,
1036 struct sw_flow_mask *mask)
1038 struct sw_flow *flow;
1039 struct hlist_head *head;
1040 int key_start = mask->range.start;
1041 int key_len = mask->range.end;
1043 struct sw_flow_key masked_key;
1045 ovs_flow_key_mask(&masked_key, flow_key, mask);
1046 hash = ovs_flow_hash(&masked_key, key_start, key_len);
1047 head = find_bucket(table, hash);
1048 hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
1049 if (flow->mask == mask &&
1050 __flow_cmp_key(flow, &masked_key, key_start, key_len))
1056 struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
1057 const struct sw_flow_key *key)
1059 struct sw_flow *flow = NULL;
1060 struct sw_flow_mask *mask;
1062 list_for_each_entry_rcu(mask, tbl->mask_list, list) {
1063 flow = ovs_masked_flow_lookup(tbl, key, mask);
1064 if (flow) /* Found */
1072 void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow)
1074 flow->hash = ovs_flow_hash(&flow->key, flow->mask->range.start,
1075 flow->mask->range.end);
1076 __tbl_insert(table, flow);
1079 void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow)
1081 BUG_ON(table->count == 0);
1082 hlist_del_rcu(&flow->hash_node[table->node_ver]);
1086 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
1087 const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
1088 [OVS_KEY_ATTR_ENCAP] = -1,
1089 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
1090 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
1091 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
1092 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
1093 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
1094 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
1095 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
1096 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
1097 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
1098 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
1099 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
1100 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
1101 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
1102 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
1103 [OVS_KEY_ATTR_TUNNEL] = -1,
1106 static bool is_all_zero(const u8 *fp, size_t size)
1113 for (i = 0; i < size; i++)
1120 static int __parse_flow_nlattrs(const struct nlattr *attr,
1121 const struct nlattr *a[],
1122 u64 *attrsp, bool nz)
1124 const struct nlattr *nla;
1129 nla_for_each_nested(nla, attr, rem) {
1130 u16 type = nla_type(nla);
1133 if (type > OVS_KEY_ATTR_MAX) {
1134 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
1135 type, OVS_KEY_ATTR_MAX);
1138 if (attrs & (1ULL << type)) {
1139 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
1143 expected_len = ovs_key_lens[type];
1144 if (nla_len(nla) != expected_len && expected_len != -1) {
1145 OVS_NLERR("Key attribute has unexpected length (type=%d"
1146 ", length=%d, expected=%d).\n", type,
1147 nla_len(nla), expected_len);
1151 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
1152 attrs |= 1ULL << type;
1157 OVS_NLERR("Message has %d unknown bytes.\n", rem);
1165 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
1166 const struct nlattr *a[], u64 *attrsp)
1168 return __parse_flow_nlattrs(attr, a, attrsp, true);
1171 static int parse_flow_nlattrs(const struct nlattr *attr,
1172 const struct nlattr *a[], u64 *attrsp)
1174 return __parse_flow_nlattrs(attr, a, attrsp, false);
1177 int ipv4_tun_from_nlattr(const struct nlattr *attr,
1178 struct sw_flow_match *match, bool is_mask)
1183 __be16 tun_flags = 0;
1185 nla_for_each_nested(a, attr, rem) {
1186 int type = nla_type(a);
1187 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1188 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
1189 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
1190 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
1191 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
1192 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
1193 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
1194 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
1197 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
1198 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
1199 type, OVS_TUNNEL_KEY_ATTR_MAX);
1203 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
1204 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
1205 " legnth (type=%d, length=%d, expected=%d).\n",
1206 type, nla_len(a), ovs_tunnel_key_lens[type]);
1211 case OVS_TUNNEL_KEY_ATTR_ID:
1212 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
1213 nla_get_be64(a), is_mask);
1214 tun_flags |= TUNNEL_KEY;
1216 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1217 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
1218 nla_get_be32(a), is_mask);
1220 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1221 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
1222 nla_get_be32(a), is_mask);
1224 case OVS_TUNNEL_KEY_ATTR_TOS:
1225 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
1226 nla_get_u8(a), is_mask);
1228 case OVS_TUNNEL_KEY_ATTR_TTL:
1229 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
1230 nla_get_u8(a), is_mask);
1233 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1234 tun_flags |= TUNNEL_DONT_FRAGMENT;
1236 case OVS_TUNNEL_KEY_ATTR_CSUM:
1237 tun_flags |= TUNNEL_CSUM;
1244 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
1247 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
1252 if (!match->key->tun_key.ipv4_dst) {
1253 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
1258 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
1266 int ipv4_tun_to_nlattr(struct sk_buff *skb,
1267 const struct ovs_key_ipv4_tunnel *tun_key,
1268 const struct ovs_key_ipv4_tunnel *output)
1272 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
1276 if (output->tun_flags & TUNNEL_KEY &&
1277 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
1279 if (output->ipv4_src &&
1280 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
1282 if (output->ipv4_dst &&
1283 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
1285 if (output->ipv4_tos &&
1286 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
1288 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
1290 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
1291 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
1293 if ((output->tun_flags & TUNNEL_CSUM) &&
1294 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
1297 nla_nest_end(skb, nla);
1302 static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
1303 const struct nlattr **a, bool is_mask)
1305 if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
1306 SW_FLOW_KEY_PUT(match, phy.priority,
1307 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1308 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
1311 if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
1312 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1315 in_port = 0xffffffff; /* Always exact match in_port. */
1316 else if (in_port >= DP_MAX_PORTS)
1319 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1320 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
1321 } else if (!is_mask) {
1322 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1325 if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
1326 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1327 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
1328 if (!is_mask && mark != 0) {
1329 OVS_NLERR("skb->mark must be zero on this kernel (mark=%d).\n", mark);
1333 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1334 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
1336 if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
1337 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1340 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
1345 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
1346 const struct nlattr **a, bool is_mask)
1349 u64 orig_attrs = attrs;
1351 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
1355 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
1356 const struct ovs_key_ethernet *eth_key;
1358 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1359 SW_FLOW_KEY_MEMCPY(match, eth.src,
1360 eth_key->eth_src, ETH_ALEN, is_mask);
1361 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1362 eth_key->eth_dst, ETH_ALEN, is_mask);
1363 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
1366 if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
1369 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1370 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1372 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
1374 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
1379 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
1380 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
1381 } else if (!is_mask)
1382 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1384 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
1387 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1389 /* Always exact match EtherType. */
1390 eth_type = htons(0xffff);
1391 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
1392 OVS_NLERR("EtherType is less than mimimum (type=%x, min=%x).\n",
1393 ntohs(eth_type), ETH_P_802_3_MIN);
1397 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1398 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1399 } else if (!is_mask) {
1400 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1403 if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1404 const struct ovs_key_ipv4 *ipv4_key;
1406 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1407 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
1408 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
1409 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
1412 SW_FLOW_KEY_PUT(match, ip.proto,
1413 ipv4_key->ipv4_proto, is_mask);
1414 SW_FLOW_KEY_PUT(match, ip.tos,
1415 ipv4_key->ipv4_tos, is_mask);
1416 SW_FLOW_KEY_PUT(match, ip.ttl,
1417 ipv4_key->ipv4_ttl, is_mask);
1418 SW_FLOW_KEY_PUT(match, ip.frag,
1419 ipv4_key->ipv4_frag, is_mask);
1420 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1421 ipv4_key->ipv4_src, is_mask);
1422 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1423 ipv4_key->ipv4_dst, is_mask);
1424 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
1427 if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
1428 const struct ovs_key_ipv6 *ipv6_key;
1430 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1431 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
1432 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
1433 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
1436 SW_FLOW_KEY_PUT(match, ipv6.label,
1437 ipv6_key->ipv6_label, is_mask);
1438 SW_FLOW_KEY_PUT(match, ip.proto,
1439 ipv6_key->ipv6_proto, is_mask);
1440 SW_FLOW_KEY_PUT(match, ip.tos,
1441 ipv6_key->ipv6_tclass, is_mask);
1442 SW_FLOW_KEY_PUT(match, ip.ttl,
1443 ipv6_key->ipv6_hlimit, is_mask);
1444 SW_FLOW_KEY_PUT(match, ip.frag,
1445 ipv6_key->ipv6_frag, is_mask);
1446 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1448 sizeof(match->key->ipv6.addr.src),
1450 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1452 sizeof(match->key->ipv6.addr.dst),
1455 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
1458 if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
1459 const struct ovs_key_arp *arp_key;
1461 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1462 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
1463 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
1468 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1469 arp_key->arp_sip, is_mask);
1470 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1471 arp_key->arp_tip, is_mask);
1472 SW_FLOW_KEY_PUT(match, ip.proto,
1473 ntohs(arp_key->arp_op), is_mask);
1474 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1475 arp_key->arp_sha, ETH_ALEN, is_mask);
1476 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1477 arp_key->arp_tha, ETH_ALEN, is_mask);
1479 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
1482 if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
1483 const struct ovs_key_tcp *tcp_key;
1485 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1486 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1487 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1488 tcp_key->tcp_src, is_mask);
1489 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1490 tcp_key->tcp_dst, is_mask);
1492 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1493 tcp_key->tcp_src, is_mask);
1494 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1495 tcp_key->tcp_dst, is_mask);
1497 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
1500 if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
1501 const struct ovs_key_udp *udp_key;
1503 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1504 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1505 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1506 udp_key->udp_src, is_mask);
1507 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1508 udp_key->udp_dst, is_mask);
1510 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1511 udp_key->udp_src, is_mask);
1512 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1513 udp_key->udp_dst, is_mask);
1515 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
1518 if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
1519 const struct ovs_key_icmp *icmp_key;
1521 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1522 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1523 htons(icmp_key->icmp_type), is_mask);
1524 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1525 htons(icmp_key->icmp_code), is_mask);
1526 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
1529 if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
1530 const struct ovs_key_icmpv6 *icmpv6_key;
1532 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1533 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1534 htons(icmpv6_key->icmpv6_type), is_mask);
1535 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1536 htons(icmpv6_key->icmpv6_code), is_mask);
1537 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
1540 if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
1541 const struct ovs_key_nd *nd_key;
1543 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1544 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1546 sizeof(match->key->ipv6.nd.target),
1548 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1549 nd_key->nd_sll, ETH_ALEN, is_mask);
1550 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1551 nd_key->nd_tll, ETH_ALEN, is_mask);
1552 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
1562 * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
1563 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1564 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1565 * does not include any don't care bit.
1566 * @match: receives the extracted flow match information.
1567 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1568 * sequence. The fields should of the packet that triggered the creation
1570 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1571 * attribute specifies the mask field of the wildcarded flow.
1573 int ovs_match_from_nlattrs(struct sw_flow_match *match,
1574 const struct nlattr *key,
1575 const struct nlattr *mask)
1577 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1578 const struct nlattr *encap;
1581 bool encap_valid = false;
1584 err = parse_flow_nlattrs(key, a, &key_attrs);
1588 if (key_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1589 encap = a[OVS_KEY_ATTR_ENCAP];
1590 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1591 if (nla_len(encap)) {
1592 __be16 eth_type = 0; /* ETH_P_8021Q */
1594 if (a[OVS_KEY_ATTR_ETHERTYPE])
1595 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1597 if ((eth_type == htons(ETH_P_8021Q)) && (a[OVS_KEY_ATTR_VLAN])) {
1599 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1600 err = parse_flow_nlattrs(encap, a, &key_attrs);
1602 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
1611 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
1616 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
1620 if ((mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) && encap_valid) {
1621 __be16 eth_type = 0;
1623 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1624 if (a[OVS_KEY_ATTR_ETHERTYPE])
1625 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1626 if (eth_type == htons(0xffff)) {
1627 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1628 encap = a[OVS_KEY_ATTR_ENCAP];
1629 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
1631 OVS_NLERR("VLAN frames must have an exact match"
1632 " on the TPID (mask=%x).\n",
1641 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
1645 /* Populate exact match flow's key mask. */
1647 ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
1650 if (!ovs_match_validate(match, key_attrs, mask_attrs))
1657 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1658 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
1659 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1662 * This parses a series of Netlink attributes that form a flow key, which must
1663 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1664 * get the metadata, that is, the parts of the flow key that cannot be
1665 * extracted from the packet itself.
1668 int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
1669 const struct nlattr *attr)
1671 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
1672 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1675 struct sw_flow_match match;
1677 flow->key.phy.in_port = DP_MAX_PORTS;
1678 flow->key.phy.priority = 0;
1679 flow->key.phy.skb_mark = 0;
1680 memset(tun_key, 0, sizeof(flow->key.tun_key));
1682 err = parse_flow_nlattrs(attr, a, &attrs);
1686 memset(&match, 0, sizeof(match));
1687 match.key = &flow->key;
1689 err = metadata_from_nlattrs(&match, &attrs, a, false);
1696 int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
1697 const struct sw_flow_key *output, struct sk_buff *skb)
1699 struct ovs_key_ethernet *eth_key;
1700 struct nlattr *nla, *encap;
1701 bool is_mask = (swkey != output);
1703 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1704 goto nla_put_failure;
1706 if ((swkey->tun_key.ipv4_dst || is_mask) &&
1707 ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
1708 goto nla_put_failure;
1710 if (swkey->phy.in_port == DP_MAX_PORTS) {
1711 if (is_mask && (output->phy.in_port == 0xffff))
1712 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1713 goto nla_put_failure;
1716 upper_u16 = !is_mask ? 0 : 0xffff;
1718 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1719 (upper_u16 << 16) | output->phy.in_port))
1720 goto nla_put_failure;
1723 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1724 goto nla_put_failure;
1726 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1728 goto nla_put_failure;
1730 eth_key = nla_data(nla);
1731 memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
1732 memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
1734 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1736 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1737 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1738 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1739 goto nla_put_failure;
1740 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1741 if (!swkey->eth.tci)
1746 if (swkey->eth.type == htons(ETH_P_802_2)) {
1748 * Ethertype 802.2 is represented in the netlink with omitted
1749 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1750 * 0xffff in the mask attribute. Ethertype can also
1753 if (is_mask && output->eth.type)
1754 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1756 goto nla_put_failure;
1760 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1761 goto nla_put_failure;
1763 if (swkey->eth.type == htons(ETH_P_IP)) {
1764 struct ovs_key_ipv4 *ipv4_key;
1766 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1768 goto nla_put_failure;
1769 ipv4_key = nla_data(nla);
1770 ipv4_key->ipv4_src = output->ipv4.addr.src;
1771 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1772 ipv4_key->ipv4_proto = output->ip.proto;
1773 ipv4_key->ipv4_tos = output->ip.tos;
1774 ipv4_key->ipv4_ttl = output->ip.ttl;
1775 ipv4_key->ipv4_frag = output->ip.frag;
1776 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1777 struct ovs_key_ipv6 *ipv6_key;
1779 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1781 goto nla_put_failure;
1782 ipv6_key = nla_data(nla);
1783 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1784 sizeof(ipv6_key->ipv6_src));
1785 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1786 sizeof(ipv6_key->ipv6_dst));
1787 ipv6_key->ipv6_label = output->ipv6.label;
1788 ipv6_key->ipv6_proto = output->ip.proto;
1789 ipv6_key->ipv6_tclass = output->ip.tos;
1790 ipv6_key->ipv6_hlimit = output->ip.ttl;
1791 ipv6_key->ipv6_frag = output->ip.frag;
1792 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1793 swkey->eth.type == htons(ETH_P_RARP)) {
1794 struct ovs_key_arp *arp_key;
1796 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1798 goto nla_put_failure;
1799 arp_key = nla_data(nla);
1800 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1801 arp_key->arp_sip = output->ipv4.addr.src;
1802 arp_key->arp_tip = output->ipv4.addr.dst;
1803 arp_key->arp_op = htons(output->ip.proto);
1804 memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
1805 memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
1808 if ((swkey->eth.type == htons(ETH_P_IP) ||
1809 swkey->eth.type == htons(ETH_P_IPV6)) &&
1810 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1812 if (swkey->ip.proto == IPPROTO_TCP) {
1813 struct ovs_key_tcp *tcp_key;
1815 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1817 goto nla_put_failure;
1818 tcp_key = nla_data(nla);
1819 if (swkey->eth.type == htons(ETH_P_IP)) {
1820 tcp_key->tcp_src = output->ipv4.tp.src;
1821 tcp_key->tcp_dst = output->ipv4.tp.dst;
1822 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1823 tcp_key->tcp_src = output->ipv6.tp.src;
1824 tcp_key->tcp_dst = output->ipv6.tp.dst;
1826 } else if (swkey->ip.proto == IPPROTO_UDP) {
1827 struct ovs_key_udp *udp_key;
1829 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1831 goto nla_put_failure;
1832 udp_key = nla_data(nla);
1833 if (swkey->eth.type == htons(ETH_P_IP)) {
1834 udp_key->udp_src = output->ipv4.tp.src;
1835 udp_key->udp_dst = output->ipv4.tp.dst;
1836 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1837 udp_key->udp_src = output->ipv6.tp.src;
1838 udp_key->udp_dst = output->ipv6.tp.dst;
1840 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1841 swkey->ip.proto == IPPROTO_ICMP) {
1842 struct ovs_key_icmp *icmp_key;
1844 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1846 goto nla_put_failure;
1847 icmp_key = nla_data(nla);
1848 icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
1849 icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
1850 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1851 swkey->ip.proto == IPPROTO_ICMPV6) {
1852 struct ovs_key_icmpv6 *icmpv6_key;
1854 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1855 sizeof(*icmpv6_key));
1857 goto nla_put_failure;
1858 icmpv6_key = nla_data(nla);
1859 icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
1860 icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
1862 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1863 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1864 struct ovs_key_nd *nd_key;
1866 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1868 goto nla_put_failure;
1869 nd_key = nla_data(nla);
1870 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1871 sizeof(nd_key->nd_target));
1872 memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
1873 memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
1880 nla_nest_end(skb, encap);
1888 /* Initializes the flow module.
1889 * Returns zero if successful or a negative error code. */
1890 int ovs_flow_init(void)
1892 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1894 if (flow_cache == NULL)
1900 /* Uninitializes the flow module. */
1901 void ovs_flow_exit(void)
1903 kmem_cache_destroy(flow_cache);
1906 struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
1908 struct sw_flow_mask *mask;
1910 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
1912 mask->ref_count = 0;
1917 void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
1922 static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu)
1924 struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu);
1929 void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
1934 BUG_ON(!mask->ref_count);
1937 if (!mask->ref_count) {
1938 list_del_rcu(&mask->list);
1940 call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
1946 static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a,
1947 const struct sw_flow_mask *b)
1949 u8 *a_ = (u8 *)&a->key + a->range.start;
1950 u8 *b_ = (u8 *)&b->key + b->range.start;
1952 return (a->range.end == b->range.end)
1953 && (a->range.start == b->range.start)
1954 && (memcmp(a_, b_, ovs_sw_flow_mask_actual_size(a)) == 0);
1957 struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
1958 const struct sw_flow_mask *mask)
1960 struct list_head *ml;
1962 list_for_each(ml, tbl->mask_list) {
1963 struct sw_flow_mask *m;
1964 m = container_of(ml, struct sw_flow_mask, list);
1965 if (ovs_sw_flow_mask_equal(mask, m))
1973 * add a new mask into the mask list.
1974 * The caller needs to make sure that 'mask' is not the same
1975 * as any masks that are already on the list.
1977 void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
1979 list_add_rcu(&mask->list, tbl->mask_list);
1983 * Set 'range' fields in the mask to the value of 'val'.
1985 static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
1986 struct sw_flow_key_range *range, u8 val)
1988 u8 *m = (u8 *)&mask->key + range->start;
1990 mask->range = *range;
1991 memset(m, val, ovs_sw_flow_mask_size_roundup(mask));