2 * Copyright (c) 2007-2012 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
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.h>
27 #include <linux/jhash.h>
28 #include <linux/delay.h>
29 #include <linux/time.h>
30 #include <linux/etherdevice.h>
31 #include <linux/genetlink.h>
32 #include <linux/kernel.h>
33 #include <linux/kthread.h>
34 #include <linux/mutex.h>
35 #include <linux/percpu.h>
36 #include <linux/rcupdate.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/ethtool.h>
40 #include <linux/wait.h>
41 #include <asm/div64.h>
42 #include <linux/highmem.h>
43 #include <linux/netfilter_bridge.h>
44 #include <linux/netfilter_ipv4.h>
45 #include <linux/inetdevice.h>
46 #include <linux/list.h>
47 #include <linux/lockdep.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <linux/workqueue.h>
52 #include <net/genetlink.h>
53 #include <net/net_namespace.h>
54 #include <net/netns/generic.h>
58 #include "vport-internal_dev.h"
59 #include "vport-netdev.h"
62 #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
63 static void rehash_flow_table(struct work_struct
*work
);
64 static DECLARE_DELAYED_WORK(rehash_flow_wq
, rehash_flow_table
);
66 int ovs_net_id __read_mostly
;
68 static void ovs_notify(struct sk_buff
*skb
, struct genl_info
*info
,
69 struct genl_multicast_group
*grp
)
71 genl_notify(skb
, genl_info_net(info
), info
->snd_portid
,
72 grp
->id
, info
->nlhdr
, GFP_KERNEL
);
78 * All writes e.g. Writes to device state (add/remove datapath, port, set
79 * operations on vports, etc.), Writes to other state (flow table
80 * modifications, set miscellaneous datapath parameters, etc.) are protected
83 * Reads are protected by RCU.
85 * There are a few special cases (mostly stats) that have their own
86 * synchronization but they nest under all of above and don't interact with
89 * The RTNL lock nests inside ovs_mutex.
92 static DEFINE_MUTEX(ovs_mutex
);
96 mutex_lock(&ovs_mutex
);
101 mutex_unlock(&ovs_mutex
);
104 #ifdef CONFIG_LOCKDEP
105 int lockdep_ovsl_is_held(void)
108 return lockdep_is_held(&ovs_mutex
);
114 static struct vport
*new_vport(const struct vport_parms
*);
115 static int queue_gso_packets(struct net
*, int dp_ifindex
, struct sk_buff
*,
116 const struct dp_upcall_info
*);
117 static int queue_userspace_packet(struct net
*, int dp_ifindex
,
119 const struct dp_upcall_info
*);
121 /* Must be called with rcu_read_lock or ovs_mutex. */
122 static struct datapath
*get_dp(struct net
*net
, int dp_ifindex
)
124 struct datapath
*dp
= NULL
;
125 struct net_device
*dev
;
128 dev
= dev_get_by_index_rcu(net
, dp_ifindex
);
130 struct vport
*vport
= ovs_internal_dev_get_vport(dev
);
139 /* Must be called with rcu_read_lock or ovs_mutex. */
140 const char *ovs_dp_name(const struct datapath
*dp
)
142 struct vport
*vport
= ovs_vport_ovsl_rcu(dp
, OVSP_LOCAL
);
143 return vport
->ops
->get_name(vport
);
146 static int get_dpifindex(struct datapath
*dp
)
153 local
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
155 ifindex
= netdev_vport_priv(local
)->dev
->ifindex
;
164 static void destroy_dp_rcu(struct rcu_head
*rcu
)
166 struct datapath
*dp
= container_of(rcu
, struct datapath
, rcu
);
168 ovs_flow_tbl_destroy((__force
struct flow_table
*)dp
->table
);
169 free_percpu(dp
->stats_percpu
);
170 release_net(ovs_dp_get_net(dp
));
175 static struct hlist_head
*vport_hash_bucket(const struct datapath
*dp
,
178 return &dp
->ports
[port_no
& (DP_VPORT_HASH_BUCKETS
- 1)];
181 struct vport
*ovs_lookup_vport(const struct datapath
*dp
, u16 port_no
)
184 struct hlist_head
*head
;
186 head
= vport_hash_bucket(dp
, port_no
);
187 hlist_for_each_entry_rcu(vport
, head
, dp_hash_node
) {
188 if (vport
->port_no
== port_no
)
194 /* Called with ovs_mutex. */
195 static struct vport
*new_vport(const struct vport_parms
*parms
)
199 vport
= ovs_vport_add(parms
);
200 if (!IS_ERR(vport
)) {
201 struct datapath
*dp
= parms
->dp
;
202 struct hlist_head
*head
= vport_hash_bucket(dp
, vport
->port_no
);
204 hlist_add_head_rcu(&vport
->dp_hash_node
, head
);
209 void ovs_dp_detach_port(struct vport
*p
)
213 /* First drop references to device. */
214 hlist_del_rcu(&p
->dp_hash_node
);
216 /* Then destroy it. */
220 /* Must be called with rcu_read_lock. */
221 void ovs_dp_process_received_packet(struct vport
*p
, struct sk_buff
*skb
)
223 struct datapath
*dp
= p
->dp
;
224 struct sw_flow
*flow
;
225 struct dp_stats_percpu
*stats
;
226 struct sw_flow_key key
;
231 stats
= this_cpu_ptr(dp
->stats_percpu
);
233 /* Extract flow from 'skb' into 'key'. */
234 error
= ovs_flow_extract(skb
, p
->port_no
, &key
, &key_len
);
235 if (unlikely(error
)) {
241 flow
= ovs_flow_tbl_lookup(rcu_dereference(dp
->table
), &key
, key_len
);
242 if (unlikely(!flow
)) {
243 struct dp_upcall_info upcall
;
245 upcall
.cmd
= OVS_PACKET_CMD_MISS
;
247 upcall
.userdata
= NULL
;
248 upcall
.portid
= p
->upcall_portid
;
249 ovs_dp_upcall(dp
, skb
, &upcall
);
251 stats_counter
= &stats
->n_missed
;
255 OVS_CB(skb
)->flow
= flow
;
257 stats_counter
= &stats
->n_hit
;
258 ovs_flow_used(OVS_CB(skb
)->flow
, skb
);
259 ovs_execute_actions(dp
, skb
);
262 /* Update datapath statistics. */
263 u64_stats_update_begin(&stats
->sync
);
265 u64_stats_update_end(&stats
->sync
);
268 static struct genl_family dp_packet_genl_family
= {
269 .id
= GENL_ID_GENERATE
,
270 .hdrsize
= sizeof(struct ovs_header
),
271 .name
= OVS_PACKET_FAMILY
,
272 .version
= OVS_PACKET_VERSION
,
273 .maxattr
= OVS_PACKET_ATTR_MAX
,
275 .parallel_ops
= true,
278 int ovs_dp_upcall(struct datapath
*dp
, struct sk_buff
*skb
,
279 const struct dp_upcall_info
*upcall_info
)
281 struct dp_stats_percpu
*stats
;
285 if (upcall_info
->portid
== 0) {
290 dp_ifindex
= get_dpifindex(dp
);
296 if (!skb_is_gso(skb
))
297 err
= queue_userspace_packet(ovs_dp_get_net(dp
), dp_ifindex
, skb
, upcall_info
);
299 err
= queue_gso_packets(ovs_dp_get_net(dp
), dp_ifindex
, skb
, upcall_info
);
306 stats
= this_cpu_ptr(dp
->stats_percpu
);
308 u64_stats_update_begin(&stats
->sync
);
310 u64_stats_update_end(&stats
->sync
);
315 static int queue_gso_packets(struct net
*net
, int dp_ifindex
,
317 const struct dp_upcall_info
*upcall_info
)
319 unsigned short gso_type
= skb_shinfo(skb
)->gso_type
;
320 struct dp_upcall_info later_info
;
321 struct sw_flow_key later_key
;
322 struct sk_buff
*segs
, *nskb
;
325 segs
= __skb_gso_segment(skb
, NETIF_F_SG
| NETIF_F_HW_CSUM
, false);
327 return PTR_ERR(segs
);
329 /* Queue all of the segments. */
332 err
= queue_userspace_packet(net
, dp_ifindex
, skb
, upcall_info
);
336 if (skb
== segs
&& gso_type
& SKB_GSO_UDP
) {
337 /* The initial flow key extracted by ovs_flow_extract()
338 * in this case is for a first fragment, so we need to
339 * properly mark later fragments.
341 later_key
= *upcall_info
->key
;
342 later_key
.ip
.frag
= OVS_FRAG_TYPE_LATER
;
344 later_info
= *upcall_info
;
345 later_info
.key
= &later_key
;
346 upcall_info
= &later_info
;
348 } while ((skb
= skb
->next
));
350 /* Free all of the segments. */
358 } while ((skb
= nskb
));
362 static size_t key_attr_size(void)
364 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
365 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
366 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
367 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
368 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
369 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
370 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
371 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
372 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
373 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
374 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
375 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
376 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
377 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
378 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
379 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
380 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
381 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
382 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
385 static size_t upcall_msg_size(const struct sk_buff
*skb
,
386 const struct nlattr
*userdata
)
388 size_t size
= NLMSG_ALIGN(sizeof(struct ovs_header
))
389 + nla_total_size(skb
->len
) /* OVS_PACKET_ATTR_PACKET */
390 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
392 /* OVS_PACKET_ATTR_USERDATA */
394 size
+= NLA_ALIGN(userdata
->nla_len
);
399 static int queue_userspace_packet(struct net
*net
, int dp_ifindex
,
401 const struct dp_upcall_info
*upcall_info
)
403 struct ovs_header
*upcall
;
404 struct sk_buff
*nskb
= NULL
;
405 struct sk_buff
*user_skb
; /* to be queued to userspace */
409 if (vlan_tx_tag_present(skb
)) {
410 nskb
= skb_clone(skb
, GFP_ATOMIC
);
414 nskb
= __vlan_put_tag(nskb
, nskb
->vlan_proto
, vlan_tx_tag_get(nskb
));
422 if (nla_attr_size(skb
->len
) > USHRT_MAX
) {
427 user_skb
= genlmsg_new(upcall_msg_size(skb
, upcall_info
->userdata
), GFP_ATOMIC
);
433 upcall
= genlmsg_put(user_skb
, 0, 0, &dp_packet_genl_family
,
434 0, upcall_info
->cmd
);
435 upcall
->dp_ifindex
= dp_ifindex
;
437 nla
= nla_nest_start(user_skb
, OVS_PACKET_ATTR_KEY
);
438 ovs_flow_to_nlattrs(upcall_info
->key
, user_skb
);
439 nla_nest_end(user_skb
, nla
);
441 if (upcall_info
->userdata
)
442 __nla_put(user_skb
, OVS_PACKET_ATTR_USERDATA
,
443 nla_len(upcall_info
->userdata
),
444 nla_data(upcall_info
->userdata
));
446 nla
= __nla_reserve(user_skb
, OVS_PACKET_ATTR_PACKET
, skb
->len
);
448 skb_copy_and_csum_dev(skb
, nla_data(nla
));
450 genlmsg_end(user_skb
, upcall
);
451 err
= genlmsg_unicast(net
, user_skb
, upcall_info
->portid
);
458 /* Called with ovs_mutex. */
459 static int flush_flows(struct datapath
*dp
)
461 struct flow_table
*old_table
;
462 struct flow_table
*new_table
;
464 old_table
= ovsl_dereference(dp
->table
);
465 new_table
= ovs_flow_tbl_alloc(TBL_MIN_BUCKETS
);
469 rcu_assign_pointer(dp
->table
, new_table
);
471 ovs_flow_tbl_deferred_destroy(old_table
);
475 static struct nlattr
*reserve_sfa_size(struct sw_flow_actions
**sfa
, int attr_len
)
478 struct sw_flow_actions
*acts
;
480 int req_size
= NLA_ALIGN(attr_len
);
481 int next_offset
= offsetof(struct sw_flow_actions
, actions
) +
484 if (req_size
<= (ksize(*sfa
) - next_offset
))
487 new_acts_size
= ksize(*sfa
) * 2;
489 if (new_acts_size
> MAX_ACTIONS_BUFSIZE
) {
490 if ((MAX_ACTIONS_BUFSIZE
- next_offset
) < req_size
)
491 return ERR_PTR(-EMSGSIZE
);
492 new_acts_size
= MAX_ACTIONS_BUFSIZE
;
495 acts
= ovs_flow_actions_alloc(new_acts_size
);
499 memcpy(acts
->actions
, (*sfa
)->actions
, (*sfa
)->actions_len
);
500 acts
->actions_len
= (*sfa
)->actions_len
;
505 (*sfa
)->actions_len
+= req_size
;
506 return (struct nlattr
*) ((unsigned char *)(*sfa
) + next_offset
);
509 static int add_action(struct sw_flow_actions
**sfa
, int attrtype
, void *data
, int len
)
513 a
= reserve_sfa_size(sfa
, nla_attr_size(len
));
517 a
->nla_type
= attrtype
;
518 a
->nla_len
= nla_attr_size(len
);
521 memcpy(nla_data(a
), data
, len
);
522 memset((unsigned char *) a
+ a
->nla_len
, 0, nla_padlen(len
));
527 static inline int add_nested_action_start(struct sw_flow_actions
**sfa
, int attrtype
)
529 int used
= (*sfa
)->actions_len
;
532 err
= add_action(sfa
, attrtype
, NULL
, 0);
539 static inline void add_nested_action_end(struct sw_flow_actions
*sfa
, int st_offset
)
541 struct nlattr
*a
= (struct nlattr
*) ((unsigned char *)sfa
->actions
+ st_offset
);
543 a
->nla_len
= sfa
->actions_len
- st_offset
;
546 static int validate_and_copy_actions(const struct nlattr
*attr
,
547 const struct sw_flow_key
*key
, int depth
,
548 struct sw_flow_actions
**sfa
);
550 static int validate_and_copy_sample(const struct nlattr
*attr
,
551 const struct sw_flow_key
*key
, int depth
,
552 struct sw_flow_actions
**sfa
)
554 const struct nlattr
*attrs
[OVS_SAMPLE_ATTR_MAX
+ 1];
555 const struct nlattr
*probability
, *actions
;
556 const struct nlattr
*a
;
557 int rem
, start
, err
, st_acts
;
559 memset(attrs
, 0, sizeof(attrs
));
560 nla_for_each_nested(a
, attr
, rem
) {
561 int type
= nla_type(a
);
562 if (!type
|| type
> OVS_SAMPLE_ATTR_MAX
|| attrs
[type
])
569 probability
= attrs
[OVS_SAMPLE_ATTR_PROBABILITY
];
570 if (!probability
|| nla_len(probability
) != sizeof(u32
))
573 actions
= attrs
[OVS_SAMPLE_ATTR_ACTIONS
];
574 if (!actions
|| (nla_len(actions
) && nla_len(actions
) < NLA_HDRLEN
))
577 /* validation done, copy sample action. */
578 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SAMPLE
);
581 err
= add_action(sfa
, OVS_SAMPLE_ATTR_PROBABILITY
, nla_data(probability
), sizeof(u32
));
584 st_acts
= add_nested_action_start(sfa
, OVS_SAMPLE_ATTR_ACTIONS
);
588 err
= validate_and_copy_actions(actions
, key
, depth
+ 1, sfa
);
592 add_nested_action_end(*sfa
, st_acts
);
593 add_nested_action_end(*sfa
, start
);
598 static int validate_tp_port(const struct sw_flow_key
*flow_key
)
600 if (flow_key
->eth
.type
== htons(ETH_P_IP
)) {
601 if (flow_key
->ipv4
.tp
.src
|| flow_key
->ipv4
.tp
.dst
)
603 } else if (flow_key
->eth
.type
== htons(ETH_P_IPV6
)) {
604 if (flow_key
->ipv6
.tp
.src
|| flow_key
->ipv6
.tp
.dst
)
611 static int validate_and_copy_set_tun(const struct nlattr
*attr
,
612 struct sw_flow_actions
**sfa
)
614 struct ovs_key_ipv4_tunnel tun_key
;
617 err
= ovs_ipv4_tun_from_nlattr(nla_data(attr
), &tun_key
);
621 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SET
);
625 err
= add_action(sfa
, OVS_KEY_ATTR_IPV4_TUNNEL
, &tun_key
, sizeof(tun_key
));
626 add_nested_action_end(*sfa
, start
);
631 static int validate_set(const struct nlattr
*a
,
632 const struct sw_flow_key
*flow_key
,
633 struct sw_flow_actions
**sfa
,
636 const struct nlattr
*ovs_key
= nla_data(a
);
637 int key_type
= nla_type(ovs_key
);
639 /* There can be only one key in a action */
640 if (nla_total_size(nla_len(ovs_key
)) != nla_len(a
))
643 if (key_type
> OVS_KEY_ATTR_MAX
||
644 (ovs_key_lens
[key_type
] != nla_len(ovs_key
) &&
645 ovs_key_lens
[key_type
] != -1))
649 const struct ovs_key_ipv4
*ipv4_key
;
650 const struct ovs_key_ipv6
*ipv6_key
;
653 case OVS_KEY_ATTR_PRIORITY
:
654 case OVS_KEY_ATTR_SKB_MARK
:
655 case OVS_KEY_ATTR_ETHERNET
:
658 case OVS_KEY_ATTR_TUNNEL
:
660 err
= validate_and_copy_set_tun(a
, sfa
);
665 case OVS_KEY_ATTR_IPV4
:
666 if (flow_key
->eth
.type
!= htons(ETH_P_IP
))
669 if (!flow_key
->ip
.proto
)
672 ipv4_key
= nla_data(ovs_key
);
673 if (ipv4_key
->ipv4_proto
!= flow_key
->ip
.proto
)
676 if (ipv4_key
->ipv4_frag
!= flow_key
->ip
.frag
)
681 case OVS_KEY_ATTR_IPV6
:
682 if (flow_key
->eth
.type
!= htons(ETH_P_IPV6
))
685 if (!flow_key
->ip
.proto
)
688 ipv6_key
= nla_data(ovs_key
);
689 if (ipv6_key
->ipv6_proto
!= flow_key
->ip
.proto
)
692 if (ipv6_key
->ipv6_frag
!= flow_key
->ip
.frag
)
695 if (ntohl(ipv6_key
->ipv6_label
) & 0xFFF00000)
700 case OVS_KEY_ATTR_TCP
:
701 if (flow_key
->ip
.proto
!= IPPROTO_TCP
)
704 return validate_tp_port(flow_key
);
706 case OVS_KEY_ATTR_UDP
:
707 if (flow_key
->ip
.proto
!= IPPROTO_UDP
)
710 return validate_tp_port(flow_key
);
719 static int validate_userspace(const struct nlattr
*attr
)
721 static const struct nla_policy userspace_policy
[OVS_USERSPACE_ATTR_MAX
+ 1] = {
722 [OVS_USERSPACE_ATTR_PID
] = {.type
= NLA_U32
},
723 [OVS_USERSPACE_ATTR_USERDATA
] = {.type
= NLA_UNSPEC
},
725 struct nlattr
*a
[OVS_USERSPACE_ATTR_MAX
+ 1];
728 error
= nla_parse_nested(a
, OVS_USERSPACE_ATTR_MAX
,
729 attr
, userspace_policy
);
733 if (!a
[OVS_USERSPACE_ATTR_PID
] ||
734 !nla_get_u32(a
[OVS_USERSPACE_ATTR_PID
]))
740 static int copy_action(const struct nlattr
*from
,
741 struct sw_flow_actions
**sfa
)
743 int totlen
= NLA_ALIGN(from
->nla_len
);
746 to
= reserve_sfa_size(sfa
, from
->nla_len
);
750 memcpy(to
, from
, totlen
);
754 static int validate_and_copy_actions(const struct nlattr
*attr
,
755 const struct sw_flow_key
*key
,
757 struct sw_flow_actions
**sfa
)
759 const struct nlattr
*a
;
762 if (depth
>= SAMPLE_ACTION_DEPTH
)
765 nla_for_each_nested(a
, attr
, rem
) {
766 /* Expected argument lengths, (u32)-1 for variable length. */
767 static const u32 action_lens
[OVS_ACTION_ATTR_MAX
+ 1] = {
768 [OVS_ACTION_ATTR_OUTPUT
] = sizeof(u32
),
769 [OVS_ACTION_ATTR_USERSPACE
] = (u32
)-1,
770 [OVS_ACTION_ATTR_PUSH_VLAN
] = sizeof(struct ovs_action_push_vlan
),
771 [OVS_ACTION_ATTR_POP_VLAN
] = 0,
772 [OVS_ACTION_ATTR_SET
] = (u32
)-1,
773 [OVS_ACTION_ATTR_SAMPLE
] = (u32
)-1
775 const struct ovs_action_push_vlan
*vlan
;
776 int type
= nla_type(a
);
779 if (type
> OVS_ACTION_ATTR_MAX
||
780 (action_lens
[type
] != nla_len(a
) &&
781 action_lens
[type
] != (u32
)-1))
786 case OVS_ACTION_ATTR_UNSPEC
:
789 case OVS_ACTION_ATTR_USERSPACE
:
790 err
= validate_userspace(a
);
795 case OVS_ACTION_ATTR_OUTPUT
:
796 if (nla_get_u32(a
) >= DP_MAX_PORTS
)
801 case OVS_ACTION_ATTR_POP_VLAN
:
804 case OVS_ACTION_ATTR_PUSH_VLAN
:
806 if (vlan
->vlan_tpid
!= htons(ETH_P_8021Q
))
808 if (!(vlan
->vlan_tci
& htons(VLAN_TAG_PRESENT
)))
812 case OVS_ACTION_ATTR_SET
:
813 err
= validate_set(a
, key
, sfa
, &skip_copy
);
818 case OVS_ACTION_ATTR_SAMPLE
:
819 err
= validate_and_copy_sample(a
, key
, depth
, sfa
);
829 err
= copy_action(a
, sfa
);
841 static void clear_stats(struct sw_flow
*flow
)
845 flow
->packet_count
= 0;
846 flow
->byte_count
= 0;
849 static int ovs_packet_cmd_execute(struct sk_buff
*skb
, struct genl_info
*info
)
851 struct ovs_header
*ovs_header
= info
->userhdr
;
852 struct nlattr
**a
= info
->attrs
;
853 struct sw_flow_actions
*acts
;
854 struct sk_buff
*packet
;
855 struct sw_flow
*flow
;
863 if (!a
[OVS_PACKET_ATTR_PACKET
] || !a
[OVS_PACKET_ATTR_KEY
] ||
864 !a
[OVS_PACKET_ATTR_ACTIONS
])
867 len
= nla_len(a
[OVS_PACKET_ATTR_PACKET
]);
868 packet
= __dev_alloc_skb(NET_IP_ALIGN
+ len
, GFP_KERNEL
);
872 skb_reserve(packet
, NET_IP_ALIGN
);
874 nla_memcpy(__skb_put(packet
, len
), a
[OVS_PACKET_ATTR_PACKET
], len
);
876 skb_reset_mac_header(packet
);
877 eth
= eth_hdr(packet
);
879 /* Normally, setting the skb 'protocol' field would be handled by a
880 * call to eth_type_trans(), but it assumes there's a sending
881 * device, which we may not have. */
882 if (ntohs(eth
->h_proto
) >= ETH_P_802_3_MIN
)
883 packet
->protocol
= eth
->h_proto
;
885 packet
->protocol
= htons(ETH_P_802_2
);
887 /* Build an sw_flow for sending this packet. */
888 flow
= ovs_flow_alloc();
893 err
= ovs_flow_extract(packet
, -1, &flow
->key
, &key_len
);
897 err
= ovs_flow_metadata_from_nlattrs(flow
, key_len
, a
[OVS_PACKET_ATTR_KEY
]);
900 acts
= ovs_flow_actions_alloc(nla_len(a
[OVS_PACKET_ATTR_ACTIONS
]));
905 err
= validate_and_copy_actions(a
[OVS_PACKET_ATTR_ACTIONS
], &flow
->key
, 0, &acts
);
906 rcu_assign_pointer(flow
->sf_acts
, acts
);
910 OVS_CB(packet
)->flow
= flow
;
911 packet
->priority
= flow
->key
.phy
.priority
;
912 packet
->mark
= flow
->key
.phy
.skb_mark
;
915 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
921 err
= ovs_execute_actions(dp
, packet
);
938 static const struct nla_policy packet_policy
[OVS_PACKET_ATTR_MAX
+ 1] = {
939 [OVS_PACKET_ATTR_PACKET
] = { .len
= ETH_HLEN
},
940 [OVS_PACKET_ATTR_KEY
] = { .type
= NLA_NESTED
},
941 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
944 static struct genl_ops dp_packet_genl_ops
[] = {
945 { .cmd
= OVS_PACKET_CMD_EXECUTE
,
946 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
947 .policy
= packet_policy
,
948 .doit
= ovs_packet_cmd_execute
952 static void get_dp_stats(struct datapath
*dp
, struct ovs_dp_stats
*stats
)
955 struct flow_table
*table
= ovsl_dereference(dp
->table
);
957 stats
->n_flows
= ovs_flow_tbl_count(table
);
959 stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
960 for_each_possible_cpu(i
) {
961 const struct dp_stats_percpu
*percpu_stats
;
962 struct dp_stats_percpu local_stats
;
965 percpu_stats
= per_cpu_ptr(dp
->stats_percpu
, i
);
968 start
= u64_stats_fetch_begin_bh(&percpu_stats
->sync
);
969 local_stats
= *percpu_stats
;
970 } while (u64_stats_fetch_retry_bh(&percpu_stats
->sync
, start
));
972 stats
->n_hit
+= local_stats
.n_hit
;
973 stats
->n_missed
+= local_stats
.n_missed
;
974 stats
->n_lost
+= local_stats
.n_lost
;
978 static const struct nla_policy flow_policy
[OVS_FLOW_ATTR_MAX
+ 1] = {
979 [OVS_FLOW_ATTR_KEY
] = { .type
= NLA_NESTED
},
980 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
981 [OVS_FLOW_ATTR_CLEAR
] = { .type
= NLA_FLAG
},
984 static struct genl_family dp_flow_genl_family
= {
985 .id
= GENL_ID_GENERATE
,
986 .hdrsize
= sizeof(struct ovs_header
),
987 .name
= OVS_FLOW_FAMILY
,
988 .version
= OVS_FLOW_VERSION
,
989 .maxattr
= OVS_FLOW_ATTR_MAX
,
991 .parallel_ops
= true,
994 static struct genl_multicast_group ovs_dp_flow_multicast_group
= {
995 .name
= OVS_FLOW_MCGROUP
998 static int actions_to_attr(const struct nlattr
*attr
, int len
, struct sk_buff
*skb
);
999 static int sample_action_to_attr(const struct nlattr
*attr
, struct sk_buff
*skb
)
1001 const struct nlattr
*a
;
1002 struct nlattr
*start
;
1005 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_SAMPLE
);
1009 nla_for_each_nested(a
, attr
, rem
) {
1010 int type
= nla_type(a
);
1011 struct nlattr
*st_sample
;
1014 case OVS_SAMPLE_ATTR_PROBABILITY
:
1015 if (nla_put(skb
, OVS_SAMPLE_ATTR_PROBABILITY
, sizeof(u32
), nla_data(a
)))
1018 case OVS_SAMPLE_ATTR_ACTIONS
:
1019 st_sample
= nla_nest_start(skb
, OVS_SAMPLE_ATTR_ACTIONS
);
1022 err
= actions_to_attr(nla_data(a
), nla_len(a
), skb
);
1025 nla_nest_end(skb
, st_sample
);
1030 nla_nest_end(skb
, start
);
1034 static int set_action_to_attr(const struct nlattr
*a
, struct sk_buff
*skb
)
1036 const struct nlattr
*ovs_key
= nla_data(a
);
1037 int key_type
= nla_type(ovs_key
);
1038 struct nlattr
*start
;
1042 case OVS_KEY_ATTR_IPV4_TUNNEL
:
1043 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_SET
);
1047 err
= ovs_ipv4_tun_to_nlattr(skb
, nla_data(ovs_key
));
1050 nla_nest_end(skb
, start
);
1053 if (nla_put(skb
, OVS_ACTION_ATTR_SET
, nla_len(a
), ovs_key
))
1061 static int actions_to_attr(const struct nlattr
*attr
, int len
, struct sk_buff
*skb
)
1063 const struct nlattr
*a
;
1066 nla_for_each_attr(a
, attr
, len
, rem
) {
1067 int type
= nla_type(a
);
1070 case OVS_ACTION_ATTR_SET
:
1071 err
= set_action_to_attr(a
, skb
);
1076 case OVS_ACTION_ATTR_SAMPLE
:
1077 err
= sample_action_to_attr(a
, skb
);
1082 if (nla_put(skb
, type
, nla_len(a
), nla_data(a
)))
1091 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions
*acts
)
1093 return NLMSG_ALIGN(sizeof(struct ovs_header
))
1094 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
1095 + nla_total_size(sizeof(struct ovs_flow_stats
)) /* OVS_FLOW_ATTR_STATS */
1096 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
1097 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
1098 + nla_total_size(acts
->actions_len
); /* OVS_FLOW_ATTR_ACTIONS */
1101 /* Called with ovs_mutex. */
1102 static int ovs_flow_cmd_fill_info(struct sw_flow
*flow
, struct datapath
*dp
,
1103 struct sk_buff
*skb
, u32 portid
,
1104 u32 seq
, u32 flags
, u8 cmd
)
1106 const int skb_orig_len
= skb
->len
;
1107 const struct sw_flow_actions
*sf_acts
;
1108 struct nlattr
*start
;
1109 struct ovs_flow_stats stats
;
1110 struct ovs_header
*ovs_header
;
1116 sf_acts
= ovsl_dereference(flow
->sf_acts
);
1118 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_flow_genl_family
, flags
, cmd
);
1122 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
1124 nla
= nla_nest_start(skb
, OVS_FLOW_ATTR_KEY
);
1126 goto nla_put_failure
;
1127 err
= ovs_flow_to_nlattrs(&flow
->key
, skb
);
1130 nla_nest_end(skb
, nla
);
1132 spin_lock_bh(&flow
->lock
);
1134 stats
.n_packets
= flow
->packet_count
;
1135 stats
.n_bytes
= flow
->byte_count
;
1136 tcp_flags
= flow
->tcp_flags
;
1137 spin_unlock_bh(&flow
->lock
);
1140 nla_put_u64(skb
, OVS_FLOW_ATTR_USED
, ovs_flow_used_time(used
)))
1141 goto nla_put_failure
;
1143 if (stats
.n_packets
&&
1144 nla_put(skb
, OVS_FLOW_ATTR_STATS
,
1145 sizeof(struct ovs_flow_stats
), &stats
))
1146 goto nla_put_failure
;
1149 nla_put_u8(skb
, OVS_FLOW_ATTR_TCP_FLAGS
, tcp_flags
))
1150 goto nla_put_failure
;
1152 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
1153 * this is the first flow to be dumped into 'skb'. This is unusual for
1154 * Netlink but individual action lists can be longer than
1155 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1156 * The userspace caller can always fetch the actions separately if it
1157 * really wants them. (Most userspace callers in fact don't care.)
1159 * This can only fail for dump operations because the skb is always
1160 * properly sized for single flows.
1162 start
= nla_nest_start(skb
, OVS_FLOW_ATTR_ACTIONS
);
1164 err
= actions_to_attr(sf_acts
->actions
, sf_acts
->actions_len
, skb
);
1166 nla_nest_end(skb
, start
);
1171 nla_nest_cancel(skb
, start
);
1173 } else if (skb_orig_len
)
1174 goto nla_put_failure
;
1176 return genlmsg_end(skb
, ovs_header
);
1181 genlmsg_cancel(skb
, ovs_header
);
1185 static struct sk_buff
*ovs_flow_cmd_alloc_info(struct sw_flow
*flow
)
1187 const struct sw_flow_actions
*sf_acts
;
1189 sf_acts
= ovsl_dereference(flow
->sf_acts
);
1191 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts
), GFP_KERNEL
);
1194 static struct sk_buff
*ovs_flow_cmd_build_info(struct sw_flow
*flow
,
1195 struct datapath
*dp
,
1196 u32 portid
, u32 seq
, u8 cmd
)
1198 struct sk_buff
*skb
;
1201 skb
= ovs_flow_cmd_alloc_info(flow
);
1203 return ERR_PTR(-ENOMEM
);
1205 retval
= ovs_flow_cmd_fill_info(flow
, dp
, skb
, portid
, seq
, 0, cmd
);
1210 static int ovs_flow_cmd_new_or_set(struct sk_buff
*skb
, struct genl_info
*info
)
1212 struct nlattr
**a
= info
->attrs
;
1213 struct ovs_header
*ovs_header
= info
->userhdr
;
1214 struct sw_flow_key key
;
1215 struct sw_flow
*flow
;
1216 struct sk_buff
*reply
;
1217 struct datapath
*dp
;
1218 struct flow_table
*table
;
1219 struct sw_flow_actions
*acts
= NULL
;
1225 if (!a
[OVS_FLOW_ATTR_KEY
])
1227 error
= ovs_flow_from_nlattrs(&key
, &key_len
, a
[OVS_FLOW_ATTR_KEY
]);
1231 /* Validate actions. */
1232 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
1233 acts
= ovs_flow_actions_alloc(nla_len(a
[OVS_FLOW_ATTR_ACTIONS
]));
1234 error
= PTR_ERR(acts
);
1238 error
= validate_and_copy_actions(a
[OVS_FLOW_ATTR_ACTIONS
], &key
, 0, &acts
);
1241 } else if (info
->genlhdr
->cmd
== OVS_FLOW_CMD_NEW
) {
1247 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1250 goto err_unlock_ovs
;
1252 table
= ovsl_dereference(dp
->table
);
1253 flow
= ovs_flow_tbl_lookup(table
, &key
, key_len
);
1255 /* Bail out if we're not allowed to create a new flow. */
1257 if (info
->genlhdr
->cmd
== OVS_FLOW_CMD_SET
)
1258 goto err_unlock_ovs
;
1260 /* Expand table, if necessary, to make room. */
1261 if (ovs_flow_tbl_need_to_expand(table
)) {
1262 struct flow_table
*new_table
;
1264 new_table
= ovs_flow_tbl_expand(table
);
1265 if (!IS_ERR(new_table
)) {
1266 rcu_assign_pointer(dp
->table
, new_table
);
1267 ovs_flow_tbl_deferred_destroy(table
);
1268 table
= ovsl_dereference(dp
->table
);
1272 /* Allocate flow. */
1273 flow
= ovs_flow_alloc();
1275 error
= PTR_ERR(flow
);
1276 goto err_unlock_ovs
;
1280 rcu_assign_pointer(flow
->sf_acts
, acts
);
1282 /* Put flow in bucket. */
1283 ovs_flow_tbl_insert(table
, flow
, &key
, key_len
);
1285 reply
= ovs_flow_cmd_build_info(flow
, dp
, info
->snd_portid
,
1289 /* We found a matching flow. */
1290 struct sw_flow_actions
*old_acts
;
1292 /* Bail out if we're not allowed to modify an existing flow.
1293 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1294 * because Generic Netlink treats the latter as a dump
1295 * request. We also accept NLM_F_EXCL in case that bug ever
1299 if (info
->genlhdr
->cmd
== OVS_FLOW_CMD_NEW
&&
1300 info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
| NLM_F_EXCL
))
1301 goto err_unlock_ovs
;
1303 /* Update actions. */
1304 old_acts
= ovsl_dereference(flow
->sf_acts
);
1305 rcu_assign_pointer(flow
->sf_acts
, acts
);
1306 ovs_flow_deferred_free_acts(old_acts
);
1308 reply
= ovs_flow_cmd_build_info(flow
, dp
, info
->snd_portid
,
1309 info
->snd_seq
, OVS_FLOW_CMD_NEW
);
1312 if (a
[OVS_FLOW_ATTR_CLEAR
]) {
1313 spin_lock_bh(&flow
->lock
);
1315 spin_unlock_bh(&flow
->lock
);
1321 ovs_notify(reply
, info
, &ovs_dp_flow_multicast_group
);
1323 netlink_set_err(sock_net(skb
->sk
)->genl_sock
, 0,
1324 ovs_dp_flow_multicast_group
.id
, PTR_ERR(reply
));
1335 static int ovs_flow_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1337 struct nlattr
**a
= info
->attrs
;
1338 struct ovs_header
*ovs_header
= info
->userhdr
;
1339 struct sw_flow_key key
;
1340 struct sk_buff
*reply
;
1341 struct sw_flow
*flow
;
1342 struct datapath
*dp
;
1343 struct flow_table
*table
;
1347 if (!a
[OVS_FLOW_ATTR_KEY
])
1349 err
= ovs_flow_from_nlattrs(&key
, &key_len
, a
[OVS_FLOW_ATTR_KEY
]);
1354 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1360 table
= ovsl_dereference(dp
->table
);
1361 flow
= ovs_flow_tbl_lookup(table
, &key
, key_len
);
1367 reply
= ovs_flow_cmd_build_info(flow
, dp
, info
->snd_portid
,
1368 info
->snd_seq
, OVS_FLOW_CMD_NEW
);
1369 if (IS_ERR(reply
)) {
1370 err
= PTR_ERR(reply
);
1375 return genlmsg_reply(reply
, info
);
1381 static int ovs_flow_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1383 struct nlattr
**a
= info
->attrs
;
1384 struct ovs_header
*ovs_header
= info
->userhdr
;
1385 struct sw_flow_key key
;
1386 struct sk_buff
*reply
;
1387 struct sw_flow
*flow
;
1388 struct datapath
*dp
;
1389 struct flow_table
*table
;
1394 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1400 if (!a
[OVS_FLOW_ATTR_KEY
]) {
1401 err
= flush_flows(dp
);
1404 err
= ovs_flow_from_nlattrs(&key
, &key_len
, a
[OVS_FLOW_ATTR_KEY
]);
1408 table
= ovsl_dereference(dp
->table
);
1409 flow
= ovs_flow_tbl_lookup(table
, &key
, key_len
);
1415 reply
= ovs_flow_cmd_alloc_info(flow
);
1421 ovs_flow_tbl_remove(table
, flow
);
1423 err
= ovs_flow_cmd_fill_info(flow
, dp
, reply
, info
->snd_portid
,
1424 info
->snd_seq
, 0, OVS_FLOW_CMD_DEL
);
1427 ovs_flow_deferred_free(flow
);
1430 ovs_notify(reply
, info
, &ovs_dp_flow_multicast_group
);
1437 static int ovs_flow_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1439 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1440 struct datapath
*dp
;
1441 struct flow_table
*table
;
1444 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1450 table
= ovsl_dereference(dp
->table
);
1453 struct sw_flow
*flow
;
1456 bucket
= cb
->args
[0];
1458 flow
= ovs_flow_tbl_next(table
, &bucket
, &obj
);
1462 if (ovs_flow_cmd_fill_info(flow
, dp
, skb
,
1463 NETLINK_CB(cb
->skb
).portid
,
1464 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1465 OVS_FLOW_CMD_NEW
) < 0)
1468 cb
->args
[0] = bucket
;
1475 static struct genl_ops dp_flow_genl_ops
[] = {
1476 { .cmd
= OVS_FLOW_CMD_NEW
,
1477 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1478 .policy
= flow_policy
,
1479 .doit
= ovs_flow_cmd_new_or_set
1481 { .cmd
= OVS_FLOW_CMD_DEL
,
1482 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1483 .policy
= flow_policy
,
1484 .doit
= ovs_flow_cmd_del
1486 { .cmd
= OVS_FLOW_CMD_GET
,
1487 .flags
= 0, /* OK for unprivileged users. */
1488 .policy
= flow_policy
,
1489 .doit
= ovs_flow_cmd_get
,
1490 .dumpit
= ovs_flow_cmd_dump
1492 { .cmd
= OVS_FLOW_CMD_SET
,
1493 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1494 .policy
= flow_policy
,
1495 .doit
= ovs_flow_cmd_new_or_set
,
1499 static const struct nla_policy datapath_policy
[OVS_DP_ATTR_MAX
+ 1] = {
1500 [OVS_DP_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1501 [OVS_DP_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1504 static struct genl_family dp_datapath_genl_family
= {
1505 .id
= GENL_ID_GENERATE
,
1506 .hdrsize
= sizeof(struct ovs_header
),
1507 .name
= OVS_DATAPATH_FAMILY
,
1508 .version
= OVS_DATAPATH_VERSION
,
1509 .maxattr
= OVS_DP_ATTR_MAX
,
1511 .parallel_ops
= true,
1514 static struct genl_multicast_group ovs_dp_datapath_multicast_group
= {
1515 .name
= OVS_DATAPATH_MCGROUP
1518 static size_t ovs_dp_cmd_msg_size(void)
1520 size_t msgsize
= NLMSG_ALIGN(sizeof(struct ovs_header
));
1522 msgsize
+= nla_total_size(IFNAMSIZ
);
1523 msgsize
+= nla_total_size(sizeof(struct ovs_dp_stats
));
1528 static int ovs_dp_cmd_fill_info(struct datapath
*dp
, struct sk_buff
*skb
,
1529 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1531 struct ovs_header
*ovs_header
;
1532 struct ovs_dp_stats dp_stats
;
1535 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_datapath_genl_family
,
1540 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
1543 err
= nla_put_string(skb
, OVS_DP_ATTR_NAME
, ovs_dp_name(dp
));
1546 goto nla_put_failure
;
1548 get_dp_stats(dp
, &dp_stats
);
1549 if (nla_put(skb
, OVS_DP_ATTR_STATS
, sizeof(struct ovs_dp_stats
), &dp_stats
))
1550 goto nla_put_failure
;
1552 return genlmsg_end(skb
, ovs_header
);
1555 genlmsg_cancel(skb
, ovs_header
);
1560 static struct sk_buff
*ovs_dp_cmd_build_info(struct datapath
*dp
, u32 portid
,
1563 struct sk_buff
*skb
;
1566 skb
= genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL
);
1568 return ERR_PTR(-ENOMEM
);
1570 retval
= ovs_dp_cmd_fill_info(dp
, skb
, portid
, seq
, 0, cmd
);
1573 return ERR_PTR(retval
);
1578 /* Called with ovs_mutex. */
1579 static struct datapath
*lookup_datapath(struct net
*net
,
1580 struct ovs_header
*ovs_header
,
1581 struct nlattr
*a
[OVS_DP_ATTR_MAX
+ 1])
1583 struct datapath
*dp
;
1585 if (!a
[OVS_DP_ATTR_NAME
])
1586 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1588 struct vport
*vport
;
1591 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_DP_ATTR_NAME
]));
1592 dp
= vport
&& vport
->port_no
== OVSP_LOCAL
? vport
->dp
: NULL
;
1595 return dp
? dp
: ERR_PTR(-ENODEV
);
1598 static int ovs_dp_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1600 struct nlattr
**a
= info
->attrs
;
1601 struct vport_parms parms
;
1602 struct sk_buff
*reply
;
1603 struct datapath
*dp
;
1604 struct vport
*vport
;
1605 struct ovs_net
*ovs_net
;
1609 if (!a
[OVS_DP_ATTR_NAME
] || !a
[OVS_DP_ATTR_UPCALL_PID
])
1615 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1617 goto err_unlock_ovs
;
1619 ovs_dp_set_net(dp
, hold_net(sock_net(skb
->sk
)));
1621 /* Allocate table. */
1623 rcu_assign_pointer(dp
->table
, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS
));
1627 dp
->stats_percpu
= alloc_percpu(struct dp_stats_percpu
);
1628 if (!dp
->stats_percpu
) {
1630 goto err_destroy_table
;
1633 dp
->ports
= kmalloc(DP_VPORT_HASH_BUCKETS
* sizeof(struct hlist_head
),
1637 goto err_destroy_percpu
;
1640 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
1641 INIT_HLIST_HEAD(&dp
->ports
[i
]);
1643 /* Set up our datapath device. */
1644 parms
.name
= nla_data(a
[OVS_DP_ATTR_NAME
]);
1645 parms
.type
= OVS_VPORT_TYPE_INTERNAL
;
1646 parms
.options
= NULL
;
1648 parms
.port_no
= OVSP_LOCAL
;
1649 parms
.upcall_portid
= nla_get_u32(a
[OVS_DP_ATTR_UPCALL_PID
]);
1651 vport
= new_vport(&parms
);
1652 if (IS_ERR(vport
)) {
1653 err
= PTR_ERR(vport
);
1657 goto err_destroy_ports_array
;
1660 reply
= ovs_dp_cmd_build_info(dp
, info
->snd_portid
,
1661 info
->snd_seq
, OVS_DP_CMD_NEW
);
1662 err
= PTR_ERR(reply
);
1664 goto err_destroy_local_port
;
1666 ovs_net
= net_generic(ovs_dp_get_net(dp
), ovs_net_id
);
1667 list_add_tail(&dp
->list_node
, &ovs_net
->dps
);
1671 ovs_notify(reply
, info
, &ovs_dp_datapath_multicast_group
);
1674 err_destroy_local_port
:
1675 ovs_dp_detach_port(ovs_vport_ovsl(dp
, OVSP_LOCAL
));
1676 err_destroy_ports_array
:
1679 free_percpu(dp
->stats_percpu
);
1681 ovs_flow_tbl_destroy(ovsl_dereference(dp
->table
));
1683 release_net(ovs_dp_get_net(dp
));
1691 /* Called with ovs_mutex. */
1692 static void __dp_destroy(struct datapath
*dp
)
1696 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1697 struct vport
*vport
;
1698 struct hlist_node
*n
;
1700 hlist_for_each_entry_safe(vport
, n
, &dp
->ports
[i
], dp_hash_node
)
1701 if (vport
->port_no
!= OVSP_LOCAL
)
1702 ovs_dp_detach_port(vport
);
1705 list_del(&dp
->list_node
);
1707 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1708 * all port in datapath are destroyed first before freeing datapath.
1710 ovs_dp_detach_port(ovs_vport_ovsl(dp
, OVSP_LOCAL
));
1712 call_rcu(&dp
->rcu
, destroy_dp_rcu
);
1715 static int ovs_dp_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1717 struct sk_buff
*reply
;
1718 struct datapath
*dp
;
1722 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1727 reply
= ovs_dp_cmd_build_info(dp
, info
->snd_portid
,
1728 info
->snd_seq
, OVS_DP_CMD_DEL
);
1729 err
= PTR_ERR(reply
);
1736 ovs_notify(reply
, info
, &ovs_dp_datapath_multicast_group
);
1744 static int ovs_dp_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1746 struct sk_buff
*reply
;
1747 struct datapath
*dp
;
1751 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1756 reply
= ovs_dp_cmd_build_info(dp
, info
->snd_portid
,
1757 info
->snd_seq
, OVS_DP_CMD_NEW
);
1758 if (IS_ERR(reply
)) {
1759 err
= PTR_ERR(reply
);
1760 netlink_set_err(sock_net(skb
->sk
)->genl_sock
, 0,
1761 ovs_dp_datapath_multicast_group
.id
, err
);
1767 ovs_notify(reply
, info
, &ovs_dp_datapath_multicast_group
);
1775 static int ovs_dp_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1777 struct sk_buff
*reply
;
1778 struct datapath
*dp
;
1782 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1788 reply
= ovs_dp_cmd_build_info(dp
, info
->snd_portid
,
1789 info
->snd_seq
, OVS_DP_CMD_NEW
);
1790 if (IS_ERR(reply
)) {
1791 err
= PTR_ERR(reply
);
1796 return genlmsg_reply(reply
, info
);
1803 static int ovs_dp_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1805 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
1806 struct datapath
*dp
;
1807 int skip
= cb
->args
[0];
1811 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
1813 ovs_dp_cmd_fill_info(dp
, skb
, NETLINK_CB(cb
->skb
).portid
,
1814 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1815 OVS_DP_CMD_NEW
) < 0)
1826 static struct genl_ops dp_datapath_genl_ops
[] = {
1827 { .cmd
= OVS_DP_CMD_NEW
,
1828 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1829 .policy
= datapath_policy
,
1830 .doit
= ovs_dp_cmd_new
1832 { .cmd
= OVS_DP_CMD_DEL
,
1833 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1834 .policy
= datapath_policy
,
1835 .doit
= ovs_dp_cmd_del
1837 { .cmd
= OVS_DP_CMD_GET
,
1838 .flags
= 0, /* OK for unprivileged users. */
1839 .policy
= datapath_policy
,
1840 .doit
= ovs_dp_cmd_get
,
1841 .dumpit
= ovs_dp_cmd_dump
1843 { .cmd
= OVS_DP_CMD_SET
,
1844 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1845 .policy
= datapath_policy
,
1846 .doit
= ovs_dp_cmd_set
,
1850 static const struct nla_policy vport_policy
[OVS_VPORT_ATTR_MAX
+ 1] = {
1851 [OVS_VPORT_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1852 [OVS_VPORT_ATTR_STATS
] = { .len
= sizeof(struct ovs_vport_stats
) },
1853 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NLA_U32
},
1854 [OVS_VPORT_ATTR_TYPE
] = { .type
= NLA_U32
},
1855 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1856 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NLA_NESTED
},
1859 static struct genl_family dp_vport_genl_family
= {
1860 .id
= GENL_ID_GENERATE
,
1861 .hdrsize
= sizeof(struct ovs_header
),
1862 .name
= OVS_VPORT_FAMILY
,
1863 .version
= OVS_VPORT_VERSION
,
1864 .maxattr
= OVS_VPORT_ATTR_MAX
,
1866 .parallel_ops
= true,
1869 struct genl_multicast_group ovs_dp_vport_multicast_group
= {
1870 .name
= OVS_VPORT_MCGROUP
1873 /* Called with ovs_mutex or RCU read lock. */
1874 static int ovs_vport_cmd_fill_info(struct vport
*vport
, struct sk_buff
*skb
,
1875 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1877 struct ovs_header
*ovs_header
;
1878 struct ovs_vport_stats vport_stats
;
1881 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_vport_genl_family
,
1886 ovs_header
->dp_ifindex
= get_dpifindex(vport
->dp
);
1888 if (nla_put_u32(skb
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
) ||
1889 nla_put_u32(skb
, OVS_VPORT_ATTR_TYPE
, vport
->ops
->type
) ||
1890 nla_put_string(skb
, OVS_VPORT_ATTR_NAME
, vport
->ops
->get_name(vport
)) ||
1891 nla_put_u32(skb
, OVS_VPORT_ATTR_UPCALL_PID
, vport
->upcall_portid
))
1892 goto nla_put_failure
;
1894 ovs_vport_get_stats(vport
, &vport_stats
);
1895 if (nla_put(skb
, OVS_VPORT_ATTR_STATS
, sizeof(struct ovs_vport_stats
),
1897 goto nla_put_failure
;
1899 err
= ovs_vport_get_options(vport
, skb
);
1900 if (err
== -EMSGSIZE
)
1903 return genlmsg_end(skb
, ovs_header
);
1908 genlmsg_cancel(skb
, ovs_header
);
1912 /* Called with ovs_mutex or RCU read lock. */
1913 struct sk_buff
*ovs_vport_cmd_build_info(struct vport
*vport
, u32 portid
,
1916 struct sk_buff
*skb
;
1919 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1921 return ERR_PTR(-ENOMEM
);
1923 retval
= ovs_vport_cmd_fill_info(vport
, skb
, portid
, seq
, 0, cmd
);
1929 /* Called with ovs_mutex or RCU read lock. */
1930 static struct vport
*lookup_vport(struct net
*net
,
1931 struct ovs_header
*ovs_header
,
1932 struct nlattr
*a
[OVS_VPORT_ATTR_MAX
+ 1])
1934 struct datapath
*dp
;
1935 struct vport
*vport
;
1937 if (a
[OVS_VPORT_ATTR_NAME
]) {
1938 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_VPORT_ATTR_NAME
]));
1940 return ERR_PTR(-ENODEV
);
1941 if (ovs_header
->dp_ifindex
&&
1942 ovs_header
->dp_ifindex
!= get_dpifindex(vport
->dp
))
1943 return ERR_PTR(-ENODEV
);
1945 } else if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
1946 u32 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
1948 if (port_no
>= DP_MAX_PORTS
)
1949 return ERR_PTR(-EFBIG
);
1951 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1953 return ERR_PTR(-ENODEV
);
1955 vport
= ovs_vport_ovsl_rcu(dp
, port_no
);
1957 return ERR_PTR(-ENODEV
);
1960 return ERR_PTR(-EINVAL
);
1963 static int ovs_vport_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1965 struct nlattr
**a
= info
->attrs
;
1966 struct ovs_header
*ovs_header
= info
->userhdr
;
1967 struct vport_parms parms
;
1968 struct sk_buff
*reply
;
1969 struct vport
*vport
;
1970 struct datapath
*dp
;
1975 if (!a
[OVS_VPORT_ATTR_NAME
] || !a
[OVS_VPORT_ATTR_TYPE
] ||
1976 !a
[OVS_VPORT_ATTR_UPCALL_PID
])
1980 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1985 if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
1986 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
1989 if (port_no
>= DP_MAX_PORTS
)
1992 vport
= ovs_vport_ovsl(dp
, port_no
);
1997 for (port_no
= 1; ; port_no
++) {
1998 if (port_no
>= DP_MAX_PORTS
) {
2002 vport
= ovs_vport_ovsl(dp
, port_no
);
2008 parms
.name
= nla_data(a
[OVS_VPORT_ATTR_NAME
]);
2009 parms
.type
= nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
2010 parms
.options
= a
[OVS_VPORT_ATTR_OPTIONS
];
2012 parms
.port_no
= port_no
;
2013 parms
.upcall_portid
= nla_get_u32(a
[OVS_VPORT_ATTR_UPCALL_PID
]);
2015 vport
= new_vport(&parms
);
2016 err
= PTR_ERR(vport
);
2021 reply
= ovs_vport_cmd_build_info(vport
, info
->snd_portid
, info
->snd_seq
,
2023 if (IS_ERR(reply
)) {
2024 err
= PTR_ERR(reply
);
2025 ovs_dp_detach_port(vport
);
2029 ovs_notify(reply
, info
, &ovs_dp_vport_multicast_group
);
2037 static int ovs_vport_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
2039 struct nlattr
**a
= info
->attrs
;
2040 struct sk_buff
*reply
;
2041 struct vport
*vport
;
2045 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2046 err
= PTR_ERR(vport
);
2050 if (a
[OVS_VPORT_ATTR_TYPE
] &&
2051 nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]) != vport
->ops
->type
) {
2056 reply
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2062 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
2063 err
= ovs_vport_set_options(vport
, a
[OVS_VPORT_ATTR_OPTIONS
]);
2068 if (a
[OVS_VPORT_ATTR_UPCALL_PID
])
2069 vport
->upcall_portid
= nla_get_u32(a
[OVS_VPORT_ATTR_UPCALL_PID
]);
2071 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
2072 info
->snd_seq
, 0, OVS_VPORT_CMD_NEW
);
2076 ovs_notify(reply
, info
, &ovs_dp_vport_multicast_group
);
2089 static int ovs_vport_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
2091 struct nlattr
**a
= info
->attrs
;
2092 struct sk_buff
*reply
;
2093 struct vport
*vport
;
2097 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2098 err
= PTR_ERR(vport
);
2102 if (vport
->port_no
== OVSP_LOCAL
) {
2107 reply
= ovs_vport_cmd_build_info(vport
, info
->snd_portid
,
2108 info
->snd_seq
, OVS_VPORT_CMD_DEL
);
2109 err
= PTR_ERR(reply
);
2114 ovs_dp_detach_port(vport
);
2116 ovs_notify(reply
, info
, &ovs_dp_vport_multicast_group
);
2123 static int ovs_vport_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
2125 struct nlattr
**a
= info
->attrs
;
2126 struct ovs_header
*ovs_header
= info
->userhdr
;
2127 struct sk_buff
*reply
;
2128 struct vport
*vport
;
2132 vport
= lookup_vport(sock_net(skb
->sk
), ovs_header
, a
);
2133 err
= PTR_ERR(vport
);
2137 reply
= ovs_vport_cmd_build_info(vport
, info
->snd_portid
,
2138 info
->snd_seq
, OVS_VPORT_CMD_NEW
);
2139 err
= PTR_ERR(reply
);
2145 return genlmsg_reply(reply
, info
);
2152 static int ovs_vport_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2154 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
2155 struct datapath
*dp
;
2156 int bucket
= cb
->args
[0], skip
= cb
->args
[1];
2159 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
2164 for (i
= bucket
; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2165 struct vport
*vport
;
2168 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
2170 ovs_vport_cmd_fill_info(vport
, skb
,
2171 NETLINK_CB(cb
->skb
).portid
,
2174 OVS_VPORT_CMD_NEW
) < 0)
2190 static struct genl_ops dp_vport_genl_ops
[] = {
2191 { .cmd
= OVS_VPORT_CMD_NEW
,
2192 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2193 .policy
= vport_policy
,
2194 .doit
= ovs_vport_cmd_new
2196 { .cmd
= OVS_VPORT_CMD_DEL
,
2197 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2198 .policy
= vport_policy
,
2199 .doit
= ovs_vport_cmd_del
2201 { .cmd
= OVS_VPORT_CMD_GET
,
2202 .flags
= 0, /* OK for unprivileged users. */
2203 .policy
= vport_policy
,
2204 .doit
= ovs_vport_cmd_get
,
2205 .dumpit
= ovs_vport_cmd_dump
2207 { .cmd
= OVS_VPORT_CMD_SET
,
2208 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2209 .policy
= vport_policy
,
2210 .doit
= ovs_vport_cmd_set
,
2214 struct genl_family_and_ops
{
2215 struct genl_family
*family
;
2216 struct genl_ops
*ops
;
2218 struct genl_multicast_group
*group
;
2221 static const struct genl_family_and_ops dp_genl_families
[] = {
2222 { &dp_datapath_genl_family
,
2223 dp_datapath_genl_ops
, ARRAY_SIZE(dp_datapath_genl_ops
),
2224 &ovs_dp_datapath_multicast_group
},
2225 { &dp_vport_genl_family
,
2226 dp_vport_genl_ops
, ARRAY_SIZE(dp_vport_genl_ops
),
2227 &ovs_dp_vport_multicast_group
},
2228 { &dp_flow_genl_family
,
2229 dp_flow_genl_ops
, ARRAY_SIZE(dp_flow_genl_ops
),
2230 &ovs_dp_flow_multicast_group
},
2231 { &dp_packet_genl_family
,
2232 dp_packet_genl_ops
, ARRAY_SIZE(dp_packet_genl_ops
),
2236 static void dp_unregister_genl(int n_families
)
2240 for (i
= 0; i
< n_families
; i
++)
2241 genl_unregister_family(dp_genl_families
[i
].family
);
2244 static int dp_register_genl(void)
2251 for (i
= 0; i
< ARRAY_SIZE(dp_genl_families
); i
++) {
2252 const struct genl_family_and_ops
*f
= &dp_genl_families
[i
];
2254 err
= genl_register_family_with_ops(f
->family
, f
->ops
,
2261 err
= genl_register_mc_group(f
->family
, f
->group
);
2270 dp_unregister_genl(n_registered
);
2274 static void rehash_flow_table(struct work_struct
*work
)
2276 struct datapath
*dp
;
2282 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2284 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
2285 struct flow_table
*old_table
= ovsl_dereference(dp
->table
);
2286 struct flow_table
*new_table
;
2288 new_table
= ovs_flow_tbl_rehash(old_table
);
2289 if (!IS_ERR(new_table
)) {
2290 rcu_assign_pointer(dp
->table
, new_table
);
2291 ovs_flow_tbl_deferred_destroy(old_table
);
2297 schedule_delayed_work(&rehash_flow_wq
, REHASH_FLOW_INTERVAL
);
2300 static int __net_init
ovs_init_net(struct net
*net
)
2302 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2304 INIT_LIST_HEAD(&ovs_net
->dps
);
2305 INIT_WORK(&ovs_net
->dp_notify_work
, ovs_dp_notify_wq
);
2309 static void __net_exit
ovs_exit_net(struct net
*net
)
2311 struct datapath
*dp
, *dp_next
;
2312 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2315 list_for_each_entry_safe(dp
, dp_next
, &ovs_net
->dps
, list_node
)
2319 cancel_work_sync(&ovs_net
->dp_notify_work
);
2322 static struct pernet_operations ovs_net_ops
= {
2323 .init
= ovs_init_net
,
2324 .exit
= ovs_exit_net
,
2326 .size
= sizeof(struct ovs_net
),
2329 static int __init
dp_init(void)
2333 BUILD_BUG_ON(sizeof(struct ovs_skb_cb
) > FIELD_SIZEOF(struct sk_buff
, cb
));
2335 pr_info("Open vSwitch switching datapath\n");
2337 err
= ovs_flow_init();
2341 err
= ovs_vport_init();
2343 goto error_flow_exit
;
2345 err
= register_pernet_device(&ovs_net_ops
);
2347 goto error_vport_exit
;
2349 err
= register_netdevice_notifier(&ovs_dp_device_notifier
);
2351 goto error_netns_exit
;
2353 err
= dp_register_genl();
2355 goto error_unreg_notifier
;
2357 schedule_delayed_work(&rehash_flow_wq
, REHASH_FLOW_INTERVAL
);
2361 error_unreg_notifier
:
2362 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2364 unregister_pernet_device(&ovs_net_ops
);
2373 static void dp_cleanup(void)
2375 cancel_delayed_work_sync(&rehash_flow_wq
);
2376 dp_unregister_genl(ARRAY_SIZE(dp_genl_families
));
2377 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2378 unregister_pernet_device(&ovs_net_ops
);
2384 module_init(dp_init
);
2385 module_exit(dp_cleanup
);
2387 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2388 MODULE_LICENSE("GPL");