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(4) /* OVS_KEY_ATTR_IN_PORT */
366 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
367 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
368 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
369 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
370 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
371 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
372 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
373 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
374 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
377 static size_t upcall_msg_size(const struct sk_buff
*skb
,
378 const struct nlattr
*userdata
)
380 size_t size
= NLMSG_ALIGN(sizeof(struct ovs_header
))
381 + nla_total_size(skb
->len
) /* OVS_PACKET_ATTR_PACKET */
382 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
384 /* OVS_PACKET_ATTR_USERDATA */
386 size
+= NLA_ALIGN(userdata
->nla_len
);
391 static int queue_userspace_packet(struct net
*net
, int dp_ifindex
,
393 const struct dp_upcall_info
*upcall_info
)
395 struct ovs_header
*upcall
;
396 struct sk_buff
*nskb
= NULL
;
397 struct sk_buff
*user_skb
; /* to be queued to userspace */
401 if (vlan_tx_tag_present(skb
)) {
402 nskb
= skb_clone(skb
, GFP_ATOMIC
);
406 nskb
= __vlan_put_tag(nskb
, nskb
->vlan_proto
, vlan_tx_tag_get(nskb
));
414 if (nla_attr_size(skb
->len
) > USHRT_MAX
) {
419 user_skb
= genlmsg_new(upcall_msg_size(skb
, upcall_info
->userdata
), GFP_ATOMIC
);
425 upcall
= genlmsg_put(user_skb
, 0, 0, &dp_packet_genl_family
,
426 0, upcall_info
->cmd
);
427 upcall
->dp_ifindex
= dp_ifindex
;
429 nla
= nla_nest_start(user_skb
, OVS_PACKET_ATTR_KEY
);
430 ovs_flow_to_nlattrs(upcall_info
->key
, user_skb
);
431 nla_nest_end(user_skb
, nla
);
433 if (upcall_info
->userdata
)
434 __nla_put(user_skb
, OVS_PACKET_ATTR_USERDATA
,
435 nla_len(upcall_info
->userdata
),
436 nla_data(upcall_info
->userdata
));
438 nla
= __nla_reserve(user_skb
, OVS_PACKET_ATTR_PACKET
, skb
->len
);
440 skb_copy_and_csum_dev(skb
, nla_data(nla
));
442 genlmsg_end(user_skb
, upcall
);
443 err
= genlmsg_unicast(net
, user_skb
, upcall_info
->portid
);
450 /* Called with ovs_mutex. */
451 static int flush_flows(struct datapath
*dp
)
453 struct flow_table
*old_table
;
454 struct flow_table
*new_table
;
456 old_table
= ovsl_dereference(dp
->table
);
457 new_table
= ovs_flow_tbl_alloc(TBL_MIN_BUCKETS
);
461 rcu_assign_pointer(dp
->table
, new_table
);
463 ovs_flow_tbl_deferred_destroy(old_table
);
467 static int validate_actions(const struct nlattr
*attr
,
468 const struct sw_flow_key
*key
, int depth
);
470 static int validate_sample(const struct nlattr
*attr
,
471 const struct sw_flow_key
*key
, int depth
)
473 const struct nlattr
*attrs
[OVS_SAMPLE_ATTR_MAX
+ 1];
474 const struct nlattr
*probability
, *actions
;
475 const struct nlattr
*a
;
478 memset(attrs
, 0, sizeof(attrs
));
479 nla_for_each_nested(a
, attr
, rem
) {
480 int type
= nla_type(a
);
481 if (!type
|| type
> OVS_SAMPLE_ATTR_MAX
|| attrs
[type
])
488 probability
= attrs
[OVS_SAMPLE_ATTR_PROBABILITY
];
489 if (!probability
|| nla_len(probability
) != sizeof(u32
))
492 actions
= attrs
[OVS_SAMPLE_ATTR_ACTIONS
];
493 if (!actions
|| (nla_len(actions
) && nla_len(actions
) < NLA_HDRLEN
))
495 return validate_actions(actions
, key
, depth
+ 1);
498 static int validate_tp_port(const struct sw_flow_key
*flow_key
)
500 if (flow_key
->eth
.type
== htons(ETH_P_IP
)) {
501 if (flow_key
->ipv4
.tp
.src
|| flow_key
->ipv4
.tp
.dst
)
503 } else if (flow_key
->eth
.type
== htons(ETH_P_IPV6
)) {
504 if (flow_key
->ipv6
.tp
.src
|| flow_key
->ipv6
.tp
.dst
)
511 static int validate_set(const struct nlattr
*a
,
512 const struct sw_flow_key
*flow_key
)
514 const struct nlattr
*ovs_key
= nla_data(a
);
515 int key_type
= nla_type(ovs_key
);
517 /* There can be only one key in a action */
518 if (nla_total_size(nla_len(ovs_key
)) != nla_len(a
))
521 if (key_type
> OVS_KEY_ATTR_MAX
||
522 nla_len(ovs_key
) != ovs_key_lens
[key_type
])
526 const struct ovs_key_ipv4
*ipv4_key
;
527 const struct ovs_key_ipv6
*ipv6_key
;
529 case OVS_KEY_ATTR_PRIORITY
:
530 case OVS_KEY_ATTR_SKB_MARK
:
531 case OVS_KEY_ATTR_ETHERNET
:
534 case OVS_KEY_ATTR_IPV4
:
535 if (flow_key
->eth
.type
!= htons(ETH_P_IP
))
538 if (!flow_key
->ip
.proto
)
541 ipv4_key
= nla_data(ovs_key
);
542 if (ipv4_key
->ipv4_proto
!= flow_key
->ip
.proto
)
545 if (ipv4_key
->ipv4_frag
!= flow_key
->ip
.frag
)
550 case OVS_KEY_ATTR_IPV6
:
551 if (flow_key
->eth
.type
!= htons(ETH_P_IPV6
))
554 if (!flow_key
->ip
.proto
)
557 ipv6_key
= nla_data(ovs_key
);
558 if (ipv6_key
->ipv6_proto
!= flow_key
->ip
.proto
)
561 if (ipv6_key
->ipv6_frag
!= flow_key
->ip
.frag
)
564 if (ntohl(ipv6_key
->ipv6_label
) & 0xFFF00000)
569 case OVS_KEY_ATTR_TCP
:
570 if (flow_key
->ip
.proto
!= IPPROTO_TCP
)
573 return validate_tp_port(flow_key
);
575 case OVS_KEY_ATTR_UDP
:
576 if (flow_key
->ip
.proto
!= IPPROTO_UDP
)
579 return validate_tp_port(flow_key
);
588 static int validate_userspace(const struct nlattr
*attr
)
590 static const struct nla_policy userspace_policy
[OVS_USERSPACE_ATTR_MAX
+ 1] = {
591 [OVS_USERSPACE_ATTR_PID
] = {.type
= NLA_U32
},
592 [OVS_USERSPACE_ATTR_USERDATA
] = {.type
= NLA_UNSPEC
},
594 struct nlattr
*a
[OVS_USERSPACE_ATTR_MAX
+ 1];
597 error
= nla_parse_nested(a
, OVS_USERSPACE_ATTR_MAX
,
598 attr
, userspace_policy
);
602 if (!a
[OVS_USERSPACE_ATTR_PID
] ||
603 !nla_get_u32(a
[OVS_USERSPACE_ATTR_PID
]))
609 static int validate_actions(const struct nlattr
*attr
,
610 const struct sw_flow_key
*key
, int depth
)
612 const struct nlattr
*a
;
615 if (depth
>= SAMPLE_ACTION_DEPTH
)
618 nla_for_each_nested(a
, attr
, rem
) {
619 /* Expected argument lengths, (u32)-1 for variable length. */
620 static const u32 action_lens
[OVS_ACTION_ATTR_MAX
+ 1] = {
621 [OVS_ACTION_ATTR_OUTPUT
] = sizeof(u32
),
622 [OVS_ACTION_ATTR_USERSPACE
] = (u32
)-1,
623 [OVS_ACTION_ATTR_PUSH_VLAN
] = sizeof(struct ovs_action_push_vlan
),
624 [OVS_ACTION_ATTR_POP_VLAN
] = 0,
625 [OVS_ACTION_ATTR_SET
] = (u32
)-1,
626 [OVS_ACTION_ATTR_SAMPLE
] = (u32
)-1
628 const struct ovs_action_push_vlan
*vlan
;
629 int type
= nla_type(a
);
631 if (type
> OVS_ACTION_ATTR_MAX
||
632 (action_lens
[type
] != nla_len(a
) &&
633 action_lens
[type
] != (u32
)-1))
637 case OVS_ACTION_ATTR_UNSPEC
:
640 case OVS_ACTION_ATTR_USERSPACE
:
641 err
= validate_userspace(a
);
646 case OVS_ACTION_ATTR_OUTPUT
:
647 if (nla_get_u32(a
) >= DP_MAX_PORTS
)
652 case OVS_ACTION_ATTR_POP_VLAN
:
655 case OVS_ACTION_ATTR_PUSH_VLAN
:
657 if (vlan
->vlan_tpid
!= htons(ETH_P_8021Q
))
659 if (!(vlan
->vlan_tci
& htons(VLAN_TAG_PRESENT
)))
663 case OVS_ACTION_ATTR_SET
:
664 err
= validate_set(a
, key
);
669 case OVS_ACTION_ATTR_SAMPLE
:
670 err
= validate_sample(a
, key
, depth
);
686 static void clear_stats(struct sw_flow
*flow
)
690 flow
->packet_count
= 0;
691 flow
->byte_count
= 0;
694 static int ovs_packet_cmd_execute(struct sk_buff
*skb
, struct genl_info
*info
)
696 struct ovs_header
*ovs_header
= info
->userhdr
;
697 struct nlattr
**a
= info
->attrs
;
698 struct sw_flow_actions
*acts
;
699 struct sk_buff
*packet
;
700 struct sw_flow
*flow
;
708 if (!a
[OVS_PACKET_ATTR_PACKET
] || !a
[OVS_PACKET_ATTR_KEY
] ||
709 !a
[OVS_PACKET_ATTR_ACTIONS
])
712 len
= nla_len(a
[OVS_PACKET_ATTR_PACKET
]);
713 packet
= __dev_alloc_skb(NET_IP_ALIGN
+ len
, GFP_KERNEL
);
717 skb_reserve(packet
, NET_IP_ALIGN
);
719 nla_memcpy(__skb_put(packet
, len
), a
[OVS_PACKET_ATTR_PACKET
], len
);
721 skb_reset_mac_header(packet
);
722 eth
= eth_hdr(packet
);
724 /* Normally, setting the skb 'protocol' field would be handled by a
725 * call to eth_type_trans(), but it assumes there's a sending
726 * device, which we may not have. */
727 if (ntohs(eth
->h_proto
) >= ETH_P_802_3_MIN
)
728 packet
->protocol
= eth
->h_proto
;
730 packet
->protocol
= htons(ETH_P_802_2
);
732 /* Build an sw_flow for sending this packet. */
733 flow
= ovs_flow_alloc();
738 err
= ovs_flow_extract(packet
, -1, &flow
->key
, &key_len
);
742 err
= ovs_flow_metadata_from_nlattrs(&flow
->key
.phy
.priority
,
743 &flow
->key
.phy
.skb_mark
,
744 &flow
->key
.phy
.in_port
,
745 a
[OVS_PACKET_ATTR_KEY
]);
749 err
= validate_actions(a
[OVS_PACKET_ATTR_ACTIONS
], &flow
->key
, 0);
753 flow
->hash
= ovs_flow_hash(&flow
->key
, key_len
);
755 acts
= ovs_flow_actions_alloc(a
[OVS_PACKET_ATTR_ACTIONS
]);
759 rcu_assign_pointer(flow
->sf_acts
, acts
);
761 OVS_CB(packet
)->flow
= flow
;
762 packet
->priority
= flow
->key
.phy
.priority
;
763 packet
->mark
= flow
->key
.phy
.skb_mark
;
766 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
772 err
= ovs_execute_actions(dp
, packet
);
789 static const struct nla_policy packet_policy
[OVS_PACKET_ATTR_MAX
+ 1] = {
790 [OVS_PACKET_ATTR_PACKET
] = { .len
= ETH_HLEN
},
791 [OVS_PACKET_ATTR_KEY
] = { .type
= NLA_NESTED
},
792 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
795 static struct genl_ops dp_packet_genl_ops
[] = {
796 { .cmd
= OVS_PACKET_CMD_EXECUTE
,
797 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
798 .policy
= packet_policy
,
799 .doit
= ovs_packet_cmd_execute
803 static void get_dp_stats(struct datapath
*dp
, struct ovs_dp_stats
*stats
)
806 struct flow_table
*table
= ovsl_dereference(dp
->table
);
808 stats
->n_flows
= ovs_flow_tbl_count(table
);
810 stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
811 for_each_possible_cpu(i
) {
812 const struct dp_stats_percpu
*percpu_stats
;
813 struct dp_stats_percpu local_stats
;
816 percpu_stats
= per_cpu_ptr(dp
->stats_percpu
, i
);
819 start
= u64_stats_fetch_begin_bh(&percpu_stats
->sync
);
820 local_stats
= *percpu_stats
;
821 } while (u64_stats_fetch_retry_bh(&percpu_stats
->sync
, start
));
823 stats
->n_hit
+= local_stats
.n_hit
;
824 stats
->n_missed
+= local_stats
.n_missed
;
825 stats
->n_lost
+= local_stats
.n_lost
;
829 static const struct nla_policy flow_policy
[OVS_FLOW_ATTR_MAX
+ 1] = {
830 [OVS_FLOW_ATTR_KEY
] = { .type
= NLA_NESTED
},
831 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
832 [OVS_FLOW_ATTR_CLEAR
] = { .type
= NLA_FLAG
},
835 static struct genl_family dp_flow_genl_family
= {
836 .id
= GENL_ID_GENERATE
,
837 .hdrsize
= sizeof(struct ovs_header
),
838 .name
= OVS_FLOW_FAMILY
,
839 .version
= OVS_FLOW_VERSION
,
840 .maxattr
= OVS_FLOW_ATTR_MAX
,
842 .parallel_ops
= true,
845 static struct genl_multicast_group ovs_dp_flow_multicast_group
= {
846 .name
= OVS_FLOW_MCGROUP
849 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions
*acts
)
851 return NLMSG_ALIGN(sizeof(struct ovs_header
))
852 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
853 + nla_total_size(sizeof(struct ovs_flow_stats
)) /* OVS_FLOW_ATTR_STATS */
854 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
855 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
856 + nla_total_size(acts
->actions_len
); /* OVS_FLOW_ATTR_ACTIONS */
859 /* Called with ovs_mutex. */
860 static int ovs_flow_cmd_fill_info(struct sw_flow
*flow
, struct datapath
*dp
,
861 struct sk_buff
*skb
, u32 portid
,
862 u32 seq
, u32 flags
, u8 cmd
)
864 const int skb_orig_len
= skb
->len
;
865 const struct sw_flow_actions
*sf_acts
;
866 struct ovs_flow_stats stats
;
867 struct ovs_header
*ovs_header
;
873 sf_acts
= ovsl_dereference(flow
->sf_acts
);
875 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_flow_genl_family
, flags
, cmd
);
879 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
881 nla
= nla_nest_start(skb
, OVS_FLOW_ATTR_KEY
);
883 goto nla_put_failure
;
884 err
= ovs_flow_to_nlattrs(&flow
->key
, skb
);
887 nla_nest_end(skb
, nla
);
889 spin_lock_bh(&flow
->lock
);
891 stats
.n_packets
= flow
->packet_count
;
892 stats
.n_bytes
= flow
->byte_count
;
893 tcp_flags
= flow
->tcp_flags
;
894 spin_unlock_bh(&flow
->lock
);
897 nla_put_u64(skb
, OVS_FLOW_ATTR_USED
, ovs_flow_used_time(used
)))
898 goto nla_put_failure
;
900 if (stats
.n_packets
&&
901 nla_put(skb
, OVS_FLOW_ATTR_STATS
,
902 sizeof(struct ovs_flow_stats
), &stats
))
903 goto nla_put_failure
;
906 nla_put_u8(skb
, OVS_FLOW_ATTR_TCP_FLAGS
, tcp_flags
))
907 goto nla_put_failure
;
909 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
910 * this is the first flow to be dumped into 'skb'. This is unusual for
911 * Netlink but individual action lists can be longer than
912 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
913 * The userspace caller can always fetch the actions separately if it
914 * really wants them. (Most userspace callers in fact don't care.)
916 * This can only fail for dump operations because the skb is always
917 * properly sized for single flows.
919 err
= nla_put(skb
, OVS_FLOW_ATTR_ACTIONS
, sf_acts
->actions_len
,
921 if (err
< 0 && skb_orig_len
)
924 return genlmsg_end(skb
, ovs_header
);
929 genlmsg_cancel(skb
, ovs_header
);
933 static struct sk_buff
*ovs_flow_cmd_alloc_info(struct sw_flow
*flow
)
935 const struct sw_flow_actions
*sf_acts
;
937 sf_acts
= ovsl_dereference(flow
->sf_acts
);
939 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts
), GFP_KERNEL
);
942 static struct sk_buff
*ovs_flow_cmd_build_info(struct sw_flow
*flow
,
944 u32 portid
, u32 seq
, u8 cmd
)
949 skb
= ovs_flow_cmd_alloc_info(flow
);
951 return ERR_PTR(-ENOMEM
);
953 retval
= ovs_flow_cmd_fill_info(flow
, dp
, skb
, portid
, seq
, 0, cmd
);
958 static int ovs_flow_cmd_new_or_set(struct sk_buff
*skb
, struct genl_info
*info
)
960 struct nlattr
**a
= info
->attrs
;
961 struct ovs_header
*ovs_header
= info
->userhdr
;
962 struct sw_flow_key key
;
963 struct sw_flow
*flow
;
964 struct sk_buff
*reply
;
966 struct flow_table
*table
;
972 if (!a
[OVS_FLOW_ATTR_KEY
])
974 error
= ovs_flow_from_nlattrs(&key
, &key_len
, a
[OVS_FLOW_ATTR_KEY
]);
978 /* Validate actions. */
979 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
980 error
= validate_actions(a
[OVS_FLOW_ATTR_ACTIONS
], &key
, 0);
983 } else if (info
->genlhdr
->cmd
== OVS_FLOW_CMD_NEW
) {
989 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
994 table
= ovsl_dereference(dp
->table
);
995 flow
= ovs_flow_tbl_lookup(table
, &key
, key_len
);
997 struct sw_flow_actions
*acts
;
999 /* Bail out if we're not allowed to create a new flow. */
1001 if (info
->genlhdr
->cmd
== OVS_FLOW_CMD_SET
)
1002 goto err_unlock_ovs
;
1004 /* Expand table, if necessary, to make room. */
1005 if (ovs_flow_tbl_need_to_expand(table
)) {
1006 struct flow_table
*new_table
;
1008 new_table
= ovs_flow_tbl_expand(table
);
1009 if (!IS_ERR(new_table
)) {
1010 rcu_assign_pointer(dp
->table
, new_table
);
1011 ovs_flow_tbl_deferred_destroy(table
);
1012 table
= ovsl_dereference(dp
->table
);
1016 /* Allocate flow. */
1017 flow
= ovs_flow_alloc();
1019 error
= PTR_ERR(flow
);
1020 goto err_unlock_ovs
;
1025 /* Obtain actions. */
1026 acts
= ovs_flow_actions_alloc(a
[OVS_FLOW_ATTR_ACTIONS
]);
1027 error
= PTR_ERR(acts
);
1029 goto error_free_flow
;
1030 rcu_assign_pointer(flow
->sf_acts
, acts
);
1032 /* Put flow in bucket. */
1033 flow
->hash
= ovs_flow_hash(&key
, key_len
);
1034 ovs_flow_tbl_insert(table
, flow
);
1036 reply
= ovs_flow_cmd_build_info(flow
, dp
, info
->snd_portid
,
1040 /* We found a matching flow. */
1041 struct sw_flow_actions
*old_acts
;
1042 struct nlattr
*acts_attrs
;
1044 /* Bail out if we're not allowed to modify an existing flow.
1045 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1046 * because Generic Netlink treats the latter as a dump
1047 * request. We also accept NLM_F_EXCL in case that bug ever
1051 if (info
->genlhdr
->cmd
== OVS_FLOW_CMD_NEW
&&
1052 info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
| NLM_F_EXCL
))
1053 goto err_unlock_ovs
;
1055 /* Update actions. */
1056 old_acts
= ovsl_dereference(flow
->sf_acts
);
1057 acts_attrs
= a
[OVS_FLOW_ATTR_ACTIONS
];
1059 (old_acts
->actions_len
!= nla_len(acts_attrs
) ||
1060 memcmp(old_acts
->actions
, nla_data(acts_attrs
),
1061 old_acts
->actions_len
))) {
1062 struct sw_flow_actions
*new_acts
;
1064 new_acts
= ovs_flow_actions_alloc(acts_attrs
);
1065 error
= PTR_ERR(new_acts
);
1066 if (IS_ERR(new_acts
))
1067 goto err_unlock_ovs
;
1069 rcu_assign_pointer(flow
->sf_acts
, new_acts
);
1070 ovs_flow_deferred_free_acts(old_acts
);
1073 reply
= ovs_flow_cmd_build_info(flow
, dp
, info
->snd_portid
,
1074 info
->snd_seq
, OVS_FLOW_CMD_NEW
);
1077 if (a
[OVS_FLOW_ATTR_CLEAR
]) {
1078 spin_lock_bh(&flow
->lock
);
1080 spin_unlock_bh(&flow
->lock
);
1086 ovs_notify(reply
, info
, &ovs_dp_flow_multicast_group
);
1088 netlink_set_err(sock_net(skb
->sk
)->genl_sock
, 0,
1089 ovs_dp_flow_multicast_group
.id
, PTR_ERR(reply
));
1093 ovs_flow_free(flow
);
1100 static int ovs_flow_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1102 struct nlattr
**a
= info
->attrs
;
1103 struct ovs_header
*ovs_header
= info
->userhdr
;
1104 struct sw_flow_key key
;
1105 struct sk_buff
*reply
;
1106 struct sw_flow
*flow
;
1107 struct datapath
*dp
;
1108 struct flow_table
*table
;
1112 if (!a
[OVS_FLOW_ATTR_KEY
])
1114 err
= ovs_flow_from_nlattrs(&key
, &key_len
, a
[OVS_FLOW_ATTR_KEY
]);
1119 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1125 table
= ovsl_dereference(dp
->table
);
1126 flow
= ovs_flow_tbl_lookup(table
, &key
, key_len
);
1132 reply
= ovs_flow_cmd_build_info(flow
, dp
, info
->snd_portid
,
1133 info
->snd_seq
, OVS_FLOW_CMD_NEW
);
1134 if (IS_ERR(reply
)) {
1135 err
= PTR_ERR(reply
);
1140 return genlmsg_reply(reply
, info
);
1146 static int ovs_flow_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1148 struct nlattr
**a
= info
->attrs
;
1149 struct ovs_header
*ovs_header
= info
->userhdr
;
1150 struct sw_flow_key key
;
1151 struct sk_buff
*reply
;
1152 struct sw_flow
*flow
;
1153 struct datapath
*dp
;
1154 struct flow_table
*table
;
1159 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1165 if (!a
[OVS_FLOW_ATTR_KEY
]) {
1166 err
= flush_flows(dp
);
1169 err
= ovs_flow_from_nlattrs(&key
, &key_len
, a
[OVS_FLOW_ATTR_KEY
]);
1173 table
= ovsl_dereference(dp
->table
);
1174 flow
= ovs_flow_tbl_lookup(table
, &key
, key_len
);
1180 reply
= ovs_flow_cmd_alloc_info(flow
);
1186 ovs_flow_tbl_remove(table
, flow
);
1188 err
= ovs_flow_cmd_fill_info(flow
, dp
, reply
, info
->snd_portid
,
1189 info
->snd_seq
, 0, OVS_FLOW_CMD_DEL
);
1192 ovs_flow_deferred_free(flow
);
1195 ovs_notify(reply
, info
, &ovs_dp_flow_multicast_group
);
1202 static int ovs_flow_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1204 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1205 struct datapath
*dp
;
1206 struct flow_table
*table
;
1209 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1215 table
= ovsl_dereference(dp
->table
);
1218 struct sw_flow
*flow
;
1221 bucket
= cb
->args
[0];
1223 flow
= ovs_flow_tbl_next(table
, &bucket
, &obj
);
1227 if (ovs_flow_cmd_fill_info(flow
, dp
, skb
,
1228 NETLINK_CB(cb
->skb
).portid
,
1229 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1230 OVS_FLOW_CMD_NEW
) < 0)
1233 cb
->args
[0] = bucket
;
1240 static struct genl_ops dp_flow_genl_ops
[] = {
1241 { .cmd
= OVS_FLOW_CMD_NEW
,
1242 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1243 .policy
= flow_policy
,
1244 .doit
= ovs_flow_cmd_new_or_set
1246 { .cmd
= OVS_FLOW_CMD_DEL
,
1247 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1248 .policy
= flow_policy
,
1249 .doit
= ovs_flow_cmd_del
1251 { .cmd
= OVS_FLOW_CMD_GET
,
1252 .flags
= 0, /* OK for unprivileged users. */
1253 .policy
= flow_policy
,
1254 .doit
= ovs_flow_cmd_get
,
1255 .dumpit
= ovs_flow_cmd_dump
1257 { .cmd
= OVS_FLOW_CMD_SET
,
1258 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1259 .policy
= flow_policy
,
1260 .doit
= ovs_flow_cmd_new_or_set
,
1264 static const struct nla_policy datapath_policy
[OVS_DP_ATTR_MAX
+ 1] = {
1265 [OVS_DP_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1266 [OVS_DP_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1269 static struct genl_family dp_datapath_genl_family
= {
1270 .id
= GENL_ID_GENERATE
,
1271 .hdrsize
= sizeof(struct ovs_header
),
1272 .name
= OVS_DATAPATH_FAMILY
,
1273 .version
= OVS_DATAPATH_VERSION
,
1274 .maxattr
= OVS_DP_ATTR_MAX
,
1276 .parallel_ops
= true,
1279 static struct genl_multicast_group ovs_dp_datapath_multicast_group
= {
1280 .name
= OVS_DATAPATH_MCGROUP
1283 static size_t ovs_dp_cmd_msg_size(void)
1285 size_t msgsize
= NLMSG_ALIGN(sizeof(struct ovs_header
));
1287 msgsize
+= nla_total_size(IFNAMSIZ
);
1288 msgsize
+= nla_total_size(sizeof(struct ovs_dp_stats
));
1293 static int ovs_dp_cmd_fill_info(struct datapath
*dp
, struct sk_buff
*skb
,
1294 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1296 struct ovs_header
*ovs_header
;
1297 struct ovs_dp_stats dp_stats
;
1300 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_datapath_genl_family
,
1305 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
1308 err
= nla_put_string(skb
, OVS_DP_ATTR_NAME
, ovs_dp_name(dp
));
1311 goto nla_put_failure
;
1313 get_dp_stats(dp
, &dp_stats
);
1314 if (nla_put(skb
, OVS_DP_ATTR_STATS
, sizeof(struct ovs_dp_stats
), &dp_stats
))
1315 goto nla_put_failure
;
1317 return genlmsg_end(skb
, ovs_header
);
1320 genlmsg_cancel(skb
, ovs_header
);
1325 static struct sk_buff
*ovs_dp_cmd_build_info(struct datapath
*dp
, u32 portid
,
1328 struct sk_buff
*skb
;
1331 skb
= genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL
);
1333 return ERR_PTR(-ENOMEM
);
1335 retval
= ovs_dp_cmd_fill_info(dp
, skb
, portid
, seq
, 0, cmd
);
1338 return ERR_PTR(retval
);
1343 /* Called with ovs_mutex. */
1344 static struct datapath
*lookup_datapath(struct net
*net
,
1345 struct ovs_header
*ovs_header
,
1346 struct nlattr
*a
[OVS_DP_ATTR_MAX
+ 1])
1348 struct datapath
*dp
;
1350 if (!a
[OVS_DP_ATTR_NAME
])
1351 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1353 struct vport
*vport
;
1356 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_DP_ATTR_NAME
]));
1357 dp
= vport
&& vport
->port_no
== OVSP_LOCAL
? vport
->dp
: NULL
;
1360 return dp
? dp
: ERR_PTR(-ENODEV
);
1363 static int ovs_dp_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1365 struct nlattr
**a
= info
->attrs
;
1366 struct vport_parms parms
;
1367 struct sk_buff
*reply
;
1368 struct datapath
*dp
;
1369 struct vport
*vport
;
1370 struct ovs_net
*ovs_net
;
1374 if (!a
[OVS_DP_ATTR_NAME
] || !a
[OVS_DP_ATTR_UPCALL_PID
])
1380 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1382 goto err_unlock_ovs
;
1384 ovs_dp_set_net(dp
, hold_net(sock_net(skb
->sk
)));
1386 /* Allocate table. */
1388 rcu_assign_pointer(dp
->table
, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS
));
1392 dp
->stats_percpu
= alloc_percpu(struct dp_stats_percpu
);
1393 if (!dp
->stats_percpu
) {
1395 goto err_destroy_table
;
1398 dp
->ports
= kmalloc(DP_VPORT_HASH_BUCKETS
* sizeof(struct hlist_head
),
1402 goto err_destroy_percpu
;
1405 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
1406 INIT_HLIST_HEAD(&dp
->ports
[i
]);
1408 /* Set up our datapath device. */
1409 parms
.name
= nla_data(a
[OVS_DP_ATTR_NAME
]);
1410 parms
.type
= OVS_VPORT_TYPE_INTERNAL
;
1411 parms
.options
= NULL
;
1413 parms
.port_no
= OVSP_LOCAL
;
1414 parms
.upcall_portid
= nla_get_u32(a
[OVS_DP_ATTR_UPCALL_PID
]);
1416 vport
= new_vport(&parms
);
1417 if (IS_ERR(vport
)) {
1418 err
= PTR_ERR(vport
);
1422 goto err_destroy_ports_array
;
1425 reply
= ovs_dp_cmd_build_info(dp
, info
->snd_portid
,
1426 info
->snd_seq
, OVS_DP_CMD_NEW
);
1427 err
= PTR_ERR(reply
);
1429 goto err_destroy_local_port
;
1431 ovs_net
= net_generic(ovs_dp_get_net(dp
), ovs_net_id
);
1432 list_add_tail(&dp
->list_node
, &ovs_net
->dps
);
1436 ovs_notify(reply
, info
, &ovs_dp_datapath_multicast_group
);
1439 err_destroy_local_port
:
1440 ovs_dp_detach_port(ovs_vport_ovsl(dp
, OVSP_LOCAL
));
1441 err_destroy_ports_array
:
1444 free_percpu(dp
->stats_percpu
);
1446 ovs_flow_tbl_destroy(ovsl_dereference(dp
->table
));
1448 release_net(ovs_dp_get_net(dp
));
1456 /* Called with ovs_mutex. */
1457 static void __dp_destroy(struct datapath
*dp
)
1461 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1462 struct vport
*vport
;
1463 struct hlist_node
*n
;
1465 hlist_for_each_entry_safe(vport
, n
, &dp
->ports
[i
], dp_hash_node
)
1466 if (vport
->port_no
!= OVSP_LOCAL
)
1467 ovs_dp_detach_port(vport
);
1470 list_del(&dp
->list_node
);
1472 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1473 * all port in datapath are destroyed first before freeing datapath.
1475 ovs_dp_detach_port(ovs_vport_ovsl(dp
, OVSP_LOCAL
));
1477 call_rcu(&dp
->rcu
, destroy_dp_rcu
);
1480 static int ovs_dp_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1482 struct sk_buff
*reply
;
1483 struct datapath
*dp
;
1487 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1492 reply
= ovs_dp_cmd_build_info(dp
, info
->snd_portid
,
1493 info
->snd_seq
, OVS_DP_CMD_DEL
);
1494 err
= PTR_ERR(reply
);
1501 ovs_notify(reply
, info
, &ovs_dp_datapath_multicast_group
);
1509 static int ovs_dp_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1511 struct sk_buff
*reply
;
1512 struct datapath
*dp
;
1516 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1521 reply
= ovs_dp_cmd_build_info(dp
, info
->snd_portid
,
1522 info
->snd_seq
, OVS_DP_CMD_NEW
);
1523 if (IS_ERR(reply
)) {
1524 err
= PTR_ERR(reply
);
1525 netlink_set_err(sock_net(skb
->sk
)->genl_sock
, 0,
1526 ovs_dp_datapath_multicast_group
.id
, err
);
1532 ovs_notify(reply
, info
, &ovs_dp_datapath_multicast_group
);
1540 static int ovs_dp_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1542 struct sk_buff
*reply
;
1543 struct datapath
*dp
;
1547 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1553 reply
= ovs_dp_cmd_build_info(dp
, info
->snd_portid
,
1554 info
->snd_seq
, OVS_DP_CMD_NEW
);
1555 if (IS_ERR(reply
)) {
1556 err
= PTR_ERR(reply
);
1561 return genlmsg_reply(reply
, info
);
1568 static int ovs_dp_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1570 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
1571 struct datapath
*dp
;
1572 int skip
= cb
->args
[0];
1576 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
1578 ovs_dp_cmd_fill_info(dp
, skb
, NETLINK_CB(cb
->skb
).portid
,
1579 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1580 OVS_DP_CMD_NEW
) < 0)
1591 static struct genl_ops dp_datapath_genl_ops
[] = {
1592 { .cmd
= OVS_DP_CMD_NEW
,
1593 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1594 .policy
= datapath_policy
,
1595 .doit
= ovs_dp_cmd_new
1597 { .cmd
= OVS_DP_CMD_DEL
,
1598 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1599 .policy
= datapath_policy
,
1600 .doit
= ovs_dp_cmd_del
1602 { .cmd
= OVS_DP_CMD_GET
,
1603 .flags
= 0, /* OK for unprivileged users. */
1604 .policy
= datapath_policy
,
1605 .doit
= ovs_dp_cmd_get
,
1606 .dumpit
= ovs_dp_cmd_dump
1608 { .cmd
= OVS_DP_CMD_SET
,
1609 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1610 .policy
= datapath_policy
,
1611 .doit
= ovs_dp_cmd_set
,
1615 static const struct nla_policy vport_policy
[OVS_VPORT_ATTR_MAX
+ 1] = {
1616 [OVS_VPORT_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1617 [OVS_VPORT_ATTR_STATS
] = { .len
= sizeof(struct ovs_vport_stats
) },
1618 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NLA_U32
},
1619 [OVS_VPORT_ATTR_TYPE
] = { .type
= NLA_U32
},
1620 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1621 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NLA_NESTED
},
1624 static struct genl_family dp_vport_genl_family
= {
1625 .id
= GENL_ID_GENERATE
,
1626 .hdrsize
= sizeof(struct ovs_header
),
1627 .name
= OVS_VPORT_FAMILY
,
1628 .version
= OVS_VPORT_VERSION
,
1629 .maxattr
= OVS_VPORT_ATTR_MAX
,
1631 .parallel_ops
= true,
1634 struct genl_multicast_group ovs_dp_vport_multicast_group
= {
1635 .name
= OVS_VPORT_MCGROUP
1638 /* Called with ovs_mutex or RCU read lock. */
1639 static int ovs_vport_cmd_fill_info(struct vport
*vport
, struct sk_buff
*skb
,
1640 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1642 struct ovs_header
*ovs_header
;
1643 struct ovs_vport_stats vport_stats
;
1646 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_vport_genl_family
,
1651 ovs_header
->dp_ifindex
= get_dpifindex(vport
->dp
);
1653 if (nla_put_u32(skb
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
) ||
1654 nla_put_u32(skb
, OVS_VPORT_ATTR_TYPE
, vport
->ops
->type
) ||
1655 nla_put_string(skb
, OVS_VPORT_ATTR_NAME
, vport
->ops
->get_name(vport
)) ||
1656 nla_put_u32(skb
, OVS_VPORT_ATTR_UPCALL_PID
, vport
->upcall_portid
))
1657 goto nla_put_failure
;
1659 ovs_vport_get_stats(vport
, &vport_stats
);
1660 if (nla_put(skb
, OVS_VPORT_ATTR_STATS
, sizeof(struct ovs_vport_stats
),
1662 goto nla_put_failure
;
1664 err
= ovs_vport_get_options(vport
, skb
);
1665 if (err
== -EMSGSIZE
)
1668 return genlmsg_end(skb
, ovs_header
);
1673 genlmsg_cancel(skb
, ovs_header
);
1677 /* Called with ovs_mutex or RCU read lock. */
1678 struct sk_buff
*ovs_vport_cmd_build_info(struct vport
*vport
, u32 portid
,
1681 struct sk_buff
*skb
;
1684 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1686 return ERR_PTR(-ENOMEM
);
1688 retval
= ovs_vport_cmd_fill_info(vport
, skb
, portid
, seq
, 0, cmd
);
1694 /* Called with ovs_mutex or RCU read lock. */
1695 static struct vport
*lookup_vport(struct net
*net
,
1696 struct ovs_header
*ovs_header
,
1697 struct nlattr
*a
[OVS_VPORT_ATTR_MAX
+ 1])
1699 struct datapath
*dp
;
1700 struct vport
*vport
;
1702 if (a
[OVS_VPORT_ATTR_NAME
]) {
1703 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_VPORT_ATTR_NAME
]));
1705 return ERR_PTR(-ENODEV
);
1706 if (ovs_header
->dp_ifindex
&&
1707 ovs_header
->dp_ifindex
!= get_dpifindex(vport
->dp
))
1708 return ERR_PTR(-ENODEV
);
1710 } else if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
1711 u32 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
1713 if (port_no
>= DP_MAX_PORTS
)
1714 return ERR_PTR(-EFBIG
);
1716 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1718 return ERR_PTR(-ENODEV
);
1720 vport
= ovs_vport_ovsl_rcu(dp
, port_no
);
1722 return ERR_PTR(-ENODEV
);
1725 return ERR_PTR(-EINVAL
);
1728 static int ovs_vport_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1730 struct nlattr
**a
= info
->attrs
;
1731 struct ovs_header
*ovs_header
= info
->userhdr
;
1732 struct vport_parms parms
;
1733 struct sk_buff
*reply
;
1734 struct vport
*vport
;
1735 struct datapath
*dp
;
1740 if (!a
[OVS_VPORT_ATTR_NAME
] || !a
[OVS_VPORT_ATTR_TYPE
] ||
1741 !a
[OVS_VPORT_ATTR_UPCALL_PID
])
1745 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1750 if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
1751 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
1754 if (port_no
>= DP_MAX_PORTS
)
1757 vport
= ovs_vport_ovsl(dp
, port_no
);
1762 for (port_no
= 1; ; port_no
++) {
1763 if (port_no
>= DP_MAX_PORTS
) {
1767 vport
= ovs_vport_ovsl(dp
, port_no
);
1773 parms
.name
= nla_data(a
[OVS_VPORT_ATTR_NAME
]);
1774 parms
.type
= nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
1775 parms
.options
= a
[OVS_VPORT_ATTR_OPTIONS
];
1777 parms
.port_no
= port_no
;
1778 parms
.upcall_portid
= nla_get_u32(a
[OVS_VPORT_ATTR_UPCALL_PID
]);
1780 vport
= new_vport(&parms
);
1781 err
= PTR_ERR(vport
);
1786 reply
= ovs_vport_cmd_build_info(vport
, info
->snd_portid
, info
->snd_seq
,
1788 if (IS_ERR(reply
)) {
1789 err
= PTR_ERR(reply
);
1790 ovs_dp_detach_port(vport
);
1794 ovs_notify(reply
, info
, &ovs_dp_vport_multicast_group
);
1802 static int ovs_vport_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1804 struct nlattr
**a
= info
->attrs
;
1805 struct sk_buff
*reply
;
1806 struct vport
*vport
;
1810 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
1811 err
= PTR_ERR(vport
);
1816 if (a
[OVS_VPORT_ATTR_TYPE
] &&
1817 nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]) != vport
->ops
->type
)
1820 reply
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1826 if (!err
&& a
[OVS_VPORT_ATTR_OPTIONS
])
1827 err
= ovs_vport_set_options(vport
, a
[OVS_VPORT_ATTR_OPTIONS
]);
1831 if (a
[OVS_VPORT_ATTR_UPCALL_PID
])
1832 vport
->upcall_portid
= nla_get_u32(a
[OVS_VPORT_ATTR_UPCALL_PID
]);
1834 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
1835 info
->snd_seq
, 0, OVS_VPORT_CMD_NEW
);
1839 ovs_notify(reply
, info
, &ovs_dp_vport_multicast_group
);
1852 static int ovs_vport_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1854 struct nlattr
**a
= info
->attrs
;
1855 struct sk_buff
*reply
;
1856 struct vport
*vport
;
1860 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
1861 err
= PTR_ERR(vport
);
1865 if (vport
->port_no
== OVSP_LOCAL
) {
1870 reply
= ovs_vport_cmd_build_info(vport
, info
->snd_portid
, info
->snd_seq
,
1872 err
= PTR_ERR(reply
);
1877 ovs_dp_detach_port(vport
);
1879 ovs_notify(reply
, info
, &ovs_dp_vport_multicast_group
);
1886 static int ovs_vport_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1888 struct nlattr
**a
= info
->attrs
;
1889 struct ovs_header
*ovs_header
= info
->userhdr
;
1890 struct sk_buff
*reply
;
1891 struct vport
*vport
;
1895 vport
= lookup_vport(sock_net(skb
->sk
), ovs_header
, a
);
1896 err
= PTR_ERR(vport
);
1900 reply
= ovs_vport_cmd_build_info(vport
, info
->snd_portid
, info
->snd_seq
,
1902 err
= PTR_ERR(reply
);
1908 return genlmsg_reply(reply
, info
);
1915 static int ovs_vport_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1917 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1918 struct datapath
*dp
;
1919 int bucket
= cb
->args
[0], skip
= cb
->args
[1];
1922 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1927 for (i
= bucket
; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1928 struct vport
*vport
;
1931 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
1933 ovs_vport_cmd_fill_info(vport
, skb
,
1934 NETLINK_CB(cb
->skb
).portid
,
1937 OVS_VPORT_CMD_NEW
) < 0)
1953 static struct genl_ops dp_vport_genl_ops
[] = {
1954 { .cmd
= OVS_VPORT_CMD_NEW
,
1955 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1956 .policy
= vport_policy
,
1957 .doit
= ovs_vport_cmd_new
1959 { .cmd
= OVS_VPORT_CMD_DEL
,
1960 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1961 .policy
= vport_policy
,
1962 .doit
= ovs_vport_cmd_del
1964 { .cmd
= OVS_VPORT_CMD_GET
,
1965 .flags
= 0, /* OK for unprivileged users. */
1966 .policy
= vport_policy
,
1967 .doit
= ovs_vport_cmd_get
,
1968 .dumpit
= ovs_vport_cmd_dump
1970 { .cmd
= OVS_VPORT_CMD_SET
,
1971 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1972 .policy
= vport_policy
,
1973 .doit
= ovs_vport_cmd_set
,
1977 struct genl_family_and_ops
{
1978 struct genl_family
*family
;
1979 struct genl_ops
*ops
;
1981 struct genl_multicast_group
*group
;
1984 static const struct genl_family_and_ops dp_genl_families
[] = {
1985 { &dp_datapath_genl_family
,
1986 dp_datapath_genl_ops
, ARRAY_SIZE(dp_datapath_genl_ops
),
1987 &ovs_dp_datapath_multicast_group
},
1988 { &dp_vport_genl_family
,
1989 dp_vport_genl_ops
, ARRAY_SIZE(dp_vport_genl_ops
),
1990 &ovs_dp_vport_multicast_group
},
1991 { &dp_flow_genl_family
,
1992 dp_flow_genl_ops
, ARRAY_SIZE(dp_flow_genl_ops
),
1993 &ovs_dp_flow_multicast_group
},
1994 { &dp_packet_genl_family
,
1995 dp_packet_genl_ops
, ARRAY_SIZE(dp_packet_genl_ops
),
1999 static void dp_unregister_genl(int n_families
)
2003 for (i
= 0; i
< n_families
; i
++)
2004 genl_unregister_family(dp_genl_families
[i
].family
);
2007 static int dp_register_genl(void)
2014 for (i
= 0; i
< ARRAY_SIZE(dp_genl_families
); i
++) {
2015 const struct genl_family_and_ops
*f
= &dp_genl_families
[i
];
2017 err
= genl_register_family_with_ops(f
->family
, f
->ops
,
2024 err
= genl_register_mc_group(f
->family
, f
->group
);
2033 dp_unregister_genl(n_registered
);
2037 static void rehash_flow_table(struct work_struct
*work
)
2039 struct datapath
*dp
;
2045 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2047 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
2048 struct flow_table
*old_table
= ovsl_dereference(dp
->table
);
2049 struct flow_table
*new_table
;
2051 new_table
= ovs_flow_tbl_rehash(old_table
);
2052 if (!IS_ERR(new_table
)) {
2053 rcu_assign_pointer(dp
->table
, new_table
);
2054 ovs_flow_tbl_deferred_destroy(old_table
);
2060 schedule_delayed_work(&rehash_flow_wq
, REHASH_FLOW_INTERVAL
);
2063 static int __net_init
ovs_init_net(struct net
*net
)
2065 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2067 INIT_LIST_HEAD(&ovs_net
->dps
);
2068 INIT_WORK(&ovs_net
->dp_notify_work
, ovs_dp_notify_wq
);
2072 static void __net_exit
ovs_exit_net(struct net
*net
)
2074 struct datapath
*dp
, *dp_next
;
2075 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2078 list_for_each_entry_safe(dp
, dp_next
, &ovs_net
->dps
, list_node
)
2082 cancel_work_sync(&ovs_net
->dp_notify_work
);
2085 static struct pernet_operations ovs_net_ops
= {
2086 .init
= ovs_init_net
,
2087 .exit
= ovs_exit_net
,
2089 .size
= sizeof(struct ovs_net
),
2092 static int __init
dp_init(void)
2096 BUILD_BUG_ON(sizeof(struct ovs_skb_cb
) > FIELD_SIZEOF(struct sk_buff
, cb
));
2098 pr_info("Open vSwitch switching datapath\n");
2100 err
= ovs_flow_init();
2104 err
= ovs_vport_init();
2106 goto error_flow_exit
;
2108 err
= register_pernet_device(&ovs_net_ops
);
2110 goto error_vport_exit
;
2112 err
= register_netdevice_notifier(&ovs_dp_device_notifier
);
2114 goto error_netns_exit
;
2116 err
= dp_register_genl();
2118 goto error_unreg_notifier
;
2120 schedule_delayed_work(&rehash_flow_wq
, REHASH_FLOW_INTERVAL
);
2124 error_unreg_notifier
:
2125 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2127 unregister_pernet_device(&ovs_net_ops
);
2136 static void dp_cleanup(void)
2138 cancel_delayed_work_sync(&rehash_flow_wq
);
2139 dp_unregister_genl(ARRAY_SIZE(dp_genl_families
));
2140 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2141 unregister_pernet_device(&ovs_net_ops
);
2147 module_init(dp_init
);
2148 module_exit(dp_cleanup
);
2150 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2151 MODULE_LICENSE("GPL");