2 * Copyright (c) 2007-2015 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/version.h>
40 #include <linux/ethtool.h>
41 #include <linux/wait.h>
42 #include <asm/div64.h>
43 #include <linux/highmem.h>
44 #include <linux/netfilter_bridge.h>
45 #include <linux/netfilter_ipv4.h>
46 #include <linux/inetdevice.h>
47 #include <linux/list.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <net/genetlink.h>
52 #include <net/net_namespace.h>
53 #include <net/netns/generic.h>
57 #include "conntrack.h"
59 #include "flow_table.h"
60 #include "flow_netlink.h"
63 #include "vport-internal_dev.h"
64 #include "vport-netdev.h"
66 unsigned int ovs_net_id __read_mostly
;
68 static struct genl_family dp_packet_genl_family
;
69 static struct genl_family dp_flow_genl_family
;
70 static struct genl_family dp_datapath_genl_family
;
72 static const struct nla_policy flow_policy
[];
74 static const struct genl_multicast_group ovs_dp_flow_multicast_group
= {
75 .name
= OVS_FLOW_MCGROUP
,
78 static const struct genl_multicast_group ovs_dp_datapath_multicast_group
= {
79 .name
= OVS_DATAPATH_MCGROUP
,
82 const struct genl_multicast_group ovs_dp_vport_multicast_group
= {
83 .name
= OVS_VPORT_MCGROUP
,
86 /* Check if need to build a reply message.
87 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply.
89 static bool ovs_must_notify(struct genl_family
*family
, struct genl_info
*info
,
92 return info
->nlhdr
->nlmsg_flags
& NLM_F_ECHO
||
93 genl_has_listeners(family
, genl_info_net(info
), group
);
96 static void ovs_notify(struct genl_family
*family
,
97 const struct genl_multicast_group
*grp
,
98 struct sk_buff
*skb
, struct genl_info
*info
)
100 genl_notify(family
, skb
, info
, GROUP_ID(grp
), GFP_KERNEL
);
106 * All writes e.g. Writes to device state (add/remove datapath, port, set
107 * operations on vports, etc.), Writes to other state (flow table
108 * modifications, set miscellaneous datapath parameters, etc.) are protected
111 * Reads are protected by RCU.
113 * There are a few special cases (mostly stats) that have their own
114 * synchronization but they nest under all of above and don't interact with
117 * The RTNL lock nests inside ovs_mutex.
120 static DEFINE_MUTEX(ovs_mutex
);
124 mutex_lock(&ovs_mutex
);
127 void ovs_unlock(void)
129 mutex_unlock(&ovs_mutex
);
132 #ifdef CONFIG_LOCKDEP
133 int lockdep_ovsl_is_held(void)
136 return lockdep_is_held(&ovs_mutex
);
142 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*,
143 const struct sw_flow_key
*,
144 const struct dp_upcall_info
*,
146 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*,
147 const struct sw_flow_key
*,
148 const struct dp_upcall_info
*,
151 /* Must be called with rcu_read_lock or ovs_mutex. */
152 const char *ovs_dp_name(const struct datapath
*dp
)
154 struct vport
*vport
= ovs_vport_ovsl_rcu(dp
, OVSP_LOCAL
);
155 return ovs_vport_name(vport
);
158 static int get_dpifindex(const struct datapath
*dp
)
165 local
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
167 ifindex
= local
->dev
->ifindex
;
176 static void destroy_dp_rcu(struct rcu_head
*rcu
)
178 struct datapath
*dp
= container_of(rcu
, struct datapath
, rcu
);
180 ovs_flow_tbl_destroy(&dp
->table
);
181 free_percpu(dp
->stats_percpu
);
187 static struct hlist_head
*vport_hash_bucket(const struct datapath
*dp
,
190 return &dp
->ports
[port_no
& (DP_VPORT_HASH_BUCKETS
- 1)];
193 /* Called with ovs_mutex or RCU read lock. */
194 struct vport
*ovs_lookup_vport(const struct datapath
*dp
, u16 port_no
)
197 struct hlist_head
*head
;
199 head
= vport_hash_bucket(dp
, port_no
);
200 hlist_for_each_entry_rcu(vport
, head
, dp_hash_node
) {
201 if (vport
->port_no
== port_no
)
207 /* Called with ovs_mutex. */
208 static struct vport
*new_vport(const struct vport_parms
*parms
)
212 vport
= ovs_vport_add(parms
);
213 if (!IS_ERR(vport
)) {
214 struct datapath
*dp
= parms
->dp
;
215 struct hlist_head
*head
= vport_hash_bucket(dp
, vport
->port_no
);
217 hlist_add_head_rcu(&vport
->dp_hash_node
, head
);
222 void ovs_dp_detach_port(struct vport
*p
)
226 /* First drop references to device. */
227 hlist_del_rcu(&p
->dp_hash_node
);
229 /* Then destroy it. */
233 /* Must be called with rcu_read_lock. */
234 void ovs_dp_process_packet(struct sk_buff
*skb
, struct sw_flow_key
*key
)
236 const struct vport
*p
= OVS_CB(skb
)->input_vport
;
237 struct datapath
*dp
= p
->dp
;
238 struct sw_flow
*flow
;
239 struct sw_flow_actions
*sf_acts
;
240 struct dp_stats_percpu
*stats
;
244 stats
= this_cpu_ptr(dp
->stats_percpu
);
247 flow
= ovs_flow_tbl_lookup_stats(&dp
->table
, key
, skb_get_hash(skb
),
249 if (unlikely(!flow
)) {
250 struct dp_upcall_info upcall
;
253 memset(&upcall
, 0, sizeof(upcall
));
254 upcall
.cmd
= OVS_PACKET_CMD_MISS
;
255 upcall
.portid
= ovs_vport_find_upcall_portid(p
, skb
);
256 upcall
.mru
= OVS_CB(skb
)->mru
;
257 error
= ovs_dp_upcall(dp
, skb
, key
, &upcall
, 0);
262 stats_counter
= &stats
->n_missed
;
266 ovs_flow_stats_update(flow
, key
->tp
.flags
, skb
);
267 sf_acts
= rcu_dereference(flow
->sf_acts
);
268 ovs_execute_actions(dp
, skb
, sf_acts
, key
);
270 stats_counter
= &stats
->n_hit
;
273 /* Update datapath statistics. */
274 u64_stats_update_begin(&stats
->syncp
);
276 stats
->n_mask_hit
+= n_mask_hit
;
277 u64_stats_update_end(&stats
->syncp
);
280 int ovs_dp_upcall(struct datapath
*dp
, struct sk_buff
*skb
,
281 const struct sw_flow_key
*key
,
282 const struct dp_upcall_info
*upcall_info
,
285 struct dp_stats_percpu
*stats
;
288 if (upcall_info
->portid
== 0) {
293 if (!skb_is_gso(skb
))
294 err
= queue_userspace_packet(dp
, skb
, key
, upcall_info
, cutlen
);
296 err
= queue_gso_packets(dp
, skb
, key
, upcall_info
, cutlen
);
303 stats
= this_cpu_ptr(dp
->stats_percpu
);
305 u64_stats_update_begin(&stats
->syncp
);
307 u64_stats_update_end(&stats
->syncp
);
312 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*skb
,
313 const struct sw_flow_key
*key
,
314 const struct dp_upcall_info
*upcall_info
,
317 #ifdef HAVE_SKB_GSO_UDP
318 unsigned int gso_type
= skb_shinfo(skb
)->gso_type
;
319 struct sw_flow_key later_key
;
321 struct sk_buff
*segs
, *nskb
;
322 struct ovs_skb_cb ovs_cb
;
325 ovs_cb
= *OVS_CB(skb
);
326 segs
= __skb_gso_segment(skb
, NETIF_F_SG
, false);
327 *OVS_CB(skb
) = ovs_cb
;
329 return PTR_ERR(segs
);
332 #ifdef HAVE_SKB_GSO_UDP
333 if (gso_type
& SKB_GSO_UDP
) {
334 /* The initial flow key extracted by ovs_flow_key_extract()
335 * in this case is for a first fragment, so we need to
336 * properly mark later fragments.
339 later_key
.ip
.frag
= OVS_FRAG_TYPE_LATER
;
342 /* Queue all of the segments. */
345 *OVS_CB(skb
) = ovs_cb
;
346 #ifdef HAVE_SKB_GSO_UDP
347 if (gso_type
& SKB_GSO_UDP
&& skb
!= segs
)
350 err
= queue_userspace_packet(dp
, skb
, key
, upcall_info
, cutlen
);
354 } while ((skb
= skb
->next
));
356 /* Free all of the segments. */
364 } while ((skb
= nskb
));
368 static size_t upcall_msg_size(const struct dp_upcall_info
*upcall_info
,
369 unsigned int hdrlen
, int actions_attrlen
)
371 size_t size
= NLMSG_ALIGN(sizeof(struct ovs_header
))
372 + nla_total_size(hdrlen
) /* OVS_PACKET_ATTR_PACKET */
373 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
374 + nla_total_size(sizeof(unsigned int)); /* OVS_PACKET_ATTR_LEN */
376 /* OVS_PACKET_ATTR_USERDATA */
377 if (upcall_info
->userdata
)
378 size
+= NLA_ALIGN(upcall_info
->userdata
->nla_len
);
380 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
381 if (upcall_info
->egress_tun_info
)
382 size
+= nla_total_size(ovs_tun_key_attr_size());
384 /* OVS_PACKET_ATTR_ACTIONS */
385 if (upcall_info
->actions_len
)
386 size
+= nla_total_size(actions_attrlen
);
388 /* OVS_PACKET_ATTR_MRU */
389 if (upcall_info
->mru
)
390 size
+= nla_total_size(sizeof(upcall_info
->mru
));
395 static void pad_packet(struct datapath
*dp
, struct sk_buff
*skb
)
397 if (!(dp
->user_features
& OVS_DP_F_UNALIGNED
)) {
398 size_t plen
= NLA_ALIGN(skb
->len
) - skb
->len
;
401 skb_put_zero(skb
, plen
);
405 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*skb
,
406 const struct sw_flow_key
*key
,
407 const struct dp_upcall_info
*upcall_info
,
410 struct ovs_header
*upcall
;
411 struct sk_buff
*nskb
= NULL
;
412 struct sk_buff
*user_skb
= NULL
; /* to be queued to userspace */
418 dp_ifindex
= get_dpifindex(dp
);
422 if (skb_vlan_tag_present(skb
)) {
423 nskb
= skb_clone(skb
, GFP_ATOMIC
);
427 nskb
= __vlan_hwaccel_push_inside(nskb
);
434 if (nla_attr_size(skb
->len
) > USHRT_MAX
) {
439 /* Complete checksum if needed */
440 if (skb
->ip_summed
== CHECKSUM_PARTIAL
&&
441 (err
= skb_csum_hwoffload_help(skb
, 0)))
444 /* Older versions of OVS user space enforce alignment of the last
445 * Netlink attribute to NLA_ALIGNTO which would require extensive
446 * padding logic. Only perform zerocopy if padding is not required.
448 if (dp
->user_features
& OVS_DP_F_UNALIGNED
)
449 hlen
= skb_zerocopy_headlen(skb
);
453 len
= upcall_msg_size(upcall_info
, hlen
- cutlen
,
454 OVS_CB(skb
)->acts_origlen
);
455 user_skb
= genlmsg_new(len
, GFP_ATOMIC
);
461 upcall
= genlmsg_put(user_skb
, 0, 0, &dp_packet_genl_family
,
462 0, upcall_info
->cmd
);
467 upcall
->dp_ifindex
= dp_ifindex
;
469 err
= ovs_nla_put_key(key
, key
, OVS_PACKET_ATTR_KEY
, false, user_skb
);
472 if (upcall_info
->userdata
)
473 __nla_put(user_skb
, OVS_PACKET_ATTR_USERDATA
,
474 nla_len(upcall_info
->userdata
),
475 nla_data(upcall_info
->userdata
));
478 if (upcall_info
->egress_tun_info
) {
479 nla
= nla_nest_start_noflag(user_skb
,
480 OVS_PACKET_ATTR_EGRESS_TUN_KEY
);
485 err
= ovs_nla_put_tunnel_info(user_skb
,
486 upcall_info
->egress_tun_info
);
488 nla_nest_end(user_skb
, nla
);
491 if (upcall_info
->actions_len
) {
492 nla
= nla_nest_start_noflag(user_skb
, OVS_PACKET_ATTR_ACTIONS
);
497 err
= ovs_nla_put_actions(upcall_info
->actions
,
498 upcall_info
->actions_len
,
501 nla_nest_end(user_skb
, nla
);
503 nla_nest_cancel(user_skb
, nla
);
506 /* Add OVS_PACKET_ATTR_MRU */
507 if (upcall_info
->mru
) {
508 if (nla_put_u16(user_skb
, OVS_PACKET_ATTR_MRU
,
513 pad_packet(dp
, user_skb
);
516 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
518 if (nla_put_u32(user_skb
, OVS_PACKET_ATTR_LEN
,
523 pad_packet(dp
, user_skb
);
526 /* Only reserve room for attribute header, packet data is added
529 if (!(nla
= nla_reserve(user_skb
, OVS_PACKET_ATTR_PACKET
, 0))) {
533 nla
->nla_len
= nla_attr_size(skb
->len
- cutlen
);
535 err
= skb_zerocopy(user_skb
, skb
, skb
->len
- cutlen
, hlen
);
539 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
540 pad_packet(dp
, user_skb
);
542 ((struct nlmsghdr
*) user_skb
->data
)->nlmsg_len
= user_skb
->len
;
544 err
= genlmsg_unicast(ovs_dp_get_net(dp
), user_skb
, upcall_info
->portid
);
554 static int ovs_packet_cmd_execute(struct sk_buff
*skb
, struct genl_info
*info
)
556 struct ovs_header
*ovs_header
= info
->userhdr
;
557 struct net
*net
= sock_net(skb
->sk
);
558 struct nlattr
**a
= info
->attrs
;
559 struct sw_flow_actions
*acts
;
560 struct sk_buff
*packet
;
561 struct sw_flow
*flow
;
562 struct sw_flow_actions
*sf_acts
;
564 struct vport
*input_vport
;
568 bool log
= !a
[OVS_PACKET_ATTR_PROBE
];
571 if (!a
[OVS_PACKET_ATTR_PACKET
] || !a
[OVS_PACKET_ATTR_KEY
] ||
572 !a
[OVS_PACKET_ATTR_ACTIONS
])
575 len
= nla_len(a
[OVS_PACKET_ATTR_PACKET
]);
576 packet
= __dev_alloc_skb(NET_IP_ALIGN
+ len
, GFP_KERNEL
);
580 skb_reserve(packet
, NET_IP_ALIGN
);
582 nla_memcpy(__skb_put(packet
, len
), a
[OVS_PACKET_ATTR_PACKET
], len
);
584 /* Set packet's mru */
585 if (a
[OVS_PACKET_ATTR_MRU
]) {
586 mru
= nla_get_u16(a
[OVS_PACKET_ATTR_MRU
]);
587 packet
->ignore_df
= 1;
589 OVS_CB(packet
)->mru
= mru
;
591 /* Build an sw_flow for sending this packet. */
592 flow
= ovs_flow_alloc();
597 err
= ovs_flow_key_extract_userspace(net
, a
[OVS_PACKET_ATTR_KEY
],
598 packet
, &flow
->key
, log
);
602 err
= ovs_nla_copy_actions(net
, a
[OVS_PACKET_ATTR_ACTIONS
],
603 &flow
->key
, &acts
, log
);
607 rcu_assign_pointer(flow
->sf_acts
, acts
);
608 packet
->priority
= flow
->key
.phy
.priority
;
609 packet
->mark
= flow
->key
.phy
.skb_mark
;
612 dp
= get_dp_rcu(net
, ovs_header
->dp_ifindex
);
617 input_vport
= ovs_vport_rcu(dp
, flow
->key
.phy
.in_port
);
619 input_vport
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
624 packet
->dev
= input_vport
->dev
;
625 OVS_CB(packet
)->input_vport
= input_vport
;
626 sf_acts
= rcu_dereference(flow
->sf_acts
);
629 err
= ovs_execute_actions(dp
, packet
, sf_acts
, &flow
->key
);
633 ovs_flow_free(flow
, false);
639 ovs_flow_free(flow
, false);
646 static const struct nla_policy packet_policy
[OVS_PACKET_ATTR_MAX
+ 1] = {
647 [OVS_PACKET_ATTR_PACKET
] = { .len
= ETH_HLEN
},
648 [OVS_PACKET_ATTR_KEY
] = { .type
= NLA_NESTED
},
649 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
650 [OVS_PACKET_ATTR_PROBE
] = { .type
= NLA_FLAG
},
651 [OVS_PACKET_ATTR_MRU
] = { .type
= NLA_U16
},
654 static struct genl_ops dp_packet_genl_ops
[] = {
655 { .cmd
= OVS_PACKET_CMD_EXECUTE
,
656 #ifdef HAVE_GENL_VALIDATE_FLAGS
657 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
659 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
660 .policy
= packet_policy
,
661 .doit
= ovs_packet_cmd_execute
665 static struct genl_family dp_packet_genl_family __ro_after_init
= {
666 .hdrsize
= sizeof(struct ovs_header
),
667 .name
= OVS_PACKET_FAMILY
,
668 .version
= OVS_PACKET_VERSION
,
669 .maxattr
= OVS_PACKET_ATTR_MAX
,
671 .parallel_ops
= true,
672 .ops
= dp_packet_genl_ops
,
673 .n_ops
= ARRAY_SIZE(dp_packet_genl_ops
),
674 .module
= THIS_MODULE
,
677 static void get_dp_stats(const struct datapath
*dp
, struct ovs_dp_stats
*stats
,
678 struct ovs_dp_megaflow_stats
*mega_stats
)
682 memset(mega_stats
, 0, sizeof(*mega_stats
));
684 stats
->n_flows
= ovs_flow_tbl_count(&dp
->table
);
685 mega_stats
->n_masks
= ovs_flow_tbl_num_masks(&dp
->table
);
687 stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
689 for_each_possible_cpu(i
) {
690 const struct dp_stats_percpu
*percpu_stats
;
691 struct dp_stats_percpu local_stats
;
694 percpu_stats
= per_cpu_ptr(dp
->stats_percpu
, i
);
697 start
= u64_stats_fetch_begin_irq(&percpu_stats
->syncp
);
698 local_stats
= *percpu_stats
;
699 } while (u64_stats_fetch_retry_irq(&percpu_stats
->syncp
, start
));
701 stats
->n_hit
+= local_stats
.n_hit
;
702 stats
->n_missed
+= local_stats
.n_missed
;
703 stats
->n_lost
+= local_stats
.n_lost
;
704 mega_stats
->n_mask_hit
+= local_stats
.n_mask_hit
;
708 static bool should_fill_key(const struct sw_flow_id
*sfid
, uint32_t ufid_flags
)
710 return ovs_identifier_is_ufid(sfid
) &&
711 !(ufid_flags
& OVS_UFID_F_OMIT_KEY
);
714 static bool should_fill_mask(uint32_t ufid_flags
)
716 return !(ufid_flags
& OVS_UFID_F_OMIT_MASK
);
719 static bool should_fill_actions(uint32_t ufid_flags
)
721 return !(ufid_flags
& OVS_UFID_F_OMIT_ACTIONS
);
724 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions
*acts
,
725 const struct sw_flow_id
*sfid
,
728 size_t len
= NLMSG_ALIGN(sizeof(struct ovs_header
));
730 /* OVS_FLOW_ATTR_UFID */
731 if (sfid
&& ovs_identifier_is_ufid(sfid
))
732 len
+= nla_total_size(sfid
->ufid_len
);
734 /* OVS_FLOW_ATTR_KEY */
735 if (!sfid
|| should_fill_key(sfid
, ufid_flags
))
736 len
+= nla_total_size(ovs_key_attr_size());
738 /* OVS_FLOW_ATTR_MASK */
739 if (should_fill_mask(ufid_flags
))
740 len
+= nla_total_size(ovs_key_attr_size());
742 /* OVS_FLOW_ATTR_ACTIONS */
743 if (should_fill_actions(ufid_flags
))
744 len
+= nla_total_size(acts
->orig_len
);
747 + nla_total_size_64bit(sizeof(struct ovs_flow_stats
)) /* OVS_FLOW_ATTR_STATS */
748 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
749 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
752 /* Called with ovs_mutex or RCU read lock. */
753 static int ovs_flow_cmd_fill_stats(const struct sw_flow
*flow
,
756 struct ovs_flow_stats stats
;
760 ovs_flow_stats_get(flow
, &stats
, &used
, &tcp_flags
);
763 nla_put_u64_64bit(skb
, OVS_FLOW_ATTR_USED
, ovs_flow_used_time(used
),
767 if (stats
.n_packets
&&
768 nla_put_64bit(skb
, OVS_FLOW_ATTR_STATS
,
769 sizeof(struct ovs_flow_stats
), &stats
,
773 if ((u8
)ntohs(tcp_flags
) &&
774 nla_put_u8(skb
, OVS_FLOW_ATTR_TCP_FLAGS
, (u8
)ntohs(tcp_flags
)))
780 /* Called with ovs_mutex or RCU read lock. */
781 static int ovs_flow_cmd_fill_actions(const struct sw_flow
*flow
,
782 struct sk_buff
*skb
, int skb_orig_len
)
784 struct nlattr
*start
;
787 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
788 * this is the first flow to be dumped into 'skb'. This is unusual for
789 * Netlink but individual action lists can be longer than
790 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
791 * The userspace caller can always fetch the actions separately if it
792 * really wants them. (Most userspace callers in fact don't care.)
794 * This can only fail for dump operations because the skb is always
795 * properly sized for single flows.
797 start
= nla_nest_start_noflag(skb
, OVS_FLOW_ATTR_ACTIONS
);
799 const struct sw_flow_actions
*sf_acts
;
801 sf_acts
= rcu_dereference_ovsl(flow
->sf_acts
);
802 err
= ovs_nla_put_actions(sf_acts
->actions
,
803 sf_acts
->actions_len
, skb
);
806 nla_nest_end(skb
, start
);
811 nla_nest_cancel(skb
, start
);
813 } else if (skb_orig_len
) {
820 /* Called with ovs_mutex or RCU read lock. */
821 static int ovs_flow_cmd_fill_info(const struct sw_flow
*flow
, int dp_ifindex
,
822 struct sk_buff
*skb
, u32 portid
,
823 u32 seq
, u32 flags
, u8 cmd
, u32 ufid_flags
)
825 const int skb_orig_len
= skb
->len
;
826 struct ovs_header
*ovs_header
;
829 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_flow_genl_family
,
834 ovs_header
->dp_ifindex
= dp_ifindex
;
836 err
= ovs_nla_put_identifier(flow
, skb
);
840 if (should_fill_key(&flow
->id
, ufid_flags
)) {
841 err
= ovs_nla_put_masked_key(flow
, skb
);
846 if (should_fill_mask(ufid_flags
)) {
847 err
= ovs_nla_put_mask(flow
, skb
);
852 err
= ovs_flow_cmd_fill_stats(flow
, skb
);
856 if (should_fill_actions(ufid_flags
)) {
857 err
= ovs_flow_cmd_fill_actions(flow
, skb
, skb_orig_len
);
862 genlmsg_end(skb
, ovs_header
);
866 genlmsg_cancel(skb
, ovs_header
);
870 /* May not be called with RCU read lock. */
871 static struct sk_buff
*ovs_flow_cmd_alloc_info(const struct sw_flow_actions
*acts
,
872 const struct sw_flow_id
*sfid
,
873 struct genl_info
*info
,
880 if (!always
&& !ovs_must_notify(&dp_flow_genl_family
, info
,
881 GROUP_ID(&ovs_dp_flow_multicast_group
)))
884 len
= ovs_flow_cmd_msg_size(acts
, sfid
, ufid_flags
);
885 skb
= genlmsg_new(len
, GFP_KERNEL
);
887 return ERR_PTR(-ENOMEM
);
892 /* Called with ovs_mutex. */
893 static struct sk_buff
*ovs_flow_cmd_build_info(const struct sw_flow
*flow
,
895 struct genl_info
*info
, u8 cmd
,
896 bool always
, u32 ufid_flags
)
901 skb
= ovs_flow_cmd_alloc_info(ovsl_dereference(flow
->sf_acts
),
902 &flow
->id
, info
, always
, ufid_flags
);
903 if (IS_ERR_OR_NULL(skb
))
906 retval
= ovs_flow_cmd_fill_info(flow
, dp_ifindex
, skb
,
907 info
->snd_portid
, info
->snd_seq
, 0,
913 static int ovs_flow_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
915 struct net
*net
= sock_net(skb
->sk
);
916 struct nlattr
**a
= info
->attrs
;
917 struct ovs_header
*ovs_header
= info
->userhdr
;
918 struct sw_flow
*flow
= NULL
, *new_flow
;
919 struct sw_flow_mask mask
;
920 struct sk_buff
*reply
;
922 struct sw_flow_actions
*acts
;
923 struct sw_flow_match match
;
924 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
926 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
928 /* Must have key and actions. */
930 if (!a
[OVS_FLOW_ATTR_KEY
]) {
931 OVS_NLERR(log
, "Flow key attr not present in new flow.");
934 if (!a
[OVS_FLOW_ATTR_ACTIONS
]) {
935 OVS_NLERR(log
, "Flow actions attr not present in new flow.");
939 /* Most of the time we need to allocate a new flow, do it before
942 new_flow
= ovs_flow_alloc();
943 if (IS_ERR(new_flow
)) {
944 error
= PTR_ERR(new_flow
);
949 ovs_match_init(&match
, &new_flow
->key
, false, &mask
);
950 error
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
],
951 a
[OVS_FLOW_ATTR_MASK
], log
);
955 /* Extract flow identifier. */
956 error
= ovs_nla_get_identifier(&new_flow
->id
, a
[OVS_FLOW_ATTR_UFID
],
957 &new_flow
->key
, log
);
961 /* unmasked key is needed to match when ufid is not used. */
962 if (ovs_identifier_is_key(&new_flow
->id
))
963 match
.key
= new_flow
->id
.unmasked_key
;
965 ovs_flow_mask_key(&new_flow
->key
, &new_flow
->key
, true, &mask
);
967 /* Validate actions. */
968 error
= ovs_nla_copy_actions(net
, a
[OVS_FLOW_ATTR_ACTIONS
],
969 &new_flow
->key
, &acts
, log
);
971 OVS_NLERR(log
, "Flow actions may not be safe on all matching packets.");
975 reply
= ovs_flow_cmd_alloc_info(acts
, &new_flow
->id
, info
, false,
978 error
= PTR_ERR(reply
);
983 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
989 /* Check if this is a duplicate flow */
990 if (ovs_identifier_is_ufid(&new_flow
->id
))
991 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &new_flow
->id
);
993 flow
= ovs_flow_tbl_lookup(&dp
->table
, &new_flow
->key
);
995 rcu_assign_pointer(new_flow
->sf_acts
, acts
);
997 /* Put flow in bucket. */
998 error
= ovs_flow_tbl_insert(&dp
->table
, new_flow
, &mask
);
999 if (unlikely(error
)) {
1001 goto err_unlock_ovs
;
1004 if (unlikely(reply
)) {
1005 error
= ovs_flow_cmd_fill_info(new_flow
,
1006 ovs_header
->dp_ifindex
,
1007 reply
, info
->snd_portid
,
1015 struct sw_flow_actions
*old_acts
;
1017 /* Bail out if we're not allowed to modify an existing flow.
1018 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1019 * because Generic Netlink treats the latter as a dump
1020 * request. We also accept NLM_F_EXCL in case that bug ever
1023 if (unlikely(info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
1026 goto err_unlock_ovs
;
1028 /* The flow identifier has to be the same for flow updates.
1029 * Look for any overlapping flow.
1031 if (unlikely(!ovs_flow_cmp(flow
, &match
))) {
1032 if (ovs_identifier_is_key(&flow
->id
))
1033 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
,
1035 else /* UFID matches but key is different */
1039 goto err_unlock_ovs
;
1042 /* Update actions. */
1043 old_acts
= ovsl_dereference(flow
->sf_acts
);
1044 rcu_assign_pointer(flow
->sf_acts
, acts
);
1046 if (unlikely(reply
)) {
1047 error
= ovs_flow_cmd_fill_info(flow
,
1048 ovs_header
->dp_ifindex
,
1049 reply
, info
->snd_portid
,
1057 ovs_nla_free_flow_actions_rcu(old_acts
);
1058 ovs_flow_free(new_flow
, false);
1062 ovs_notify(&dp_flow_genl_family
, &ovs_dp_flow_multicast_group
, reply
, info
);
1069 ovs_nla_free_flow_actions(acts
);
1071 ovs_flow_free(new_flow
, false);
1076 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1077 static noinline_for_stack
struct sw_flow_actions
*get_flow_actions(struct net
*net
,
1078 const struct nlattr
*a
,
1079 const struct sw_flow_key
*key
,
1080 const struct sw_flow_mask
*mask
,
1083 struct sw_flow_actions
*acts
;
1084 struct sw_flow_key masked_key
;
1087 ovs_flow_mask_key(&masked_key
, key
, true, mask
);
1088 error
= ovs_nla_copy_actions(net
, a
, &masked_key
, &acts
, log
);
1091 "Actions may not be safe on all matching packets");
1092 return ERR_PTR(error
);
1098 /* Factor out match-init and action-copy to avoid
1099 * "Wframe-larger-than=1024" warning. Because mask is only
1100 * used to get actions, we new a function to save some
1103 * If there are not key and action attrs, we return 0
1104 * directly. In the case, the caller will also not use the
1105 * match as before. If there is action attr, we try to get
1106 * actions and save them to *acts. Before returning from
1107 * the function, we reset the match->mask pointer. Because
1108 * we should not to return match object with dangling reference
1111 static noinline_for_stack
int
1112 ovs_nla_init_match_and_action(struct net
*net
,
1113 struct sw_flow_match
*match
,
1114 struct sw_flow_key
*key
,
1116 struct sw_flow_actions
**acts
,
1119 struct sw_flow_mask mask
;
1122 if (a
[OVS_FLOW_ATTR_KEY
]) {
1123 ovs_match_init(match
, key
, true, &mask
);
1124 error
= ovs_nla_get_match(net
, match
, a
[OVS_FLOW_ATTR_KEY
],
1125 a
[OVS_FLOW_ATTR_MASK
], log
);
1130 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
1131 if (!a
[OVS_FLOW_ATTR_KEY
]) {
1133 "Flow key attribute not present in set flow.");
1138 *acts
= get_flow_actions(net
, a
[OVS_FLOW_ATTR_ACTIONS
], key
,
1140 if (IS_ERR(*acts
)) {
1141 error
= PTR_ERR(*acts
);
1146 /* On success, error is 0. */
1152 static int ovs_flow_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1154 struct net
*net
= sock_net(skb
->sk
);
1155 struct nlattr
**a
= info
->attrs
;
1156 struct ovs_header
*ovs_header
= info
->userhdr
;
1157 struct sw_flow_key key
;
1158 struct sw_flow
*flow
;
1159 struct sk_buff
*reply
= NULL
;
1160 struct datapath
*dp
;
1161 struct sw_flow_actions
*old_acts
= NULL
, *acts
= NULL
;
1162 struct sw_flow_match match
;
1163 struct sw_flow_id sfid
;
1164 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1166 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1169 ufid_present
= ovs_nla_get_ufid(&sfid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1170 if (!a
[OVS_FLOW_ATTR_KEY
] && !ufid_present
) {
1172 "Flow set message rejected, Key attribute missing.");
1176 error
= ovs_nla_init_match_and_action(net
, &match
, &key
, a
,
1182 /* Can allocate before locking if have acts. */
1183 reply
= ovs_flow_cmd_alloc_info(acts
, &sfid
, info
, false,
1185 if (IS_ERR(reply
)) {
1186 error
= PTR_ERR(reply
);
1187 goto err_kfree_acts
;
1192 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1193 if (unlikely(!dp
)) {
1195 goto err_unlock_ovs
;
1197 /* Check that the flow exists. */
1199 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &sfid
);
1201 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1202 if (unlikely(!flow
)) {
1204 goto err_unlock_ovs
;
1207 /* Update actions, if present. */
1209 old_acts
= ovsl_dereference(flow
->sf_acts
);
1210 rcu_assign_pointer(flow
->sf_acts
, acts
);
1212 if (unlikely(reply
)) {
1213 error
= ovs_flow_cmd_fill_info(flow
,
1214 ovs_header
->dp_ifindex
,
1215 reply
, info
->snd_portid
,
1222 /* Could not alloc without acts before locking. */
1223 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
,
1224 info
, OVS_FLOW_CMD_SET
, false,
1227 if (unlikely(IS_ERR(reply
))) {
1228 error
= PTR_ERR(reply
);
1229 goto err_unlock_ovs
;
1234 if (a
[OVS_FLOW_ATTR_CLEAR
])
1235 ovs_flow_stats_clear(flow
);
1239 ovs_notify(&dp_flow_genl_family
, &ovs_dp_flow_multicast_group
, reply
, info
);
1241 ovs_nla_free_flow_actions_rcu(old_acts
);
1249 ovs_nla_free_flow_actions(acts
);
1254 static int ovs_flow_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1256 struct nlattr
**a
= info
->attrs
;
1257 struct ovs_header
*ovs_header
= info
->userhdr
;
1258 struct net
*net
= sock_net(skb
->sk
);
1259 struct sw_flow_key key
;
1260 struct sk_buff
*reply
;
1261 struct sw_flow
*flow
;
1262 struct datapath
*dp
;
1263 struct sw_flow_match match
;
1264 struct sw_flow_id ufid
;
1265 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1267 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1270 ufid_present
= ovs_nla_get_ufid(&ufid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1271 if (a
[OVS_FLOW_ATTR_KEY
]) {
1272 ovs_match_init(&match
, &key
, true, NULL
);
1273 err
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
], NULL
,
1275 } else if (!ufid_present
) {
1277 "Flow get message rejected, Key attribute missing.");
1284 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1291 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &ufid
);
1293 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1299 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
, info
,
1300 OVS_FLOW_CMD_GET
, true, ufid_flags
);
1301 if (IS_ERR(reply
)) {
1302 err
= PTR_ERR(reply
);
1307 return genlmsg_reply(reply
, info
);
1313 static int ovs_flow_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1315 struct nlattr
**a
= info
->attrs
;
1316 struct ovs_header
*ovs_header
= info
->userhdr
;
1317 struct net
*net
= sock_net(skb
->sk
);
1318 struct sw_flow_key key
;
1319 struct sk_buff
*reply
;
1320 struct sw_flow
*flow
= NULL
;
1321 struct datapath
*dp
;
1322 struct sw_flow_match match
;
1323 struct sw_flow_id ufid
;
1324 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1326 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1329 ufid_present
= ovs_nla_get_ufid(&ufid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1330 if (a
[OVS_FLOW_ATTR_KEY
]) {
1331 ovs_match_init(&match
, &key
, true, NULL
);
1332 err
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
],
1339 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1340 if (unlikely(!dp
)) {
1345 if (unlikely(!a
[OVS_FLOW_ATTR_KEY
] && !ufid_present
)) {
1346 err
= ovs_flow_tbl_flush(&dp
->table
);
1351 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &ufid
);
1353 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1354 if (unlikely(!flow
)) {
1359 ovs_flow_tbl_remove(&dp
->table
, flow
);
1362 reply
= ovs_flow_cmd_alloc_info(rcu_dereference_raw(flow
->sf_acts
),
1363 &flow
->id
, info
, false, ufid_flags
);
1365 if (likely(reply
)) {
1366 if (likely(!IS_ERR(reply
))) {
1367 rcu_read_lock(); /*To keep RCU checker happy. */
1368 err
= ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
,
1369 reply
, info
->snd_portid
,
1375 ovs_notify(&dp_flow_genl_family
, &ovs_dp_flow_multicast_group
, reply
, info
);
1377 genl_set_err(&dp_flow_genl_family
, sock_net(skb
->sk
), 0,
1378 GROUP_ID(&ovs_dp_flow_multicast_group
), PTR_ERR(reply
));
1383 ovs_flow_free(flow
, true);
1390 static int ovs_flow_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1392 struct nlattr
*a
[__OVS_FLOW_ATTR_MAX
];
1393 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1394 struct table_instance
*ti
;
1395 struct datapath
*dp
;
1399 err
= genlmsg_parse(cb
->nlh
, &dp_flow_genl_family
, a
,
1400 OVS_FLOW_ATTR_MAX
, flow_policy
, NULL
);
1403 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1406 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1412 ti
= rcu_dereference(dp
->table
.ti
);
1414 struct sw_flow
*flow
;
1417 bucket
= cb
->args
[0];
1419 flow
= ovs_flow_tbl_dump_next(ti
, &bucket
, &obj
);
1423 if (ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
, skb
,
1424 NETLINK_CB(cb
->skb
).portid
,
1425 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1426 OVS_FLOW_CMD_GET
, ufid_flags
) < 0)
1429 cb
->args
[0] = bucket
;
1436 static const struct nla_policy flow_policy
[OVS_FLOW_ATTR_MAX
+ 1] = {
1437 [OVS_FLOW_ATTR_KEY
] = { .type
= NLA_NESTED
},
1438 [OVS_FLOW_ATTR_MASK
] = { .type
= NLA_NESTED
},
1439 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
1440 [OVS_FLOW_ATTR_CLEAR
] = { .type
= NLA_FLAG
},
1441 [OVS_FLOW_ATTR_PROBE
] = { .type
= NLA_FLAG
},
1442 [OVS_FLOW_ATTR_UFID
] = { .type
= NLA_UNSPEC
, .len
= 1 },
1443 [OVS_FLOW_ATTR_UFID_FLAGS
] = { .type
= NLA_U32
},
1446 static const struct genl_ops dp_flow_genl_ops
[] = {
1447 { .cmd
= OVS_FLOW_CMD_NEW
,
1448 #ifdef HAVE_GENL_VALIDATE_FLAGS
1449 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1451 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1452 .policy
= flow_policy
,
1453 .doit
= ovs_flow_cmd_new
1455 { .cmd
= OVS_FLOW_CMD_DEL
,
1456 #ifdef HAVE_GENL_VALIDATE_FLAGS
1457 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1459 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1460 .policy
= flow_policy
,
1461 .doit
= ovs_flow_cmd_del
1463 { .cmd
= OVS_FLOW_CMD_GET
,
1464 #ifdef HAVE_GENL_VALIDATE_FLAGS
1465 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1467 .flags
= 0, /* OK for unprivileged users. */
1468 .policy
= flow_policy
,
1469 .doit
= ovs_flow_cmd_get
,
1470 .dumpit
= ovs_flow_cmd_dump
1472 { .cmd
= OVS_FLOW_CMD_SET
,
1473 #ifdef HAVE_GENL_VALIDATE_FLAGS
1474 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1476 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1477 .policy
= flow_policy
,
1478 .doit
= ovs_flow_cmd_set
,
1482 static struct genl_family dp_flow_genl_family __ro_after_init
= {
1483 .hdrsize
= sizeof(struct ovs_header
),
1484 .name
= OVS_FLOW_FAMILY
,
1485 .version
= OVS_FLOW_VERSION
,
1486 .maxattr
= OVS_FLOW_ATTR_MAX
,
1488 .parallel_ops
= true,
1489 .ops
= dp_flow_genl_ops
,
1490 .n_ops
= ARRAY_SIZE(dp_flow_genl_ops
),
1491 .mcgrps
= &ovs_dp_flow_multicast_group
,
1493 .module
= THIS_MODULE
,
1496 static size_t ovs_dp_cmd_msg_size(void)
1498 size_t msgsize
= NLMSG_ALIGN(sizeof(struct ovs_header
));
1500 msgsize
+= nla_total_size(IFNAMSIZ
);
1501 msgsize
+= nla_total_size_64bit(sizeof(struct ovs_dp_stats
));
1502 msgsize
+= nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats
));
1503 msgsize
+= nla_total_size(sizeof(u32
)); /* OVS_DP_ATTR_USER_FEATURES */
1508 /* Called with ovs_mutex. */
1509 static int ovs_dp_cmd_fill_info(struct datapath
*dp
, struct sk_buff
*skb
,
1510 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1512 struct ovs_header
*ovs_header
;
1513 struct ovs_dp_stats dp_stats
;
1514 struct ovs_dp_megaflow_stats dp_megaflow_stats
;
1517 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_datapath_genl_family
,
1522 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
1524 err
= nla_put_string(skb
, OVS_DP_ATTR_NAME
, ovs_dp_name(dp
));
1526 goto nla_put_failure
;
1528 get_dp_stats(dp
, &dp_stats
, &dp_megaflow_stats
);
1529 if (nla_put_64bit(skb
, OVS_DP_ATTR_STATS
, sizeof(struct ovs_dp_stats
),
1530 &dp_stats
, OVS_DP_ATTR_PAD
))
1531 goto nla_put_failure
;
1533 if (nla_put_64bit(skb
, OVS_DP_ATTR_MEGAFLOW_STATS
,
1534 sizeof(struct ovs_dp_megaflow_stats
),
1535 &dp_megaflow_stats
, OVS_DP_ATTR_PAD
))
1536 goto nla_put_failure
;
1538 if (nla_put_u32(skb
, OVS_DP_ATTR_USER_FEATURES
, dp
->user_features
))
1539 goto nla_put_failure
;
1541 genlmsg_end(skb
, ovs_header
);
1545 genlmsg_cancel(skb
, ovs_header
);
1550 static struct sk_buff
*ovs_dp_cmd_alloc_info(void)
1552 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL
);
1555 /* Called with rcu_read_lock or ovs_mutex. */
1556 static struct datapath
*lookup_datapath(struct net
*net
,
1557 const struct ovs_header
*ovs_header
,
1558 struct nlattr
*a
[OVS_DP_ATTR_MAX
+ 1])
1560 struct datapath
*dp
;
1562 if (!a
[OVS_DP_ATTR_NAME
])
1563 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1565 struct vport
*vport
;
1567 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_DP_ATTR_NAME
]));
1568 dp
= vport
&& vport
->port_no
== OVSP_LOCAL
? vport
->dp
: NULL
;
1570 return dp
? dp
: ERR_PTR(-ENODEV
);
1573 static void ovs_dp_reset_user_features(struct sk_buff
*skb
, struct genl_info
*info
)
1575 struct datapath
*dp
;
1577 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1581 WARN(dp
->user_features
, "Dropping previously announced user features\n");
1582 dp
->user_features
= 0;
1585 static void ovs_dp_change(struct datapath
*dp
, struct nlattr
*a
[])
1587 if (a
[OVS_DP_ATTR_USER_FEATURES
])
1588 dp
->user_features
= nla_get_u32(a
[OVS_DP_ATTR_USER_FEATURES
]);
1591 static int ovs_dp_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1593 struct nlattr
**a
= info
->attrs
;
1594 struct vport_parms parms
;
1595 struct sk_buff
*reply
;
1596 struct datapath
*dp
;
1597 struct vport
*vport
;
1598 struct ovs_net
*ovs_net
;
1602 if (!a
[OVS_DP_ATTR_NAME
] || !a
[OVS_DP_ATTR_UPCALL_PID
])
1605 reply
= ovs_dp_cmd_alloc_info();
1610 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1612 goto err_free_reply
;
1614 ovs_dp_set_net(dp
, sock_net(skb
->sk
));
1616 /* Allocate table. */
1617 err
= ovs_flow_tbl_init(&dp
->table
);
1621 dp
->stats_percpu
= netdev_alloc_pcpu_stats(struct dp_stats_percpu
);
1622 if (!dp
->stats_percpu
) {
1624 goto err_destroy_table
;
1627 dp
->ports
= kmalloc_array(DP_VPORT_HASH_BUCKETS
,
1628 sizeof(struct hlist_head
),
1632 goto err_destroy_percpu
;
1635 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
1636 INIT_HLIST_HEAD(&dp
->ports
[i
]);
1638 err
= ovs_meters_init(dp
);
1640 goto err_destroy_ports_array
;
1642 /* Set up our datapath device. */
1643 parms
.name
= nla_data(a
[OVS_DP_ATTR_NAME
]);
1644 parms
.type
= OVS_VPORT_TYPE_INTERNAL
;
1645 parms
.options
= NULL
;
1647 parms
.port_no
= OVSP_LOCAL
;
1648 parms
.upcall_portids
= a
[OVS_DP_ATTR_UPCALL_PID
];
1650 ovs_dp_change(dp
, a
);
1652 /* So far only local changes have been made, now need the lock. */
1655 vport
= new_vport(&parms
);
1656 if (IS_ERR(vport
)) {
1657 err
= PTR_ERR(vport
);
1661 if (err
== -EEXIST
) {
1662 /* An outdated user space instance that does not understand
1663 * the concept of user_features has attempted to create a new
1664 * datapath and is likely to reuse it. Drop all user features.
1666 if (info
->genlhdr
->version
< OVS_DP_VER_FEATURES
)
1667 ovs_dp_reset_user_features(skb
, info
);
1670 goto err_destroy_meters
;
1673 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1674 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1677 ovs_net
= net_generic(ovs_dp_get_net(dp
), ovs_net_id
);
1678 list_add_tail_rcu(&dp
->list_node
, &ovs_net
->dps
);
1682 ovs_notify(&dp_datapath_genl_family
, &ovs_dp_datapath_multicast_group
, reply
, info
);
1687 ovs_meters_exit(dp
);
1688 err_destroy_ports_array
:
1691 free_percpu(dp
->stats_percpu
);
1693 ovs_flow_tbl_destroy(&dp
->table
);
1702 /* Called with ovs_mutex. */
1703 static void __dp_destroy(struct datapath
*dp
)
1707 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1708 struct vport
*vport
;
1709 struct hlist_node
*n
;
1711 hlist_for_each_entry_safe(vport
, n
, &dp
->ports
[i
], dp_hash_node
)
1712 if (vport
->port_no
!= OVSP_LOCAL
)
1713 ovs_dp_detach_port(vport
);
1716 list_del_rcu(&dp
->list_node
);
1718 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1719 * all ports in datapath are destroyed first before freeing datapath.
1721 ovs_dp_detach_port(ovs_vport_ovsl(dp
, OVSP_LOCAL
));
1723 /* RCU destroy the flow table */
1724 call_rcu(&dp
->rcu
, destroy_dp_rcu
);
1727 static int ovs_dp_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1729 struct sk_buff
*reply
;
1730 struct datapath
*dp
;
1733 reply
= ovs_dp_cmd_alloc_info();
1738 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1741 goto err_unlock_free
;
1743 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1744 info
->snd_seq
, 0, OVS_DP_CMD_DEL
);
1750 ovs_notify(&dp_datapath_genl_family
, &ovs_dp_datapath_multicast_group
, reply
, info
);
1759 static int ovs_dp_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1761 struct sk_buff
*reply
;
1762 struct datapath
*dp
;
1765 reply
= ovs_dp_cmd_alloc_info();
1770 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1773 goto err_unlock_free
;
1775 ovs_dp_change(dp
, info
->attrs
);
1777 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1778 info
->snd_seq
, 0, OVS_DP_CMD_GET
);
1783 ovs_notify(&dp_datapath_genl_family
, &ovs_dp_datapath_multicast_group
, reply
, info
);
1792 static int ovs_dp_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1794 struct sk_buff
*reply
;
1795 struct datapath
*dp
;
1798 reply
= ovs_dp_cmd_alloc_info();
1803 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1806 goto err_unlock_free
;
1808 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1809 info
->snd_seq
, 0, OVS_DP_CMD_GET
);
1813 return genlmsg_reply(reply
, info
);
1821 static int ovs_dp_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1823 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
1824 struct datapath
*dp
;
1825 int skip
= cb
->args
[0];
1829 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
1831 ovs_dp_cmd_fill_info(dp
, skb
, NETLINK_CB(cb
->skb
).portid
,
1832 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1833 OVS_DP_CMD_GET
) < 0)
1844 static const struct nla_policy datapath_policy
[OVS_DP_ATTR_MAX
+ 1] = {
1845 [OVS_DP_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1846 [OVS_DP_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1847 [OVS_DP_ATTR_USER_FEATURES
] = { .type
= NLA_U32
},
1850 static const struct genl_ops dp_datapath_genl_ops
[] = {
1851 { .cmd
= OVS_DP_CMD_NEW
,
1852 #ifdef HAVE_GENL_VALIDATE_FLAGS
1853 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1855 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1856 .policy
= datapath_policy
,
1857 .doit
= ovs_dp_cmd_new
1859 { .cmd
= OVS_DP_CMD_DEL
,
1860 #ifdef HAVE_GENL_VALIDATE_FLAGS
1861 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1863 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1864 .policy
= datapath_policy
,
1865 .doit
= ovs_dp_cmd_del
1867 { .cmd
= OVS_DP_CMD_GET
,
1868 #ifdef HAVE_GENL_VALIDATE_FLAGS
1869 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1871 .flags
= 0, /* OK for unprivileged users. */
1872 .policy
= datapath_policy
,
1873 .doit
= ovs_dp_cmd_get
,
1874 .dumpit
= ovs_dp_cmd_dump
1876 { .cmd
= OVS_DP_CMD_SET
,
1877 #ifdef HAVE_GENL_VALIDATE_FLAGS
1878 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1880 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1881 .policy
= datapath_policy
,
1882 .doit
= ovs_dp_cmd_set
,
1886 static struct genl_family dp_datapath_genl_family __ro_after_init
= {
1887 .hdrsize
= sizeof(struct ovs_header
),
1888 .name
= OVS_DATAPATH_FAMILY
,
1889 .version
= OVS_DATAPATH_VERSION
,
1890 .maxattr
= OVS_DP_ATTR_MAX
,
1892 .parallel_ops
= true,
1893 .ops
= dp_datapath_genl_ops
,
1894 .n_ops
= ARRAY_SIZE(dp_datapath_genl_ops
),
1895 .mcgrps
= &ovs_dp_datapath_multicast_group
,
1897 .module
= THIS_MODULE
,
1900 /* Called with ovs_mutex or RCU read lock. */
1901 static int ovs_vport_cmd_fill_info(struct vport
*vport
, struct sk_buff
*skb
,
1902 struct net
*net
, u32 portid
, u32 seq
,
1905 struct ovs_header
*ovs_header
;
1906 struct ovs_vport_stats vport_stats
;
1909 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_vport_genl_family
,
1914 ovs_header
->dp_ifindex
= get_dpifindex(vport
->dp
);
1916 if (nla_put_u32(skb
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
) ||
1917 nla_put_u32(skb
, OVS_VPORT_ATTR_TYPE
, vport
->ops
->type
) ||
1918 nla_put_string(skb
, OVS_VPORT_ATTR_NAME
,
1919 ovs_vport_name(vport
)) ||
1920 nla_put_u32(skb
, OVS_VPORT_ATTR_IFINDEX
, vport
->dev
->ifindex
))
1921 goto nla_put_failure
;
1923 #ifdef HAVE_PEERNET2ID_ALLOC
1924 if (!net_eq(net
, dev_net(vport
->dev
))) {
1925 int id
= peernet2id_alloc(net
, dev_net(vport
->dev
));
1927 if (nla_put_s32(skb
, OVS_VPORT_ATTR_NETNSID
, id
))
1928 goto nla_put_failure
;
1932 ovs_vport_get_stats(vport
, &vport_stats
);
1933 if (nla_put_64bit(skb
, OVS_VPORT_ATTR_STATS
,
1934 sizeof(struct ovs_vport_stats
), &vport_stats
,
1935 OVS_VPORT_ATTR_PAD
))
1936 goto nla_put_failure
;
1938 if (ovs_vport_get_upcall_portids(vport
, skb
))
1939 goto nla_put_failure
;
1941 err
= ovs_vport_get_options(vport
, skb
);
1942 if (err
== -EMSGSIZE
)
1945 genlmsg_end(skb
, ovs_header
);
1951 genlmsg_cancel(skb
, ovs_header
);
1955 static struct sk_buff
*ovs_vport_cmd_alloc_info(void)
1957 return nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1960 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1961 struct sk_buff
*ovs_vport_cmd_build_info(struct vport
*vport
, struct net
*net
,
1962 u32 portid
, u32 seq
, u8 cmd
)
1964 struct sk_buff
*skb
;
1967 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1969 return ERR_PTR(-ENOMEM
);
1971 retval
= ovs_vport_cmd_fill_info(vport
, skb
, net
, portid
, seq
, 0, cmd
);
1977 /* Called with ovs_mutex or RCU read lock. */
1978 static struct vport
*lookup_vport(struct net
*net
,
1979 const struct ovs_header
*ovs_header
,
1980 struct nlattr
*a
[OVS_VPORT_ATTR_MAX
+ 1])
1982 struct datapath
*dp
;
1983 struct vport
*vport
;
1985 if (a
[OVS_VPORT_ATTR_IFINDEX
])
1986 return ERR_PTR(-EOPNOTSUPP
);
1987 if (a
[OVS_VPORT_ATTR_NAME
]) {
1988 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_VPORT_ATTR_NAME
]));
1990 return ERR_PTR(-ENODEV
);
1991 if (ovs_header
->dp_ifindex
&&
1992 ovs_header
->dp_ifindex
!= get_dpifindex(vport
->dp
))
1993 return ERR_PTR(-ENODEV
);
1995 } else if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
1996 u32 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
1998 if (port_no
>= DP_MAX_PORTS
)
1999 return ERR_PTR(-EFBIG
);
2001 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
2003 return ERR_PTR(-ENODEV
);
2005 vport
= ovs_vport_ovsl_rcu(dp
, port_no
);
2007 return ERR_PTR(-ENODEV
);
2010 return ERR_PTR(-EINVAL
);
2014 /* Called with ovs_mutex */
2015 static void update_headroom(struct datapath
*dp
)
2017 unsigned dev_headroom
, max_headroom
= 0;
2018 struct net_device
*dev
;
2019 struct vport
*vport
;
2022 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2023 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
2025 dev_headroom
= netdev_get_fwd_headroom(dev
);
2026 if (dev_headroom
> max_headroom
)
2027 max_headroom
= dev_headroom
;
2031 dp
->max_headroom
= max_headroom
;
2032 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
2033 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
)
2034 netdev_set_rx_headroom(vport
->dev
, max_headroom
);
2037 static int ovs_vport_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
2039 struct nlattr
**a
= info
->attrs
;
2040 struct ovs_header
*ovs_header
= info
->userhdr
;
2041 struct vport_parms parms
;
2042 struct sk_buff
*reply
;
2043 struct vport
*vport
;
2044 struct datapath
*dp
;
2048 if (!a
[OVS_VPORT_ATTR_NAME
] || !a
[OVS_VPORT_ATTR_TYPE
] ||
2049 !a
[OVS_VPORT_ATTR_UPCALL_PID
])
2051 if (a
[OVS_VPORT_ATTR_IFINDEX
])
2054 port_no
= a
[OVS_VPORT_ATTR_PORT_NO
]
2055 ? nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]) : 0;
2056 if (port_no
>= DP_MAX_PORTS
)
2059 reply
= ovs_vport_cmd_alloc_info();
2065 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
2068 goto exit_unlock_free
;
2071 vport
= ovs_vport_ovsl(dp
, port_no
);
2074 goto exit_unlock_free
;
2076 for (port_no
= 1; ; port_no
++) {
2077 if (port_no
>= DP_MAX_PORTS
) {
2079 goto exit_unlock_free
;
2081 vport
= ovs_vport_ovsl(dp
, port_no
);
2087 parms
.name
= nla_data(a
[OVS_VPORT_ATTR_NAME
]);
2088 parms
.type
= nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
2089 parms
.options
= a
[OVS_VPORT_ATTR_OPTIONS
];
2091 parms
.port_no
= port_no
;
2092 parms
.upcall_portids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
2094 vport
= new_vport(&parms
);
2095 err
= PTR_ERR(vport
);
2096 if (IS_ERR(vport
)) {
2099 goto exit_unlock_free
;
2102 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2103 info
->snd_portid
, info
->snd_seq
, 0,
2107 if (netdev_get_fwd_headroom(vport
->dev
) > dp
->max_headroom
)
2108 update_headroom(dp
);
2110 netdev_set_rx_headroom(vport
->dev
, dp
->max_headroom
);
2114 ovs_notify(&dp_vport_genl_family
, &ovs_dp_vport_multicast_group
, reply
, info
);
2123 static int ovs_vport_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
2125 struct nlattr
**a
= info
->attrs
;
2126 struct sk_buff
*reply
;
2127 struct vport
*vport
;
2130 reply
= ovs_vport_cmd_alloc_info();
2135 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2136 err
= PTR_ERR(vport
);
2138 goto exit_unlock_free
;
2140 if (a
[OVS_VPORT_ATTR_TYPE
] &&
2141 nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]) != vport
->ops
->type
) {
2143 goto exit_unlock_free
;
2146 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
2147 err
= ovs_vport_set_options(vport
, a
[OVS_VPORT_ATTR_OPTIONS
]);
2149 goto exit_unlock_free
;
2152 if (a
[OVS_VPORT_ATTR_UPCALL_PID
]) {
2153 struct nlattr
*ids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
2155 err
= ovs_vport_set_upcall_portids(vport
, ids
);
2157 goto exit_unlock_free
;
2160 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2161 info
->snd_portid
, info
->snd_seq
, 0,
2166 ovs_notify(&dp_vport_genl_family
, &ovs_dp_vport_multicast_group
, reply
, info
);
2175 static int ovs_vport_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
2177 bool must_update_headroom
= false;
2178 struct nlattr
**a
= info
->attrs
;
2179 struct sk_buff
*reply
;
2180 struct datapath
*dp
;
2181 struct vport
*vport
;
2184 reply
= ovs_vport_cmd_alloc_info();
2189 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2190 err
= PTR_ERR(vport
);
2192 goto exit_unlock_free
;
2194 if (vport
->port_no
== OVSP_LOCAL
) {
2196 goto exit_unlock_free
;
2199 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2200 info
->snd_portid
, info
->snd_seq
, 0,
2204 /* the vport deletion may trigger dp headroom update */
2206 if (netdev_get_fwd_headroom(vport
->dev
) == dp
->max_headroom
)
2207 must_update_headroom
= true;
2208 netdev_reset_rx_headroom(vport
->dev
);
2209 ovs_dp_detach_port(vport
);
2211 if (must_update_headroom
)
2212 update_headroom(dp
);
2216 ovs_notify(&dp_vport_genl_family
, &ovs_dp_vport_multicast_group
, reply
, info
);
2225 static int ovs_vport_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
2227 struct nlattr
**a
= info
->attrs
;
2228 struct ovs_header
*ovs_header
= info
->userhdr
;
2229 struct sk_buff
*reply
;
2230 struct vport
*vport
;
2233 reply
= ovs_vport_cmd_alloc_info();
2238 vport
= lookup_vport(sock_net(skb
->sk
), ovs_header
, a
);
2239 err
= PTR_ERR(vport
);
2241 goto exit_unlock_free
;
2242 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2243 info
->snd_portid
, info
->snd_seq
, 0,
2248 return genlmsg_reply(reply
, info
);
2256 static int ovs_vport_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2258 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
2259 struct datapath
*dp
;
2260 int bucket
= cb
->args
[0], skip
= cb
->args
[1];
2264 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
2269 for (i
= bucket
; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2270 struct vport
*vport
;
2273 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
2275 ovs_vport_cmd_fill_info(vport
, skb
,
2277 NETLINK_CB(cb
->skb
).portid
,
2280 OVS_VPORT_CMD_GET
) < 0)
2296 static const struct nla_policy vport_policy
[OVS_VPORT_ATTR_MAX
+ 1] = {
2297 [OVS_VPORT_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
2298 [OVS_VPORT_ATTR_STATS
] = { .len
= sizeof(struct ovs_vport_stats
) },
2299 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NLA_U32
},
2300 [OVS_VPORT_ATTR_TYPE
] = { .type
= NLA_U32
},
2301 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NLA_UNSPEC
},
2302 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NLA_NESTED
},
2303 [OVS_VPORT_ATTR_IFINDEX
] = { .type
= NLA_U32
},
2304 [OVS_VPORT_ATTR_NETNSID
] = { .type
= NLA_S32
},
2307 static const struct genl_ops dp_vport_genl_ops
[] = {
2308 { .cmd
= OVS_VPORT_CMD_NEW
,
2309 #ifdef HAVE_GENL_VALIDATE_FLAGS
2310 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2312 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2313 .policy
= vport_policy
,
2314 .doit
= ovs_vport_cmd_new
2316 { .cmd
= OVS_VPORT_CMD_DEL
,
2317 #ifdef HAVE_GENL_VALIDATE_FLAGS
2318 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2320 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2321 .policy
= vport_policy
,
2322 .doit
= ovs_vport_cmd_del
2324 { .cmd
= OVS_VPORT_CMD_GET
,
2325 #ifdef HAVE_GENL_VALIDATE_FLAGS
2326 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2328 .flags
= 0, /* OK for unprivileged users. */
2329 .policy
= vport_policy
,
2330 .doit
= ovs_vport_cmd_get
,
2331 .dumpit
= ovs_vport_cmd_dump
2333 { .cmd
= OVS_VPORT_CMD_SET
,
2334 #ifdef HAVE_GENL_VALIDATE_FLAGS
2335 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2337 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2338 .policy
= vport_policy
,
2339 .doit
= ovs_vport_cmd_set
,
2343 struct genl_family dp_vport_genl_family __ro_after_init
= {
2344 .hdrsize
= sizeof(struct ovs_header
),
2345 .name
= OVS_VPORT_FAMILY
,
2346 .version
= OVS_VPORT_VERSION
,
2347 .maxattr
= OVS_VPORT_ATTR_MAX
,
2349 .parallel_ops
= true,
2350 .ops
= dp_vport_genl_ops
,
2351 .n_ops
= ARRAY_SIZE(dp_vport_genl_ops
),
2352 .mcgrps
= &ovs_dp_vport_multicast_group
,
2354 .module
= THIS_MODULE
,
2357 static struct genl_family
*dp_genl_families
[] = {
2358 &dp_datapath_genl_family
,
2359 &dp_vport_genl_family
,
2360 &dp_flow_genl_family
,
2361 &dp_packet_genl_family
,
2362 &dp_meter_genl_family
,
2363 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2364 &dp_ct_limit_genl_family
,
2368 static void dp_unregister_genl(int n_families
)
2372 for (i
= 0; i
< n_families
; i
++)
2373 genl_unregister_family(dp_genl_families
[i
]);
2376 static int __init
dp_register_genl(void)
2381 for (i
= 0; i
< ARRAY_SIZE(dp_genl_families
); i
++) {
2383 err
= genl_register_family(dp_genl_families
[i
]);
2391 dp_unregister_genl(i
);
2395 static int __net_init
ovs_init_net(struct net
*net
)
2397 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2399 INIT_LIST_HEAD(&ovs_net
->dps
);
2400 INIT_WORK(&ovs_net
->dp_notify_work
, ovs_dp_notify_wq
);
2401 ovs_netns_frags_init(net
);
2402 ovs_netns_frags6_init(net
);
2403 return ovs_ct_init(net
);
2406 static void __net_exit
list_vports_from_net(struct net
*net
, struct net
*dnet
,
2407 struct list_head
*head
)
2409 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2410 struct datapath
*dp
;
2412 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
2415 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2416 struct vport
*vport
;
2418 hlist_for_each_entry(vport
, &dp
->ports
[i
], dp_hash_node
) {
2420 if (vport
->ops
->type
!= OVS_VPORT_TYPE_INTERNAL
)
2423 if (dev_net(vport
->dev
) == dnet
)
2424 list_add(&vport
->detach_list
, head
);
2430 static void __net_exit
ovs_exit_net(struct net
*dnet
)
2432 struct datapath
*dp
, *dp_next
;
2433 struct ovs_net
*ovs_net
= net_generic(dnet
, ovs_net_id
);
2434 struct vport
*vport
, *vport_next
;
2438 ovs_netns_frags6_exit(dnet
);
2439 ovs_netns_frags_exit(dnet
);
2442 list_for_each_entry_safe(dp
, dp_next
, &ovs_net
->dps
, list_node
)
2445 #ifdef HAVE_NET_RWSEM
2446 down_read(&net_rwsem
);
2451 list_vports_from_net(net
, dnet
, &head
);
2452 #ifdef HAVE_NET_RWSEM
2453 up_read(&net_rwsem
);
2458 /* Detach all vports from given namespace. */
2459 list_for_each_entry_safe(vport
, vport_next
, &head
, detach_list
) {
2460 list_del(&vport
->detach_list
);
2461 ovs_dp_detach_port(vport
);
2466 cancel_work_sync(&ovs_net
->dp_notify_work
);
2469 static struct pernet_operations ovs_net_ops
= {
2470 .init
= ovs_init_net
,
2471 .exit
= ovs_exit_net
,
2473 .size
= sizeof(struct ovs_net
),
2476 static int __init
dp_init(void)
2480 BUILD_BUG_ON(sizeof(struct ovs_skb_cb
) > FIELD_SIZEOF(struct sk_buff
, cb
));
2482 pr_info("Open vSwitch switching datapath %s\n", VERSION
);
2485 err
= action_fifos_init();
2489 err
= ovs_internal_dev_rtnl_link_register();
2491 goto error_action_fifos_exit
;
2493 err
= ovs_flow_init();
2495 goto error_unreg_rtnl_link
;
2497 err
= ovs_vport_init();
2499 goto error_flow_exit
;
2501 err
= register_pernet_device(&ovs_net_ops
);
2503 goto error_vport_exit
;
2505 err
= compat_init();
2507 goto error_netns_exit
;
2509 err
= register_netdevice_notifier(&ovs_dp_device_notifier
);
2511 goto error_compat_exit
;
2513 err
= ovs_netdev_init();
2515 goto error_unreg_notifier
;
2517 err
= dp_register_genl();
2519 goto error_unreg_netdev
;
2525 error_unreg_notifier
:
2526 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2530 unregister_pernet_device(&ovs_net_ops
);
2535 error_unreg_rtnl_link
:
2536 ovs_internal_dev_rtnl_link_unregister();
2537 error_action_fifos_exit
:
2538 action_fifos_exit();
2544 static void dp_cleanup(void)
2546 dp_unregister_genl(ARRAY_SIZE(dp_genl_families
));
2548 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2550 unregister_pernet_device(&ovs_net_ops
);
2554 ovs_internal_dev_rtnl_link_unregister();
2555 action_fifos_exit();
2559 module_init(dp_init
);
2560 module_exit(dp_cleanup
);
2562 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2563 MODULE_LICENSE("GPL");
2564 MODULE_VERSION(VERSION
);
2565 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY
);
2566 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY
);
2567 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY
);
2568 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY
);
2569 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY
);
2570 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY
);