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
;
245 stats
= this_cpu_ptr(dp
->stats_percpu
);
248 flow
= ovs_flow_tbl_lookup_stats(&dp
->table
, key
, skb_get_hash(skb
),
250 if (unlikely(!flow
)) {
251 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 error
= ovs_execute_actions(dp
, skb
, sf_acts
, key
);
270 net_dbg_ratelimited("ovs: action execution error on datapath %s: %d\n",
271 ovs_dp_name(dp
), error
);
273 stats_counter
= &stats
->n_hit
;
276 /* Update datapath statistics. */
277 u64_stats_update_begin(&stats
->syncp
);
279 stats
->n_mask_hit
+= n_mask_hit
;
280 u64_stats_update_end(&stats
->syncp
);
283 int ovs_dp_upcall(struct datapath
*dp
, struct sk_buff
*skb
,
284 const struct sw_flow_key
*key
,
285 const struct dp_upcall_info
*upcall_info
,
288 struct dp_stats_percpu
*stats
;
291 if (upcall_info
->portid
== 0) {
296 if (!skb_is_gso(skb
))
297 err
= queue_userspace_packet(dp
, skb
, key
, upcall_info
, cutlen
);
299 err
= queue_gso_packets(dp
, skb
, key
, upcall_info
, cutlen
);
306 stats
= this_cpu_ptr(dp
->stats_percpu
);
308 u64_stats_update_begin(&stats
->syncp
);
310 u64_stats_update_end(&stats
->syncp
);
315 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*skb
,
316 const struct sw_flow_key
*key
,
317 const struct dp_upcall_info
*upcall_info
,
320 #ifdef HAVE_SKB_GSO_UDP
321 unsigned int gso_type
= skb_shinfo(skb
)->gso_type
;
322 struct sw_flow_key later_key
;
324 struct sk_buff
*segs
, *nskb
;
325 struct ovs_skb_cb ovs_cb
;
328 ovs_cb
= *OVS_CB(skb
);
329 segs
= __skb_gso_segment(skb
, NETIF_F_SG
, false);
330 *OVS_CB(skb
) = ovs_cb
;
332 return PTR_ERR(segs
);
335 #ifdef HAVE_SKB_GSO_UDP
336 if (gso_type
& SKB_GSO_UDP
) {
337 /* The initial flow key extracted by ovs_flow_key_extract()
338 * in this case is for a first fragment, so we need to
339 * properly mark later fragments.
342 later_key
.ip
.frag
= OVS_FRAG_TYPE_LATER
;
345 /* Queue all of the segments. */
346 skb_list_walk_safe(segs
, skb
, nskb
) {
347 *OVS_CB(skb
) = ovs_cb
;
348 #ifdef HAVE_SKB_GSO_UDP
349 if (gso_type
& SKB_GSO_UDP
&& skb
!= segs
)
352 err
= queue_userspace_packet(dp
, skb
, key
, upcall_info
, cutlen
);
358 /* Free all of the segments. */
359 skb_list_walk_safe(segs
, 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 */
375 + nla_total_size(sizeof(u64
)); /* OVS_PACKET_ATTR_HASH */
377 /* OVS_PACKET_ATTR_USERDATA */
378 if (upcall_info
->userdata
)
379 size
+= NLA_ALIGN(upcall_info
->userdata
->nla_len
);
381 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
382 if (upcall_info
->egress_tun_info
)
383 size
+= nla_total_size(ovs_tun_key_attr_size());
385 /* OVS_PACKET_ATTR_ACTIONS */
386 if (upcall_info
->actions_len
)
387 size
+= nla_total_size(actions_attrlen
);
389 /* OVS_PACKET_ATTR_MRU */
390 if (upcall_info
->mru
)
391 size
+= nla_total_size(sizeof(upcall_info
->mru
));
396 static void pad_packet(struct datapath
*dp
, struct sk_buff
*skb
)
398 if (!(dp
->user_features
& OVS_DP_F_UNALIGNED
)) {
399 size_t plen
= NLA_ALIGN(skb
->len
) - skb
->len
;
402 skb_put_zero(skb
, plen
);
406 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*skb
,
407 const struct sw_flow_key
*key
,
408 const struct dp_upcall_info
*upcall_info
,
411 struct ovs_header
*upcall
;
412 struct sk_buff
*nskb
= NULL
;
413 struct sk_buff
*user_skb
= NULL
; /* to be queued to userspace */
420 dp_ifindex
= get_dpifindex(dp
);
424 if (skb_vlan_tag_present(skb
)) {
425 nskb
= skb_clone(skb
, GFP_ATOMIC
);
429 nskb
= __vlan_hwaccel_push_inside(nskb
);
436 if (nla_attr_size(skb
->len
) > USHRT_MAX
) {
441 /* Complete checksum if needed */
442 if (skb
->ip_summed
== CHECKSUM_PARTIAL
&&
443 (err
= skb_csum_hwoffload_help(skb
, 0)))
446 /* Older versions of OVS user space enforce alignment of the last
447 * Netlink attribute to NLA_ALIGNTO which would require extensive
448 * padding logic. Only perform zerocopy if padding is not required.
450 if (dp
->user_features
& OVS_DP_F_UNALIGNED
)
451 hlen
= skb_zerocopy_headlen(skb
);
455 len
= upcall_msg_size(upcall_info
, hlen
- cutlen
,
456 OVS_CB(skb
)->acts_origlen
);
457 user_skb
= genlmsg_new(len
, GFP_ATOMIC
);
463 upcall
= genlmsg_put(user_skb
, 0, 0, &dp_packet_genl_family
,
464 0, upcall_info
->cmd
);
469 upcall
->dp_ifindex
= dp_ifindex
;
471 err
= ovs_nla_put_key(key
, key
, OVS_PACKET_ATTR_KEY
, false, user_skb
);
475 if (upcall_info
->userdata
)
476 __nla_put(user_skb
, OVS_PACKET_ATTR_USERDATA
,
477 nla_len(upcall_info
->userdata
),
478 nla_data(upcall_info
->userdata
));
481 if (upcall_info
->egress_tun_info
) {
482 nla
= nla_nest_start_noflag(user_skb
,
483 OVS_PACKET_ATTR_EGRESS_TUN_KEY
);
488 err
= ovs_nla_put_tunnel_info(user_skb
,
489 upcall_info
->egress_tun_info
);
493 nla_nest_end(user_skb
, nla
);
496 if (upcall_info
->actions_len
) {
497 nla
= nla_nest_start_noflag(user_skb
, OVS_PACKET_ATTR_ACTIONS
);
502 err
= ovs_nla_put_actions(upcall_info
->actions
,
503 upcall_info
->actions_len
,
506 nla_nest_end(user_skb
, nla
);
508 nla_nest_cancel(user_skb
, nla
);
511 /* Add OVS_PACKET_ATTR_MRU */
512 if (upcall_info
->mru
&&
513 nla_put_u16(user_skb
, OVS_PACKET_ATTR_MRU
, upcall_info
->mru
)) {
518 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
520 nla_put_u32(user_skb
, OVS_PACKET_ATTR_LEN
, skb
->len
)) {
525 /* Add OVS_PACKET_ATTR_HASH */
526 hash
= skb_get_hash_raw(skb
);
529 hash
|= OVS_PACKET_HASH_SW_BIT
;
533 hash
|= OVS_PACKET_HASH_L4_BIT
;
535 if (nla_put(user_skb
, OVS_PACKET_ATTR_HASH
, sizeof (u64
), &hash
)) {
540 /* Only reserve room for attribute header, packet data is added
543 if (!(nla
= nla_reserve(user_skb
, OVS_PACKET_ATTR_PACKET
, 0))) {
547 nla
->nla_len
= nla_attr_size(skb
->len
- cutlen
);
549 err
= skb_zerocopy(user_skb
, skb
, skb
->len
- cutlen
, hlen
);
553 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
554 pad_packet(dp
, user_skb
);
556 ((struct nlmsghdr
*) user_skb
->data
)->nlmsg_len
= user_skb
->len
;
558 err
= genlmsg_unicast(ovs_dp_get_net(dp
), user_skb
, upcall_info
->portid
);
568 static int ovs_packet_cmd_execute(struct sk_buff
*skb
, struct genl_info
*info
)
570 struct ovs_header
*ovs_header
= info
->userhdr
;
571 struct net
*net
= sock_net(skb
->sk
);
572 struct nlattr
**a
= info
->attrs
;
573 struct sw_flow_actions
*acts
;
574 struct sk_buff
*packet
;
575 struct sw_flow
*flow
;
576 struct sw_flow_actions
*sf_acts
;
578 struct vport
*input_vport
;
583 bool log
= !a
[OVS_PACKET_ATTR_PROBE
];
586 if (!a
[OVS_PACKET_ATTR_PACKET
] || !a
[OVS_PACKET_ATTR_KEY
] ||
587 !a
[OVS_PACKET_ATTR_ACTIONS
])
590 len
= nla_len(a
[OVS_PACKET_ATTR_PACKET
]);
591 packet
= __dev_alloc_skb(NET_IP_ALIGN
+ len
, GFP_KERNEL
);
595 skb_reserve(packet
, NET_IP_ALIGN
);
597 nla_memcpy(__skb_put(packet
, len
), a
[OVS_PACKET_ATTR_PACKET
], len
);
599 /* Set packet's mru */
600 if (a
[OVS_PACKET_ATTR_MRU
]) {
601 mru
= nla_get_u16(a
[OVS_PACKET_ATTR_MRU
]);
602 packet
->ignore_df
= 1;
604 OVS_CB(packet
)->mru
= mru
;
606 if (a
[OVS_PACKET_ATTR_HASH
]) {
607 hash
= nla_get_u64(a
[OVS_PACKET_ATTR_HASH
]);
609 __skb_set_hash(packet
, hash
& 0xFFFFFFFFULL
,
610 !!(hash
& OVS_PACKET_HASH_SW_BIT
),
611 !!(hash
& OVS_PACKET_HASH_L4_BIT
));
614 /* Build an sw_flow for sending this packet. */
615 flow
= ovs_flow_alloc();
620 err
= ovs_flow_key_extract_userspace(net
, a
[OVS_PACKET_ATTR_KEY
],
621 packet
, &flow
->key
, log
);
625 err
= ovs_nla_copy_actions(net
, a
[OVS_PACKET_ATTR_ACTIONS
],
626 &flow
->key
, &acts
, log
);
630 rcu_assign_pointer(flow
->sf_acts
, acts
);
631 packet
->priority
= flow
->key
.phy
.priority
;
632 packet
->mark
= flow
->key
.phy
.skb_mark
;
635 dp
= get_dp_rcu(net
, ovs_header
->dp_ifindex
);
640 input_vport
= ovs_vport_rcu(dp
, flow
->key
.phy
.in_port
);
642 input_vport
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
647 packet
->dev
= input_vport
->dev
;
648 OVS_CB(packet
)->input_vport
= input_vport
;
649 sf_acts
= rcu_dereference(flow
->sf_acts
);
652 err
= ovs_execute_actions(dp
, packet
, sf_acts
, &flow
->key
);
656 ovs_flow_free(flow
, false);
662 ovs_flow_free(flow
, false);
669 static const struct nla_policy packet_policy
[OVS_PACKET_ATTR_MAX
+ 1] = {
670 [OVS_PACKET_ATTR_PACKET
] = { .len
= ETH_HLEN
},
671 [OVS_PACKET_ATTR_KEY
] = { .type
= NLA_NESTED
},
672 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
673 [OVS_PACKET_ATTR_PROBE
] = { .type
= NLA_FLAG
},
674 [OVS_PACKET_ATTR_MRU
] = { .type
= NLA_U16
},
675 [OVS_PACKET_ATTR_HASH
] = { .type
= NLA_U64
},
678 static struct genl_ops dp_packet_genl_ops
[] = {
679 { .cmd
= OVS_PACKET_CMD_EXECUTE
,
680 #ifdef HAVE_GENL_VALIDATE_FLAGS
681 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
683 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
684 #ifdef HAVE_GENL_OPS_POLICY
685 .policy
= packet_policy
,
687 .doit
= ovs_packet_cmd_execute
691 static struct genl_family dp_packet_genl_family __ro_after_init
= {
692 .hdrsize
= sizeof(struct ovs_header
),
693 .name
= OVS_PACKET_FAMILY
,
694 .version
= OVS_PACKET_VERSION
,
695 .maxattr
= OVS_PACKET_ATTR_MAX
,
696 #ifndef HAVE_GENL_OPS_POLICY
697 .policy
= packet_policy
,
700 .parallel_ops
= true,
701 .ops
= dp_packet_genl_ops
,
702 .n_ops
= ARRAY_SIZE(dp_packet_genl_ops
),
703 .module
= THIS_MODULE
,
706 static void get_dp_stats(const struct datapath
*dp
, struct ovs_dp_stats
*stats
,
707 struct ovs_dp_megaflow_stats
*mega_stats
)
711 memset(mega_stats
, 0, sizeof(*mega_stats
));
713 stats
->n_flows
= ovs_flow_tbl_count(&dp
->table
);
714 mega_stats
->n_masks
= ovs_flow_tbl_num_masks(&dp
->table
);
716 stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
718 for_each_possible_cpu(i
) {
719 const struct dp_stats_percpu
*percpu_stats
;
720 struct dp_stats_percpu local_stats
;
723 percpu_stats
= per_cpu_ptr(dp
->stats_percpu
, i
);
726 start
= u64_stats_fetch_begin_irq(&percpu_stats
->syncp
);
727 local_stats
= *percpu_stats
;
728 } while (u64_stats_fetch_retry_irq(&percpu_stats
->syncp
, start
));
730 stats
->n_hit
+= local_stats
.n_hit
;
731 stats
->n_missed
+= local_stats
.n_missed
;
732 stats
->n_lost
+= local_stats
.n_lost
;
733 mega_stats
->n_mask_hit
+= local_stats
.n_mask_hit
;
737 static bool should_fill_key(const struct sw_flow_id
*sfid
, uint32_t ufid_flags
)
739 return ovs_identifier_is_ufid(sfid
) &&
740 !(ufid_flags
& OVS_UFID_F_OMIT_KEY
);
743 static bool should_fill_mask(uint32_t ufid_flags
)
745 return !(ufid_flags
& OVS_UFID_F_OMIT_MASK
);
748 static bool should_fill_actions(uint32_t ufid_flags
)
750 return !(ufid_flags
& OVS_UFID_F_OMIT_ACTIONS
);
753 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions
*acts
,
754 const struct sw_flow_id
*sfid
,
757 size_t len
= NLMSG_ALIGN(sizeof(struct ovs_header
));
759 /* OVS_FLOW_ATTR_UFID, or unmasked flow key as fallback
760 * see ovs_nla_put_identifier()
762 if (sfid
&& ovs_identifier_is_ufid(sfid
))
763 len
+= nla_total_size(sfid
->ufid_len
);
765 len
+= nla_total_size(ovs_key_attr_size());
767 /* OVS_FLOW_ATTR_KEY */
768 if (!sfid
|| should_fill_key(sfid
, ufid_flags
))
769 len
+= nla_total_size(ovs_key_attr_size());
771 /* OVS_FLOW_ATTR_MASK */
772 if (should_fill_mask(ufid_flags
))
773 len
+= nla_total_size(ovs_key_attr_size());
775 /* OVS_FLOW_ATTR_ACTIONS */
776 if (should_fill_actions(ufid_flags
))
777 len
+= nla_total_size(acts
->orig_len
);
780 + nla_total_size_64bit(sizeof(struct ovs_flow_stats
)) /* OVS_FLOW_ATTR_STATS */
781 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
782 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
785 /* Called with ovs_mutex or RCU read lock. */
786 static int ovs_flow_cmd_fill_stats(const struct sw_flow
*flow
,
789 struct ovs_flow_stats stats
;
793 ovs_flow_stats_get(flow
, &stats
, &used
, &tcp_flags
);
796 nla_put_u64_64bit(skb
, OVS_FLOW_ATTR_USED
, ovs_flow_used_time(used
),
800 if (stats
.n_packets
&&
801 nla_put_64bit(skb
, OVS_FLOW_ATTR_STATS
,
802 sizeof(struct ovs_flow_stats
), &stats
,
806 if ((u8
)ntohs(tcp_flags
) &&
807 nla_put_u8(skb
, OVS_FLOW_ATTR_TCP_FLAGS
, (u8
)ntohs(tcp_flags
)))
813 /* Called with ovs_mutex or RCU read lock. */
814 static int ovs_flow_cmd_fill_actions(const struct sw_flow
*flow
,
815 struct sk_buff
*skb
, int skb_orig_len
)
817 struct nlattr
*start
;
820 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
821 * this is the first flow to be dumped into 'skb'. This is unusual for
822 * Netlink but individual action lists can be longer than
823 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
824 * The userspace caller can always fetch the actions separately if it
825 * really wants them. (Most userspace callers in fact don't care.)
827 * This can only fail for dump operations because the skb is always
828 * properly sized for single flows.
830 start
= nla_nest_start_noflag(skb
, OVS_FLOW_ATTR_ACTIONS
);
832 const struct sw_flow_actions
*sf_acts
;
834 sf_acts
= rcu_dereference_ovsl(flow
->sf_acts
);
835 err
= ovs_nla_put_actions(sf_acts
->actions
,
836 sf_acts
->actions_len
, skb
);
839 nla_nest_end(skb
, start
);
844 nla_nest_cancel(skb
, start
);
846 } else if (skb_orig_len
) {
853 /* Called with ovs_mutex or RCU read lock. */
854 static int ovs_flow_cmd_fill_info(const struct sw_flow
*flow
, int dp_ifindex
,
855 struct sk_buff
*skb
, u32 portid
,
856 u32 seq
, u32 flags
, u8 cmd
, u32 ufid_flags
)
858 const int skb_orig_len
= skb
->len
;
859 struct ovs_header
*ovs_header
;
862 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_flow_genl_family
,
867 ovs_header
->dp_ifindex
= dp_ifindex
;
869 err
= ovs_nla_put_identifier(flow
, skb
);
873 if (should_fill_key(&flow
->id
, ufid_flags
)) {
874 err
= ovs_nla_put_masked_key(flow
, skb
);
879 if (should_fill_mask(ufid_flags
)) {
880 err
= ovs_nla_put_mask(flow
, skb
);
885 err
= ovs_flow_cmd_fill_stats(flow
, skb
);
889 if (should_fill_actions(ufid_flags
)) {
890 err
= ovs_flow_cmd_fill_actions(flow
, skb
, skb_orig_len
);
895 genlmsg_end(skb
, ovs_header
);
899 genlmsg_cancel(skb
, ovs_header
);
903 /* May not be called with RCU read lock. */
904 static struct sk_buff
*ovs_flow_cmd_alloc_info(const struct sw_flow_actions
*acts
,
905 const struct sw_flow_id
*sfid
,
906 struct genl_info
*info
,
913 if (!always
&& !ovs_must_notify(&dp_flow_genl_family
, info
,
914 GROUP_ID(&ovs_dp_flow_multicast_group
)))
917 len
= ovs_flow_cmd_msg_size(acts
, sfid
, ufid_flags
);
918 skb
= genlmsg_new(len
, GFP_KERNEL
);
920 return ERR_PTR(-ENOMEM
);
925 /* Called with ovs_mutex. */
926 static struct sk_buff
*ovs_flow_cmd_build_info(const struct sw_flow
*flow
,
928 struct genl_info
*info
, u8 cmd
,
929 bool always
, u32 ufid_flags
)
934 skb
= ovs_flow_cmd_alloc_info(ovsl_dereference(flow
->sf_acts
),
935 &flow
->id
, info
, always
, ufid_flags
);
936 if (IS_ERR_OR_NULL(skb
))
939 retval
= ovs_flow_cmd_fill_info(flow
, dp_ifindex
, skb
,
940 info
->snd_portid
, info
->snd_seq
, 0,
942 if (WARN_ON_ONCE(retval
< 0)) {
944 skb
= ERR_PTR(retval
);
949 static int ovs_flow_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
951 struct net
*net
= sock_net(skb
->sk
);
952 struct nlattr
**a
= info
->attrs
;
953 struct ovs_header
*ovs_header
= info
->userhdr
;
954 struct sw_flow
*flow
= NULL
, *new_flow
;
955 struct sw_flow_mask mask
;
956 struct sk_buff
*reply
;
958 struct sw_flow_actions
*acts
;
959 struct sw_flow_match match
;
960 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
962 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
964 /* Must have key and actions. */
966 if (!a
[OVS_FLOW_ATTR_KEY
]) {
967 OVS_NLERR(log
, "Flow key attr not present in new flow.");
970 if (!a
[OVS_FLOW_ATTR_ACTIONS
]) {
971 OVS_NLERR(log
, "Flow actions attr not present in new flow.");
975 /* Most of the time we need to allocate a new flow, do it before
978 new_flow
= ovs_flow_alloc();
979 if (IS_ERR(new_flow
)) {
980 error
= PTR_ERR(new_flow
);
985 ovs_match_init(&match
, &new_flow
->key
, false, &mask
);
986 error
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
],
987 a
[OVS_FLOW_ATTR_MASK
], log
);
991 /* Extract flow identifier. */
992 error
= ovs_nla_get_identifier(&new_flow
->id
, a
[OVS_FLOW_ATTR_UFID
],
993 &new_flow
->key
, log
);
997 /* unmasked key is needed to match when ufid is not used. */
998 if (ovs_identifier_is_key(&new_flow
->id
))
999 match
.key
= new_flow
->id
.unmasked_key
;
1001 ovs_flow_mask_key(&new_flow
->key
, &new_flow
->key
, true, &mask
);
1003 /* Validate actions. */
1004 error
= ovs_nla_copy_actions(net
, a
[OVS_FLOW_ATTR_ACTIONS
],
1005 &new_flow
->key
, &acts
, log
);
1007 OVS_NLERR(log
, "Flow actions may not be safe on all matching packets.");
1008 goto err_kfree_flow
;
1011 reply
= ovs_flow_cmd_alloc_info(acts
, &new_flow
->id
, info
, false,
1013 if (IS_ERR(reply
)) {
1014 error
= PTR_ERR(reply
);
1015 goto err_kfree_acts
;
1019 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1020 if (unlikely(!dp
)) {
1022 goto err_unlock_ovs
;
1025 /* Check if this is a duplicate flow */
1026 if (ovs_identifier_is_ufid(&new_flow
->id
))
1027 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &new_flow
->id
);
1029 flow
= ovs_flow_tbl_lookup(&dp
->table
, &new_flow
->key
);
1030 if (likely(!flow
)) {
1031 rcu_assign_pointer(new_flow
->sf_acts
, acts
);
1033 /* Put flow in bucket. */
1034 error
= ovs_flow_tbl_insert(&dp
->table
, new_flow
, &mask
);
1035 if (unlikely(error
)) {
1037 goto err_unlock_ovs
;
1040 if (unlikely(reply
)) {
1041 error
= ovs_flow_cmd_fill_info(new_flow
,
1042 ovs_header
->dp_ifindex
,
1043 reply
, info
->snd_portid
,
1051 struct sw_flow_actions
*old_acts
;
1053 /* Bail out if we're not allowed to modify an existing flow.
1054 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1055 * because Generic Netlink treats the latter as a dump
1056 * request. We also accept NLM_F_EXCL in case that bug ever
1059 if (unlikely(info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
1062 goto err_unlock_ovs
;
1064 /* The flow identifier has to be the same for flow updates.
1065 * Look for any overlapping flow.
1067 if (unlikely(!ovs_flow_cmp(flow
, &match
))) {
1068 if (ovs_identifier_is_key(&flow
->id
))
1069 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
,
1071 else /* UFID matches but key is different */
1075 goto err_unlock_ovs
;
1078 /* Update actions. */
1079 old_acts
= ovsl_dereference(flow
->sf_acts
);
1080 rcu_assign_pointer(flow
->sf_acts
, acts
);
1082 if (unlikely(reply
)) {
1083 error
= ovs_flow_cmd_fill_info(flow
,
1084 ovs_header
->dp_ifindex
,
1085 reply
, info
->snd_portid
,
1093 ovs_nla_free_flow_actions_rcu(old_acts
);
1094 ovs_flow_free(new_flow
, false);
1098 ovs_notify(&dp_flow_genl_family
, &ovs_dp_flow_multicast_group
, reply
, info
);
1105 ovs_nla_free_flow_actions(acts
);
1107 ovs_flow_free(new_flow
, false);
1112 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1113 static noinline_for_stack
struct sw_flow_actions
*get_flow_actions(struct net
*net
,
1114 const struct nlattr
*a
,
1115 const struct sw_flow_key
*key
,
1116 const struct sw_flow_mask
*mask
,
1119 struct sw_flow_actions
*acts
;
1120 struct sw_flow_key masked_key
;
1123 ovs_flow_mask_key(&masked_key
, key
, true, mask
);
1124 error
= ovs_nla_copy_actions(net
, a
, &masked_key
, &acts
, log
);
1127 "Actions may not be safe on all matching packets");
1128 return ERR_PTR(error
);
1134 /* Factor out match-init and action-copy to avoid
1135 * "Wframe-larger-than=1024" warning. Because mask is only
1136 * used to get actions, we new a function to save some
1139 * If there are not key and action attrs, we return 0
1140 * directly. In the case, the caller will also not use the
1141 * match as before. If there is action attr, we try to get
1142 * actions and save them to *acts. Before returning from
1143 * the function, we reset the match->mask pointer. Because
1144 * we should not to return match object with dangling reference
1147 static noinline_for_stack
int
1148 ovs_nla_init_match_and_action(struct net
*net
,
1149 struct sw_flow_match
*match
,
1150 struct sw_flow_key
*key
,
1152 struct sw_flow_actions
**acts
,
1155 struct sw_flow_mask mask
;
1158 if (a
[OVS_FLOW_ATTR_KEY
]) {
1159 ovs_match_init(match
, key
, true, &mask
);
1160 error
= ovs_nla_get_match(net
, match
, a
[OVS_FLOW_ATTR_KEY
],
1161 a
[OVS_FLOW_ATTR_MASK
], log
);
1166 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
1167 if (!a
[OVS_FLOW_ATTR_KEY
]) {
1169 "Flow key attribute not present in set flow.");
1174 *acts
= get_flow_actions(net
, a
[OVS_FLOW_ATTR_ACTIONS
], key
,
1176 if (IS_ERR(*acts
)) {
1177 error
= PTR_ERR(*acts
);
1182 /* On success, error is 0. */
1188 static int ovs_flow_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1190 struct net
*net
= sock_net(skb
->sk
);
1191 struct nlattr
**a
= info
->attrs
;
1192 struct ovs_header
*ovs_header
= info
->userhdr
;
1193 struct sw_flow_key key
;
1194 struct sw_flow
*flow
;
1195 struct sk_buff
*reply
= NULL
;
1196 struct datapath
*dp
;
1197 struct sw_flow_actions
*old_acts
= NULL
, *acts
= NULL
;
1198 struct sw_flow_match match
;
1199 struct sw_flow_id sfid
;
1200 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1202 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1205 ufid_present
= ovs_nla_get_ufid(&sfid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1206 if (!a
[OVS_FLOW_ATTR_KEY
] && !ufid_present
) {
1208 "Flow set message rejected, Key attribute missing.");
1212 error
= ovs_nla_init_match_and_action(net
, &match
, &key
, a
,
1218 /* Can allocate before locking if have acts. */
1219 reply
= ovs_flow_cmd_alloc_info(acts
, &sfid
, info
, false,
1221 if (IS_ERR(reply
)) {
1222 error
= PTR_ERR(reply
);
1223 goto err_kfree_acts
;
1228 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1229 if (unlikely(!dp
)) {
1231 goto err_unlock_ovs
;
1233 /* Check that the flow exists. */
1235 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &sfid
);
1237 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1238 if (unlikely(!flow
)) {
1240 goto err_unlock_ovs
;
1243 /* Update actions, if present. */
1245 old_acts
= ovsl_dereference(flow
->sf_acts
);
1246 rcu_assign_pointer(flow
->sf_acts
, acts
);
1248 if (unlikely(reply
)) {
1249 error
= ovs_flow_cmd_fill_info(flow
,
1250 ovs_header
->dp_ifindex
,
1251 reply
, info
->snd_portid
,
1258 /* Could not alloc without acts before locking. */
1259 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
,
1260 info
, OVS_FLOW_CMD_SET
, false,
1263 if (unlikely(IS_ERR(reply
))) {
1264 error
= PTR_ERR(reply
);
1265 goto err_unlock_ovs
;
1270 if (a
[OVS_FLOW_ATTR_CLEAR
])
1271 ovs_flow_stats_clear(flow
);
1275 ovs_notify(&dp_flow_genl_family
, &ovs_dp_flow_multicast_group
, reply
, info
);
1277 ovs_nla_free_flow_actions_rcu(old_acts
);
1285 ovs_nla_free_flow_actions(acts
);
1290 static int ovs_flow_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1292 struct nlattr
**a
= info
->attrs
;
1293 struct ovs_header
*ovs_header
= info
->userhdr
;
1294 struct net
*net
= sock_net(skb
->sk
);
1295 struct sw_flow_key key
;
1296 struct sk_buff
*reply
;
1297 struct sw_flow
*flow
;
1298 struct datapath
*dp
;
1299 struct sw_flow_match match
;
1300 struct sw_flow_id ufid
;
1301 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1303 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1306 ufid_present
= ovs_nla_get_ufid(&ufid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1307 if (a
[OVS_FLOW_ATTR_KEY
]) {
1308 ovs_match_init(&match
, &key
, true, NULL
);
1309 err
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
], NULL
,
1311 } else if (!ufid_present
) {
1313 "Flow get message rejected, Key attribute missing.");
1320 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1327 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &ufid
);
1329 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1335 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
, info
,
1336 OVS_FLOW_CMD_GET
, true, ufid_flags
);
1337 if (IS_ERR(reply
)) {
1338 err
= PTR_ERR(reply
);
1343 return genlmsg_reply(reply
, info
);
1349 static int ovs_flow_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1351 struct nlattr
**a
= info
->attrs
;
1352 struct ovs_header
*ovs_header
= info
->userhdr
;
1353 struct net
*net
= sock_net(skb
->sk
);
1354 struct sw_flow_key key
;
1355 struct sk_buff
*reply
;
1356 struct sw_flow
*flow
= NULL
;
1357 struct datapath
*dp
;
1358 struct sw_flow_match match
;
1359 struct sw_flow_id ufid
;
1360 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1362 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1365 ufid_present
= ovs_nla_get_ufid(&ufid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1366 if (a
[OVS_FLOW_ATTR_KEY
]) {
1367 ovs_match_init(&match
, &key
, true, NULL
);
1368 err
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
],
1375 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1376 if (unlikely(!dp
)) {
1381 if (unlikely(!a
[OVS_FLOW_ATTR_KEY
] && !ufid_present
)) {
1382 err
= ovs_flow_tbl_flush(&dp
->table
);
1387 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &ufid
);
1389 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1390 if (unlikely(!flow
)) {
1395 ovs_flow_tbl_remove(&dp
->table
, flow
);
1398 reply
= ovs_flow_cmd_alloc_info(rcu_dereference_raw(flow
->sf_acts
),
1399 &flow
->id
, info
, false, ufid_flags
);
1401 if (likely(reply
)) {
1402 if (!IS_ERR(reply
)) {
1403 rcu_read_lock(); /*To keep RCU checker happy. */
1404 err
= ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
,
1405 reply
, info
->snd_portid
,
1410 if (WARN_ON_ONCE(err
< 0)) {
1414 ovs_notify(&dp_flow_genl_family
, &ovs_dp_flow_multicast_group
, reply
, info
);
1416 genl_set_err(&dp_flow_genl_family
, sock_net(skb
->sk
), 0,
1417 GROUP_ID(&ovs_dp_flow_multicast_group
), PTR_ERR(reply
));
1423 ovs_flow_free(flow
, true);
1430 static int ovs_flow_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1432 struct nlattr
*a
[__OVS_FLOW_ATTR_MAX
];
1433 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1434 struct table_instance
*ti
;
1435 struct datapath
*dp
;
1439 err
= genlmsg_parse_deprecated(cb
->nlh
, &dp_flow_genl_family
, a
,
1440 OVS_FLOW_ATTR_MAX
, flow_policy
, NULL
);
1443 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1446 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1452 ti
= rcu_dereference(dp
->table
.ti
);
1454 struct sw_flow
*flow
;
1457 bucket
= cb
->args
[0];
1459 flow
= ovs_flow_tbl_dump_next(ti
, &bucket
, &obj
);
1463 if (ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
, skb
,
1464 NETLINK_CB(cb
->skb
).portid
,
1465 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1466 OVS_FLOW_CMD_GET
, ufid_flags
) < 0)
1469 cb
->args
[0] = bucket
;
1476 static const struct nla_policy flow_policy
[OVS_FLOW_ATTR_MAX
+ 1] = {
1477 [OVS_FLOW_ATTR_KEY
] = { .type
= NLA_NESTED
},
1478 [OVS_FLOW_ATTR_MASK
] = { .type
= NLA_NESTED
},
1479 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
1480 [OVS_FLOW_ATTR_CLEAR
] = { .type
= NLA_FLAG
},
1481 [OVS_FLOW_ATTR_PROBE
] = { .type
= NLA_FLAG
},
1482 [OVS_FLOW_ATTR_UFID
] = { .type
= NLA_UNSPEC
, .len
= 1 },
1483 [OVS_FLOW_ATTR_UFID_FLAGS
] = { .type
= NLA_U32
},
1486 static const struct genl_ops dp_flow_genl_ops
[] = {
1487 { .cmd
= OVS_FLOW_CMD_NEW
,
1488 #ifdef HAVE_GENL_VALIDATE_FLAGS
1489 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1491 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1492 #ifdef HAVE_GENL_OPS_POLICY
1493 .policy
= flow_policy
,
1495 .doit
= ovs_flow_cmd_new
1497 { .cmd
= OVS_FLOW_CMD_DEL
,
1498 #ifdef HAVE_GENL_VALIDATE_FLAGS
1499 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1501 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1502 #ifdef HAVE_GENL_OPS_POLICY
1503 .policy
= flow_policy
,
1505 .doit
= ovs_flow_cmd_del
1507 { .cmd
= OVS_FLOW_CMD_GET
,
1508 #ifdef HAVE_GENL_VALIDATE_FLAGS
1509 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1511 .flags
= 0, /* OK for unprivileged users. */
1512 #ifdef HAVE_GENL_OPS_POLICY
1513 .policy
= flow_policy
,
1515 .doit
= ovs_flow_cmd_get
,
1516 .dumpit
= ovs_flow_cmd_dump
1518 { .cmd
= OVS_FLOW_CMD_SET
,
1519 #ifdef HAVE_GENL_VALIDATE_FLAGS
1520 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1522 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1523 #ifdef HAVE_GENL_OPS_POLICY
1524 .policy
= flow_policy
,
1526 .doit
= ovs_flow_cmd_set
,
1530 static struct genl_family dp_flow_genl_family __ro_after_init
= {
1531 .hdrsize
= sizeof(struct ovs_header
),
1532 .name
= OVS_FLOW_FAMILY
,
1533 .version
= OVS_FLOW_VERSION
,
1534 .maxattr
= OVS_FLOW_ATTR_MAX
,
1535 #ifndef HAVE_GENL_OPS_POLICY
1536 .policy
= flow_policy
,
1539 .parallel_ops
= true,
1540 .ops
= dp_flow_genl_ops
,
1541 .n_ops
= ARRAY_SIZE(dp_flow_genl_ops
),
1542 .mcgrps
= &ovs_dp_flow_multicast_group
,
1544 .module
= THIS_MODULE
,
1547 static size_t ovs_dp_cmd_msg_size(void)
1549 size_t msgsize
= NLMSG_ALIGN(sizeof(struct ovs_header
));
1551 msgsize
+= nla_total_size(IFNAMSIZ
);
1552 msgsize
+= nla_total_size_64bit(sizeof(struct ovs_dp_stats
));
1553 msgsize
+= nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats
));
1554 msgsize
+= nla_total_size(sizeof(u32
)); /* OVS_DP_ATTR_USER_FEATURES */
1559 /* Called with ovs_mutex. */
1560 static int ovs_dp_cmd_fill_info(struct datapath
*dp
, struct sk_buff
*skb
,
1561 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1563 struct ovs_header
*ovs_header
;
1564 struct ovs_dp_stats dp_stats
;
1565 struct ovs_dp_megaflow_stats dp_megaflow_stats
;
1568 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_datapath_genl_family
,
1573 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
1575 err
= nla_put_string(skb
, OVS_DP_ATTR_NAME
, ovs_dp_name(dp
));
1577 goto nla_put_failure
;
1579 get_dp_stats(dp
, &dp_stats
, &dp_megaflow_stats
);
1580 if (nla_put_64bit(skb
, OVS_DP_ATTR_STATS
, sizeof(struct ovs_dp_stats
),
1581 &dp_stats
, OVS_DP_ATTR_PAD
))
1582 goto nla_put_failure
;
1584 if (nla_put_64bit(skb
, OVS_DP_ATTR_MEGAFLOW_STATS
,
1585 sizeof(struct ovs_dp_megaflow_stats
),
1586 &dp_megaflow_stats
, OVS_DP_ATTR_PAD
))
1587 goto nla_put_failure
;
1589 if (nla_put_u32(skb
, OVS_DP_ATTR_USER_FEATURES
, dp
->user_features
))
1590 goto nla_put_failure
;
1592 genlmsg_end(skb
, ovs_header
);
1596 genlmsg_cancel(skb
, ovs_header
);
1601 static struct sk_buff
*ovs_dp_cmd_alloc_info(void)
1603 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL
);
1606 /* Called with rcu_read_lock or ovs_mutex. */
1607 static struct datapath
*lookup_datapath(struct net
*net
,
1608 const struct ovs_header
*ovs_header
,
1609 struct nlattr
*a
[OVS_DP_ATTR_MAX
+ 1])
1611 struct datapath
*dp
;
1613 if (!a
[OVS_DP_ATTR_NAME
])
1614 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1616 struct vport
*vport
;
1618 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_DP_ATTR_NAME
]));
1619 dp
= vport
&& vport
->port_no
== OVSP_LOCAL
? vport
->dp
: NULL
;
1621 return dp
? dp
: ERR_PTR(-ENODEV
);
1624 static void ovs_dp_reset_user_features(struct sk_buff
*skb
, struct genl_info
*info
)
1626 struct datapath
*dp
;
1628 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1632 WARN(dp
->user_features
, "Dropping previously announced user features\n");
1633 dp
->user_features
= 0;
1636 DEFINE_STATIC_KEY_FALSE(tc_recirc_sharing_support
);
1638 static int ovs_dp_change(struct datapath
*dp
, struct nlattr
*a
[])
1640 u32 user_features
= 0;
1642 if (a
[OVS_DP_ATTR_USER_FEATURES
]) {
1643 user_features
= nla_get_u32(a
[OVS_DP_ATTR_USER_FEATURES
]);
1645 if (user_features
& ~(OVS_DP_F_VPORT_PIDS
|
1646 OVS_DP_F_UNALIGNED
|
1647 OVS_DP_F_TC_RECIRC_SHARING
))
1650 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1651 if (user_features
& OVS_DP_F_TC_RECIRC_SHARING
)
1656 dp
->user_features
= user_features
;
1658 if (dp
->user_features
& OVS_DP_F_TC_RECIRC_SHARING
)
1659 static_branch_enable(&tc_recirc_sharing_support
);
1661 static_branch_disable(&tc_recirc_sharing_support
);
1666 static int ovs_dp_stats_init(struct datapath
*dp
)
1668 dp
->stats_percpu
= netdev_alloc_pcpu_stats(struct dp_stats_percpu
);
1669 if (!dp
->stats_percpu
)
1675 static int ovs_dp_vport_init(struct datapath
*dp
)
1679 dp
->ports
= kmalloc_array(DP_VPORT_HASH_BUCKETS
,
1680 sizeof(struct hlist_head
),
1685 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
1686 INIT_HLIST_HEAD(&dp
->ports
[i
]);
1691 static int ovs_dp_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1693 struct nlattr
**a
= info
->attrs
;
1694 struct vport_parms parms
;
1695 struct sk_buff
*reply
;
1696 struct datapath
*dp
;
1697 struct vport
*vport
;
1698 struct ovs_net
*ovs_net
;
1702 if (!a
[OVS_DP_ATTR_NAME
] || !a
[OVS_DP_ATTR_UPCALL_PID
])
1705 reply
= ovs_dp_cmd_alloc_info();
1710 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1712 goto err_destroy_reply
;
1714 ovs_dp_set_net(dp
, sock_net(skb
->sk
));
1716 /* Allocate table. */
1717 err
= ovs_flow_tbl_init(&dp
->table
);
1719 goto err_destroy_dp
;
1721 err
= ovs_dp_stats_init(dp
);
1723 goto err_destroy_table
;
1725 err
= ovs_dp_vport_init(dp
);
1727 goto err_destroy_stats
;
1729 err
= ovs_meters_init(dp
);
1731 goto err_destroy_ports
;
1733 /* Set up our datapath device. */
1734 parms
.name
= nla_data(a
[OVS_DP_ATTR_NAME
]);
1735 parms
.type
= OVS_VPORT_TYPE_INTERNAL
;
1736 parms
.options
= NULL
;
1738 parms
.port_no
= OVSP_LOCAL
;
1739 parms
.upcall_portids
= a
[OVS_DP_ATTR_UPCALL_PID
];
1741 err
= ovs_dp_change(dp
, a
);
1743 goto err_destroy_meters
;
1745 /* So far only local changes have been made, now need the lock. */
1748 vport
= new_vport(&parms
);
1749 if (IS_ERR(vport
)) {
1750 err
= PTR_ERR(vport
);
1754 if (err
== -EEXIST
) {
1755 /* An outdated user space instance that does not understand
1756 * the concept of user_features has attempted to create a new
1757 * datapath and is likely to reuse it. Drop all user features.
1759 if (info
->genlhdr
->version
< OVS_DP_VER_FEATURES
)
1760 ovs_dp_reset_user_features(skb
, info
);
1764 goto err_destroy_meters
;
1767 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1768 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1771 ovs_net
= net_generic(ovs_dp_get_net(dp
), ovs_net_id
);
1772 list_add_tail_rcu(&dp
->list_node
, &ovs_net
->dps
);
1776 ovs_notify(&dp_datapath_genl_family
, &ovs_dp_datapath_multicast_group
, reply
, info
);
1780 ovs_meters_exit(dp
);
1784 free_percpu(dp
->stats_percpu
);
1786 ovs_flow_tbl_destroy(&dp
->table
);
1795 /* Called with ovs_mutex. */
1796 static void __dp_destroy(struct datapath
*dp
)
1800 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1801 struct vport
*vport
;
1802 struct hlist_node
*n
;
1804 hlist_for_each_entry_safe(vport
, n
, &dp
->ports
[i
], dp_hash_node
)
1805 if (vport
->port_no
!= OVSP_LOCAL
)
1806 ovs_dp_detach_port(vport
);
1809 list_del_rcu(&dp
->list_node
);
1811 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1812 * all ports in datapath are destroyed first before freeing datapath.
1814 ovs_dp_detach_port(ovs_vport_ovsl(dp
, OVSP_LOCAL
));
1816 /* RCU destroy the flow table */
1817 call_rcu(&dp
->rcu
, destroy_dp_rcu
);
1820 static int ovs_dp_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1822 struct sk_buff
*reply
;
1823 struct datapath
*dp
;
1826 reply
= ovs_dp_cmd_alloc_info();
1831 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1834 goto err_unlock_free
;
1836 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1837 info
->snd_seq
, 0, OVS_DP_CMD_DEL
);
1843 ovs_notify(&dp_datapath_genl_family
, &ovs_dp_datapath_multicast_group
, reply
, info
);
1852 static int ovs_dp_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1854 struct sk_buff
*reply
;
1855 struct datapath
*dp
;
1858 reply
= ovs_dp_cmd_alloc_info();
1863 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1866 goto err_unlock_free
;
1868 err
= ovs_dp_change(dp
, info
->attrs
);
1870 goto err_unlock_free
;
1872 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1873 info
->snd_seq
, 0, OVS_DP_CMD_GET
);
1878 ovs_notify(&dp_datapath_genl_family
, &ovs_dp_datapath_multicast_group
, reply
, info
);
1887 static int ovs_dp_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1889 struct sk_buff
*reply
;
1890 struct datapath
*dp
;
1893 reply
= ovs_dp_cmd_alloc_info();
1898 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1901 goto err_unlock_free
;
1903 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1904 info
->snd_seq
, 0, OVS_DP_CMD_GET
);
1908 return genlmsg_reply(reply
, info
);
1916 static int ovs_dp_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1918 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
1919 struct datapath
*dp
;
1920 int skip
= cb
->args
[0];
1924 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
1926 ovs_dp_cmd_fill_info(dp
, skb
, NETLINK_CB(cb
->skb
).portid
,
1927 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1928 OVS_DP_CMD_GET
) < 0)
1939 static const struct nla_policy datapath_policy
[OVS_DP_ATTR_MAX
+ 1] = {
1940 [OVS_DP_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1941 [OVS_DP_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1942 [OVS_DP_ATTR_USER_FEATURES
] = { .type
= NLA_U32
},
1945 static const struct genl_ops dp_datapath_genl_ops
[] = {
1946 { .cmd
= OVS_DP_CMD_NEW
,
1947 #ifdef HAVE_GENL_VALIDATE_FLAGS
1948 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1950 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1951 #ifdef HAVE_GENL_OPS_POLICY
1952 .policy
= datapath_policy
,
1954 .doit
= ovs_dp_cmd_new
1956 { .cmd
= OVS_DP_CMD_DEL
,
1957 #ifdef HAVE_GENL_VALIDATE_FLAGS
1958 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1960 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1961 #ifdef HAVE_GENL_OPS_POLICY
1962 .policy
= datapath_policy
,
1964 .doit
= ovs_dp_cmd_del
1966 { .cmd
= OVS_DP_CMD_GET
,
1967 #ifdef HAVE_GENL_VALIDATE_FLAGS
1968 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1970 .flags
= 0, /* OK for unprivileged users. */
1971 #ifdef HAVE_GENL_OPS_POLICY
1972 .policy
= datapath_policy
,
1974 .doit
= ovs_dp_cmd_get
,
1975 .dumpit
= ovs_dp_cmd_dump
1977 { .cmd
= OVS_DP_CMD_SET
,
1978 #ifdef HAVE_GENL_VALIDATE_FLAGS
1979 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1981 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1982 #ifdef HAVE_GENL_OPS_POLICY
1983 .policy
= datapath_policy
,
1985 .doit
= ovs_dp_cmd_set
,
1989 static struct genl_family dp_datapath_genl_family __ro_after_init
= {
1990 .hdrsize
= sizeof(struct ovs_header
),
1991 .name
= OVS_DATAPATH_FAMILY
,
1992 .version
= OVS_DATAPATH_VERSION
,
1993 .maxattr
= OVS_DP_ATTR_MAX
,
1994 #ifndef HAVE_GENL_OPS_POLICY
1995 .policy
= datapath_policy
,
1998 .parallel_ops
= true,
1999 .ops
= dp_datapath_genl_ops
,
2000 .n_ops
= ARRAY_SIZE(dp_datapath_genl_ops
),
2001 .mcgrps
= &ovs_dp_datapath_multicast_group
,
2003 .module
= THIS_MODULE
,
2006 /* Called with ovs_mutex or RCU read lock. */
2007 static int ovs_vport_cmd_fill_info(struct vport
*vport
, struct sk_buff
*skb
,
2008 struct net
*net
, u32 portid
, u32 seq
,
2009 u32 flags
, u8 cmd
, gfp_t gfp
)
2011 struct ovs_header
*ovs_header
;
2012 struct ovs_vport_stats vport_stats
;
2015 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_vport_genl_family
,
2020 ovs_header
->dp_ifindex
= get_dpifindex(vport
->dp
);
2022 if (nla_put_u32(skb
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
) ||
2023 nla_put_u32(skb
, OVS_VPORT_ATTR_TYPE
, vport
->ops
->type
) ||
2024 nla_put_string(skb
, OVS_VPORT_ATTR_NAME
,
2025 ovs_vport_name(vport
)) ||
2026 nla_put_u32(skb
, OVS_VPORT_ATTR_IFINDEX
, vport
->dev
->ifindex
))
2027 goto nla_put_failure
;
2029 #ifdef HAVE_PEERNET2ID_ALLOC
2030 if (!net_eq(net
, dev_net(vport
->dev
))) {
2031 int id
= peernet2id_alloc(net
, dev_net(vport
->dev
), gfp
);
2033 if (nla_put_s32(skb
, OVS_VPORT_ATTR_NETNSID
, id
))
2034 goto nla_put_failure
;
2038 ovs_vport_get_stats(vport
, &vport_stats
);
2039 if (nla_put_64bit(skb
, OVS_VPORT_ATTR_STATS
,
2040 sizeof(struct ovs_vport_stats
), &vport_stats
,
2041 OVS_VPORT_ATTR_PAD
))
2042 goto nla_put_failure
;
2044 if (ovs_vport_get_upcall_portids(vport
, skb
))
2045 goto nla_put_failure
;
2047 err
= ovs_vport_get_options(vport
, skb
);
2048 if (err
== -EMSGSIZE
)
2051 genlmsg_end(skb
, ovs_header
);
2057 genlmsg_cancel(skb
, ovs_header
);
2061 static struct sk_buff
*ovs_vport_cmd_alloc_info(void)
2063 return nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2066 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
2067 struct sk_buff
*ovs_vport_cmd_build_info(struct vport
*vport
, struct net
*net
,
2068 u32 portid
, u32 seq
, u8 cmd
)
2070 struct sk_buff
*skb
;
2073 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2075 return ERR_PTR(-ENOMEM
);
2077 retval
= ovs_vport_cmd_fill_info(vport
, skb
, net
, portid
, seq
, 0, cmd
,
2084 /* Called with ovs_mutex or RCU read lock. */
2085 static struct vport
*lookup_vport(struct net
*net
,
2086 const struct ovs_header
*ovs_header
,
2087 struct nlattr
*a
[OVS_VPORT_ATTR_MAX
+ 1])
2089 struct datapath
*dp
;
2090 struct vport
*vport
;
2092 if (a
[OVS_VPORT_ATTR_IFINDEX
])
2093 return ERR_PTR(-EOPNOTSUPP
);
2094 if (a
[OVS_VPORT_ATTR_NAME
]) {
2095 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_VPORT_ATTR_NAME
]));
2097 return ERR_PTR(-ENODEV
);
2098 if (ovs_header
->dp_ifindex
&&
2099 ovs_header
->dp_ifindex
!= get_dpifindex(vport
->dp
))
2100 return ERR_PTR(-ENODEV
);
2102 } else if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
2103 u32 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
2105 if (port_no
>= DP_MAX_PORTS
)
2106 return ERR_PTR(-EFBIG
);
2108 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
2110 return ERR_PTR(-ENODEV
);
2112 vport
= ovs_vport_ovsl_rcu(dp
, port_no
);
2114 return ERR_PTR(-ENODEV
);
2117 return ERR_PTR(-EINVAL
);
2121 static unsigned int ovs_get_max_headroom(struct datapath
*dp
)
2123 unsigned int dev_headroom
, max_headroom
= 0;
2124 struct net_device
*dev
;
2125 struct vport
*vport
;
2128 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2129 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
2131 dev_headroom
= netdev_get_fwd_headroom(dev
);
2132 if (dev_headroom
> max_headroom
)
2133 max_headroom
= dev_headroom
;
2137 return max_headroom
;
2140 /* Called with ovs_mutex */
2141 static void ovs_update_headroom(struct datapath
*dp
, unsigned int new_headroom
)
2143 struct vport
*vport
;
2146 dp
->max_headroom
= new_headroom
;
2147 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
2148 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
)
2149 netdev_set_rx_headroom(vport
->dev
, new_headroom
);
2152 static int ovs_vport_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
2154 struct nlattr
**a
= info
->attrs
;
2155 struct ovs_header
*ovs_header
= info
->userhdr
;
2156 struct vport_parms parms
;
2157 struct sk_buff
*reply
;
2158 struct vport
*vport
;
2159 struct datapath
*dp
;
2160 unsigned int new_headroom
;
2164 if (!a
[OVS_VPORT_ATTR_NAME
] || !a
[OVS_VPORT_ATTR_TYPE
] ||
2165 !a
[OVS_VPORT_ATTR_UPCALL_PID
])
2167 if (a
[OVS_VPORT_ATTR_IFINDEX
])
2170 port_no
= a
[OVS_VPORT_ATTR_PORT_NO
]
2171 ? nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]) : 0;
2172 if (port_no
>= DP_MAX_PORTS
)
2175 reply
= ovs_vport_cmd_alloc_info();
2181 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
2184 goto exit_unlock_free
;
2187 vport
= ovs_vport_ovsl(dp
, port_no
);
2190 goto exit_unlock_free
;
2192 for (port_no
= 1; ; port_no
++) {
2193 if (port_no
>= DP_MAX_PORTS
) {
2195 goto exit_unlock_free
;
2197 vport
= ovs_vport_ovsl(dp
, port_no
);
2203 parms
.name
= nla_data(a
[OVS_VPORT_ATTR_NAME
]);
2204 parms
.type
= nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
2205 parms
.options
= a
[OVS_VPORT_ATTR_OPTIONS
];
2207 parms
.port_no
= port_no
;
2208 parms
.upcall_portids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
2210 vport
= new_vport(&parms
);
2211 err
= PTR_ERR(vport
);
2212 if (IS_ERR(vport
)) {
2215 goto exit_unlock_free
;
2218 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2219 info
->snd_portid
, info
->snd_seq
, 0,
2220 OVS_VPORT_CMD_NEW
, GFP_KERNEL
);
2223 new_headroom
= netdev_get_fwd_headroom(vport
->dev
);
2225 if (new_headroom
> dp
->max_headroom
)
2226 ovs_update_headroom(dp
, new_headroom
);
2228 netdev_set_rx_headroom(vport
->dev
, dp
->max_headroom
);
2232 ovs_notify(&dp_vport_genl_family
, &ovs_dp_vport_multicast_group
, reply
, info
);
2241 static int ovs_vport_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
2243 struct nlattr
**a
= info
->attrs
;
2244 struct sk_buff
*reply
;
2245 struct vport
*vport
;
2248 reply
= ovs_vport_cmd_alloc_info();
2253 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2254 err
= PTR_ERR(vport
);
2256 goto exit_unlock_free
;
2258 if (a
[OVS_VPORT_ATTR_TYPE
] &&
2259 nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]) != vport
->ops
->type
) {
2261 goto exit_unlock_free
;
2264 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
2265 err
= ovs_vport_set_options(vport
, a
[OVS_VPORT_ATTR_OPTIONS
]);
2267 goto exit_unlock_free
;
2270 if (a
[OVS_VPORT_ATTR_UPCALL_PID
]) {
2271 struct nlattr
*ids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
2273 err
= ovs_vport_set_upcall_portids(vport
, ids
);
2275 goto exit_unlock_free
;
2278 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2279 info
->snd_portid
, info
->snd_seq
, 0,
2280 OVS_VPORT_CMD_SET
, GFP_KERNEL
);
2284 ovs_notify(&dp_vport_genl_family
, &ovs_dp_vport_multicast_group
, reply
, info
);
2293 static int ovs_vport_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
2295 bool update_headroom
= false;
2296 struct nlattr
**a
= info
->attrs
;
2297 struct sk_buff
*reply
;
2298 struct datapath
*dp
;
2299 struct vport
*vport
;
2300 unsigned int new_headroom
;
2303 reply
= ovs_vport_cmd_alloc_info();
2308 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2309 err
= PTR_ERR(vport
);
2311 goto exit_unlock_free
;
2313 if (vport
->port_no
== OVSP_LOCAL
) {
2315 goto exit_unlock_free
;
2318 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2319 info
->snd_portid
, info
->snd_seq
, 0,
2320 OVS_VPORT_CMD_DEL
, GFP_KERNEL
);
2323 /* the vport deletion may trigger dp headroom update */
2325 if (netdev_get_fwd_headroom(vport
->dev
) == dp
->max_headroom
)
2326 update_headroom
= true;
2328 netdev_reset_rx_headroom(vport
->dev
);
2329 ovs_dp_detach_port(vport
);
2331 if (update_headroom
) {
2332 new_headroom
= ovs_get_max_headroom(dp
);
2334 if (new_headroom
< dp
->max_headroom
)
2335 ovs_update_headroom(dp
, new_headroom
);
2339 ovs_notify(&dp_vport_genl_family
, &ovs_dp_vport_multicast_group
, reply
, info
);
2348 static int ovs_vport_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
2350 struct nlattr
**a
= info
->attrs
;
2351 struct ovs_header
*ovs_header
= info
->userhdr
;
2352 struct sk_buff
*reply
;
2353 struct vport
*vport
;
2356 reply
= ovs_vport_cmd_alloc_info();
2361 vport
= lookup_vport(sock_net(skb
->sk
), ovs_header
, a
);
2362 err
= PTR_ERR(vport
);
2364 goto exit_unlock_free
;
2365 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2366 info
->snd_portid
, info
->snd_seq
, 0,
2367 OVS_VPORT_CMD_GET
, GFP_ATOMIC
);
2371 return genlmsg_reply(reply
, info
);
2379 static int ovs_vport_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2381 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
2382 struct datapath
*dp
;
2383 int bucket
= cb
->args
[0], skip
= cb
->args
[1];
2387 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
2392 for (i
= bucket
; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2393 struct vport
*vport
;
2396 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
2398 ovs_vport_cmd_fill_info(vport
, skb
,
2400 NETLINK_CB(cb
->skb
).portid
,
2420 static const struct nla_policy vport_policy
[OVS_VPORT_ATTR_MAX
+ 1] = {
2421 [OVS_VPORT_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
2422 [OVS_VPORT_ATTR_STATS
] = { .len
= sizeof(struct ovs_vport_stats
) },
2423 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NLA_U32
},
2424 [OVS_VPORT_ATTR_TYPE
] = { .type
= NLA_U32
},
2425 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NLA_UNSPEC
},
2426 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NLA_NESTED
},
2427 [OVS_VPORT_ATTR_IFINDEX
] = { .type
= NLA_U32
},
2428 [OVS_VPORT_ATTR_NETNSID
] = { .type
= NLA_S32
},
2431 static const struct genl_ops dp_vport_genl_ops
[] = {
2432 { .cmd
= OVS_VPORT_CMD_NEW
,
2433 #ifdef HAVE_GENL_VALIDATE_FLAGS
2434 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2436 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2437 #ifdef HAVE_GENL_OPS_POLICY
2438 .policy
= vport_policy
,
2440 .doit
= ovs_vport_cmd_new
2442 { .cmd
= OVS_VPORT_CMD_DEL
,
2443 #ifdef HAVE_GENL_VALIDATE_FLAGS
2444 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2446 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2447 #ifdef HAVE_GENL_OPS_POLICY
2448 .policy
= vport_policy
,
2450 .doit
= ovs_vport_cmd_del
2452 { .cmd
= OVS_VPORT_CMD_GET
,
2453 #ifdef HAVE_GENL_VALIDATE_FLAGS
2454 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2456 .flags
= 0, /* OK for unprivileged users. */
2457 #ifdef HAVE_GENL_OPS_POLICY
2458 .policy
= vport_policy
,
2460 .doit
= ovs_vport_cmd_get
,
2461 .dumpit
= ovs_vport_cmd_dump
2463 { .cmd
= OVS_VPORT_CMD_SET
,
2464 #ifdef HAVE_GENL_VALIDATE_FLAGS
2465 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2467 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2468 #ifdef HAVE_GENL_OPS_POLICY
2469 .policy
= vport_policy
,
2471 .doit
= ovs_vport_cmd_set
,
2475 struct genl_family dp_vport_genl_family __ro_after_init
= {
2476 .hdrsize
= sizeof(struct ovs_header
),
2477 .name
= OVS_VPORT_FAMILY
,
2478 .version
= OVS_VPORT_VERSION
,
2479 .maxattr
= OVS_VPORT_ATTR_MAX
,
2480 #ifndef HAVE_GENL_OPS_POLICY
2481 .policy
= vport_policy
,
2484 .parallel_ops
= true,
2485 .ops
= dp_vport_genl_ops
,
2486 .n_ops
= ARRAY_SIZE(dp_vport_genl_ops
),
2487 .mcgrps
= &ovs_dp_vport_multicast_group
,
2489 .module
= THIS_MODULE
,
2492 static struct genl_family
*dp_genl_families
[] = {
2493 &dp_datapath_genl_family
,
2494 &dp_vport_genl_family
,
2495 &dp_flow_genl_family
,
2496 &dp_packet_genl_family
,
2497 &dp_meter_genl_family
,
2498 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2499 &dp_ct_limit_genl_family
,
2503 static void dp_unregister_genl(int n_families
)
2507 for (i
= 0; i
< n_families
; i
++)
2508 genl_unregister_family(dp_genl_families
[i
]);
2511 static int __init
dp_register_genl(void)
2516 for (i
= 0; i
< ARRAY_SIZE(dp_genl_families
); i
++) {
2518 err
= genl_register_family(dp_genl_families
[i
]);
2526 dp_unregister_genl(i
);
2530 static int __net_init
ovs_init_net(struct net
*net
)
2532 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2534 INIT_LIST_HEAD(&ovs_net
->dps
);
2535 INIT_WORK(&ovs_net
->dp_notify_work
, ovs_dp_notify_wq
);
2536 ovs_netns_frags_init(net
);
2537 ovs_netns_frags6_init(net
);
2538 return ovs_ct_init(net
);
2541 static void __net_exit
list_vports_from_net(struct net
*net
, struct net
*dnet
,
2542 struct list_head
*head
)
2544 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2545 struct datapath
*dp
;
2547 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
2550 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2551 struct vport
*vport
;
2553 hlist_for_each_entry(vport
, &dp
->ports
[i
], dp_hash_node
) {
2555 if (vport
->ops
->type
!= OVS_VPORT_TYPE_INTERNAL
)
2558 if (dev_net(vport
->dev
) == dnet
)
2559 list_add(&vport
->detach_list
, head
);
2565 static void __net_exit
ovs_exit_net(struct net
*dnet
)
2567 struct datapath
*dp
, *dp_next
;
2568 struct ovs_net
*ovs_net
= net_generic(dnet
, ovs_net_id
);
2569 struct vport
*vport
, *vport_next
;
2573 ovs_netns_frags6_exit(dnet
);
2574 ovs_netns_frags_exit(dnet
);
2579 list_for_each_entry_safe(dp
, dp_next
, &ovs_net
->dps
, list_node
)
2582 #ifdef HAVE_NET_RWSEM
2583 down_read(&net_rwsem
);
2588 list_vports_from_net(net
, dnet
, &head
);
2589 #ifdef HAVE_NET_RWSEM
2590 up_read(&net_rwsem
);
2595 /* Detach all vports from given namespace. */
2596 list_for_each_entry_safe(vport
, vport_next
, &head
, detach_list
) {
2597 list_del(&vport
->detach_list
);
2598 ovs_dp_detach_port(vport
);
2603 cancel_work_sync(&ovs_net
->dp_notify_work
);
2606 static struct pernet_operations ovs_net_ops
= {
2607 .init
= ovs_init_net
,
2608 .exit
= ovs_exit_net
,
2610 .size
= sizeof(struct ovs_net
),
2613 static int __init
dp_init(void)
2617 BUILD_BUG_ON(sizeof(struct ovs_skb_cb
) > sizeof_field(struct sk_buff
, cb
));
2619 pr_info("Open vSwitch switching datapath %s\n", VERSION
);
2622 err
= action_fifos_init();
2626 err
= ovs_internal_dev_rtnl_link_register();
2628 goto error_action_fifos_exit
;
2630 err
= ovs_flow_init();
2632 goto error_unreg_rtnl_link
;
2634 err
= ovs_vport_init();
2636 goto error_flow_exit
;
2638 err
= register_pernet_device(&ovs_net_ops
);
2640 goto error_vport_exit
;
2642 err
= compat_init();
2644 goto error_netns_exit
;
2646 err
= register_netdevice_notifier(&ovs_dp_device_notifier
);
2648 goto error_compat_exit
;
2650 err
= ovs_netdev_init();
2652 goto error_unreg_notifier
;
2654 err
= dp_register_genl();
2656 goto error_unreg_netdev
;
2662 error_unreg_notifier
:
2663 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2667 unregister_pernet_device(&ovs_net_ops
);
2672 error_unreg_rtnl_link
:
2673 ovs_internal_dev_rtnl_link_unregister();
2674 error_action_fifos_exit
:
2675 action_fifos_exit();
2681 static void dp_cleanup(void)
2683 dp_unregister_genl(ARRAY_SIZE(dp_genl_families
));
2685 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2687 unregister_pernet_device(&ovs_net_ops
);
2691 ovs_internal_dev_rtnl_link_unregister();
2692 action_fifos_exit();
2696 module_init(dp_init
);
2697 module_exit(dp_cleanup
);
2699 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2700 MODULE_LICENSE("GPL");
2701 MODULE_VERSION(VERSION
);
2702 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY
);
2703 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY
);
2704 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY
);
2705 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY
);
2706 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY
);
2707 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY
);