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. */
348 *OVS_CB(skb
) = ovs_cb
;
349 #ifdef HAVE_SKB_GSO_UDP
350 if (gso_type
& SKB_GSO_UDP
&& skb
!= segs
)
353 err
= queue_userspace_packet(dp
, skb
, key
, upcall_info
, cutlen
);
357 } while ((skb
= skb
->next
));
359 /* Free all of the segments. */
367 } while ((skb
= nskb
));
371 static size_t upcall_msg_size(const struct dp_upcall_info
*upcall_info
,
372 unsigned int hdrlen
, int actions_attrlen
)
374 size_t size
= NLMSG_ALIGN(sizeof(struct ovs_header
))
375 + nla_total_size(hdrlen
) /* OVS_PACKET_ATTR_PACKET */
376 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
377 + nla_total_size(sizeof(unsigned int)) /* OVS_PACKET_ATTR_LEN */
378 + nla_total_size(sizeof(u64
)); /* OVS_PACKET_ATTR_HASH */
380 /* OVS_PACKET_ATTR_USERDATA */
381 if (upcall_info
->userdata
)
382 size
+= NLA_ALIGN(upcall_info
->userdata
->nla_len
);
384 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
385 if (upcall_info
->egress_tun_info
)
386 size
+= nla_total_size(ovs_tun_key_attr_size());
388 /* OVS_PACKET_ATTR_ACTIONS */
389 if (upcall_info
->actions_len
)
390 size
+= nla_total_size(actions_attrlen
);
392 /* OVS_PACKET_ATTR_MRU */
393 if (upcall_info
->mru
)
394 size
+= nla_total_size(sizeof(upcall_info
->mru
));
399 static void pad_packet(struct datapath
*dp
, struct sk_buff
*skb
)
401 if (!(dp
->user_features
& OVS_DP_F_UNALIGNED
)) {
402 size_t plen
= NLA_ALIGN(skb
->len
) - skb
->len
;
405 skb_put_zero(skb
, plen
);
409 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*skb
,
410 const struct sw_flow_key
*key
,
411 const struct dp_upcall_info
*upcall_info
,
414 struct ovs_header
*upcall
;
415 struct sk_buff
*nskb
= NULL
;
416 struct sk_buff
*user_skb
= NULL
; /* to be queued to userspace */
423 dp_ifindex
= get_dpifindex(dp
);
427 if (skb_vlan_tag_present(skb
)) {
428 nskb
= skb_clone(skb
, GFP_ATOMIC
);
432 nskb
= __vlan_hwaccel_push_inside(nskb
);
439 if (nla_attr_size(skb
->len
) > USHRT_MAX
) {
444 /* Complete checksum if needed */
445 if (skb
->ip_summed
== CHECKSUM_PARTIAL
&&
446 (err
= skb_csum_hwoffload_help(skb
, 0)))
449 /* Older versions of OVS user space enforce alignment of the last
450 * Netlink attribute to NLA_ALIGNTO which would require extensive
451 * padding logic. Only perform zerocopy if padding is not required.
453 if (dp
->user_features
& OVS_DP_F_UNALIGNED
)
454 hlen
= skb_zerocopy_headlen(skb
);
458 len
= upcall_msg_size(upcall_info
, hlen
- cutlen
,
459 OVS_CB(skb
)->acts_origlen
);
460 user_skb
= genlmsg_new(len
, GFP_ATOMIC
);
466 upcall
= genlmsg_put(user_skb
, 0, 0, &dp_packet_genl_family
,
467 0, upcall_info
->cmd
);
472 upcall
->dp_ifindex
= dp_ifindex
;
474 err
= ovs_nla_put_key(key
, key
, OVS_PACKET_ATTR_KEY
, false, user_skb
);
478 if (upcall_info
->userdata
)
479 __nla_put(user_skb
, OVS_PACKET_ATTR_USERDATA
,
480 nla_len(upcall_info
->userdata
),
481 nla_data(upcall_info
->userdata
));
484 if (upcall_info
->egress_tun_info
) {
485 nla
= nla_nest_start_noflag(user_skb
,
486 OVS_PACKET_ATTR_EGRESS_TUN_KEY
);
491 err
= ovs_nla_put_tunnel_info(user_skb
,
492 upcall_info
->egress_tun_info
);
496 nla_nest_end(user_skb
, nla
);
499 if (upcall_info
->actions_len
) {
500 nla
= nla_nest_start_noflag(user_skb
, OVS_PACKET_ATTR_ACTIONS
);
505 err
= ovs_nla_put_actions(upcall_info
->actions
,
506 upcall_info
->actions_len
,
509 nla_nest_end(user_skb
, nla
);
511 nla_nest_cancel(user_skb
, nla
);
514 /* Add OVS_PACKET_ATTR_MRU */
515 if (upcall_info
->mru
) {
516 if (nla_put_u16(user_skb
, OVS_PACKET_ATTR_MRU
,
521 pad_packet(dp
, user_skb
);
524 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
526 if (nla_put_u32(user_skb
, OVS_PACKET_ATTR_LEN
,
531 pad_packet(dp
, user_skb
);
534 /* Add OVS_PACKET_ATTR_HASH */
535 hash
= skb_get_hash_raw(skb
);
538 hash
|= OVS_PACKET_HASH_SW_BIT
;
541 #ifdef HAVE_L4_RXHASH
546 hash
|= OVS_PACKET_HASH_L4_BIT
;
548 if (nla_put(user_skb
, OVS_PACKET_ATTR_HASH
, sizeof (u64
), &hash
)) {
553 /* Only reserve room for attribute header, packet data is added
556 if (!(nla
= nla_reserve(user_skb
, OVS_PACKET_ATTR_PACKET
, 0))) {
560 nla
->nla_len
= nla_attr_size(skb
->len
- cutlen
);
562 err
= skb_zerocopy(user_skb
, skb
, skb
->len
- cutlen
, hlen
);
566 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
567 pad_packet(dp
, user_skb
);
569 ((struct nlmsghdr
*) user_skb
->data
)->nlmsg_len
= user_skb
->len
;
571 err
= genlmsg_unicast(ovs_dp_get_net(dp
), user_skb
, upcall_info
->portid
);
581 static int ovs_packet_cmd_execute(struct sk_buff
*skb
, struct genl_info
*info
)
583 struct ovs_header
*ovs_header
= info
->userhdr
;
584 struct net
*net
= sock_net(skb
->sk
);
585 struct nlattr
**a
= info
->attrs
;
586 struct sw_flow_actions
*acts
;
587 struct sk_buff
*packet
;
588 struct sw_flow
*flow
;
589 struct sw_flow_actions
*sf_acts
;
591 struct vport
*input_vport
;
596 bool log
= !a
[OVS_PACKET_ATTR_PROBE
];
599 if (!a
[OVS_PACKET_ATTR_PACKET
] || !a
[OVS_PACKET_ATTR_KEY
] ||
600 !a
[OVS_PACKET_ATTR_ACTIONS
])
603 len
= nla_len(a
[OVS_PACKET_ATTR_PACKET
]);
604 packet
= __dev_alloc_skb(NET_IP_ALIGN
+ len
, GFP_KERNEL
);
608 skb_reserve(packet
, NET_IP_ALIGN
);
610 nla_memcpy(__skb_put(packet
, len
), a
[OVS_PACKET_ATTR_PACKET
], len
);
612 /* Set packet's mru */
613 if (a
[OVS_PACKET_ATTR_MRU
]) {
614 mru
= nla_get_u16(a
[OVS_PACKET_ATTR_MRU
]);
615 packet
->ignore_df
= 1;
617 OVS_CB(packet
)->mru
= mru
;
619 if (a
[OVS_PACKET_ATTR_HASH
]) {
620 hash
= nla_get_u64(a
[OVS_PACKET_ATTR_HASH
]);
622 __skb_set_hash(packet
, hash
& 0xFFFFFFFFULL
,
623 !!(hash
& OVS_PACKET_HASH_SW_BIT
),
624 !!(hash
& OVS_PACKET_HASH_L4_BIT
));
627 /* Build an sw_flow for sending this packet. */
628 flow
= ovs_flow_alloc();
633 err
= ovs_flow_key_extract_userspace(net
, a
[OVS_PACKET_ATTR_KEY
],
634 packet
, &flow
->key
, log
);
638 err
= ovs_nla_copy_actions(net
, a
[OVS_PACKET_ATTR_ACTIONS
],
639 &flow
->key
, &acts
, log
);
643 rcu_assign_pointer(flow
->sf_acts
, acts
);
644 packet
->priority
= flow
->key
.phy
.priority
;
645 packet
->mark
= flow
->key
.phy
.skb_mark
;
648 dp
= get_dp_rcu(net
, ovs_header
->dp_ifindex
);
653 input_vport
= ovs_vport_rcu(dp
, flow
->key
.phy
.in_port
);
655 input_vport
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
660 packet
->dev
= input_vport
->dev
;
661 OVS_CB(packet
)->input_vport
= input_vport
;
662 sf_acts
= rcu_dereference(flow
->sf_acts
);
665 err
= ovs_execute_actions(dp
, packet
, sf_acts
, &flow
->key
);
669 ovs_flow_free(flow
, false);
675 ovs_flow_free(flow
, false);
682 static const struct nla_policy packet_policy
[OVS_PACKET_ATTR_MAX
+ 1] = {
683 [OVS_PACKET_ATTR_PACKET
] = { .len
= ETH_HLEN
},
684 [OVS_PACKET_ATTR_KEY
] = { .type
= NLA_NESTED
},
685 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
686 [OVS_PACKET_ATTR_PROBE
] = { .type
= NLA_FLAG
},
687 [OVS_PACKET_ATTR_MRU
] = { .type
= NLA_U16
},
688 [OVS_PACKET_ATTR_HASH
] = { .type
= NLA_U64
},
691 static struct genl_ops dp_packet_genl_ops
[] = {
692 { .cmd
= OVS_PACKET_CMD_EXECUTE
,
693 #ifdef HAVE_GENL_VALIDATE_FLAGS
694 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
696 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
697 #ifdef HAVE_GENL_OPS_POLICY
698 .policy
= packet_policy
,
700 .doit
= ovs_packet_cmd_execute
704 static struct genl_family dp_packet_genl_family __ro_after_init
= {
705 .hdrsize
= sizeof(struct ovs_header
),
706 .name
= OVS_PACKET_FAMILY
,
707 .version
= OVS_PACKET_VERSION
,
708 .maxattr
= OVS_PACKET_ATTR_MAX
,
709 #ifndef HAVE_GENL_OPS_POLICY
710 .policy
= packet_policy
,
713 .parallel_ops
= true,
714 .ops
= dp_packet_genl_ops
,
715 .n_ops
= ARRAY_SIZE(dp_packet_genl_ops
),
716 .module
= THIS_MODULE
,
719 static void get_dp_stats(const struct datapath
*dp
, struct ovs_dp_stats
*stats
,
720 struct ovs_dp_megaflow_stats
*mega_stats
)
724 memset(mega_stats
, 0, sizeof(*mega_stats
));
726 stats
->n_flows
= ovs_flow_tbl_count(&dp
->table
);
727 mega_stats
->n_masks
= ovs_flow_tbl_num_masks(&dp
->table
);
729 stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
731 for_each_possible_cpu(i
) {
732 const struct dp_stats_percpu
*percpu_stats
;
733 struct dp_stats_percpu local_stats
;
736 percpu_stats
= per_cpu_ptr(dp
->stats_percpu
, i
);
739 start
= u64_stats_fetch_begin_irq(&percpu_stats
->syncp
);
740 local_stats
= *percpu_stats
;
741 } while (u64_stats_fetch_retry_irq(&percpu_stats
->syncp
, start
));
743 stats
->n_hit
+= local_stats
.n_hit
;
744 stats
->n_missed
+= local_stats
.n_missed
;
745 stats
->n_lost
+= local_stats
.n_lost
;
746 mega_stats
->n_mask_hit
+= local_stats
.n_mask_hit
;
750 static bool should_fill_key(const struct sw_flow_id
*sfid
, uint32_t ufid_flags
)
752 return ovs_identifier_is_ufid(sfid
) &&
753 !(ufid_flags
& OVS_UFID_F_OMIT_KEY
);
756 static bool should_fill_mask(uint32_t ufid_flags
)
758 return !(ufid_flags
& OVS_UFID_F_OMIT_MASK
);
761 static bool should_fill_actions(uint32_t ufid_flags
)
763 return !(ufid_flags
& OVS_UFID_F_OMIT_ACTIONS
);
766 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions
*acts
,
767 const struct sw_flow_id
*sfid
,
770 size_t len
= NLMSG_ALIGN(sizeof(struct ovs_header
));
772 /* OVS_FLOW_ATTR_UFID */
773 if (sfid
&& ovs_identifier_is_ufid(sfid
))
774 len
+= nla_total_size(sfid
->ufid_len
);
776 /* OVS_FLOW_ATTR_KEY */
777 if (!sfid
|| should_fill_key(sfid
, ufid_flags
))
778 len
+= nla_total_size(ovs_key_attr_size());
780 /* OVS_FLOW_ATTR_MASK */
781 if (should_fill_mask(ufid_flags
))
782 len
+= nla_total_size(ovs_key_attr_size());
784 /* OVS_FLOW_ATTR_ACTIONS */
785 if (should_fill_actions(ufid_flags
))
786 len
+= nla_total_size(acts
->orig_len
);
789 + nla_total_size_64bit(sizeof(struct ovs_flow_stats
)) /* OVS_FLOW_ATTR_STATS */
790 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
791 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
794 /* Called with ovs_mutex or RCU read lock. */
795 static int ovs_flow_cmd_fill_stats(const struct sw_flow
*flow
,
798 struct ovs_flow_stats stats
;
802 ovs_flow_stats_get(flow
, &stats
, &used
, &tcp_flags
);
805 nla_put_u64_64bit(skb
, OVS_FLOW_ATTR_USED
, ovs_flow_used_time(used
),
809 if (stats
.n_packets
&&
810 nla_put_64bit(skb
, OVS_FLOW_ATTR_STATS
,
811 sizeof(struct ovs_flow_stats
), &stats
,
815 if ((u8
)ntohs(tcp_flags
) &&
816 nla_put_u8(skb
, OVS_FLOW_ATTR_TCP_FLAGS
, (u8
)ntohs(tcp_flags
)))
822 /* Called with ovs_mutex or RCU read lock. */
823 static int ovs_flow_cmd_fill_actions(const struct sw_flow
*flow
,
824 struct sk_buff
*skb
, int skb_orig_len
)
826 struct nlattr
*start
;
829 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
830 * this is the first flow to be dumped into 'skb'. This is unusual for
831 * Netlink but individual action lists can be longer than
832 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
833 * The userspace caller can always fetch the actions separately if it
834 * really wants them. (Most userspace callers in fact don't care.)
836 * This can only fail for dump operations because the skb is always
837 * properly sized for single flows.
839 start
= nla_nest_start_noflag(skb
, OVS_FLOW_ATTR_ACTIONS
);
841 const struct sw_flow_actions
*sf_acts
;
843 sf_acts
= rcu_dereference_ovsl(flow
->sf_acts
);
844 err
= ovs_nla_put_actions(sf_acts
->actions
,
845 sf_acts
->actions_len
, skb
);
848 nla_nest_end(skb
, start
);
853 nla_nest_cancel(skb
, start
);
855 } else if (skb_orig_len
) {
862 /* Called with ovs_mutex or RCU read lock. */
863 static int ovs_flow_cmd_fill_info(const struct sw_flow
*flow
, int dp_ifindex
,
864 struct sk_buff
*skb
, u32 portid
,
865 u32 seq
, u32 flags
, u8 cmd
, u32 ufid_flags
)
867 const int skb_orig_len
= skb
->len
;
868 struct ovs_header
*ovs_header
;
871 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_flow_genl_family
,
876 ovs_header
->dp_ifindex
= dp_ifindex
;
878 err
= ovs_nla_put_identifier(flow
, skb
);
882 if (should_fill_key(&flow
->id
, ufid_flags
)) {
883 err
= ovs_nla_put_masked_key(flow
, skb
);
888 if (should_fill_mask(ufid_flags
)) {
889 err
= ovs_nla_put_mask(flow
, skb
);
894 err
= ovs_flow_cmd_fill_stats(flow
, skb
);
898 if (should_fill_actions(ufid_flags
)) {
899 err
= ovs_flow_cmd_fill_actions(flow
, skb
, skb_orig_len
);
904 genlmsg_end(skb
, ovs_header
);
908 genlmsg_cancel(skb
, ovs_header
);
912 /* May not be called with RCU read lock. */
913 static struct sk_buff
*ovs_flow_cmd_alloc_info(const struct sw_flow_actions
*acts
,
914 const struct sw_flow_id
*sfid
,
915 struct genl_info
*info
,
922 if (!always
&& !ovs_must_notify(&dp_flow_genl_family
, info
,
923 GROUP_ID(&ovs_dp_flow_multicast_group
)))
926 len
= ovs_flow_cmd_msg_size(acts
, sfid
, ufid_flags
);
927 skb
= genlmsg_new(len
, GFP_KERNEL
);
929 return ERR_PTR(-ENOMEM
);
934 /* Called with ovs_mutex. */
935 static struct sk_buff
*ovs_flow_cmd_build_info(const struct sw_flow
*flow
,
937 struct genl_info
*info
, u8 cmd
,
938 bool always
, u32 ufid_flags
)
943 skb
= ovs_flow_cmd_alloc_info(ovsl_dereference(flow
->sf_acts
),
944 &flow
->id
, info
, always
, ufid_flags
);
945 if (IS_ERR_OR_NULL(skb
))
948 retval
= ovs_flow_cmd_fill_info(flow
, dp_ifindex
, skb
,
949 info
->snd_portid
, info
->snd_seq
, 0,
955 static int ovs_flow_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
957 struct net
*net
= sock_net(skb
->sk
);
958 struct nlattr
**a
= info
->attrs
;
959 struct ovs_header
*ovs_header
= info
->userhdr
;
960 struct sw_flow
*flow
= NULL
, *new_flow
;
961 struct sw_flow_mask mask
;
962 struct sk_buff
*reply
;
964 struct sw_flow_actions
*acts
;
965 struct sw_flow_match match
;
966 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
968 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
970 /* Must have key and actions. */
972 if (!a
[OVS_FLOW_ATTR_KEY
]) {
973 OVS_NLERR(log
, "Flow key attr not present in new flow.");
976 if (!a
[OVS_FLOW_ATTR_ACTIONS
]) {
977 OVS_NLERR(log
, "Flow actions attr not present in new flow.");
981 /* Most of the time we need to allocate a new flow, do it before
984 new_flow
= ovs_flow_alloc();
985 if (IS_ERR(new_flow
)) {
986 error
= PTR_ERR(new_flow
);
991 ovs_match_init(&match
, &new_flow
->key
, false, &mask
);
992 error
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
],
993 a
[OVS_FLOW_ATTR_MASK
], log
);
997 /* Extract flow identifier. */
998 error
= ovs_nla_get_identifier(&new_flow
->id
, a
[OVS_FLOW_ATTR_UFID
],
999 &new_flow
->key
, log
);
1001 goto err_kfree_flow
;
1003 /* unmasked key is needed to match when ufid is not used. */
1004 if (ovs_identifier_is_key(&new_flow
->id
))
1005 match
.key
= new_flow
->id
.unmasked_key
;
1007 ovs_flow_mask_key(&new_flow
->key
, &new_flow
->key
, true, &mask
);
1009 /* Validate actions. */
1010 error
= ovs_nla_copy_actions(net
, a
[OVS_FLOW_ATTR_ACTIONS
],
1011 &new_flow
->key
, &acts
, log
);
1013 OVS_NLERR(log
, "Flow actions may not be safe on all matching packets.");
1014 goto err_kfree_flow
;
1017 reply
= ovs_flow_cmd_alloc_info(acts
, &new_flow
->id
, info
, false,
1019 if (IS_ERR(reply
)) {
1020 error
= PTR_ERR(reply
);
1021 goto err_kfree_acts
;
1025 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1026 if (unlikely(!dp
)) {
1028 goto err_unlock_ovs
;
1031 /* Check if this is a duplicate flow */
1032 if (ovs_identifier_is_ufid(&new_flow
->id
))
1033 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &new_flow
->id
);
1035 flow
= ovs_flow_tbl_lookup(&dp
->table
, &new_flow
->key
);
1036 if (likely(!flow
)) {
1037 rcu_assign_pointer(new_flow
->sf_acts
, acts
);
1039 /* Put flow in bucket. */
1040 error
= ovs_flow_tbl_insert(&dp
->table
, new_flow
, &mask
);
1041 if (unlikely(error
)) {
1043 goto err_unlock_ovs
;
1046 if (unlikely(reply
)) {
1047 error
= ovs_flow_cmd_fill_info(new_flow
,
1048 ovs_header
->dp_ifindex
,
1049 reply
, info
->snd_portid
,
1057 struct sw_flow_actions
*old_acts
;
1059 /* Bail out if we're not allowed to modify an existing flow.
1060 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1061 * because Generic Netlink treats the latter as a dump
1062 * request. We also accept NLM_F_EXCL in case that bug ever
1065 if (unlikely(info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
1068 goto err_unlock_ovs
;
1070 /* The flow identifier has to be the same for flow updates.
1071 * Look for any overlapping flow.
1073 if (unlikely(!ovs_flow_cmp(flow
, &match
))) {
1074 if (ovs_identifier_is_key(&flow
->id
))
1075 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
,
1077 else /* UFID matches but key is different */
1081 goto err_unlock_ovs
;
1084 /* Update actions. */
1085 old_acts
= ovsl_dereference(flow
->sf_acts
);
1086 rcu_assign_pointer(flow
->sf_acts
, acts
);
1088 if (unlikely(reply
)) {
1089 error
= ovs_flow_cmd_fill_info(flow
,
1090 ovs_header
->dp_ifindex
,
1091 reply
, info
->snd_portid
,
1099 ovs_nla_free_flow_actions_rcu(old_acts
);
1100 ovs_flow_free(new_flow
, false);
1104 ovs_notify(&dp_flow_genl_family
, &ovs_dp_flow_multicast_group
, reply
, info
);
1111 ovs_nla_free_flow_actions(acts
);
1113 ovs_flow_free(new_flow
, false);
1118 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1119 static noinline_for_stack
struct sw_flow_actions
*get_flow_actions(struct net
*net
,
1120 const struct nlattr
*a
,
1121 const struct sw_flow_key
*key
,
1122 const struct sw_flow_mask
*mask
,
1125 struct sw_flow_actions
*acts
;
1126 struct sw_flow_key masked_key
;
1129 ovs_flow_mask_key(&masked_key
, key
, true, mask
);
1130 error
= ovs_nla_copy_actions(net
, a
, &masked_key
, &acts
, log
);
1133 "Actions may not be safe on all matching packets");
1134 return ERR_PTR(error
);
1140 /* Factor out match-init and action-copy to avoid
1141 * "Wframe-larger-than=1024" warning. Because mask is only
1142 * used to get actions, we new a function to save some
1145 * If there are not key and action attrs, we return 0
1146 * directly. In the case, the caller will also not use the
1147 * match as before. If there is action attr, we try to get
1148 * actions and save them to *acts. Before returning from
1149 * the function, we reset the match->mask pointer. Because
1150 * we should not to return match object with dangling reference
1153 static noinline_for_stack
int
1154 ovs_nla_init_match_and_action(struct net
*net
,
1155 struct sw_flow_match
*match
,
1156 struct sw_flow_key
*key
,
1158 struct sw_flow_actions
**acts
,
1161 struct sw_flow_mask mask
;
1164 if (a
[OVS_FLOW_ATTR_KEY
]) {
1165 ovs_match_init(match
, key
, true, &mask
);
1166 error
= ovs_nla_get_match(net
, match
, a
[OVS_FLOW_ATTR_KEY
],
1167 a
[OVS_FLOW_ATTR_MASK
], log
);
1172 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
1173 if (!a
[OVS_FLOW_ATTR_KEY
]) {
1175 "Flow key attribute not present in set flow.");
1180 *acts
= get_flow_actions(net
, a
[OVS_FLOW_ATTR_ACTIONS
], key
,
1182 if (IS_ERR(*acts
)) {
1183 error
= PTR_ERR(*acts
);
1188 /* On success, error is 0. */
1194 static int ovs_flow_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1196 struct net
*net
= sock_net(skb
->sk
);
1197 struct nlattr
**a
= info
->attrs
;
1198 struct ovs_header
*ovs_header
= info
->userhdr
;
1199 struct sw_flow_key key
;
1200 struct sw_flow
*flow
;
1201 struct sk_buff
*reply
= NULL
;
1202 struct datapath
*dp
;
1203 struct sw_flow_actions
*old_acts
= NULL
, *acts
= NULL
;
1204 struct sw_flow_match match
;
1205 struct sw_flow_id sfid
;
1206 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1208 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1211 ufid_present
= ovs_nla_get_ufid(&sfid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1212 if (!a
[OVS_FLOW_ATTR_KEY
] && !ufid_present
) {
1214 "Flow set message rejected, Key attribute missing.");
1218 error
= ovs_nla_init_match_and_action(net
, &match
, &key
, a
,
1224 /* Can allocate before locking if have acts. */
1225 reply
= ovs_flow_cmd_alloc_info(acts
, &sfid
, info
, false,
1227 if (IS_ERR(reply
)) {
1228 error
= PTR_ERR(reply
);
1229 goto err_kfree_acts
;
1234 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1235 if (unlikely(!dp
)) {
1237 goto err_unlock_ovs
;
1239 /* Check that the flow exists. */
1241 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &sfid
);
1243 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1244 if (unlikely(!flow
)) {
1246 goto err_unlock_ovs
;
1249 /* Update actions, if present. */
1251 old_acts
= ovsl_dereference(flow
->sf_acts
);
1252 rcu_assign_pointer(flow
->sf_acts
, acts
);
1254 if (unlikely(reply
)) {
1255 error
= ovs_flow_cmd_fill_info(flow
,
1256 ovs_header
->dp_ifindex
,
1257 reply
, info
->snd_portid
,
1264 /* Could not alloc without acts before locking. */
1265 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
,
1266 info
, OVS_FLOW_CMD_SET
, false,
1269 if (unlikely(IS_ERR(reply
))) {
1270 error
= PTR_ERR(reply
);
1271 goto err_unlock_ovs
;
1276 if (a
[OVS_FLOW_ATTR_CLEAR
])
1277 ovs_flow_stats_clear(flow
);
1281 ovs_notify(&dp_flow_genl_family
, &ovs_dp_flow_multicast_group
, reply
, info
);
1283 ovs_nla_free_flow_actions_rcu(old_acts
);
1291 ovs_nla_free_flow_actions(acts
);
1296 static int ovs_flow_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1298 struct nlattr
**a
= info
->attrs
;
1299 struct ovs_header
*ovs_header
= info
->userhdr
;
1300 struct net
*net
= sock_net(skb
->sk
);
1301 struct sw_flow_key key
;
1302 struct sk_buff
*reply
;
1303 struct sw_flow
*flow
;
1304 struct datapath
*dp
;
1305 struct sw_flow_match match
;
1306 struct sw_flow_id ufid
;
1307 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1309 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1312 ufid_present
= ovs_nla_get_ufid(&ufid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1313 if (a
[OVS_FLOW_ATTR_KEY
]) {
1314 ovs_match_init(&match
, &key
, true, NULL
);
1315 err
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
], NULL
,
1317 } else if (!ufid_present
) {
1319 "Flow get message rejected, Key attribute missing.");
1326 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1333 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &ufid
);
1335 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1341 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
, info
,
1342 OVS_FLOW_CMD_GET
, true, ufid_flags
);
1343 if (IS_ERR(reply
)) {
1344 err
= PTR_ERR(reply
);
1349 return genlmsg_reply(reply
, info
);
1355 static int ovs_flow_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1357 struct nlattr
**a
= info
->attrs
;
1358 struct ovs_header
*ovs_header
= info
->userhdr
;
1359 struct net
*net
= sock_net(skb
->sk
);
1360 struct sw_flow_key key
;
1361 struct sk_buff
*reply
;
1362 struct sw_flow
*flow
= NULL
;
1363 struct datapath
*dp
;
1364 struct sw_flow_match match
;
1365 struct sw_flow_id ufid
;
1366 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1368 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1371 ufid_present
= ovs_nla_get_ufid(&ufid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1372 if (a
[OVS_FLOW_ATTR_KEY
]) {
1373 ovs_match_init(&match
, &key
, true, NULL
);
1374 err
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
],
1381 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1382 if (unlikely(!dp
)) {
1387 if (unlikely(!a
[OVS_FLOW_ATTR_KEY
] && !ufid_present
)) {
1388 err
= ovs_flow_tbl_flush(&dp
->table
);
1393 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &ufid
);
1395 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1396 if (unlikely(!flow
)) {
1401 ovs_flow_tbl_remove(&dp
->table
, flow
);
1404 reply
= ovs_flow_cmd_alloc_info(rcu_dereference_raw(flow
->sf_acts
),
1405 &flow
->id
, info
, false, ufid_flags
);
1407 if (likely(reply
)) {
1408 if (!IS_ERR(reply
)) {
1409 rcu_read_lock(); /*To keep RCU checker happy. */
1410 err
= ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
,
1411 reply
, info
->snd_portid
,
1417 ovs_notify(&dp_flow_genl_family
, &ovs_dp_flow_multicast_group
, reply
, info
);
1419 genl_set_err(&dp_flow_genl_family
, sock_net(skb
->sk
), 0,
1420 GROUP_ID(&ovs_dp_flow_multicast_group
), PTR_ERR(reply
));
1425 ovs_flow_free(flow
, true);
1432 static int ovs_flow_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1434 struct nlattr
*a
[__OVS_FLOW_ATTR_MAX
];
1435 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1436 struct table_instance
*ti
;
1437 struct datapath
*dp
;
1441 err
= genlmsg_parse_deprecated(cb
->nlh
, &dp_flow_genl_family
, a
,
1442 OVS_FLOW_ATTR_MAX
, flow_policy
, NULL
);
1445 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1448 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1454 ti
= rcu_dereference(dp
->table
.ti
);
1456 struct sw_flow
*flow
;
1459 bucket
= cb
->args
[0];
1461 flow
= ovs_flow_tbl_dump_next(ti
, &bucket
, &obj
);
1465 if (ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
, skb
,
1466 NETLINK_CB(cb
->skb
).portid
,
1467 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1468 OVS_FLOW_CMD_GET
, ufid_flags
) < 0)
1471 cb
->args
[0] = bucket
;
1478 static const struct nla_policy flow_policy
[OVS_FLOW_ATTR_MAX
+ 1] = {
1479 [OVS_FLOW_ATTR_KEY
] = { .type
= NLA_NESTED
},
1480 [OVS_FLOW_ATTR_MASK
] = { .type
= NLA_NESTED
},
1481 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
1482 [OVS_FLOW_ATTR_CLEAR
] = { .type
= NLA_FLAG
},
1483 [OVS_FLOW_ATTR_PROBE
] = { .type
= NLA_FLAG
},
1484 [OVS_FLOW_ATTR_UFID
] = { .type
= NLA_UNSPEC
, .len
= 1 },
1485 [OVS_FLOW_ATTR_UFID_FLAGS
] = { .type
= NLA_U32
},
1488 static const struct genl_ops dp_flow_genl_ops
[] = {
1489 { .cmd
= OVS_FLOW_CMD_NEW
,
1490 #ifdef HAVE_GENL_VALIDATE_FLAGS
1491 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1493 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1494 #ifdef HAVE_GENL_OPS_POLICY
1495 .policy
= flow_policy
,
1497 .doit
= ovs_flow_cmd_new
1499 { .cmd
= OVS_FLOW_CMD_DEL
,
1500 #ifdef HAVE_GENL_VALIDATE_FLAGS
1501 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1503 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1504 #ifdef HAVE_GENL_OPS_POLICY
1505 .policy
= flow_policy
,
1507 .doit
= ovs_flow_cmd_del
1509 { .cmd
= OVS_FLOW_CMD_GET
,
1510 #ifdef HAVE_GENL_VALIDATE_FLAGS
1511 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1513 .flags
= 0, /* OK for unprivileged users. */
1514 #ifdef HAVE_GENL_OPS_POLICY
1515 .policy
= flow_policy
,
1517 .doit
= ovs_flow_cmd_get
,
1518 .dumpit
= ovs_flow_cmd_dump
1520 { .cmd
= OVS_FLOW_CMD_SET
,
1521 #ifdef HAVE_GENL_VALIDATE_FLAGS
1522 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1524 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1525 #ifdef HAVE_GENL_OPS_POLICY
1526 .policy
= flow_policy
,
1528 .doit
= ovs_flow_cmd_set
,
1532 static struct genl_family dp_flow_genl_family __ro_after_init
= {
1533 .hdrsize
= sizeof(struct ovs_header
),
1534 .name
= OVS_FLOW_FAMILY
,
1535 .version
= OVS_FLOW_VERSION
,
1536 .maxattr
= OVS_FLOW_ATTR_MAX
,
1537 #ifndef HAVE_GENL_OPS_POLICY
1538 .policy
= flow_policy
,
1541 .parallel_ops
= true,
1542 .ops
= dp_flow_genl_ops
,
1543 .n_ops
= ARRAY_SIZE(dp_flow_genl_ops
),
1544 .mcgrps
= &ovs_dp_flow_multicast_group
,
1546 .module
= THIS_MODULE
,
1549 static size_t ovs_dp_cmd_msg_size(void)
1551 size_t msgsize
= NLMSG_ALIGN(sizeof(struct ovs_header
));
1553 msgsize
+= nla_total_size(IFNAMSIZ
);
1554 msgsize
+= nla_total_size_64bit(sizeof(struct ovs_dp_stats
));
1555 msgsize
+= nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats
));
1556 msgsize
+= nla_total_size(sizeof(u32
)); /* OVS_DP_ATTR_USER_FEATURES */
1561 /* Called with ovs_mutex. */
1562 static int ovs_dp_cmd_fill_info(struct datapath
*dp
, struct sk_buff
*skb
,
1563 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1565 struct ovs_header
*ovs_header
;
1566 struct ovs_dp_stats dp_stats
;
1567 struct ovs_dp_megaflow_stats dp_megaflow_stats
;
1570 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_datapath_genl_family
,
1575 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
1577 err
= nla_put_string(skb
, OVS_DP_ATTR_NAME
, ovs_dp_name(dp
));
1579 goto nla_put_failure
;
1581 get_dp_stats(dp
, &dp_stats
, &dp_megaflow_stats
);
1582 if (nla_put_64bit(skb
, OVS_DP_ATTR_STATS
, sizeof(struct ovs_dp_stats
),
1583 &dp_stats
, OVS_DP_ATTR_PAD
))
1584 goto nla_put_failure
;
1586 if (nla_put_64bit(skb
, OVS_DP_ATTR_MEGAFLOW_STATS
,
1587 sizeof(struct ovs_dp_megaflow_stats
),
1588 &dp_megaflow_stats
, OVS_DP_ATTR_PAD
))
1589 goto nla_put_failure
;
1591 if (nla_put_u32(skb
, OVS_DP_ATTR_USER_FEATURES
, dp
->user_features
))
1592 goto nla_put_failure
;
1594 genlmsg_end(skb
, ovs_header
);
1598 genlmsg_cancel(skb
, ovs_header
);
1603 static struct sk_buff
*ovs_dp_cmd_alloc_info(void)
1605 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL
);
1608 /* Called with rcu_read_lock or ovs_mutex. */
1609 static struct datapath
*lookup_datapath(struct net
*net
,
1610 const struct ovs_header
*ovs_header
,
1611 struct nlattr
*a
[OVS_DP_ATTR_MAX
+ 1])
1613 struct datapath
*dp
;
1615 if (!a
[OVS_DP_ATTR_NAME
])
1616 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1618 struct vport
*vport
;
1620 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_DP_ATTR_NAME
]));
1621 dp
= vport
&& vport
->port_no
== OVSP_LOCAL
? vport
->dp
: NULL
;
1623 return dp
? dp
: ERR_PTR(-ENODEV
);
1626 static void ovs_dp_reset_user_features(struct sk_buff
*skb
, struct genl_info
*info
)
1628 struct datapath
*dp
;
1630 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1634 WARN(dp
->user_features
, "Dropping previously announced user features\n");
1635 dp
->user_features
= 0;
1638 DEFINE_STATIC_KEY_FALSE(tc_recirc_sharing_support
);
1640 static int ovs_dp_change(struct datapath
*dp
, struct nlattr
*a
[])
1642 u32 user_features
= 0;
1644 if (a
[OVS_DP_ATTR_USER_FEATURES
]) {
1645 user_features
= nla_get_u32(a
[OVS_DP_ATTR_USER_FEATURES
]);
1647 if (user_features
& ~(OVS_DP_F_VPORT_PIDS
|
1648 OVS_DP_F_UNALIGNED
|
1649 OVS_DP_F_TC_RECIRC_SHARING
))
1652 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1653 if (user_features
& OVS_DP_F_TC_RECIRC_SHARING
)
1658 dp
->user_features
= user_features
;
1660 if (dp
->user_features
& OVS_DP_F_TC_RECIRC_SHARING
)
1661 static_branch_enable(&tc_recirc_sharing_support
);
1663 static_branch_disable(&tc_recirc_sharing_support
);
1668 static int ovs_dp_stats_init(struct datapath
*dp
)
1670 dp
->stats_percpu
= netdev_alloc_pcpu_stats(struct dp_stats_percpu
);
1671 if (!dp
->stats_percpu
)
1677 static int ovs_dp_vport_init(struct datapath
*dp
)
1681 dp
->ports
= kmalloc_array(DP_VPORT_HASH_BUCKETS
,
1682 sizeof(struct hlist_head
),
1687 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
1688 INIT_HLIST_HEAD(&dp
->ports
[i
]);
1693 static int ovs_dp_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1695 struct nlattr
**a
= info
->attrs
;
1696 struct vport_parms parms
;
1697 struct sk_buff
*reply
;
1698 struct datapath
*dp
;
1699 struct vport
*vport
;
1700 struct ovs_net
*ovs_net
;
1704 if (!a
[OVS_DP_ATTR_NAME
] || !a
[OVS_DP_ATTR_UPCALL_PID
])
1707 reply
= ovs_dp_cmd_alloc_info();
1712 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1714 goto err_destroy_reply
;
1716 ovs_dp_set_net(dp
, sock_net(skb
->sk
));
1718 /* Allocate table. */
1719 err
= ovs_flow_tbl_init(&dp
->table
);
1721 goto err_destroy_dp
;
1723 err
= ovs_dp_stats_init(dp
);
1725 goto err_destroy_table
;
1727 err
= ovs_dp_vport_init(dp
);
1729 goto err_destroy_stats
;
1731 err
= ovs_meters_init(dp
);
1733 goto err_destroy_ports
;
1735 /* Set up our datapath device. */
1736 parms
.name
= nla_data(a
[OVS_DP_ATTR_NAME
]);
1737 parms
.type
= OVS_VPORT_TYPE_INTERNAL
;
1738 parms
.options
= NULL
;
1740 parms
.port_no
= OVSP_LOCAL
;
1741 parms
.upcall_portids
= a
[OVS_DP_ATTR_UPCALL_PID
];
1743 err
= ovs_dp_change(dp
, a
);
1745 goto err_destroy_meters
;
1747 /* So far only local changes have been made, now need the lock. */
1750 vport
= new_vport(&parms
);
1751 if (IS_ERR(vport
)) {
1752 err
= PTR_ERR(vport
);
1756 if (err
== -EEXIST
) {
1757 /* An outdated user space instance that does not understand
1758 * the concept of user_features has attempted to create a new
1759 * datapath and is likely to reuse it. Drop all user features.
1761 if (info
->genlhdr
->version
< OVS_DP_VER_FEATURES
)
1762 ovs_dp_reset_user_features(skb
, info
);
1766 goto err_destroy_meters
;
1769 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1770 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1773 ovs_net
= net_generic(ovs_dp_get_net(dp
), ovs_net_id
);
1774 list_add_tail_rcu(&dp
->list_node
, &ovs_net
->dps
);
1778 ovs_notify(&dp_datapath_genl_family
, &ovs_dp_datapath_multicast_group
, reply
, info
);
1782 ovs_meters_exit(dp
);
1786 free_percpu(dp
->stats_percpu
);
1788 ovs_flow_tbl_destroy(&dp
->table
);
1797 /* Called with ovs_mutex. */
1798 static void __dp_destroy(struct datapath
*dp
)
1802 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1803 struct vport
*vport
;
1804 struct hlist_node
*n
;
1806 hlist_for_each_entry_safe(vport
, n
, &dp
->ports
[i
], dp_hash_node
)
1807 if (vport
->port_no
!= OVSP_LOCAL
)
1808 ovs_dp_detach_port(vport
);
1811 list_del_rcu(&dp
->list_node
);
1813 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1814 * all ports in datapath are destroyed first before freeing datapath.
1816 ovs_dp_detach_port(ovs_vport_ovsl(dp
, OVSP_LOCAL
));
1818 /* RCU destroy the flow table */
1819 call_rcu(&dp
->rcu
, destroy_dp_rcu
);
1822 static int ovs_dp_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1824 struct sk_buff
*reply
;
1825 struct datapath
*dp
;
1828 reply
= ovs_dp_cmd_alloc_info();
1833 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1836 goto err_unlock_free
;
1838 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1839 info
->snd_seq
, 0, OVS_DP_CMD_DEL
);
1845 ovs_notify(&dp_datapath_genl_family
, &ovs_dp_datapath_multicast_group
, reply
, info
);
1854 static int ovs_dp_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1856 struct sk_buff
*reply
;
1857 struct datapath
*dp
;
1860 reply
= ovs_dp_cmd_alloc_info();
1865 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1868 goto err_unlock_free
;
1870 err
= ovs_dp_change(dp
, info
->attrs
);
1872 goto err_unlock_free
;
1874 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1875 info
->snd_seq
, 0, OVS_DP_CMD_GET
);
1880 ovs_notify(&dp_datapath_genl_family
, &ovs_dp_datapath_multicast_group
, reply
, info
);
1889 static int ovs_dp_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1891 struct sk_buff
*reply
;
1892 struct datapath
*dp
;
1895 reply
= ovs_dp_cmd_alloc_info();
1900 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1903 goto err_unlock_free
;
1905 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1906 info
->snd_seq
, 0, OVS_DP_CMD_GET
);
1910 return genlmsg_reply(reply
, info
);
1918 static int ovs_dp_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1920 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
1921 struct datapath
*dp
;
1922 int skip
= cb
->args
[0];
1926 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
1928 ovs_dp_cmd_fill_info(dp
, skb
, NETLINK_CB(cb
->skb
).portid
,
1929 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1930 OVS_DP_CMD_GET
) < 0)
1941 static const struct nla_policy datapath_policy
[OVS_DP_ATTR_MAX
+ 1] = {
1942 [OVS_DP_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1943 [OVS_DP_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1944 [OVS_DP_ATTR_USER_FEATURES
] = { .type
= NLA_U32
},
1947 static const struct genl_ops dp_datapath_genl_ops
[] = {
1948 { .cmd
= OVS_DP_CMD_NEW
,
1949 #ifdef HAVE_GENL_VALIDATE_FLAGS
1950 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1952 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1953 #ifdef HAVE_GENL_OPS_POLICY
1954 .policy
= datapath_policy
,
1956 .doit
= ovs_dp_cmd_new
1958 { .cmd
= OVS_DP_CMD_DEL
,
1959 #ifdef HAVE_GENL_VALIDATE_FLAGS
1960 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1962 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1963 #ifdef HAVE_GENL_OPS_POLICY
1964 .policy
= datapath_policy
,
1966 .doit
= ovs_dp_cmd_del
1968 { .cmd
= OVS_DP_CMD_GET
,
1969 #ifdef HAVE_GENL_VALIDATE_FLAGS
1970 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1972 .flags
= 0, /* OK for unprivileged users. */
1973 #ifdef HAVE_GENL_OPS_POLICY
1974 .policy
= datapath_policy
,
1976 .doit
= ovs_dp_cmd_get
,
1977 .dumpit
= ovs_dp_cmd_dump
1979 { .cmd
= OVS_DP_CMD_SET
,
1980 #ifdef HAVE_GENL_VALIDATE_FLAGS
1981 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1983 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1984 #ifdef HAVE_GENL_OPS_POLICY
1985 .policy
= datapath_policy
,
1987 .doit
= ovs_dp_cmd_set
,
1991 static struct genl_family dp_datapath_genl_family __ro_after_init
= {
1992 .hdrsize
= sizeof(struct ovs_header
),
1993 .name
= OVS_DATAPATH_FAMILY
,
1994 .version
= OVS_DATAPATH_VERSION
,
1995 .maxattr
= OVS_DP_ATTR_MAX
,
1996 #ifndef HAVE_GENL_OPS_POLICY
1997 .policy
= datapath_policy
,
2000 .parallel_ops
= true,
2001 .ops
= dp_datapath_genl_ops
,
2002 .n_ops
= ARRAY_SIZE(dp_datapath_genl_ops
),
2003 .mcgrps
= &ovs_dp_datapath_multicast_group
,
2005 .module
= THIS_MODULE
,
2008 /* Called with ovs_mutex or RCU read lock. */
2009 static int ovs_vport_cmd_fill_info(struct vport
*vport
, struct sk_buff
*skb
,
2010 struct net
*net
, u32 portid
, u32 seq
,
2011 u32 flags
, u8 cmd
, gfp_t gfp
)
2013 struct ovs_header
*ovs_header
;
2014 struct ovs_vport_stats vport_stats
;
2017 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_vport_genl_family
,
2022 ovs_header
->dp_ifindex
= get_dpifindex(vport
->dp
);
2024 if (nla_put_u32(skb
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
) ||
2025 nla_put_u32(skb
, OVS_VPORT_ATTR_TYPE
, vport
->ops
->type
) ||
2026 nla_put_string(skb
, OVS_VPORT_ATTR_NAME
,
2027 ovs_vport_name(vport
)) ||
2028 nla_put_u32(skb
, OVS_VPORT_ATTR_IFINDEX
, vport
->dev
->ifindex
))
2029 goto nla_put_failure
;
2031 #ifdef HAVE_PEERNET2ID_ALLOC
2032 if (!net_eq(net
, dev_net(vport
->dev
))) {
2033 int id
= peernet2id_alloc(net
, dev_net(vport
->dev
), gfp
);
2035 if (nla_put_s32(skb
, OVS_VPORT_ATTR_NETNSID
, id
))
2036 goto nla_put_failure
;
2040 ovs_vport_get_stats(vport
, &vport_stats
);
2041 if (nla_put_64bit(skb
, OVS_VPORT_ATTR_STATS
,
2042 sizeof(struct ovs_vport_stats
), &vport_stats
,
2043 OVS_VPORT_ATTR_PAD
))
2044 goto nla_put_failure
;
2046 if (ovs_vport_get_upcall_portids(vport
, skb
))
2047 goto nla_put_failure
;
2049 err
= ovs_vport_get_options(vport
, skb
);
2050 if (err
== -EMSGSIZE
)
2053 genlmsg_end(skb
, ovs_header
);
2059 genlmsg_cancel(skb
, ovs_header
);
2063 static struct sk_buff
*ovs_vport_cmd_alloc_info(void)
2065 return nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2068 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
2069 struct sk_buff
*ovs_vport_cmd_build_info(struct vport
*vport
, struct net
*net
,
2070 u32 portid
, u32 seq
, u8 cmd
)
2072 struct sk_buff
*skb
;
2075 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2077 return ERR_PTR(-ENOMEM
);
2079 retval
= ovs_vport_cmd_fill_info(vport
, skb
, net
, portid
, seq
, 0, cmd
,
2086 /* Called with ovs_mutex or RCU read lock. */
2087 static struct vport
*lookup_vport(struct net
*net
,
2088 const struct ovs_header
*ovs_header
,
2089 struct nlattr
*a
[OVS_VPORT_ATTR_MAX
+ 1])
2091 struct datapath
*dp
;
2092 struct vport
*vport
;
2094 if (a
[OVS_VPORT_ATTR_IFINDEX
])
2095 return ERR_PTR(-EOPNOTSUPP
);
2096 if (a
[OVS_VPORT_ATTR_NAME
]) {
2097 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_VPORT_ATTR_NAME
]));
2099 return ERR_PTR(-ENODEV
);
2100 if (ovs_header
->dp_ifindex
&&
2101 ovs_header
->dp_ifindex
!= get_dpifindex(vport
->dp
))
2102 return ERR_PTR(-ENODEV
);
2104 } else if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
2105 u32 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
2107 if (port_no
>= DP_MAX_PORTS
)
2108 return ERR_PTR(-EFBIG
);
2110 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
2112 return ERR_PTR(-ENODEV
);
2114 vport
= ovs_vport_ovsl_rcu(dp
, port_no
);
2116 return ERR_PTR(-ENODEV
);
2119 return ERR_PTR(-EINVAL
);
2123 static unsigned int ovs_get_max_headroom(struct datapath
*dp
)
2125 unsigned int dev_headroom
, max_headroom
= 0;
2126 struct net_device
*dev
;
2127 struct vport
*vport
;
2130 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2131 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
2133 dev_headroom
= netdev_get_fwd_headroom(dev
);
2134 if (dev_headroom
> max_headroom
)
2135 max_headroom
= dev_headroom
;
2139 return max_headroom
;
2142 /* Called with ovs_mutex */
2143 static void ovs_update_headroom(struct datapath
*dp
, unsigned int new_headroom
)
2145 struct vport
*vport
;
2148 dp
->max_headroom
= new_headroom
;
2149 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
2150 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
)
2151 netdev_set_rx_headroom(vport
->dev
, new_headroom
);
2154 static int ovs_vport_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
2156 struct nlattr
**a
= info
->attrs
;
2157 struct ovs_header
*ovs_header
= info
->userhdr
;
2158 struct vport_parms parms
;
2159 struct sk_buff
*reply
;
2160 struct vport
*vport
;
2161 struct datapath
*dp
;
2162 unsigned int new_headroom
;
2166 if (!a
[OVS_VPORT_ATTR_NAME
] || !a
[OVS_VPORT_ATTR_TYPE
] ||
2167 !a
[OVS_VPORT_ATTR_UPCALL_PID
])
2169 if (a
[OVS_VPORT_ATTR_IFINDEX
])
2172 port_no
= a
[OVS_VPORT_ATTR_PORT_NO
]
2173 ? nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]) : 0;
2174 if (port_no
>= DP_MAX_PORTS
)
2177 reply
= ovs_vport_cmd_alloc_info();
2183 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
2186 goto exit_unlock_free
;
2189 vport
= ovs_vport_ovsl(dp
, port_no
);
2192 goto exit_unlock_free
;
2194 for (port_no
= 1; ; port_no
++) {
2195 if (port_no
>= DP_MAX_PORTS
) {
2197 goto exit_unlock_free
;
2199 vport
= ovs_vport_ovsl(dp
, port_no
);
2205 parms
.name
= nla_data(a
[OVS_VPORT_ATTR_NAME
]);
2206 parms
.type
= nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
2207 parms
.options
= a
[OVS_VPORT_ATTR_OPTIONS
];
2209 parms
.port_no
= port_no
;
2210 parms
.upcall_portids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
2212 vport
= new_vport(&parms
);
2213 err
= PTR_ERR(vport
);
2214 if (IS_ERR(vport
)) {
2217 goto exit_unlock_free
;
2220 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2221 info
->snd_portid
, info
->snd_seq
, 0,
2222 OVS_VPORT_CMD_NEW
, GFP_KERNEL
);
2225 new_headroom
= netdev_get_fwd_headroom(vport
->dev
);
2227 if (new_headroom
> dp
->max_headroom
)
2228 ovs_update_headroom(dp
, new_headroom
);
2230 netdev_set_rx_headroom(vport
->dev
, dp
->max_headroom
);
2234 ovs_notify(&dp_vport_genl_family
, &ovs_dp_vport_multicast_group
, reply
, info
);
2243 static int ovs_vport_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
2245 struct nlattr
**a
= info
->attrs
;
2246 struct sk_buff
*reply
;
2247 struct vport
*vport
;
2250 reply
= ovs_vport_cmd_alloc_info();
2255 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2256 err
= PTR_ERR(vport
);
2258 goto exit_unlock_free
;
2260 if (a
[OVS_VPORT_ATTR_TYPE
] &&
2261 nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]) != vport
->ops
->type
) {
2263 goto exit_unlock_free
;
2266 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
2267 err
= ovs_vport_set_options(vport
, a
[OVS_VPORT_ATTR_OPTIONS
]);
2269 goto exit_unlock_free
;
2272 if (a
[OVS_VPORT_ATTR_UPCALL_PID
]) {
2273 struct nlattr
*ids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
2275 err
= ovs_vport_set_upcall_portids(vport
, ids
);
2277 goto exit_unlock_free
;
2280 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2281 info
->snd_portid
, info
->snd_seq
, 0,
2282 OVS_VPORT_CMD_SET
, GFP_KERNEL
);
2286 ovs_notify(&dp_vport_genl_family
, &ovs_dp_vport_multicast_group
, reply
, info
);
2295 static int ovs_vport_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
2297 bool update_headroom
= false;
2298 struct nlattr
**a
= info
->attrs
;
2299 struct sk_buff
*reply
;
2300 struct datapath
*dp
;
2301 struct vport
*vport
;
2302 unsigned int new_headroom
;
2305 reply
= ovs_vport_cmd_alloc_info();
2310 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2311 err
= PTR_ERR(vport
);
2313 goto exit_unlock_free
;
2315 if (vport
->port_no
== OVSP_LOCAL
) {
2317 goto exit_unlock_free
;
2320 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2321 info
->snd_portid
, info
->snd_seq
, 0,
2322 OVS_VPORT_CMD_DEL
, GFP_KERNEL
);
2325 /* the vport deletion may trigger dp headroom update */
2327 if (netdev_get_fwd_headroom(vport
->dev
) == dp
->max_headroom
)
2328 update_headroom
= true;
2330 netdev_reset_rx_headroom(vport
->dev
);
2331 ovs_dp_detach_port(vport
);
2333 if (update_headroom
) {
2334 new_headroom
= ovs_get_max_headroom(dp
);
2336 if (new_headroom
< dp
->max_headroom
)
2337 ovs_update_headroom(dp
, new_headroom
);
2341 ovs_notify(&dp_vport_genl_family
, &ovs_dp_vport_multicast_group
, reply
, info
);
2350 static int ovs_vport_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
2352 struct nlattr
**a
= info
->attrs
;
2353 struct ovs_header
*ovs_header
= info
->userhdr
;
2354 struct sk_buff
*reply
;
2355 struct vport
*vport
;
2358 reply
= ovs_vport_cmd_alloc_info();
2363 vport
= lookup_vport(sock_net(skb
->sk
), ovs_header
, a
);
2364 err
= PTR_ERR(vport
);
2366 goto exit_unlock_free
;
2367 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2368 info
->snd_portid
, info
->snd_seq
, 0,
2369 OVS_VPORT_CMD_GET
, GFP_ATOMIC
);
2373 return genlmsg_reply(reply
, info
);
2381 static int ovs_vport_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2383 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
2384 struct datapath
*dp
;
2385 int bucket
= cb
->args
[0], skip
= cb
->args
[1];
2389 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
2394 for (i
= bucket
; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2395 struct vport
*vport
;
2398 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
2400 ovs_vport_cmd_fill_info(vport
, skb
,
2402 NETLINK_CB(cb
->skb
).portid
,
2422 static const struct nla_policy vport_policy
[OVS_VPORT_ATTR_MAX
+ 1] = {
2423 [OVS_VPORT_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
2424 [OVS_VPORT_ATTR_STATS
] = { .len
= sizeof(struct ovs_vport_stats
) },
2425 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NLA_U32
},
2426 [OVS_VPORT_ATTR_TYPE
] = { .type
= NLA_U32
},
2427 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NLA_UNSPEC
},
2428 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NLA_NESTED
},
2429 [OVS_VPORT_ATTR_IFINDEX
] = { .type
= NLA_U32
},
2430 [OVS_VPORT_ATTR_NETNSID
] = { .type
= NLA_S32
},
2433 static const struct genl_ops dp_vport_genl_ops
[] = {
2434 { .cmd
= OVS_VPORT_CMD_NEW
,
2435 #ifdef HAVE_GENL_VALIDATE_FLAGS
2436 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2438 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2439 #ifdef HAVE_GENL_OPS_POLICY
2440 .policy
= vport_policy
,
2442 .doit
= ovs_vport_cmd_new
2444 { .cmd
= OVS_VPORT_CMD_DEL
,
2445 #ifdef HAVE_GENL_VALIDATE_FLAGS
2446 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2448 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2449 #ifdef HAVE_GENL_OPS_POLICY
2450 .policy
= vport_policy
,
2452 .doit
= ovs_vport_cmd_del
2454 { .cmd
= OVS_VPORT_CMD_GET
,
2455 #ifdef HAVE_GENL_VALIDATE_FLAGS
2456 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2458 .flags
= 0, /* OK for unprivileged users. */
2459 #ifdef HAVE_GENL_OPS_POLICY
2460 .policy
= vport_policy
,
2462 .doit
= ovs_vport_cmd_get
,
2463 .dumpit
= ovs_vport_cmd_dump
2465 { .cmd
= OVS_VPORT_CMD_SET
,
2466 #ifdef HAVE_GENL_VALIDATE_FLAGS
2467 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2469 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2470 #ifdef HAVE_GENL_OPS_POLICY
2471 .policy
= vport_policy
,
2473 .doit
= ovs_vport_cmd_set
,
2477 struct genl_family dp_vport_genl_family __ro_after_init
= {
2478 .hdrsize
= sizeof(struct ovs_header
),
2479 .name
= OVS_VPORT_FAMILY
,
2480 .version
= OVS_VPORT_VERSION
,
2481 .maxattr
= OVS_VPORT_ATTR_MAX
,
2482 #ifndef HAVE_GENL_OPS_POLICY
2483 .policy
= vport_policy
,
2486 .parallel_ops
= true,
2487 .ops
= dp_vport_genl_ops
,
2488 .n_ops
= ARRAY_SIZE(dp_vport_genl_ops
),
2489 .mcgrps
= &ovs_dp_vport_multicast_group
,
2491 .module
= THIS_MODULE
,
2494 static struct genl_family
*dp_genl_families
[] = {
2495 &dp_datapath_genl_family
,
2496 &dp_vport_genl_family
,
2497 &dp_flow_genl_family
,
2498 &dp_packet_genl_family
,
2499 &dp_meter_genl_family
,
2500 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2501 &dp_ct_limit_genl_family
,
2505 static void dp_unregister_genl(int n_families
)
2509 for (i
= 0; i
< n_families
; i
++)
2510 genl_unregister_family(dp_genl_families
[i
]);
2513 static int __init
dp_register_genl(void)
2518 for (i
= 0; i
< ARRAY_SIZE(dp_genl_families
); i
++) {
2520 err
= genl_register_family(dp_genl_families
[i
]);
2528 dp_unregister_genl(i
);
2532 static int __net_init
ovs_init_net(struct net
*net
)
2534 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2536 INIT_LIST_HEAD(&ovs_net
->dps
);
2537 INIT_WORK(&ovs_net
->dp_notify_work
, ovs_dp_notify_wq
);
2538 ovs_netns_frags_init(net
);
2539 ovs_netns_frags6_init(net
);
2540 return ovs_ct_init(net
);
2543 static void __net_exit
list_vports_from_net(struct net
*net
, struct net
*dnet
,
2544 struct list_head
*head
)
2546 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2547 struct datapath
*dp
;
2549 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
2552 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2553 struct vport
*vport
;
2555 hlist_for_each_entry(vport
, &dp
->ports
[i
], dp_hash_node
) {
2557 if (vport
->ops
->type
!= OVS_VPORT_TYPE_INTERNAL
)
2560 if (dev_net(vport
->dev
) == dnet
)
2561 list_add(&vport
->detach_list
, head
);
2567 static void __net_exit
ovs_exit_net(struct net
*dnet
)
2569 struct datapath
*dp
, *dp_next
;
2570 struct ovs_net
*ovs_net
= net_generic(dnet
, ovs_net_id
);
2571 struct vport
*vport
, *vport_next
;
2575 ovs_netns_frags6_exit(dnet
);
2576 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
);