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>
43 #include <net/ip_tunnels.h>
45 #include <net/ndisc.h>
47 static struct kmem_cache
*flow_cache
;
49 static int check_header(struct sk_buff
*skb
, int len
)
51 if (unlikely(skb
->len
< len
))
53 if (unlikely(!pskb_may_pull(skb
, len
)))
58 static bool arphdr_ok(struct sk_buff
*skb
)
60 return pskb_may_pull(skb
, skb_network_offset(skb
) +
61 sizeof(struct arp_eth_header
));
64 static int check_iphdr(struct sk_buff
*skb
)
66 unsigned int nh_ofs
= skb_network_offset(skb
);
70 err
= check_header(skb
, nh_ofs
+ sizeof(struct iphdr
));
74 ip_len
= ip_hdrlen(skb
);
75 if (unlikely(ip_len
< sizeof(struct iphdr
) ||
76 skb
->len
< nh_ofs
+ ip_len
))
79 skb_set_transport_header(skb
, nh_ofs
+ ip_len
);
83 static bool tcphdr_ok(struct sk_buff
*skb
)
85 int th_ofs
= skb_transport_offset(skb
);
88 if (unlikely(!pskb_may_pull(skb
, th_ofs
+ sizeof(struct tcphdr
))))
91 tcp_len
= tcp_hdrlen(skb
);
92 if (unlikely(tcp_len
< sizeof(struct tcphdr
) ||
93 skb
->len
< th_ofs
+ tcp_len
))
99 static bool udphdr_ok(struct sk_buff
*skb
)
101 return pskb_may_pull(skb
, skb_transport_offset(skb
) +
102 sizeof(struct udphdr
));
105 static bool icmphdr_ok(struct sk_buff
*skb
)
107 return pskb_may_pull(skb
, skb_transport_offset(skb
) +
108 sizeof(struct icmphdr
));
111 u64
ovs_flow_used_time(unsigned long flow_jiffies
)
113 struct timespec cur_ts
;
116 ktime_get_ts(&cur_ts
);
117 idle_ms
= jiffies_to_msecs(jiffies
- flow_jiffies
);
118 cur_ms
= (u64
)cur_ts
.tv_sec
* MSEC_PER_SEC
+
119 cur_ts
.tv_nsec
/ NSEC_PER_MSEC
;
121 return cur_ms
- idle_ms
;
124 #define SW_FLOW_KEY_OFFSET(field) \
125 (offsetof(struct sw_flow_key, field) + \
126 FIELD_SIZEOF(struct sw_flow_key, field))
128 static int parse_ipv6hdr(struct sk_buff
*skb
, struct sw_flow_key
*key
,
131 unsigned int nh_ofs
= skb_network_offset(skb
);
139 *key_lenp
= SW_FLOW_KEY_OFFSET(ipv6
.label
);
141 err
= check_header(skb
, nh_ofs
+ sizeof(*nh
));
146 nexthdr
= nh
->nexthdr
;
147 payload_ofs
= (u8
*)(nh
+ 1) - skb
->data
;
149 key
->ip
.proto
= NEXTHDR_NONE
;
150 key
->ip
.tos
= ipv6_get_dsfield(nh
);
151 key
->ip
.ttl
= nh
->hop_limit
;
152 key
->ipv6
.label
= *(__be32
*)nh
& htonl(IPV6_FLOWINFO_FLOWLABEL
);
153 key
->ipv6
.addr
.src
= nh
->saddr
;
154 key
->ipv6
.addr
.dst
= nh
->daddr
;
156 payload_ofs
= ipv6_skip_exthdr(skb
, payload_ofs
, &nexthdr
, &frag_off
);
157 if (unlikely(payload_ofs
< 0))
161 if (frag_off
& htons(~0x7))
162 key
->ip
.frag
= OVS_FRAG_TYPE_LATER
;
164 key
->ip
.frag
= OVS_FRAG_TYPE_FIRST
;
167 nh_len
= payload_ofs
- nh_ofs
;
168 skb_set_transport_header(skb
, nh_ofs
+ nh_len
);
169 key
->ip
.proto
= nexthdr
;
173 static bool icmp6hdr_ok(struct sk_buff
*skb
)
175 return pskb_may_pull(skb
, skb_transport_offset(skb
) +
176 sizeof(struct icmp6hdr
));
179 #define TCP_FLAGS_OFFSET 13
180 #define TCP_FLAG_MASK 0x3f
182 void ovs_flow_used(struct sw_flow
*flow
, struct sk_buff
*skb
)
186 if ((flow
->key
.eth
.type
== htons(ETH_P_IP
) ||
187 flow
->key
.eth
.type
== htons(ETH_P_IPV6
)) &&
188 flow
->key
.ip
.proto
== IPPROTO_TCP
&&
189 likely(skb
->len
>= skb_transport_offset(skb
) + sizeof(struct tcphdr
))) {
190 u8
*tcp
= (u8
*)tcp_hdr(skb
);
191 tcp_flags
= *(tcp
+ TCP_FLAGS_OFFSET
) & TCP_FLAG_MASK
;
194 spin_lock(&flow
->lock
);
195 flow
->used
= jiffies
;
196 flow
->packet_count
++;
197 flow
->byte_count
+= skb
->len
;
198 flow
->tcp_flags
|= tcp_flags
;
199 spin_unlock(&flow
->lock
);
202 struct sw_flow_actions
*ovs_flow_actions_alloc(int size
)
204 struct sw_flow_actions
*sfa
;
206 if (size
> MAX_ACTIONS_BUFSIZE
)
207 return ERR_PTR(-EINVAL
);
209 sfa
= kmalloc(sizeof(*sfa
) + size
, GFP_KERNEL
);
211 return ERR_PTR(-ENOMEM
);
213 sfa
->actions_len
= 0;
217 struct sw_flow
*ovs_flow_alloc(void)
219 struct sw_flow
*flow
;
221 flow
= kmem_cache_alloc(flow_cache
, GFP_KERNEL
);
223 return ERR_PTR(-ENOMEM
);
225 spin_lock_init(&flow
->lock
);
226 flow
->sf_acts
= NULL
;
231 static struct hlist_head
*find_bucket(struct flow_table
*table
, u32 hash
)
233 hash
= jhash_1word(hash
, table
->hash_seed
);
234 return flex_array_get(table
->buckets
,
235 (hash
& (table
->n_buckets
- 1)));
238 static struct flex_array
*alloc_buckets(unsigned int n_buckets
)
240 struct flex_array
*buckets
;
243 buckets
= flex_array_alloc(sizeof(struct hlist_head
*),
244 n_buckets
, GFP_KERNEL
);
248 err
= flex_array_prealloc(buckets
, 0, n_buckets
, GFP_KERNEL
);
250 flex_array_free(buckets
);
254 for (i
= 0; i
< n_buckets
; i
++)
255 INIT_HLIST_HEAD((struct hlist_head
*)
256 flex_array_get(buckets
, i
));
261 static void free_buckets(struct flex_array
*buckets
)
263 flex_array_free(buckets
);
266 struct flow_table
*ovs_flow_tbl_alloc(int new_size
)
268 struct flow_table
*table
= kmalloc(sizeof(*table
), GFP_KERNEL
);
273 table
->buckets
= alloc_buckets(new_size
);
275 if (!table
->buckets
) {
279 table
->n_buckets
= new_size
;
282 table
->keep_flows
= false;
283 get_random_bytes(&table
->hash_seed
, sizeof(u32
));
288 void ovs_flow_tbl_destroy(struct flow_table
*table
)
295 if (table
->keep_flows
)
298 for (i
= 0; i
< table
->n_buckets
; i
++) {
299 struct sw_flow
*flow
;
300 struct hlist_head
*head
= flex_array_get(table
->buckets
, i
);
301 struct hlist_node
*n
;
302 int ver
= table
->node_ver
;
304 hlist_for_each_entry_safe(flow
, n
, head
, hash_node
[ver
]) {
305 hlist_del_rcu(&flow
->hash_node
[ver
]);
311 free_buckets(table
->buckets
);
315 static void flow_tbl_destroy_rcu_cb(struct rcu_head
*rcu
)
317 struct flow_table
*table
= container_of(rcu
, struct flow_table
, rcu
);
319 ovs_flow_tbl_destroy(table
);
322 void ovs_flow_tbl_deferred_destroy(struct flow_table
*table
)
327 call_rcu(&table
->rcu
, flow_tbl_destroy_rcu_cb
);
330 struct sw_flow
*ovs_flow_tbl_next(struct flow_table
*table
, u32
*bucket
, u32
*last
)
332 struct sw_flow
*flow
;
333 struct hlist_head
*head
;
337 ver
= table
->node_ver
;
338 while (*bucket
< table
->n_buckets
) {
340 head
= flex_array_get(table
->buckets
, *bucket
);
341 hlist_for_each_entry_rcu(flow
, head
, hash_node
[ver
]) {
356 static void flow_table_copy_flows(struct flow_table
*old
, struct flow_table
*new)
361 old_ver
= old
->node_ver
;
362 new->node_ver
= !old_ver
;
364 /* Insert in new table. */
365 for (i
= 0; i
< old
->n_buckets
; i
++) {
366 struct sw_flow
*flow
;
367 struct hlist_head
*head
;
369 head
= flex_array_get(old
->buckets
, i
);
371 hlist_for_each_entry(flow
, head
, hash_node
[old_ver
])
372 ovs_flow_tbl_insert(new, flow
);
374 old
->keep_flows
= true;
377 static struct flow_table
*__flow_tbl_rehash(struct flow_table
*table
, int n_buckets
)
379 struct flow_table
*new_table
;
381 new_table
= ovs_flow_tbl_alloc(n_buckets
);
383 return ERR_PTR(-ENOMEM
);
385 flow_table_copy_flows(table
, new_table
);
390 struct flow_table
*ovs_flow_tbl_rehash(struct flow_table
*table
)
392 return __flow_tbl_rehash(table
, table
->n_buckets
);
395 struct flow_table
*ovs_flow_tbl_expand(struct flow_table
*table
)
397 return __flow_tbl_rehash(table
, table
->n_buckets
* 2);
400 void ovs_flow_free(struct sw_flow
*flow
)
405 kfree((struct sf_flow_acts __force
*)flow
->sf_acts
);
406 kmem_cache_free(flow_cache
, flow
);
409 /* RCU callback used by ovs_flow_deferred_free. */
410 static void rcu_free_flow_callback(struct rcu_head
*rcu
)
412 struct sw_flow
*flow
= container_of(rcu
, struct sw_flow
, rcu
);
417 /* Schedules 'flow' to be freed after the next RCU grace period.
418 * The caller must hold rcu_read_lock for this to be sensible. */
419 void ovs_flow_deferred_free(struct sw_flow
*flow
)
421 call_rcu(&flow
->rcu
, rcu_free_flow_callback
);
424 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
425 * The caller must hold rcu_read_lock for this to be sensible. */
426 void ovs_flow_deferred_free_acts(struct sw_flow_actions
*sf_acts
)
428 kfree_rcu(sf_acts
, rcu
);
431 static int parse_vlan(struct sk_buff
*skb
, struct sw_flow_key
*key
)
434 __be16 eth_type
; /* ETH_P_8021Q */
437 struct qtag_prefix
*qp
;
439 if (unlikely(skb
->len
< sizeof(struct qtag_prefix
) + sizeof(__be16
)))
442 if (unlikely(!pskb_may_pull(skb
, sizeof(struct qtag_prefix
) +
446 qp
= (struct qtag_prefix
*) skb
->data
;
447 key
->eth
.tci
= qp
->tci
| htons(VLAN_TAG_PRESENT
);
448 __skb_pull(skb
, sizeof(struct qtag_prefix
));
453 static __be16
parse_ethertype(struct sk_buff
*skb
)
455 struct llc_snap_hdr
{
456 u8 dsap
; /* Always 0xAA */
457 u8 ssap
; /* Always 0xAA */
462 struct llc_snap_hdr
*llc
;
465 proto
= *(__be16
*) skb
->data
;
466 __skb_pull(skb
, sizeof(__be16
));
468 if (ntohs(proto
) >= ETH_P_802_3_MIN
)
471 if (skb
->len
< sizeof(struct llc_snap_hdr
))
472 return htons(ETH_P_802_2
);
474 if (unlikely(!pskb_may_pull(skb
, sizeof(struct llc_snap_hdr
))))
477 llc
= (struct llc_snap_hdr
*) skb
->data
;
478 if (llc
->dsap
!= LLC_SAP_SNAP
||
479 llc
->ssap
!= LLC_SAP_SNAP
||
480 (llc
->oui
[0] | llc
->oui
[1] | llc
->oui
[2]) != 0)
481 return htons(ETH_P_802_2
);
483 __skb_pull(skb
, sizeof(struct llc_snap_hdr
));
485 if (ntohs(llc
->ethertype
) >= ETH_P_802_3_MIN
)
486 return llc
->ethertype
;
488 return htons(ETH_P_802_2
);
491 static int parse_icmpv6(struct sk_buff
*skb
, struct sw_flow_key
*key
,
492 int *key_lenp
, int nh_len
)
494 struct icmp6hdr
*icmp
= icmp6_hdr(skb
);
498 /* The ICMPv6 type and code fields use the 16-bit transport port
499 * fields, so we need to store them in 16-bit network byte order.
501 key
->ipv6
.tp
.src
= htons(icmp
->icmp6_type
);
502 key
->ipv6
.tp
.dst
= htons(icmp
->icmp6_code
);
503 key_len
= SW_FLOW_KEY_OFFSET(ipv6
.tp
);
505 if (icmp
->icmp6_code
== 0 &&
506 (icmp
->icmp6_type
== NDISC_NEIGHBOUR_SOLICITATION
||
507 icmp
->icmp6_type
== NDISC_NEIGHBOUR_ADVERTISEMENT
)) {
508 int icmp_len
= skb
->len
- skb_transport_offset(skb
);
512 key_len
= SW_FLOW_KEY_OFFSET(ipv6
.nd
);
514 /* In order to process neighbor discovery options, we need the
517 if (unlikely(icmp_len
< sizeof(*nd
)))
519 if (unlikely(skb_linearize(skb
))) {
524 nd
= (struct nd_msg
*)skb_transport_header(skb
);
525 key
->ipv6
.nd
.target
= nd
->target
;
526 key_len
= SW_FLOW_KEY_OFFSET(ipv6
.nd
);
528 icmp_len
-= sizeof(*nd
);
530 while (icmp_len
>= 8) {
531 struct nd_opt_hdr
*nd_opt
=
532 (struct nd_opt_hdr
*)(nd
->opt
+ offset
);
533 int opt_len
= nd_opt
->nd_opt_len
* 8;
535 if (unlikely(!opt_len
|| opt_len
> icmp_len
))
538 /* Store the link layer address if the appropriate
539 * option is provided. It is considered an error if
540 * the same link layer option is specified twice.
542 if (nd_opt
->nd_opt_type
== ND_OPT_SOURCE_LL_ADDR
544 if (unlikely(!is_zero_ether_addr(key
->ipv6
.nd
.sll
)))
546 memcpy(key
->ipv6
.nd
.sll
,
547 &nd
->opt
[offset
+sizeof(*nd_opt
)], ETH_ALEN
);
548 } else if (nd_opt
->nd_opt_type
== ND_OPT_TARGET_LL_ADDR
550 if (unlikely(!is_zero_ether_addr(key
->ipv6
.nd
.tll
)))
552 memcpy(key
->ipv6
.nd
.tll
,
553 &nd
->opt
[offset
+sizeof(*nd_opt
)], ETH_ALEN
);
564 memset(&key
->ipv6
.nd
.target
, 0, sizeof(key
->ipv6
.nd
.target
));
565 memset(key
->ipv6
.nd
.sll
, 0, sizeof(key
->ipv6
.nd
.sll
));
566 memset(key
->ipv6
.nd
.tll
, 0, sizeof(key
->ipv6
.nd
.tll
));
574 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
575 * @skb: sk_buff that contains the frame, with skb->data pointing to the
577 * @in_port: port number on which @skb was received.
578 * @key: output flow key
579 * @key_lenp: length of output flow key
581 * The caller must ensure that skb->len >= ETH_HLEN.
583 * Returns 0 if successful, otherwise a negative errno value.
585 * Initializes @skb header pointers as follows:
587 * - skb->mac_header: the Ethernet header.
589 * - skb->network_header: just past the Ethernet header, or just past the
590 * VLAN header, to the first byte of the Ethernet payload.
592 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
593 * on output, then just past the IP header, if one is present and
594 * of a correct length, otherwise the same as skb->network_header.
595 * For other key->eth.type values it is left untouched.
597 int ovs_flow_extract(struct sk_buff
*skb
, u16 in_port
, struct sw_flow_key
*key
,
601 int key_len
= SW_FLOW_KEY_OFFSET(eth
);
604 memset(key
, 0, sizeof(*key
));
606 key
->phy
.priority
= skb
->priority
;
607 if (OVS_CB(skb
)->tun_key
)
608 memcpy(&key
->tun_key
, OVS_CB(skb
)->tun_key
, sizeof(key
->tun_key
));
609 key
->phy
.in_port
= in_port
;
610 key
->phy
.skb_mark
= skb
->mark
;
612 skb_reset_mac_header(skb
);
614 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
615 * header in the linear data area.
618 memcpy(key
->eth
.src
, eth
->h_source
, ETH_ALEN
);
619 memcpy(key
->eth
.dst
, eth
->h_dest
, ETH_ALEN
);
621 __skb_pull(skb
, 2 * ETH_ALEN
);
622 /* We are going to push all headers that we pull, so no need to
623 * update skb->csum here.
626 if (vlan_tx_tag_present(skb
))
627 key
->eth
.tci
= htons(skb
->vlan_tci
);
628 else if (eth
->h_proto
== htons(ETH_P_8021Q
))
629 if (unlikely(parse_vlan(skb
, key
)))
632 key
->eth
.type
= parse_ethertype(skb
);
633 if (unlikely(key
->eth
.type
== htons(0)))
636 skb_reset_network_header(skb
);
637 __skb_push(skb
, skb
->data
- skb_mac_header(skb
));
640 if (key
->eth
.type
== htons(ETH_P_IP
)) {
644 key_len
= SW_FLOW_KEY_OFFSET(ipv4
.addr
);
646 error
= check_iphdr(skb
);
647 if (unlikely(error
)) {
648 if (error
== -EINVAL
) {
649 skb
->transport_header
= skb
->network_header
;
656 key
->ipv4
.addr
.src
= nh
->saddr
;
657 key
->ipv4
.addr
.dst
= nh
->daddr
;
659 key
->ip
.proto
= nh
->protocol
;
660 key
->ip
.tos
= nh
->tos
;
661 key
->ip
.ttl
= nh
->ttl
;
663 offset
= nh
->frag_off
& htons(IP_OFFSET
);
665 key
->ip
.frag
= OVS_FRAG_TYPE_LATER
;
668 if (nh
->frag_off
& htons(IP_MF
) ||
669 skb_shinfo(skb
)->gso_type
& SKB_GSO_UDP
)
670 key
->ip
.frag
= OVS_FRAG_TYPE_FIRST
;
672 /* Transport layer. */
673 if (key
->ip
.proto
== IPPROTO_TCP
) {
674 key_len
= SW_FLOW_KEY_OFFSET(ipv4
.tp
);
675 if (tcphdr_ok(skb
)) {
676 struct tcphdr
*tcp
= tcp_hdr(skb
);
677 key
->ipv4
.tp
.src
= tcp
->source
;
678 key
->ipv4
.tp
.dst
= tcp
->dest
;
680 } else if (key
->ip
.proto
== IPPROTO_UDP
) {
681 key_len
= SW_FLOW_KEY_OFFSET(ipv4
.tp
);
682 if (udphdr_ok(skb
)) {
683 struct udphdr
*udp
= udp_hdr(skb
);
684 key
->ipv4
.tp
.src
= udp
->source
;
685 key
->ipv4
.tp
.dst
= udp
->dest
;
687 } else if (key
->ip
.proto
== IPPROTO_ICMP
) {
688 key_len
= SW_FLOW_KEY_OFFSET(ipv4
.tp
);
689 if (icmphdr_ok(skb
)) {
690 struct icmphdr
*icmp
= icmp_hdr(skb
);
691 /* The ICMP type and code fields use the 16-bit
692 * transport port fields, so we need to store
693 * them in 16-bit network byte order. */
694 key
->ipv4
.tp
.src
= htons(icmp
->type
);
695 key
->ipv4
.tp
.dst
= htons(icmp
->code
);
699 } else if ((key
->eth
.type
== htons(ETH_P_ARP
) ||
700 key
->eth
.type
== htons(ETH_P_RARP
)) && arphdr_ok(skb
)) {
701 struct arp_eth_header
*arp
;
703 arp
= (struct arp_eth_header
*)skb_network_header(skb
);
705 if (arp
->ar_hrd
== htons(ARPHRD_ETHER
)
706 && arp
->ar_pro
== htons(ETH_P_IP
)
707 && arp
->ar_hln
== ETH_ALEN
708 && arp
->ar_pln
== 4) {
710 /* We only match on the lower 8 bits of the opcode. */
711 if (ntohs(arp
->ar_op
) <= 0xff)
712 key
->ip
.proto
= ntohs(arp
->ar_op
);
713 memcpy(&key
->ipv4
.addr
.src
, arp
->ar_sip
, sizeof(key
->ipv4
.addr
.src
));
714 memcpy(&key
->ipv4
.addr
.dst
, arp
->ar_tip
, sizeof(key
->ipv4
.addr
.dst
));
715 memcpy(key
->ipv4
.arp
.sha
, arp
->ar_sha
, ETH_ALEN
);
716 memcpy(key
->ipv4
.arp
.tha
, arp
->ar_tha
, ETH_ALEN
);
717 key_len
= SW_FLOW_KEY_OFFSET(ipv4
.arp
);
719 } else if (key
->eth
.type
== htons(ETH_P_IPV6
)) {
720 int nh_len
; /* IPv6 Header + Extensions */
722 nh_len
= parse_ipv6hdr(skb
, key
, &key_len
);
723 if (unlikely(nh_len
< 0)) {
724 if (nh_len
== -EINVAL
)
725 skb
->transport_header
= skb
->network_header
;
731 if (key
->ip
.frag
== OVS_FRAG_TYPE_LATER
)
733 if (skb_shinfo(skb
)->gso_type
& SKB_GSO_UDP
)
734 key
->ip
.frag
= OVS_FRAG_TYPE_FIRST
;
736 /* Transport layer. */
737 if (key
->ip
.proto
== NEXTHDR_TCP
) {
738 key_len
= SW_FLOW_KEY_OFFSET(ipv6
.tp
);
739 if (tcphdr_ok(skb
)) {
740 struct tcphdr
*tcp
= tcp_hdr(skb
);
741 key
->ipv6
.tp
.src
= tcp
->source
;
742 key
->ipv6
.tp
.dst
= tcp
->dest
;
744 } else if (key
->ip
.proto
== NEXTHDR_UDP
) {
745 key_len
= SW_FLOW_KEY_OFFSET(ipv6
.tp
);
746 if (udphdr_ok(skb
)) {
747 struct udphdr
*udp
= udp_hdr(skb
);
748 key
->ipv6
.tp
.src
= udp
->source
;
749 key
->ipv6
.tp
.dst
= udp
->dest
;
751 } else if (key
->ip
.proto
== NEXTHDR_ICMP
) {
752 key_len
= SW_FLOW_KEY_OFFSET(ipv6
.tp
);
753 if (icmp6hdr_ok(skb
)) {
754 error
= parse_icmpv6(skb
, key
, &key_len
, nh_len
);
766 u32
ovs_flow_hash(const struct sw_flow_key
*key
, int key_len
)
768 return jhash2((u32
*)key
, DIV_ROUND_UP(key_len
, sizeof(u32
)), 0);
771 struct sw_flow
*ovs_flow_tbl_lookup(struct flow_table
*table
,
772 struct sw_flow_key
*key
, int key_len
)
774 struct sw_flow
*flow
;
775 struct hlist_head
*head
;
778 hash
= ovs_flow_hash(key
, key_len
);
780 head
= find_bucket(table
, hash
);
781 hlist_for_each_entry_rcu(flow
, head
, hash_node
[table
->node_ver
]) {
783 if (flow
->hash
== hash
&&
784 !memcmp(&flow
->key
, key
, key_len
)) {
791 void ovs_flow_tbl_insert(struct flow_table
*table
, struct sw_flow
*flow
)
793 struct hlist_head
*head
;
795 head
= find_bucket(table
, flow
->hash
);
796 hlist_add_head_rcu(&flow
->hash_node
[table
->node_ver
], head
);
800 void ovs_flow_tbl_remove(struct flow_table
*table
, struct sw_flow
*flow
)
802 BUG_ON(table
->count
== 0);
803 hlist_del_rcu(&flow
->hash_node
[table
->node_ver
]);
807 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
808 const int ovs_key_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
809 [OVS_KEY_ATTR_ENCAP
] = -1,
810 [OVS_KEY_ATTR_PRIORITY
] = sizeof(u32
),
811 [OVS_KEY_ATTR_IN_PORT
] = sizeof(u32
),
812 [OVS_KEY_ATTR_SKB_MARK
] = sizeof(u32
),
813 [OVS_KEY_ATTR_ETHERNET
] = sizeof(struct ovs_key_ethernet
),
814 [OVS_KEY_ATTR_VLAN
] = sizeof(__be16
),
815 [OVS_KEY_ATTR_ETHERTYPE
] = sizeof(__be16
),
816 [OVS_KEY_ATTR_IPV4
] = sizeof(struct ovs_key_ipv4
),
817 [OVS_KEY_ATTR_IPV6
] = sizeof(struct ovs_key_ipv6
),
818 [OVS_KEY_ATTR_TCP
] = sizeof(struct ovs_key_tcp
),
819 [OVS_KEY_ATTR_UDP
] = sizeof(struct ovs_key_udp
),
820 [OVS_KEY_ATTR_ICMP
] = sizeof(struct ovs_key_icmp
),
821 [OVS_KEY_ATTR_ICMPV6
] = sizeof(struct ovs_key_icmpv6
),
822 [OVS_KEY_ATTR_ARP
] = sizeof(struct ovs_key_arp
),
823 [OVS_KEY_ATTR_ND
] = sizeof(struct ovs_key_nd
),
824 [OVS_KEY_ATTR_TUNNEL
] = -1,
827 static int ipv4_flow_from_nlattrs(struct sw_flow_key
*swkey
, int *key_len
,
828 const struct nlattr
*a
[], u32
*attrs
)
830 const struct ovs_key_icmp
*icmp_key
;
831 const struct ovs_key_tcp
*tcp_key
;
832 const struct ovs_key_udp
*udp_key
;
834 switch (swkey
->ip
.proto
) {
836 if (!(*attrs
& (1 << OVS_KEY_ATTR_TCP
)))
838 *attrs
&= ~(1 << OVS_KEY_ATTR_TCP
);
840 *key_len
= SW_FLOW_KEY_OFFSET(ipv4
.tp
);
841 tcp_key
= nla_data(a
[OVS_KEY_ATTR_TCP
]);
842 swkey
->ipv4
.tp
.src
= tcp_key
->tcp_src
;
843 swkey
->ipv4
.tp
.dst
= tcp_key
->tcp_dst
;
847 if (!(*attrs
& (1 << OVS_KEY_ATTR_UDP
)))
849 *attrs
&= ~(1 << OVS_KEY_ATTR_UDP
);
851 *key_len
= SW_FLOW_KEY_OFFSET(ipv4
.tp
);
852 udp_key
= nla_data(a
[OVS_KEY_ATTR_UDP
]);
853 swkey
->ipv4
.tp
.src
= udp_key
->udp_src
;
854 swkey
->ipv4
.tp
.dst
= udp_key
->udp_dst
;
858 if (!(*attrs
& (1 << OVS_KEY_ATTR_ICMP
)))
860 *attrs
&= ~(1 << OVS_KEY_ATTR_ICMP
);
862 *key_len
= SW_FLOW_KEY_OFFSET(ipv4
.tp
);
863 icmp_key
= nla_data(a
[OVS_KEY_ATTR_ICMP
]);
864 swkey
->ipv4
.tp
.src
= htons(icmp_key
->icmp_type
);
865 swkey
->ipv4
.tp
.dst
= htons(icmp_key
->icmp_code
);
872 static int ipv6_flow_from_nlattrs(struct sw_flow_key
*swkey
, int *key_len
,
873 const struct nlattr
*a
[], u32
*attrs
)
875 const struct ovs_key_icmpv6
*icmpv6_key
;
876 const struct ovs_key_tcp
*tcp_key
;
877 const struct ovs_key_udp
*udp_key
;
879 switch (swkey
->ip
.proto
) {
881 if (!(*attrs
& (1 << OVS_KEY_ATTR_TCP
)))
883 *attrs
&= ~(1 << OVS_KEY_ATTR_TCP
);
885 *key_len
= SW_FLOW_KEY_OFFSET(ipv6
.tp
);
886 tcp_key
= nla_data(a
[OVS_KEY_ATTR_TCP
]);
887 swkey
->ipv6
.tp
.src
= tcp_key
->tcp_src
;
888 swkey
->ipv6
.tp
.dst
= tcp_key
->tcp_dst
;
892 if (!(*attrs
& (1 << OVS_KEY_ATTR_UDP
)))
894 *attrs
&= ~(1 << OVS_KEY_ATTR_UDP
);
896 *key_len
= SW_FLOW_KEY_OFFSET(ipv6
.tp
);
897 udp_key
= nla_data(a
[OVS_KEY_ATTR_UDP
]);
898 swkey
->ipv6
.tp
.src
= udp_key
->udp_src
;
899 swkey
->ipv6
.tp
.dst
= udp_key
->udp_dst
;
903 if (!(*attrs
& (1 << OVS_KEY_ATTR_ICMPV6
)))
905 *attrs
&= ~(1 << OVS_KEY_ATTR_ICMPV6
);
907 *key_len
= SW_FLOW_KEY_OFFSET(ipv6
.tp
);
908 icmpv6_key
= nla_data(a
[OVS_KEY_ATTR_ICMPV6
]);
909 swkey
->ipv6
.tp
.src
= htons(icmpv6_key
->icmpv6_type
);
910 swkey
->ipv6
.tp
.dst
= htons(icmpv6_key
->icmpv6_code
);
912 if (swkey
->ipv6
.tp
.src
== htons(NDISC_NEIGHBOUR_SOLICITATION
) ||
913 swkey
->ipv6
.tp
.src
== htons(NDISC_NEIGHBOUR_ADVERTISEMENT
)) {
914 const struct ovs_key_nd
*nd_key
;
916 if (!(*attrs
& (1 << OVS_KEY_ATTR_ND
)))
918 *attrs
&= ~(1 << OVS_KEY_ATTR_ND
);
920 *key_len
= SW_FLOW_KEY_OFFSET(ipv6
.nd
);
921 nd_key
= nla_data(a
[OVS_KEY_ATTR_ND
]);
922 memcpy(&swkey
->ipv6
.nd
.target
, nd_key
->nd_target
,
923 sizeof(swkey
->ipv6
.nd
.target
));
924 memcpy(swkey
->ipv6
.nd
.sll
, nd_key
->nd_sll
, ETH_ALEN
);
925 memcpy(swkey
->ipv6
.nd
.tll
, nd_key
->nd_tll
, ETH_ALEN
);
933 static int parse_flow_nlattrs(const struct nlattr
*attr
,
934 const struct nlattr
*a
[], u32
*attrsp
)
936 const struct nlattr
*nla
;
941 nla_for_each_nested(nla
, attr
, rem
) {
942 u16 type
= nla_type(nla
);
945 if (type
> OVS_KEY_ATTR_MAX
|| attrs
& (1 << type
))
948 expected_len
= ovs_key_lens
[type
];
949 if (nla_len(nla
) != expected_len
&& expected_len
!= -1)
962 int ovs_ipv4_tun_from_nlattr(const struct nlattr
*attr
,
963 struct ovs_key_ipv4_tunnel
*tun_key
)
969 memset(tun_key
, 0, sizeof(*tun_key
));
971 nla_for_each_nested(a
, attr
, rem
) {
972 int type
= nla_type(a
);
973 static const u32 ovs_tunnel_key_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
974 [OVS_TUNNEL_KEY_ATTR_ID
] = sizeof(u64
),
975 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = sizeof(u32
),
976 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = sizeof(u32
),
977 [OVS_TUNNEL_KEY_ATTR_TOS
] = 1,
978 [OVS_TUNNEL_KEY_ATTR_TTL
] = 1,
979 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = 0,
980 [OVS_TUNNEL_KEY_ATTR_CSUM
] = 0,
983 if (type
> OVS_TUNNEL_KEY_ATTR_MAX
||
984 ovs_tunnel_key_lens
[type
] != nla_len(a
))
988 case OVS_TUNNEL_KEY_ATTR_ID
:
989 tun_key
->tun_id
= nla_get_be64(a
);
990 tun_key
->tun_flags
|= TUNNEL_KEY
;
992 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
993 tun_key
->ipv4_src
= nla_get_be32(a
);
995 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
996 tun_key
->ipv4_dst
= nla_get_be32(a
);
998 case OVS_TUNNEL_KEY_ATTR_TOS
:
999 tun_key
->ipv4_tos
= nla_get_u8(a
);
1001 case OVS_TUNNEL_KEY_ATTR_TTL
:
1002 tun_key
->ipv4_ttl
= nla_get_u8(a
);
1005 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
1006 tun_key
->tun_flags
|= TUNNEL_DONT_FRAGMENT
;
1008 case OVS_TUNNEL_KEY_ATTR_CSUM
:
1009 tun_key
->tun_flags
|= TUNNEL_CSUM
;
1019 if (!tun_key
->ipv4_dst
)
1028 int ovs_ipv4_tun_to_nlattr(struct sk_buff
*skb
,
1029 const struct ovs_key_ipv4_tunnel
*tun_key
)
1033 nla
= nla_nest_start(skb
, OVS_KEY_ATTR_TUNNEL
);
1037 if (tun_key
->tun_flags
& TUNNEL_KEY
&&
1038 nla_put_be64(skb
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
))
1040 if (tun_key
->ipv4_src
&&
1041 nla_put_be32(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ipv4_src
))
1043 if (nla_put_be32(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ipv4_dst
))
1045 if (tun_key
->ipv4_tos
&&
1046 nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ipv4_tos
))
1048 if (nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ipv4_ttl
))
1050 if ((tun_key
->tun_flags
& TUNNEL_DONT_FRAGMENT
) &&
1051 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
))
1053 if ((tun_key
->tun_flags
& TUNNEL_CSUM
) &&
1054 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_CSUM
))
1057 nla_nest_end(skb
, nla
);
1062 * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
1063 * @swkey: receives the extracted flow key.
1064 * @key_lenp: number of bytes used in @swkey.
1065 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1068 int ovs_flow_from_nlattrs(struct sw_flow_key
*swkey
, int *key_lenp
,
1069 const struct nlattr
*attr
)
1071 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1];
1072 const struct ovs_key_ethernet
*eth_key
;
1077 memset(swkey
, 0, sizeof(struct sw_flow_key
));
1078 key_len
= SW_FLOW_KEY_OFFSET(eth
);
1080 err
= parse_flow_nlattrs(attr
, a
, &attrs
);
1084 /* Metadata attributes. */
1085 if (attrs
& (1 << OVS_KEY_ATTR_PRIORITY
)) {
1086 swkey
->phy
.priority
= nla_get_u32(a
[OVS_KEY_ATTR_PRIORITY
]);
1087 attrs
&= ~(1 << OVS_KEY_ATTR_PRIORITY
);
1089 if (attrs
& (1 << OVS_KEY_ATTR_IN_PORT
)) {
1090 u32 in_port
= nla_get_u32(a
[OVS_KEY_ATTR_IN_PORT
]);
1091 if (in_port
>= DP_MAX_PORTS
)
1093 swkey
->phy
.in_port
= in_port
;
1094 attrs
&= ~(1 << OVS_KEY_ATTR_IN_PORT
);
1096 swkey
->phy
.in_port
= DP_MAX_PORTS
;
1098 if (attrs
& (1 << OVS_KEY_ATTR_SKB_MARK
)) {
1099 swkey
->phy
.skb_mark
= nla_get_u32(a
[OVS_KEY_ATTR_SKB_MARK
]);
1100 attrs
&= ~(1 << OVS_KEY_ATTR_SKB_MARK
);
1103 if (attrs
& (1 << OVS_KEY_ATTR_TUNNEL
)) {
1104 err
= ovs_ipv4_tun_from_nlattr(a
[OVS_KEY_ATTR_TUNNEL
], &swkey
->tun_key
);
1108 attrs
&= ~(1 << OVS_KEY_ATTR_TUNNEL
);
1111 /* Data attributes. */
1112 if (!(attrs
& (1 << OVS_KEY_ATTR_ETHERNET
)))
1114 attrs
&= ~(1 << OVS_KEY_ATTR_ETHERNET
);
1116 eth_key
= nla_data(a
[OVS_KEY_ATTR_ETHERNET
]);
1117 memcpy(swkey
->eth
.src
, eth_key
->eth_src
, ETH_ALEN
);
1118 memcpy(swkey
->eth
.dst
, eth_key
->eth_dst
, ETH_ALEN
);
1120 if (attrs
& (1u << OVS_KEY_ATTR_ETHERTYPE
) &&
1121 nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]) == htons(ETH_P_8021Q
)) {
1122 const struct nlattr
*encap
;
1125 if (attrs
!= ((1 << OVS_KEY_ATTR_VLAN
) |
1126 (1 << OVS_KEY_ATTR_ETHERTYPE
) |
1127 (1 << OVS_KEY_ATTR_ENCAP
)))
1130 encap
= a
[OVS_KEY_ATTR_ENCAP
];
1131 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
1132 if (tci
& htons(VLAN_TAG_PRESENT
)) {
1133 swkey
->eth
.tci
= tci
;
1135 err
= parse_flow_nlattrs(encap
, a
, &attrs
);
1139 /* Corner case for truncated 802.1Q header. */
1143 swkey
->eth
.type
= htons(ETH_P_8021Q
);
1144 *key_lenp
= key_len
;
1151 if (attrs
& (1 << OVS_KEY_ATTR_ETHERTYPE
)) {
1152 swkey
->eth
.type
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
1153 if (ntohs(swkey
->eth
.type
) < ETH_P_802_3_MIN
)
1155 attrs
&= ~(1 << OVS_KEY_ATTR_ETHERTYPE
);
1157 swkey
->eth
.type
= htons(ETH_P_802_2
);
1160 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
1161 const struct ovs_key_ipv4
*ipv4_key
;
1163 if (!(attrs
& (1 << OVS_KEY_ATTR_IPV4
)))
1165 attrs
&= ~(1 << OVS_KEY_ATTR_IPV4
);
1167 key_len
= SW_FLOW_KEY_OFFSET(ipv4
.addr
);
1168 ipv4_key
= nla_data(a
[OVS_KEY_ATTR_IPV4
]);
1169 if (ipv4_key
->ipv4_frag
> OVS_FRAG_TYPE_MAX
)
1171 swkey
->ip
.proto
= ipv4_key
->ipv4_proto
;
1172 swkey
->ip
.tos
= ipv4_key
->ipv4_tos
;
1173 swkey
->ip
.ttl
= ipv4_key
->ipv4_ttl
;
1174 swkey
->ip
.frag
= ipv4_key
->ipv4_frag
;
1175 swkey
->ipv4
.addr
.src
= ipv4_key
->ipv4_src
;
1176 swkey
->ipv4
.addr
.dst
= ipv4_key
->ipv4_dst
;
1178 if (swkey
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
1179 err
= ipv4_flow_from_nlattrs(swkey
, &key_len
, a
, &attrs
);
1183 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
1184 const struct ovs_key_ipv6
*ipv6_key
;
1186 if (!(attrs
& (1 << OVS_KEY_ATTR_IPV6
)))
1188 attrs
&= ~(1 << OVS_KEY_ATTR_IPV6
);
1190 key_len
= SW_FLOW_KEY_OFFSET(ipv6
.label
);
1191 ipv6_key
= nla_data(a
[OVS_KEY_ATTR_IPV6
]);
1192 if (ipv6_key
->ipv6_frag
> OVS_FRAG_TYPE_MAX
)
1194 swkey
->ipv6
.label
= ipv6_key
->ipv6_label
;
1195 swkey
->ip
.proto
= ipv6_key
->ipv6_proto
;
1196 swkey
->ip
.tos
= ipv6_key
->ipv6_tclass
;
1197 swkey
->ip
.ttl
= ipv6_key
->ipv6_hlimit
;
1198 swkey
->ip
.frag
= ipv6_key
->ipv6_frag
;
1199 memcpy(&swkey
->ipv6
.addr
.src
, ipv6_key
->ipv6_src
,
1200 sizeof(swkey
->ipv6
.addr
.src
));
1201 memcpy(&swkey
->ipv6
.addr
.dst
, ipv6_key
->ipv6_dst
,
1202 sizeof(swkey
->ipv6
.addr
.dst
));
1204 if (swkey
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
1205 err
= ipv6_flow_from_nlattrs(swkey
, &key_len
, a
, &attrs
);
1209 } else if (swkey
->eth
.type
== htons(ETH_P_ARP
) ||
1210 swkey
->eth
.type
== htons(ETH_P_RARP
)) {
1211 const struct ovs_key_arp
*arp_key
;
1213 if (!(attrs
& (1 << OVS_KEY_ATTR_ARP
)))
1215 attrs
&= ~(1 << OVS_KEY_ATTR_ARP
);
1217 key_len
= SW_FLOW_KEY_OFFSET(ipv4
.arp
);
1218 arp_key
= nla_data(a
[OVS_KEY_ATTR_ARP
]);
1219 swkey
->ipv4
.addr
.src
= arp_key
->arp_sip
;
1220 swkey
->ipv4
.addr
.dst
= arp_key
->arp_tip
;
1221 if (arp_key
->arp_op
& htons(0xff00))
1223 swkey
->ip
.proto
= ntohs(arp_key
->arp_op
);
1224 memcpy(swkey
->ipv4
.arp
.sha
, arp_key
->arp_sha
, ETH_ALEN
);
1225 memcpy(swkey
->ipv4
.arp
.tha
, arp_key
->arp_tha
, ETH_ALEN
);
1230 *key_lenp
= key_len
;
1236 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1237 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
1238 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1241 * This parses a series of Netlink attributes that form a flow key, which must
1242 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1243 * get the metadata, that is, the parts of the flow key that cannot be
1244 * extracted from the packet itself.
1246 int ovs_flow_metadata_from_nlattrs(struct sw_flow
*flow
,
1247 const struct nlattr
*attr
)
1249 struct ovs_key_ipv4_tunnel
*tun_key
= &flow
->key
.tun_key
;
1250 const struct nlattr
*nla
;
1253 flow
->key
.phy
.in_port
= DP_MAX_PORTS
;
1254 flow
->key
.phy
.priority
= 0;
1255 flow
->key
.phy
.skb_mark
= 0;
1256 memset(tun_key
, 0, sizeof(flow
->key
.tun_key
));
1258 nla_for_each_nested(nla
, attr
, rem
) {
1259 int type
= nla_type(nla
);
1261 if (type
<= OVS_KEY_ATTR_MAX
&& ovs_key_lens
[type
] > 0) {
1264 if (nla_len(nla
) != ovs_key_lens
[type
])
1268 case OVS_KEY_ATTR_PRIORITY
:
1269 flow
->key
.phy
.priority
= nla_get_u32(nla
);
1272 case OVS_KEY_ATTR_TUNNEL
:
1273 err
= ovs_ipv4_tun_from_nlattr(nla
, tun_key
);
1278 case OVS_KEY_ATTR_IN_PORT
:
1279 if (nla_get_u32(nla
) >= DP_MAX_PORTS
)
1281 flow
->key
.phy
.in_port
= nla_get_u32(nla
);
1284 case OVS_KEY_ATTR_SKB_MARK
:
1285 flow
->key
.phy
.skb_mark
= nla_get_u32(nla
);
1295 int ovs_flow_to_nlattrs(const struct sw_flow_key
*swkey
, struct sk_buff
*skb
)
1297 struct ovs_key_ethernet
*eth_key
;
1298 struct nlattr
*nla
, *encap
;
1300 if (swkey
->phy
.priority
&&
1301 nla_put_u32(skb
, OVS_KEY_ATTR_PRIORITY
, swkey
->phy
.priority
))
1302 goto nla_put_failure
;
1304 if (swkey
->tun_key
.ipv4_dst
&&
1305 ovs_ipv4_tun_to_nlattr(skb
, &swkey
->tun_key
))
1306 goto nla_put_failure
;
1308 if (swkey
->phy
.in_port
!= DP_MAX_PORTS
&&
1309 nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
, swkey
->phy
.in_port
))
1310 goto nla_put_failure
;
1312 if (swkey
->phy
.skb_mark
&&
1313 nla_put_u32(skb
, OVS_KEY_ATTR_SKB_MARK
, swkey
->phy
.skb_mark
))
1314 goto nla_put_failure
;
1316 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ETHERNET
, sizeof(*eth_key
));
1318 goto nla_put_failure
;
1319 eth_key
= nla_data(nla
);
1320 memcpy(eth_key
->eth_src
, swkey
->eth
.src
, ETH_ALEN
);
1321 memcpy(eth_key
->eth_dst
, swkey
->eth
.dst
, ETH_ALEN
);
1323 if (swkey
->eth
.tci
|| swkey
->eth
.type
== htons(ETH_P_8021Q
)) {
1324 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_P_8021Q
)) ||
1325 nla_put_be16(skb
, OVS_KEY_ATTR_VLAN
, swkey
->eth
.tci
))
1326 goto nla_put_failure
;
1327 encap
= nla_nest_start(skb
, OVS_KEY_ATTR_ENCAP
);
1328 if (!swkey
->eth
.tci
)
1334 if (swkey
->eth
.type
== htons(ETH_P_802_2
))
1337 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, swkey
->eth
.type
))
1338 goto nla_put_failure
;
1340 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
1341 struct ovs_key_ipv4
*ipv4_key
;
1343 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV4
, sizeof(*ipv4_key
));
1345 goto nla_put_failure
;
1346 ipv4_key
= nla_data(nla
);
1347 ipv4_key
->ipv4_src
= swkey
->ipv4
.addr
.src
;
1348 ipv4_key
->ipv4_dst
= swkey
->ipv4
.addr
.dst
;
1349 ipv4_key
->ipv4_proto
= swkey
->ip
.proto
;
1350 ipv4_key
->ipv4_tos
= swkey
->ip
.tos
;
1351 ipv4_key
->ipv4_ttl
= swkey
->ip
.ttl
;
1352 ipv4_key
->ipv4_frag
= swkey
->ip
.frag
;
1353 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
1354 struct ovs_key_ipv6
*ipv6_key
;
1356 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV6
, sizeof(*ipv6_key
));
1358 goto nla_put_failure
;
1359 ipv6_key
= nla_data(nla
);
1360 memcpy(ipv6_key
->ipv6_src
, &swkey
->ipv6
.addr
.src
,
1361 sizeof(ipv6_key
->ipv6_src
));
1362 memcpy(ipv6_key
->ipv6_dst
, &swkey
->ipv6
.addr
.dst
,
1363 sizeof(ipv6_key
->ipv6_dst
));
1364 ipv6_key
->ipv6_label
= swkey
->ipv6
.label
;
1365 ipv6_key
->ipv6_proto
= swkey
->ip
.proto
;
1366 ipv6_key
->ipv6_tclass
= swkey
->ip
.tos
;
1367 ipv6_key
->ipv6_hlimit
= swkey
->ip
.ttl
;
1368 ipv6_key
->ipv6_frag
= swkey
->ip
.frag
;
1369 } else if (swkey
->eth
.type
== htons(ETH_P_ARP
) ||
1370 swkey
->eth
.type
== htons(ETH_P_RARP
)) {
1371 struct ovs_key_arp
*arp_key
;
1373 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ARP
, sizeof(*arp_key
));
1375 goto nla_put_failure
;
1376 arp_key
= nla_data(nla
);
1377 memset(arp_key
, 0, sizeof(struct ovs_key_arp
));
1378 arp_key
->arp_sip
= swkey
->ipv4
.addr
.src
;
1379 arp_key
->arp_tip
= swkey
->ipv4
.addr
.dst
;
1380 arp_key
->arp_op
= htons(swkey
->ip
.proto
);
1381 memcpy(arp_key
->arp_sha
, swkey
->ipv4
.arp
.sha
, ETH_ALEN
);
1382 memcpy(arp_key
->arp_tha
, swkey
->ipv4
.arp
.tha
, ETH_ALEN
);
1385 if ((swkey
->eth
.type
== htons(ETH_P_IP
) ||
1386 swkey
->eth
.type
== htons(ETH_P_IPV6
)) &&
1387 swkey
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
1389 if (swkey
->ip
.proto
== IPPROTO_TCP
) {
1390 struct ovs_key_tcp
*tcp_key
;
1392 nla
= nla_reserve(skb
, OVS_KEY_ATTR_TCP
, sizeof(*tcp_key
));
1394 goto nla_put_failure
;
1395 tcp_key
= nla_data(nla
);
1396 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
1397 tcp_key
->tcp_src
= swkey
->ipv4
.tp
.src
;
1398 tcp_key
->tcp_dst
= swkey
->ipv4
.tp
.dst
;
1399 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
1400 tcp_key
->tcp_src
= swkey
->ipv6
.tp
.src
;
1401 tcp_key
->tcp_dst
= swkey
->ipv6
.tp
.dst
;
1403 } else if (swkey
->ip
.proto
== IPPROTO_UDP
) {
1404 struct ovs_key_udp
*udp_key
;
1406 nla
= nla_reserve(skb
, OVS_KEY_ATTR_UDP
, sizeof(*udp_key
));
1408 goto nla_put_failure
;
1409 udp_key
= nla_data(nla
);
1410 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
1411 udp_key
->udp_src
= swkey
->ipv4
.tp
.src
;
1412 udp_key
->udp_dst
= swkey
->ipv4
.tp
.dst
;
1413 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
1414 udp_key
->udp_src
= swkey
->ipv6
.tp
.src
;
1415 udp_key
->udp_dst
= swkey
->ipv6
.tp
.dst
;
1417 } else if (swkey
->eth
.type
== htons(ETH_P_IP
) &&
1418 swkey
->ip
.proto
== IPPROTO_ICMP
) {
1419 struct ovs_key_icmp
*icmp_key
;
1421 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMP
, sizeof(*icmp_key
));
1423 goto nla_put_failure
;
1424 icmp_key
= nla_data(nla
);
1425 icmp_key
->icmp_type
= ntohs(swkey
->ipv4
.tp
.src
);
1426 icmp_key
->icmp_code
= ntohs(swkey
->ipv4
.tp
.dst
);
1427 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
) &&
1428 swkey
->ip
.proto
== IPPROTO_ICMPV6
) {
1429 struct ovs_key_icmpv6
*icmpv6_key
;
1431 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMPV6
,
1432 sizeof(*icmpv6_key
));
1434 goto nla_put_failure
;
1435 icmpv6_key
= nla_data(nla
);
1436 icmpv6_key
->icmpv6_type
= ntohs(swkey
->ipv6
.tp
.src
);
1437 icmpv6_key
->icmpv6_code
= ntohs(swkey
->ipv6
.tp
.dst
);
1439 if (icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_SOLICITATION
||
1440 icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_ADVERTISEMENT
) {
1441 struct ovs_key_nd
*nd_key
;
1443 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ND
, sizeof(*nd_key
));
1445 goto nla_put_failure
;
1446 nd_key
= nla_data(nla
);
1447 memcpy(nd_key
->nd_target
, &swkey
->ipv6
.nd
.target
,
1448 sizeof(nd_key
->nd_target
));
1449 memcpy(nd_key
->nd_sll
, swkey
->ipv6
.nd
.sll
, ETH_ALEN
);
1450 memcpy(nd_key
->nd_tll
, swkey
->ipv6
.nd
.tll
, ETH_ALEN
);
1457 nla_nest_end(skb
, encap
);
1465 /* Initializes the flow module.
1466 * Returns zero if successful or a negative error code. */
1467 int ovs_flow_init(void)
1469 flow_cache
= kmem_cache_create("sw_flow", sizeof(struct sw_flow
), 0,
1471 if (flow_cache
== NULL
)
1477 /* Uninitializes the flow module. */
1478 void ovs_flow_exit(void)
1480 kmem_cache_destroy(flow_cache
);