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>
48 static struct kmem_cache
*flow_cache
;
50 static int check_header(struct sk_buff
*skb
, int len
)
52 if (unlikely(skb
->len
< len
))
54 if (unlikely(!pskb_may_pull(skb
, len
)))
59 static bool arphdr_ok(struct sk_buff
*skb
)
61 return pskb_may_pull(skb
, skb_network_offset(skb
) +
62 sizeof(struct arp_eth_header
));
65 static int check_iphdr(struct sk_buff
*skb
)
67 unsigned int nh_ofs
= skb_network_offset(skb
);
71 err
= check_header(skb
, nh_ofs
+ sizeof(struct iphdr
));
75 ip_len
= ip_hdrlen(skb
);
76 if (unlikely(ip_len
< sizeof(struct iphdr
) ||
77 skb
->len
< nh_ofs
+ ip_len
))
80 skb_set_transport_header(skb
, nh_ofs
+ ip_len
);
84 static bool tcphdr_ok(struct sk_buff
*skb
)
86 int th_ofs
= skb_transport_offset(skb
);
89 if (unlikely(!pskb_may_pull(skb
, th_ofs
+ sizeof(struct tcphdr
))))
92 tcp_len
= tcp_hdrlen(skb
);
93 if (unlikely(tcp_len
< sizeof(struct tcphdr
) ||
94 skb
->len
< th_ofs
+ tcp_len
))
100 static bool udphdr_ok(struct sk_buff
*skb
)
102 return pskb_may_pull(skb
, skb_transport_offset(skb
) +
103 sizeof(struct udphdr
));
106 static bool icmphdr_ok(struct sk_buff
*skb
)
108 return pskb_may_pull(skb
, skb_transport_offset(skb
) +
109 sizeof(struct icmphdr
));
112 u64
ovs_flow_used_time(unsigned long flow_jiffies
)
114 struct timespec cur_ts
;
117 ktime_get_ts(&cur_ts
);
118 idle_ms
= jiffies_to_msecs(jiffies
- flow_jiffies
);
119 cur_ms
= (u64
)cur_ts
.tv_sec
* MSEC_PER_SEC
+
120 cur_ts
.tv_nsec
/ NSEC_PER_MSEC
;
122 return cur_ms
- idle_ms
;
125 #define SW_FLOW_KEY_OFFSET(field) \
126 (offsetof(struct sw_flow_key, field) + \
127 FIELD_SIZEOF(struct sw_flow_key, field))
129 static int parse_ipv6hdr(struct sk_buff
*skb
, struct sw_flow_key
*key
,
132 unsigned int nh_ofs
= skb_network_offset(skb
);
140 *key_lenp
= SW_FLOW_KEY_OFFSET(ipv6
.label
);
142 err
= check_header(skb
, nh_ofs
+ sizeof(*nh
));
147 nexthdr
= nh
->nexthdr
;
148 payload_ofs
= (u8
*)(nh
+ 1) - skb
->data
;
150 key
->ip
.proto
= NEXTHDR_NONE
;
151 key
->ip
.tos
= ipv6_get_dsfield(nh
);
152 key
->ip
.ttl
= nh
->hop_limit
;
153 key
->ipv6
.label
= *(__be32
*)nh
& htonl(IPV6_FLOWINFO_FLOWLABEL
);
154 key
->ipv6
.addr
.src
= nh
->saddr
;
155 key
->ipv6
.addr
.dst
= nh
->daddr
;
157 payload_ofs
= ipv6_skip_exthdr(skb
, payload_ofs
, &nexthdr
, &frag_off
);
158 if (unlikely(payload_ofs
< 0))
162 if (frag_off
& htons(~0x7))
163 key
->ip
.frag
= OVS_FRAG_TYPE_LATER
;
165 key
->ip
.frag
= OVS_FRAG_TYPE_FIRST
;
168 nh_len
= payload_ofs
- nh_ofs
;
169 skb_set_transport_header(skb
, nh_ofs
+ nh_len
);
170 key
->ip
.proto
= nexthdr
;
174 static bool icmp6hdr_ok(struct sk_buff
*skb
)
176 return pskb_may_pull(skb
, skb_transport_offset(skb
) +
177 sizeof(struct icmp6hdr
));
180 #define TCP_FLAGS_OFFSET 13
181 #define TCP_FLAG_MASK 0x3f
183 void ovs_flow_used(struct sw_flow
*flow
, struct sk_buff
*skb
)
187 if ((flow
->key
.eth
.type
== htons(ETH_P_IP
) ||
188 flow
->key
.eth
.type
== htons(ETH_P_IPV6
)) &&
189 flow
->key
.ip
.proto
== IPPROTO_TCP
&&
190 likely(skb
->len
>= skb_transport_offset(skb
) + sizeof(struct tcphdr
))) {
191 u8
*tcp
= (u8
*)tcp_hdr(skb
);
192 tcp_flags
= *(tcp
+ TCP_FLAGS_OFFSET
) & TCP_FLAG_MASK
;
195 spin_lock(&flow
->lock
);
196 flow
->used
= jiffies
;
197 flow
->packet_count
++;
198 flow
->byte_count
+= skb
->len
;
199 flow
->tcp_flags
|= tcp_flags
;
200 spin_unlock(&flow
->lock
);
203 struct sw_flow_actions
*ovs_flow_actions_alloc(const struct nlattr
*actions
)
205 int actions_len
= nla_len(actions
);
206 struct sw_flow_actions
*sfa
;
208 if (actions_len
> MAX_ACTIONS_BUFSIZE
)
209 return ERR_PTR(-EINVAL
);
211 sfa
= kmalloc(sizeof(*sfa
) + actions_len
, GFP_KERNEL
);
213 return ERR_PTR(-ENOMEM
);
215 sfa
->actions_len
= actions_len
;
216 memcpy(sfa
->actions
, nla_data(actions
), actions_len
);
220 struct sw_flow
*ovs_flow_alloc(void)
222 struct sw_flow
*flow
;
224 flow
= kmem_cache_alloc(flow_cache
, GFP_KERNEL
);
226 return ERR_PTR(-ENOMEM
);
228 spin_lock_init(&flow
->lock
);
229 flow
->sf_acts
= NULL
;
234 static struct hlist_head
*find_bucket(struct flow_table
*table
, u32 hash
)
236 hash
= jhash_1word(hash
, table
->hash_seed
);
237 return flex_array_get(table
->buckets
,
238 (hash
& (table
->n_buckets
- 1)));
241 static struct flex_array
*alloc_buckets(unsigned int n_buckets
)
243 struct flex_array
*buckets
;
246 buckets
= flex_array_alloc(sizeof(struct hlist_head
*),
247 n_buckets
, GFP_KERNEL
);
251 err
= flex_array_prealloc(buckets
, 0, n_buckets
, GFP_KERNEL
);
253 flex_array_free(buckets
);
257 for (i
= 0; i
< n_buckets
; i
++)
258 INIT_HLIST_HEAD((struct hlist_head
*)
259 flex_array_get(buckets
, i
));
264 static void free_buckets(struct flex_array
*buckets
)
266 flex_array_free(buckets
);
269 struct flow_table
*ovs_flow_tbl_alloc(int new_size
)
271 struct flow_table
*table
= kmalloc(sizeof(*table
), GFP_KERNEL
);
276 table
->buckets
= alloc_buckets(new_size
);
278 if (!table
->buckets
) {
282 table
->n_buckets
= new_size
;
285 table
->keep_flows
= false;
286 get_random_bytes(&table
->hash_seed
, sizeof(u32
));
291 void ovs_flow_tbl_destroy(struct flow_table
*table
)
298 if (table
->keep_flows
)
301 for (i
= 0; i
< table
->n_buckets
; i
++) {
302 struct sw_flow
*flow
;
303 struct hlist_head
*head
= flex_array_get(table
->buckets
, i
);
304 struct hlist_node
*node
, *n
;
305 int ver
= table
->node_ver
;
307 hlist_for_each_entry_safe(flow
, node
, n
, head
, hash_node
[ver
]) {
308 hlist_del_rcu(&flow
->hash_node
[ver
]);
314 free_buckets(table
->buckets
);
318 static void flow_tbl_destroy_rcu_cb(struct rcu_head
*rcu
)
320 struct flow_table
*table
= container_of(rcu
, struct flow_table
, rcu
);
322 ovs_flow_tbl_destroy(table
);
325 void ovs_flow_tbl_deferred_destroy(struct flow_table
*table
)
330 call_rcu(&table
->rcu
, flow_tbl_destroy_rcu_cb
);
333 struct sw_flow
*ovs_flow_tbl_next(struct flow_table
*table
, u32
*bucket
, u32
*last
)
335 struct sw_flow
*flow
;
336 struct hlist_head
*head
;
337 struct hlist_node
*n
;
341 ver
= table
->node_ver
;
342 while (*bucket
< table
->n_buckets
) {
344 head
= flex_array_get(table
->buckets
, *bucket
);
345 hlist_for_each_entry_rcu(flow
, n
, head
, hash_node
[ver
]) {
360 static void __flow_tbl_insert(struct flow_table
*table
, struct sw_flow
*flow
)
362 struct hlist_head
*head
;
363 head
= find_bucket(table
, flow
->hash
);
364 hlist_add_head_rcu(&flow
->hash_node
[table
->node_ver
], head
);
368 static void flow_table_copy_flows(struct flow_table
*old
, struct flow_table
*new)
373 old_ver
= old
->node_ver
;
374 new->node_ver
= !old_ver
;
376 /* Insert in new table. */
377 for (i
= 0; i
< old
->n_buckets
; i
++) {
378 struct sw_flow
*flow
;
379 struct hlist_head
*head
;
380 struct hlist_node
*n
;
382 head
= flex_array_get(old
->buckets
, i
);
384 hlist_for_each_entry(flow
, n
, head
, hash_node
[old_ver
])
385 __flow_tbl_insert(new, flow
);
387 old
->keep_flows
= true;
390 static struct flow_table
*__flow_tbl_rehash(struct flow_table
*table
, int n_buckets
)
392 struct flow_table
*new_table
;
394 new_table
= ovs_flow_tbl_alloc(n_buckets
);
396 return ERR_PTR(-ENOMEM
);
398 flow_table_copy_flows(table
, new_table
);
403 struct flow_table
*ovs_flow_tbl_rehash(struct flow_table
*table
)
405 return __flow_tbl_rehash(table
, table
->n_buckets
);
408 struct flow_table
*ovs_flow_tbl_expand(struct flow_table
*table
)
410 return __flow_tbl_rehash(table
, table
->n_buckets
* 2);
413 void ovs_flow_free(struct sw_flow
*flow
)
418 kfree((struct sf_flow_acts __force
*)flow
->sf_acts
);
419 kmem_cache_free(flow_cache
, flow
);
422 /* RCU callback used by ovs_flow_deferred_free. */
423 static void rcu_free_flow_callback(struct rcu_head
*rcu
)
425 struct sw_flow
*flow
= container_of(rcu
, struct sw_flow
, rcu
);
430 /* Schedules 'flow' to be freed after the next RCU grace period.
431 * The caller must hold rcu_read_lock for this to be sensible. */
432 void ovs_flow_deferred_free(struct sw_flow
*flow
)
434 call_rcu(&flow
->rcu
, rcu_free_flow_callback
);
437 /* RCU callback used by ovs_flow_deferred_free_acts. */
438 static void rcu_free_acts_callback(struct rcu_head
*rcu
)
440 struct sw_flow_actions
*sf_acts
= container_of(rcu
,
441 struct sw_flow_actions
, rcu
);
445 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
446 * The caller must hold rcu_read_lock for this to be sensible. */
447 void ovs_flow_deferred_free_acts(struct sw_flow_actions
*sf_acts
)
449 call_rcu(&sf_acts
->rcu
, rcu_free_acts_callback
);
452 static int parse_vlan(struct sk_buff
*skb
, struct sw_flow_key
*key
)
455 __be16 eth_type
; /* ETH_P_8021Q */
458 struct qtag_prefix
*qp
;
460 if (unlikely(skb
->len
< sizeof(struct qtag_prefix
) + sizeof(__be16
)))
463 if (unlikely(!pskb_may_pull(skb
, sizeof(struct qtag_prefix
) +
467 qp
= (struct qtag_prefix
*) skb
->data
;
468 key
->eth
.tci
= qp
->tci
| htons(VLAN_TAG_PRESENT
);
469 __skb_pull(skb
, sizeof(struct qtag_prefix
));
474 static __be16
parse_ethertype(struct sk_buff
*skb
)
476 struct llc_snap_hdr
{
477 u8 dsap
; /* Always 0xAA */
478 u8 ssap
; /* Always 0xAA */
483 struct llc_snap_hdr
*llc
;
486 proto
= *(__be16
*) skb
->data
;
487 __skb_pull(skb
, sizeof(__be16
));
489 if (ntohs(proto
) >= 1536)
492 if (skb
->len
< sizeof(struct llc_snap_hdr
))
493 return htons(ETH_P_802_2
);
495 if (unlikely(!pskb_may_pull(skb
, sizeof(struct llc_snap_hdr
))))
498 llc
= (struct llc_snap_hdr
*) skb
->data
;
499 if (llc
->dsap
!= LLC_SAP_SNAP
||
500 llc
->ssap
!= LLC_SAP_SNAP
||
501 (llc
->oui
[0] | llc
->oui
[1] | llc
->oui
[2]) != 0)
502 return htons(ETH_P_802_2
);
504 __skb_pull(skb
, sizeof(struct llc_snap_hdr
));
505 return llc
->ethertype
;
508 static int parse_icmpv6(struct sk_buff
*skb
, struct sw_flow_key
*key
,
509 int *key_lenp
, int nh_len
)
511 struct icmp6hdr
*icmp
= icmp6_hdr(skb
);
515 /* The ICMPv6 type and code fields use the 16-bit transport port
516 * fields, so we need to store them in 16-bit network byte order.
518 key
->ipv6
.tp
.src
= htons(icmp
->icmp6_type
);
519 key
->ipv6
.tp
.dst
= htons(icmp
->icmp6_code
);
520 key_len
= SW_FLOW_KEY_OFFSET(ipv6
.tp
);
522 if (icmp
->icmp6_code
== 0 &&
523 (icmp
->icmp6_type
== NDISC_NEIGHBOUR_SOLICITATION
||
524 icmp
->icmp6_type
== NDISC_NEIGHBOUR_ADVERTISEMENT
)) {
525 int icmp_len
= skb
->len
- skb_transport_offset(skb
);
529 key_len
= SW_FLOW_KEY_OFFSET(ipv6
.nd
);
531 /* In order to process neighbor discovery options, we need the
534 if (unlikely(icmp_len
< sizeof(*nd
)))
536 if (unlikely(skb_linearize(skb
))) {
541 nd
= (struct nd_msg
*)skb_transport_header(skb
);
542 key
->ipv6
.nd
.target
= nd
->target
;
543 key_len
= SW_FLOW_KEY_OFFSET(ipv6
.nd
);
545 icmp_len
-= sizeof(*nd
);
547 while (icmp_len
>= 8) {
548 struct nd_opt_hdr
*nd_opt
=
549 (struct nd_opt_hdr
*)(nd
->opt
+ offset
);
550 int opt_len
= nd_opt
->nd_opt_len
* 8;
552 if (unlikely(!opt_len
|| opt_len
> icmp_len
))
555 /* Store the link layer address if the appropriate
556 * option is provided. It is considered an error if
557 * the same link layer option is specified twice.
559 if (nd_opt
->nd_opt_type
== ND_OPT_SOURCE_LL_ADDR
561 if (unlikely(!is_zero_ether_addr(key
->ipv6
.nd
.sll
)))
563 memcpy(key
->ipv6
.nd
.sll
,
564 &nd
->opt
[offset
+sizeof(*nd_opt
)], ETH_ALEN
);
565 } else if (nd_opt
->nd_opt_type
== ND_OPT_TARGET_LL_ADDR
567 if (unlikely(!is_zero_ether_addr(key
->ipv6
.nd
.tll
)))
569 memcpy(key
->ipv6
.nd
.tll
,
570 &nd
->opt
[offset
+sizeof(*nd_opt
)], ETH_ALEN
);
581 memset(&key
->ipv6
.nd
.target
, 0, sizeof(key
->ipv6
.nd
.target
));
582 memset(key
->ipv6
.nd
.sll
, 0, sizeof(key
->ipv6
.nd
.sll
));
583 memset(key
->ipv6
.nd
.tll
, 0, sizeof(key
->ipv6
.nd
.tll
));
591 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
592 * @skb: sk_buff that contains the frame, with skb->data pointing to the
594 * @in_port: port number on which @skb was received.
595 * @key: output flow key
596 * @key_lenp: length of output flow key
598 * The caller must ensure that skb->len >= ETH_HLEN.
600 * Returns 0 if successful, otherwise a negative errno value.
602 * Initializes @skb header pointers as follows:
604 * - skb->mac_header: the Ethernet header.
606 * - skb->network_header: just past the Ethernet header, or just past the
607 * VLAN header, to the first byte of the Ethernet payload.
609 * - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
610 * on output, then just past the IP header, if one is present and
611 * of a correct length, otherwise the same as skb->network_header.
612 * For other key->dl_type values it is left untouched.
614 int ovs_flow_extract(struct sk_buff
*skb
, u16 in_port
, struct sw_flow_key
*key
,
618 int key_len
= SW_FLOW_KEY_OFFSET(eth
);
621 memset(key
, 0, sizeof(*key
));
623 key
->phy
.priority
= skb
->priority
;
624 if (OVS_CB(skb
)->tun_key
)
625 memcpy(&key
->phy
.tun
.tun_key
, OVS_CB(skb
)->tun_key
, sizeof(key
->phy
.tun
.tun_key
));
626 key
->phy
.in_port
= in_port
;
628 skb_reset_mac_header(skb
);
630 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
631 * header in the linear data area.
634 memcpy(key
->eth
.src
, eth
->h_source
, ETH_ALEN
);
635 memcpy(key
->eth
.dst
, eth
->h_dest
, ETH_ALEN
);
637 __skb_pull(skb
, 2 * ETH_ALEN
);
639 if (vlan_tx_tag_present(skb
))
640 key
->eth
.tci
= htons(vlan_get_tci(skb
));
641 else if (eth
->h_proto
== htons(ETH_P_8021Q
))
642 if (unlikely(parse_vlan(skb
, key
)))
645 key
->eth
.type
= parse_ethertype(skb
);
646 if (unlikely(key
->eth
.type
== htons(0)))
649 skb_reset_network_header(skb
);
650 __skb_push(skb
, skb
->data
- skb_mac_header(skb
));
653 if (key
->eth
.type
== htons(ETH_P_IP
)) {
657 key_len
= SW_FLOW_KEY_OFFSET(ipv4
.addr
);
659 error
= check_iphdr(skb
);
660 if (unlikely(error
)) {
661 if (error
== -EINVAL
) {
662 skb
->transport_header
= skb
->network_header
;
669 key
->ipv4
.addr
.src
= nh
->saddr
;
670 key
->ipv4
.addr
.dst
= nh
->daddr
;
672 key
->ip
.proto
= nh
->protocol
;
673 key
->ip
.tos
= nh
->tos
;
674 key
->ip
.ttl
= nh
->ttl
;
676 offset
= nh
->frag_off
& htons(IP_OFFSET
);
678 key
->ip
.frag
= OVS_FRAG_TYPE_LATER
;
681 if (nh
->frag_off
& htons(IP_MF
) ||
682 skb_shinfo(skb
)->gso_type
& SKB_GSO_UDP
)
683 key
->ip
.frag
= OVS_FRAG_TYPE_FIRST
;
685 /* Transport layer. */
686 if (key
->ip
.proto
== IPPROTO_TCP
) {
687 key_len
= SW_FLOW_KEY_OFFSET(ipv4
.tp
);
688 if (tcphdr_ok(skb
)) {
689 struct tcphdr
*tcp
= tcp_hdr(skb
);
690 key
->ipv4
.tp
.src
= tcp
->source
;
691 key
->ipv4
.tp
.dst
= tcp
->dest
;
693 } else if (key
->ip
.proto
== IPPROTO_UDP
) {
694 key_len
= SW_FLOW_KEY_OFFSET(ipv4
.tp
);
695 if (udphdr_ok(skb
)) {
696 struct udphdr
*udp
= udp_hdr(skb
);
697 key
->ipv4
.tp
.src
= udp
->source
;
698 key
->ipv4
.tp
.dst
= udp
->dest
;
700 } else if (key
->ip
.proto
== IPPROTO_ICMP
) {
701 key_len
= SW_FLOW_KEY_OFFSET(ipv4
.tp
);
702 if (icmphdr_ok(skb
)) {
703 struct icmphdr
*icmp
= icmp_hdr(skb
);
704 /* The ICMP type and code fields use the 16-bit
705 * transport port fields, so we need to store
706 * them in 16-bit network byte order. */
707 key
->ipv4
.tp
.src
= htons(icmp
->type
);
708 key
->ipv4
.tp
.dst
= htons(icmp
->code
);
712 } else if ((key
->eth
.type
== htons(ETH_P_ARP
) ||
713 key
->eth
.type
== htons(ETH_P_RARP
)) && arphdr_ok(skb
)) {
714 struct arp_eth_header
*arp
;
716 arp
= (struct arp_eth_header
*)skb_network_header(skb
);
718 if (arp
->ar_hrd
== htons(ARPHRD_ETHER
)
719 && arp
->ar_pro
== htons(ETH_P_IP
)
720 && arp
->ar_hln
== ETH_ALEN
721 && arp
->ar_pln
== 4) {
723 /* We only match on the lower 8 bits of the opcode. */
724 if (ntohs(arp
->ar_op
) <= 0xff)
725 key
->ip
.proto
= ntohs(arp
->ar_op
);
726 memcpy(&key
->ipv4
.addr
.src
, arp
->ar_sip
, sizeof(key
->ipv4
.addr
.src
));
727 memcpy(&key
->ipv4
.addr
.dst
, arp
->ar_tip
, sizeof(key
->ipv4
.addr
.dst
));
728 memcpy(key
->ipv4
.arp
.sha
, arp
->ar_sha
, ETH_ALEN
);
729 memcpy(key
->ipv4
.arp
.tha
, arp
->ar_tha
, ETH_ALEN
);
730 key_len
= SW_FLOW_KEY_OFFSET(ipv4
.arp
);
732 } else if (key
->eth
.type
== htons(ETH_P_IPV6
)) {
733 int nh_len
; /* IPv6 Header + Extensions */
735 nh_len
= parse_ipv6hdr(skb
, key
, &key_len
);
736 if (unlikely(nh_len
< 0)) {
737 if (nh_len
== -EINVAL
)
738 skb
->transport_header
= skb
->network_header
;
744 if (key
->ip
.frag
== OVS_FRAG_TYPE_LATER
)
746 if (skb_shinfo(skb
)->gso_type
& SKB_GSO_UDP
)
747 key
->ip
.frag
= OVS_FRAG_TYPE_FIRST
;
749 /* Transport layer. */
750 if (key
->ip
.proto
== NEXTHDR_TCP
) {
751 key_len
= SW_FLOW_KEY_OFFSET(ipv6
.tp
);
752 if (tcphdr_ok(skb
)) {
753 struct tcphdr
*tcp
= tcp_hdr(skb
);
754 key
->ipv6
.tp
.src
= tcp
->source
;
755 key
->ipv6
.tp
.dst
= tcp
->dest
;
757 } else if (key
->ip
.proto
== NEXTHDR_UDP
) {
758 key_len
= SW_FLOW_KEY_OFFSET(ipv6
.tp
);
759 if (udphdr_ok(skb
)) {
760 struct udphdr
*udp
= udp_hdr(skb
);
761 key
->ipv6
.tp
.src
= udp
->source
;
762 key
->ipv6
.tp
.dst
= udp
->dest
;
764 } else if (key
->ip
.proto
== NEXTHDR_ICMP
) {
765 key_len
= SW_FLOW_KEY_OFFSET(ipv6
.tp
);
766 if (icmp6hdr_ok(skb
)) {
767 error
= parse_icmpv6(skb
, key
, &key_len
, nh_len
);
779 static u32
ovs_flow_hash(const struct sw_flow_key
*key
, int key_start
, int key_len
)
781 return jhash2((u32
*)((u8
*)key
+ key_start
),
782 DIV_ROUND_UP(key_len
- key_start
, sizeof(u32
)), 0);
785 static int flow_key_start(struct sw_flow_key
*key
)
787 if (key
->phy
.tun
.tun_key
.ipv4_dst
)
790 return offsetof(struct sw_flow_key
, phy
.priority
);
793 struct sw_flow
*ovs_flow_tbl_lookup(struct flow_table
*table
,
794 struct sw_flow_key
*key
, int key_len
)
796 struct sw_flow
*flow
;
797 struct hlist_node
*n
;
798 struct hlist_head
*head
;
803 key_start
= flow_key_start(key
);
804 hash
= ovs_flow_hash(key
, key_start
, key_len
);
806 _key
= (u8
*) key
+ key_start
;
807 head
= find_bucket(table
, hash
);
808 hlist_for_each_entry_rcu(flow
, n
, head
, hash_node
[table
->node_ver
]) {
810 if (flow
->hash
== hash
&&
811 !memcmp((u8
*)&flow
->key
+ key_start
, _key
, key_len
- key_start
)) {
818 void ovs_flow_tbl_insert(struct flow_table
*table
, struct sw_flow
*flow
,
819 struct sw_flow_key
*key
, int key_len
)
821 flow
->hash
= ovs_flow_hash(key
, flow_key_start(key
), key_len
);
822 memcpy(&flow
->key
, key
, sizeof(flow
->key
));
823 __flow_tbl_insert(table
, flow
);
826 void ovs_flow_tbl_remove(struct flow_table
*table
, struct sw_flow
*flow
)
828 hlist_del_rcu(&flow
->hash_node
[table
->node_ver
]);
830 BUG_ON(table
->count
< 0);
833 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
834 const int ovs_key_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
835 [OVS_KEY_ATTR_ENCAP
] = -1,
836 [OVS_KEY_ATTR_PRIORITY
] = sizeof(u32
),
837 [OVS_KEY_ATTR_IN_PORT
] = sizeof(u32
),
838 [OVS_KEY_ATTR_ETHERNET
] = sizeof(struct ovs_key_ethernet
),
839 [OVS_KEY_ATTR_VLAN
] = sizeof(__be16
),
840 [OVS_KEY_ATTR_ETHERTYPE
] = sizeof(__be16
),
841 [OVS_KEY_ATTR_IPV4
] = sizeof(struct ovs_key_ipv4
),
842 [OVS_KEY_ATTR_IPV6
] = sizeof(struct ovs_key_ipv6
),
843 [OVS_KEY_ATTR_TCP
] = sizeof(struct ovs_key_tcp
),
844 [OVS_KEY_ATTR_UDP
] = sizeof(struct ovs_key_udp
),
845 [OVS_KEY_ATTR_ICMP
] = sizeof(struct ovs_key_icmp
),
846 [OVS_KEY_ATTR_ICMPV6
] = sizeof(struct ovs_key_icmpv6
),
847 [OVS_KEY_ATTR_ARP
] = sizeof(struct ovs_key_arp
),
848 [OVS_KEY_ATTR_ND
] = sizeof(struct ovs_key_nd
),
851 [OVS_KEY_ATTR_TUN_ID
] = sizeof(__be64
),
852 [OVS_KEY_ATTR_IPV4_TUNNEL
] = sizeof(struct ovs_key_ipv4_tunnel
),
855 static int ipv4_flow_from_nlattrs(struct sw_flow_key
*swkey
, int *key_len
,
856 const struct nlattr
*a
[], u64
*attrs
)
858 const struct ovs_key_icmp
*icmp_key
;
859 const struct ovs_key_tcp
*tcp_key
;
860 const struct ovs_key_udp
*udp_key
;
862 switch (swkey
->ip
.proto
) {
864 if (!(*attrs
& (1 << OVS_KEY_ATTR_TCP
)))
866 *attrs
&= ~(1 << OVS_KEY_ATTR_TCP
);
868 *key_len
= SW_FLOW_KEY_OFFSET(ipv4
.tp
);
869 tcp_key
= nla_data(a
[OVS_KEY_ATTR_TCP
]);
870 swkey
->ipv4
.tp
.src
= tcp_key
->tcp_src
;
871 swkey
->ipv4
.tp
.dst
= tcp_key
->tcp_dst
;
875 if (!(*attrs
& (1 << OVS_KEY_ATTR_UDP
)))
877 *attrs
&= ~(1 << OVS_KEY_ATTR_UDP
);
879 *key_len
= SW_FLOW_KEY_OFFSET(ipv4
.tp
);
880 udp_key
= nla_data(a
[OVS_KEY_ATTR_UDP
]);
881 swkey
->ipv4
.tp
.src
= udp_key
->udp_src
;
882 swkey
->ipv4
.tp
.dst
= udp_key
->udp_dst
;
886 if (!(*attrs
& (1 << OVS_KEY_ATTR_ICMP
)))
888 *attrs
&= ~(1 << OVS_KEY_ATTR_ICMP
);
890 *key_len
= SW_FLOW_KEY_OFFSET(ipv4
.tp
);
891 icmp_key
= nla_data(a
[OVS_KEY_ATTR_ICMP
]);
892 swkey
->ipv4
.tp
.src
= htons(icmp_key
->icmp_type
);
893 swkey
->ipv4
.tp
.dst
= htons(icmp_key
->icmp_code
);
900 static int ipv6_flow_from_nlattrs(struct sw_flow_key
*swkey
, int *key_len
,
901 const struct nlattr
*a
[], u64
*attrs
)
903 const struct ovs_key_icmpv6
*icmpv6_key
;
904 const struct ovs_key_tcp
*tcp_key
;
905 const struct ovs_key_udp
*udp_key
;
907 switch (swkey
->ip
.proto
) {
909 if (!(*attrs
& (1 << OVS_KEY_ATTR_TCP
)))
911 *attrs
&= ~(1 << OVS_KEY_ATTR_TCP
);
913 *key_len
= SW_FLOW_KEY_OFFSET(ipv6
.tp
);
914 tcp_key
= nla_data(a
[OVS_KEY_ATTR_TCP
]);
915 swkey
->ipv6
.tp
.src
= tcp_key
->tcp_src
;
916 swkey
->ipv6
.tp
.dst
= tcp_key
->tcp_dst
;
920 if (!(*attrs
& (1 << OVS_KEY_ATTR_UDP
)))
922 *attrs
&= ~(1 << OVS_KEY_ATTR_UDP
);
924 *key_len
= SW_FLOW_KEY_OFFSET(ipv6
.tp
);
925 udp_key
= nla_data(a
[OVS_KEY_ATTR_UDP
]);
926 swkey
->ipv6
.tp
.src
= udp_key
->udp_src
;
927 swkey
->ipv6
.tp
.dst
= udp_key
->udp_dst
;
931 if (!(*attrs
& (1 << OVS_KEY_ATTR_ICMPV6
)))
933 *attrs
&= ~(1 << OVS_KEY_ATTR_ICMPV6
);
935 *key_len
= SW_FLOW_KEY_OFFSET(ipv6
.tp
);
936 icmpv6_key
= nla_data(a
[OVS_KEY_ATTR_ICMPV6
]);
937 swkey
->ipv6
.tp
.src
= htons(icmpv6_key
->icmpv6_type
);
938 swkey
->ipv6
.tp
.dst
= htons(icmpv6_key
->icmpv6_code
);
940 if (swkey
->ipv6
.tp
.src
== htons(NDISC_NEIGHBOUR_SOLICITATION
) ||
941 swkey
->ipv6
.tp
.src
== htons(NDISC_NEIGHBOUR_ADVERTISEMENT
)) {
942 const struct ovs_key_nd
*nd_key
;
944 if (!(*attrs
& (1 << OVS_KEY_ATTR_ND
)))
946 *attrs
&= ~(1 << OVS_KEY_ATTR_ND
);
948 *key_len
= SW_FLOW_KEY_OFFSET(ipv6
.nd
);
949 nd_key
= nla_data(a
[OVS_KEY_ATTR_ND
]);
950 memcpy(&swkey
->ipv6
.nd
.target
, nd_key
->nd_target
,
951 sizeof(swkey
->ipv6
.nd
.target
));
952 memcpy(swkey
->ipv6
.nd
.sll
, nd_key
->nd_sll
, ETH_ALEN
);
953 memcpy(swkey
->ipv6
.nd
.tll
, nd_key
->nd_tll
, ETH_ALEN
);
961 static int parse_flow_nlattrs(const struct nlattr
*attr
,
962 const struct nlattr
*a
[], u64
*attrsp
)
964 const struct nlattr
*nla
;
969 nla_for_each_nested(nla
, attr
, rem
) {
970 u16 type
= nla_type(nla
);
973 if (type
> OVS_KEY_ATTR_MAX
|| attrs
& (1ULL << type
))
976 expected_len
= ovs_key_lens
[type
];
977 if (nla_len(nla
) != expected_len
&& expected_len
!= -1)
980 attrs
|= 1ULL << type
;
991 * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
992 * @swkey: receives the extracted flow key.
993 * @key_lenp: number of bytes used in @swkey.
994 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
997 int ovs_flow_from_nlattrs(struct sw_flow_key
*swkey
, int *key_lenp
,
998 const struct nlattr
*attr
)
1000 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1];
1001 const struct ovs_key_ethernet
*eth_key
;
1006 memset(swkey
, 0, sizeof(struct sw_flow_key
));
1007 key_len
= SW_FLOW_KEY_OFFSET(eth
);
1009 err
= parse_flow_nlattrs(attr
, a
, &attrs
);
1013 /* Metadata attributes. */
1014 if (attrs
& (1 << OVS_KEY_ATTR_PRIORITY
)) {
1015 swkey
->phy
.priority
= nla_get_u32(a
[OVS_KEY_ATTR_PRIORITY
]);
1016 attrs
&= ~(1 << OVS_KEY_ATTR_PRIORITY
);
1018 if (attrs
& (1 << OVS_KEY_ATTR_IN_PORT
)) {
1019 u32 in_port
= nla_get_u32(a
[OVS_KEY_ATTR_IN_PORT
]);
1020 if (in_port
>= DP_MAX_PORTS
)
1022 swkey
->phy
.in_port
= in_port
;
1023 attrs
&= ~(1 << OVS_KEY_ATTR_IN_PORT
);
1025 swkey
->phy
.in_port
= DP_MAX_PORTS
;
1028 if (attrs
& (1ULL << OVS_KEY_ATTR_TUN_ID
) &&
1029 attrs
& (1ULL << OVS_KEY_ATTR_IPV4_TUNNEL
)) {
1030 struct ovs_key_ipv4_tunnel
*tun_key
;
1033 tun_key
= nla_data(a
[OVS_KEY_ATTR_IPV4_TUNNEL
]);
1035 if (!tun_key
->ipv4_dst
)
1037 if (!(tun_key
->tun_flags
& OVS_FLOW_TNL_F_KEY
))
1040 tun_id
= nla_get_be64(a
[OVS_KEY_ATTR_TUN_ID
]);
1041 if (tun_id
!= tun_key
->tun_id
)
1044 memcpy(&swkey
->phy
.tun
.tun_key
, tun_key
, sizeof(swkey
->phy
.tun
.tun_key
));
1045 attrs
&= ~(1ULL << OVS_KEY_ATTR_TUN_ID
);
1046 attrs
&= ~(1ULL << OVS_KEY_ATTR_IPV4_TUNNEL
);
1047 } else if (attrs
& (1ULL << OVS_KEY_ATTR_TUN_ID
)) {
1048 swkey
->phy
.tun
.tun_key
.tun_id
= nla_get_be64(a
[OVS_KEY_ATTR_TUN_ID
]);
1049 swkey
->phy
.tun
.tun_key
.tun_flags
|= OVS_FLOW_TNL_F_KEY
;
1051 attrs
&= ~(1ULL << OVS_KEY_ATTR_TUN_ID
);
1052 } else if (attrs
& (1ULL << OVS_KEY_ATTR_IPV4_TUNNEL
)) {
1053 struct ovs_key_ipv4_tunnel
*tun_key
;
1054 tun_key
= nla_data(a
[OVS_KEY_ATTR_IPV4_TUNNEL
]);
1056 if (!tun_key
->ipv4_dst
)
1059 memcpy(&swkey
->phy
.tun
.tun_key
, tun_key
, sizeof(swkey
->phy
.tun
.tun_key
));
1060 attrs
&= ~(1ULL << OVS_KEY_ATTR_IPV4_TUNNEL
);
1063 /* Data attributes. */
1064 if (!(attrs
& (1 << OVS_KEY_ATTR_ETHERNET
)))
1066 attrs
&= ~(1 << OVS_KEY_ATTR_ETHERNET
);
1068 eth_key
= nla_data(a
[OVS_KEY_ATTR_ETHERNET
]);
1069 memcpy(swkey
->eth
.src
, eth_key
->eth_src
, ETH_ALEN
);
1070 memcpy(swkey
->eth
.dst
, eth_key
->eth_dst
, ETH_ALEN
);
1072 if (attrs
& (1u << OVS_KEY_ATTR_ETHERTYPE
) &&
1073 nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]) == htons(ETH_P_8021Q
)) {
1074 const struct nlattr
*encap
;
1077 if (attrs
!= ((1 << OVS_KEY_ATTR_VLAN
) |
1078 (1 << OVS_KEY_ATTR_ETHERTYPE
) |
1079 (1 << OVS_KEY_ATTR_ENCAP
)))
1082 encap
= a
[OVS_KEY_ATTR_ENCAP
];
1083 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
1084 if (tci
& htons(VLAN_TAG_PRESENT
)) {
1085 swkey
->eth
.tci
= tci
;
1087 err
= parse_flow_nlattrs(encap
, a
, &attrs
);
1091 /* Corner case for truncated 802.1Q header. */
1095 swkey
->eth
.type
= htons(ETH_P_8021Q
);
1096 *key_lenp
= key_len
;
1103 if (attrs
& (1 << OVS_KEY_ATTR_ETHERTYPE
)) {
1104 swkey
->eth
.type
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
1105 if (ntohs(swkey
->eth
.type
) < 1536)
1107 attrs
&= ~(1 << OVS_KEY_ATTR_ETHERTYPE
);
1109 swkey
->eth
.type
= htons(ETH_P_802_2
);
1112 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
1113 const struct ovs_key_ipv4
*ipv4_key
;
1115 if (!(attrs
& (1 << OVS_KEY_ATTR_IPV4
)))
1117 attrs
&= ~(1 << OVS_KEY_ATTR_IPV4
);
1119 key_len
= SW_FLOW_KEY_OFFSET(ipv4
.addr
);
1120 ipv4_key
= nla_data(a
[OVS_KEY_ATTR_IPV4
]);
1121 if (ipv4_key
->ipv4_frag
> OVS_FRAG_TYPE_MAX
)
1123 swkey
->ip
.proto
= ipv4_key
->ipv4_proto
;
1124 swkey
->ip
.tos
= ipv4_key
->ipv4_tos
;
1125 swkey
->ip
.ttl
= ipv4_key
->ipv4_ttl
;
1126 swkey
->ip
.frag
= ipv4_key
->ipv4_frag
;
1127 swkey
->ipv4
.addr
.src
= ipv4_key
->ipv4_src
;
1128 swkey
->ipv4
.addr
.dst
= ipv4_key
->ipv4_dst
;
1130 if (swkey
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
1131 err
= ipv4_flow_from_nlattrs(swkey
, &key_len
, a
, &attrs
);
1135 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
1136 const struct ovs_key_ipv6
*ipv6_key
;
1138 if (!(attrs
& (1 << OVS_KEY_ATTR_IPV6
)))
1140 attrs
&= ~(1 << OVS_KEY_ATTR_IPV6
);
1142 key_len
= SW_FLOW_KEY_OFFSET(ipv6
.label
);
1143 ipv6_key
= nla_data(a
[OVS_KEY_ATTR_IPV6
]);
1144 if (ipv6_key
->ipv6_frag
> OVS_FRAG_TYPE_MAX
)
1146 swkey
->ipv6
.label
= ipv6_key
->ipv6_label
;
1147 swkey
->ip
.proto
= ipv6_key
->ipv6_proto
;
1148 swkey
->ip
.tos
= ipv6_key
->ipv6_tclass
;
1149 swkey
->ip
.ttl
= ipv6_key
->ipv6_hlimit
;
1150 swkey
->ip
.frag
= ipv6_key
->ipv6_frag
;
1151 memcpy(&swkey
->ipv6
.addr
.src
, ipv6_key
->ipv6_src
,
1152 sizeof(swkey
->ipv6
.addr
.src
));
1153 memcpy(&swkey
->ipv6
.addr
.dst
, ipv6_key
->ipv6_dst
,
1154 sizeof(swkey
->ipv6
.addr
.dst
));
1156 if (swkey
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
1157 err
= ipv6_flow_from_nlattrs(swkey
, &key_len
, a
, &attrs
);
1161 } else if (swkey
->eth
.type
== htons(ETH_P_ARP
) ||
1162 swkey
->eth
.type
== htons(ETH_P_RARP
)) {
1163 const struct ovs_key_arp
*arp_key
;
1165 if (!(attrs
& (1 << OVS_KEY_ATTR_ARP
)))
1167 attrs
&= ~(1 << OVS_KEY_ATTR_ARP
);
1169 key_len
= SW_FLOW_KEY_OFFSET(ipv4
.arp
);
1170 arp_key
= nla_data(a
[OVS_KEY_ATTR_ARP
]);
1171 swkey
->ipv4
.addr
.src
= arp_key
->arp_sip
;
1172 swkey
->ipv4
.addr
.dst
= arp_key
->arp_tip
;
1173 if (arp_key
->arp_op
& htons(0xff00))
1175 swkey
->ip
.proto
= ntohs(arp_key
->arp_op
);
1176 memcpy(swkey
->ipv4
.arp
.sha
, arp_key
->arp_sha
, ETH_ALEN
);
1177 memcpy(swkey
->ipv4
.arp
.tha
, arp_key
->arp_tha
, ETH_ALEN
);
1182 *key_lenp
= key_len
;
1188 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1189 * @in_port: receives the extracted input port.
1190 * @tun_id: receives the extracted tunnel ID.
1191 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1194 * This parses a series of Netlink attributes that form a flow key, which must
1195 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1196 * get the metadata, that is, the parts of the flow key that cannot be
1197 * extracted from the packet itself.
1200 int ovs_flow_metadata_from_nlattrs(struct sw_flow
*flow
, int key_len
, const struct nlattr
*attr
)
1202 struct ovs_key_ipv4_tunnel
*tun_key
= &flow
->key
.phy
.tun
.tun_key
;
1203 const struct nlattr
*nla
;
1207 flow
->key
.phy
.in_port
= DP_MAX_PORTS
;
1208 flow
->key
.phy
.priority
= 0;
1209 memset(tun_key
, 0, sizeof(flow
->key
.phy
.tun
.tun_key
));
1211 nla_for_each_nested(nla
, attr
, rem
) {
1212 int type
= nla_type(nla
);
1214 if (type
<= OVS_KEY_ATTR_MAX
&& ovs_key_lens
[type
] > 0) {
1215 if (nla_len(nla
) != ovs_key_lens
[type
])
1219 case OVS_KEY_ATTR_PRIORITY
:
1220 flow
->key
.phy
.priority
= nla_get_u32(nla
);
1223 case OVS_KEY_ATTR_TUN_ID
:
1224 tun_id
= nla_get_be64(nla
);
1226 if (tun_key
->ipv4_dst
) {
1227 if (!(tun_key
->tun_flags
& OVS_FLOW_TNL_F_KEY
))
1229 if (tun_key
->tun_id
!= tun_id
)
1233 tun_key
->tun_id
= tun_id
;
1234 tun_key
->tun_flags
|= OVS_FLOW_TNL_F_KEY
;
1238 case OVS_KEY_ATTR_IPV4_TUNNEL
:
1239 if (tun_key
->tun_flags
& OVS_FLOW_TNL_F_KEY
) {
1240 tun_id
= tun_key
->tun_id
;
1242 memcpy(tun_key
, nla_data(nla
), sizeof(*tun_key
));
1243 if (!(tun_key
->tun_flags
& OVS_FLOW_TNL_F_KEY
))
1246 if (tun_key
->tun_id
!= tun_id
)
1249 memcpy(tun_key
, nla_data(nla
), sizeof(*tun_key
));
1251 if (!tun_key
->ipv4_dst
)
1255 case OVS_KEY_ATTR_IN_PORT
:
1256 if (nla_get_u32(nla
) >= DP_MAX_PORTS
)
1258 flow
->key
.phy
.in_port
= nla_get_u32(nla
);
1266 flow
->hash
= ovs_flow_hash(&flow
->key
,
1267 flow_key_start(&flow
->key
), key_len
);
1272 int ovs_flow_to_nlattrs(const struct sw_flow_key
*swkey
, struct sk_buff
*skb
)
1274 struct ovs_key_ethernet
*eth_key
;
1275 struct nlattr
*nla
, *encap
;
1277 if (swkey
->phy
.priority
&&
1278 nla_put_u32(skb
, OVS_KEY_ATTR_PRIORITY
, swkey
->phy
.priority
))
1279 goto nla_put_failure
;
1281 if (swkey
->phy
.tun
.tun_key
.ipv4_dst
) {
1282 struct ovs_key_ipv4_tunnel
*tun_key
;
1283 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV4_TUNNEL
, sizeof(*tun_key
));
1285 goto nla_put_failure
;
1286 tun_key
= nla_data(nla
);
1287 memcpy(tun_key
, &swkey
->phy
.tun
.tun_key
, sizeof(*tun_key
));
1289 if ((swkey
->phy
.tun
.tun_key
.tun_flags
& OVS_FLOW_TNL_F_KEY
) &&
1290 nla_put_be64(skb
, OVS_KEY_ATTR_TUN_ID
, swkey
->phy
.tun
.tun_key
.tun_id
))
1291 goto nla_put_failure
;
1293 if (swkey
->phy
.in_port
!= DP_MAX_PORTS
&&
1294 nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
, swkey
->phy
.in_port
))
1295 goto nla_put_failure
;
1297 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ETHERNET
, sizeof(*eth_key
));
1299 goto nla_put_failure
;
1300 eth_key
= nla_data(nla
);
1301 memcpy(eth_key
->eth_src
, swkey
->eth
.src
, ETH_ALEN
);
1302 memcpy(eth_key
->eth_dst
, swkey
->eth
.dst
, ETH_ALEN
);
1304 if (swkey
->eth
.tci
|| swkey
->eth
.type
== htons(ETH_P_8021Q
)) {
1305 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_P_8021Q
)) ||
1306 nla_put_be16(skb
, OVS_KEY_ATTR_VLAN
, swkey
->eth
.tci
))
1307 goto nla_put_failure
;
1308 encap
= nla_nest_start(skb
, OVS_KEY_ATTR_ENCAP
);
1309 if (!swkey
->eth
.tci
)
1315 if (swkey
->eth
.type
== htons(ETH_P_802_2
))
1318 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, swkey
->eth
.type
))
1319 goto nla_put_failure
;
1321 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
1322 struct ovs_key_ipv4
*ipv4_key
;
1324 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV4
, sizeof(*ipv4_key
));
1326 goto nla_put_failure
;
1327 ipv4_key
= nla_data(nla
);
1328 ipv4_key
->ipv4_src
= swkey
->ipv4
.addr
.src
;
1329 ipv4_key
->ipv4_dst
= swkey
->ipv4
.addr
.dst
;
1330 ipv4_key
->ipv4_proto
= swkey
->ip
.proto
;
1331 ipv4_key
->ipv4_tos
= swkey
->ip
.tos
;
1332 ipv4_key
->ipv4_ttl
= swkey
->ip
.ttl
;
1333 ipv4_key
->ipv4_frag
= swkey
->ip
.frag
;
1334 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
1335 struct ovs_key_ipv6
*ipv6_key
;
1337 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV6
, sizeof(*ipv6_key
));
1339 goto nla_put_failure
;
1340 ipv6_key
= nla_data(nla
);
1341 memcpy(ipv6_key
->ipv6_src
, &swkey
->ipv6
.addr
.src
,
1342 sizeof(ipv6_key
->ipv6_src
));
1343 memcpy(ipv6_key
->ipv6_dst
, &swkey
->ipv6
.addr
.dst
,
1344 sizeof(ipv6_key
->ipv6_dst
));
1345 ipv6_key
->ipv6_label
= swkey
->ipv6
.label
;
1346 ipv6_key
->ipv6_proto
= swkey
->ip
.proto
;
1347 ipv6_key
->ipv6_tclass
= swkey
->ip
.tos
;
1348 ipv6_key
->ipv6_hlimit
= swkey
->ip
.ttl
;
1349 ipv6_key
->ipv6_frag
= swkey
->ip
.frag
;
1350 } else if (swkey
->eth
.type
== htons(ETH_P_ARP
) ||
1351 swkey
->eth
.type
== htons(ETH_P_RARP
)) {
1352 struct ovs_key_arp
*arp_key
;
1354 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ARP
, sizeof(*arp_key
));
1356 goto nla_put_failure
;
1357 arp_key
= nla_data(nla
);
1358 memset(arp_key
, 0, sizeof(struct ovs_key_arp
));
1359 arp_key
->arp_sip
= swkey
->ipv4
.addr
.src
;
1360 arp_key
->arp_tip
= swkey
->ipv4
.addr
.dst
;
1361 arp_key
->arp_op
= htons(swkey
->ip
.proto
);
1362 memcpy(arp_key
->arp_sha
, swkey
->ipv4
.arp
.sha
, ETH_ALEN
);
1363 memcpy(arp_key
->arp_tha
, swkey
->ipv4
.arp
.tha
, ETH_ALEN
);
1366 if ((swkey
->eth
.type
== htons(ETH_P_IP
) ||
1367 swkey
->eth
.type
== htons(ETH_P_IPV6
)) &&
1368 swkey
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
1370 if (swkey
->ip
.proto
== IPPROTO_TCP
) {
1371 struct ovs_key_tcp
*tcp_key
;
1373 nla
= nla_reserve(skb
, OVS_KEY_ATTR_TCP
, sizeof(*tcp_key
));
1375 goto nla_put_failure
;
1376 tcp_key
= nla_data(nla
);
1377 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
1378 tcp_key
->tcp_src
= swkey
->ipv4
.tp
.src
;
1379 tcp_key
->tcp_dst
= swkey
->ipv4
.tp
.dst
;
1380 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
1381 tcp_key
->tcp_src
= swkey
->ipv6
.tp
.src
;
1382 tcp_key
->tcp_dst
= swkey
->ipv6
.tp
.dst
;
1384 } else if (swkey
->ip
.proto
== IPPROTO_UDP
) {
1385 struct ovs_key_udp
*udp_key
;
1387 nla
= nla_reserve(skb
, OVS_KEY_ATTR_UDP
, sizeof(*udp_key
));
1389 goto nla_put_failure
;
1390 udp_key
= nla_data(nla
);
1391 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
1392 udp_key
->udp_src
= swkey
->ipv4
.tp
.src
;
1393 udp_key
->udp_dst
= swkey
->ipv4
.tp
.dst
;
1394 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
1395 udp_key
->udp_src
= swkey
->ipv6
.tp
.src
;
1396 udp_key
->udp_dst
= swkey
->ipv6
.tp
.dst
;
1398 } else if (swkey
->eth
.type
== htons(ETH_P_IP
) &&
1399 swkey
->ip
.proto
== IPPROTO_ICMP
) {
1400 struct ovs_key_icmp
*icmp_key
;
1402 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMP
, sizeof(*icmp_key
));
1404 goto nla_put_failure
;
1405 icmp_key
= nla_data(nla
);
1406 icmp_key
->icmp_type
= ntohs(swkey
->ipv4
.tp
.src
);
1407 icmp_key
->icmp_code
= ntohs(swkey
->ipv4
.tp
.dst
);
1408 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
) &&
1409 swkey
->ip
.proto
== IPPROTO_ICMPV6
) {
1410 struct ovs_key_icmpv6
*icmpv6_key
;
1412 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMPV6
,
1413 sizeof(*icmpv6_key
));
1415 goto nla_put_failure
;
1416 icmpv6_key
= nla_data(nla
);
1417 icmpv6_key
->icmpv6_type
= ntohs(swkey
->ipv6
.tp
.src
);
1418 icmpv6_key
->icmpv6_code
= ntohs(swkey
->ipv6
.tp
.dst
);
1420 if (icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_SOLICITATION
||
1421 icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_ADVERTISEMENT
) {
1422 struct ovs_key_nd
*nd_key
;
1424 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ND
, sizeof(*nd_key
));
1426 goto nla_put_failure
;
1427 nd_key
= nla_data(nla
);
1428 memcpy(nd_key
->nd_target
, &swkey
->ipv6
.nd
.target
,
1429 sizeof(nd_key
->nd_target
));
1430 memcpy(nd_key
->nd_sll
, swkey
->ipv6
.nd
.sll
, ETH_ALEN
);
1431 memcpy(nd_key
->nd_tll
, swkey
->ipv6
.nd
.tll
, ETH_ALEN
);
1438 nla_nest_end(skb
, encap
);
1446 /* Initializes the flow module.
1447 * Returns zero if successful or a negative error code. */
1448 int ovs_flow_init(void)
1450 flow_cache
= kmem_cache_create("sw_flow", sizeof(struct sw_flow
), 0,
1452 if (flow_cache
== NULL
)
1458 /* Uninitializes the flow module. */
1459 void ovs_flow_exit(void)
1461 kmem_cache_destroy(flow_cache
);