1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2007-2014 Nicira, Inc.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/if_arp.h>
11 #include <linux/if_vlan.h>
14 #include <linux/jhash.h>
15 #include <linux/delay.h>
16 #include <linux/time.h>
17 #include <linux/etherdevice.h>
18 #include <linux/genetlink.h>
19 #include <linux/kernel.h>
20 #include <linux/kthread.h>
21 #include <linux/mutex.h>
22 #include <linux/percpu.h>
23 #include <linux/rcupdate.h>
24 #include <linux/tcp.h>
25 #include <linux/udp.h>
26 #include <linux/ethtool.h>
27 #include <linux/wait.h>
28 #include <asm/div64.h>
29 #include <linux/highmem.h>
30 #include <linux/netfilter_bridge.h>
31 #include <linux/netfilter_ipv4.h>
32 #include <linux/inetdevice.h>
33 #include <linux/list.h>
34 #include <linux/openvswitch.h>
35 #include <linux/rculist.h>
36 #include <linux/dmi.h>
37 #include <net/genetlink.h>
38 #include <net/net_namespace.h>
39 #include <net/netns/generic.h>
43 #include "flow_table.h"
44 #include "flow_netlink.h"
46 #include "vport-internal_dev.h"
47 #include "vport-netdev.h"
49 unsigned int ovs_net_id __read_mostly;
51 static struct genl_family dp_packet_genl_family;
52 static struct genl_family dp_flow_genl_family;
53 static struct genl_family dp_datapath_genl_family;
55 static const struct nla_policy flow_policy[];
57 static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
58 .name = OVS_FLOW_MCGROUP,
61 static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
62 .name = OVS_DATAPATH_MCGROUP,
65 static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
66 .name = OVS_VPORT_MCGROUP,
69 /* Check if need to build a reply message.
70 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
71 static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
74 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
75 genl_has_listeners(family, genl_info_net(info), group);
78 static void ovs_notify(struct genl_family *family,
79 struct sk_buff *skb, struct genl_info *info)
81 genl_notify(family, skb, info, 0, GFP_KERNEL);
87 * All writes e.g. Writes to device state (add/remove datapath, port, set
88 * operations on vports, etc.), Writes to other state (flow table
89 * modifications, set miscellaneous datapath parameters, etc.) are protected
92 * Reads are protected by RCU.
94 * There are a few special cases (mostly stats) that have their own
95 * synchronization but they nest under all of above and don't interact with
98 * The RTNL lock nests inside ovs_mutex.
101 static DEFINE_MUTEX(ovs_mutex);
105 mutex_lock(&ovs_mutex);
108 void ovs_unlock(void)
110 mutex_unlock(&ovs_mutex);
113 #ifdef CONFIG_LOCKDEP
114 int lockdep_ovsl_is_held(void)
117 return lockdep_is_held(&ovs_mutex);
123 static struct vport *new_vport(const struct vport_parms *);
124 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
125 const struct sw_flow_key *,
126 const struct dp_upcall_info *,
128 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
129 const struct sw_flow_key *,
130 const struct dp_upcall_info *,
133 static void ovs_dp_masks_rebalance(struct work_struct *work);
135 /* Must be called with rcu_read_lock or ovs_mutex. */
136 const char *ovs_dp_name(const struct datapath *dp)
138 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
139 return ovs_vport_name(vport);
142 static int get_dpifindex(const struct datapath *dp)
149 local = ovs_vport_rcu(dp, OVSP_LOCAL);
151 ifindex = local->dev->ifindex;
160 static void destroy_dp_rcu(struct rcu_head *rcu)
162 struct datapath *dp = container_of(rcu, struct datapath, rcu);
164 ovs_flow_tbl_destroy(&dp->table);
165 free_percpu(dp->stats_percpu);
171 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
174 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
177 /* Called with ovs_mutex or RCU read lock. */
178 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
181 struct hlist_head *head;
183 head = vport_hash_bucket(dp, port_no);
184 hlist_for_each_entry_rcu(vport, head, dp_hash_node,
185 lockdep_ovsl_is_held()) {
186 if (vport->port_no == port_no)
192 /* Called with ovs_mutex. */
193 static struct vport *new_vport(const struct vport_parms *parms)
197 vport = ovs_vport_add(parms);
198 if (!IS_ERR(vport)) {
199 struct datapath *dp = parms->dp;
200 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
202 hlist_add_head_rcu(&vport->dp_hash_node, head);
207 void ovs_dp_detach_port(struct vport *p)
211 /* First drop references to device. */
212 hlist_del_rcu(&p->dp_hash_node);
214 /* Then destroy it. */
218 /* Must be called with rcu_read_lock. */
219 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
221 const struct vport *p = OVS_CB(skb)->input_vport;
222 struct datapath *dp = p->dp;
223 struct sw_flow *flow;
224 struct sw_flow_actions *sf_acts;
225 struct dp_stats_percpu *stats;
231 stats = this_cpu_ptr(dp->stats_percpu);
234 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
235 &n_mask_hit, &n_cache_hit);
236 if (unlikely(!flow)) {
237 struct dp_upcall_info upcall;
239 memset(&upcall, 0, sizeof(upcall));
240 upcall.cmd = OVS_PACKET_CMD_MISS;
241 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
242 upcall.mru = OVS_CB(skb)->mru;
243 error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
255 stats_counter = &stats->n_missed;
259 ovs_flow_stats_update(flow, key->tp.flags, skb);
260 sf_acts = rcu_dereference(flow->sf_acts);
261 error = ovs_execute_actions(dp, skb, sf_acts, key);
263 net_dbg_ratelimited("ovs: action execution error on datapath %s: %d\n",
264 ovs_dp_name(dp), error);
266 stats_counter = &stats->n_hit;
269 /* Update datapath statistics. */
270 u64_stats_update_begin(&stats->syncp);
272 stats->n_mask_hit += n_mask_hit;
273 stats->n_cache_hit += n_cache_hit;
274 u64_stats_update_end(&stats->syncp);
277 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
278 const struct sw_flow_key *key,
279 const struct dp_upcall_info *upcall_info,
282 struct dp_stats_percpu *stats;
285 if (upcall_info->portid == 0) {
290 if (!skb_is_gso(skb))
291 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
293 err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
300 stats = this_cpu_ptr(dp->stats_percpu);
302 u64_stats_update_begin(&stats->syncp);
304 u64_stats_update_end(&stats->syncp);
309 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
310 const struct sw_flow_key *key,
311 const struct dp_upcall_info *upcall_info,
314 unsigned int gso_type = skb_shinfo(skb)->gso_type;
315 struct sw_flow_key later_key;
316 struct sk_buff *segs, *nskb;
319 BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_GSO_CB_OFFSET);
320 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
322 return PTR_ERR(segs);
326 if (gso_type & SKB_GSO_UDP) {
327 /* The initial flow key extracted by ovs_flow_key_extract()
328 * in this case is for a first fragment, so we need to
329 * properly mark later fragments.
332 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
335 /* Queue all of the segments. */
336 skb_list_walk_safe(segs, skb, nskb) {
337 if (gso_type & SKB_GSO_UDP && skb != segs)
340 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
346 /* Free all of the segments. */
347 skb_list_walk_safe(segs, skb, nskb) {
356 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
357 unsigned int hdrlen, int actions_attrlen)
359 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
360 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
361 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
362 + nla_total_size(sizeof(unsigned int)) /* OVS_PACKET_ATTR_LEN */
363 + nla_total_size(sizeof(u64)); /* OVS_PACKET_ATTR_HASH */
365 /* OVS_PACKET_ATTR_USERDATA */
366 if (upcall_info->userdata)
367 size += NLA_ALIGN(upcall_info->userdata->nla_len);
369 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
370 if (upcall_info->egress_tun_info)
371 size += nla_total_size(ovs_tun_key_attr_size());
373 /* OVS_PACKET_ATTR_ACTIONS */
374 if (upcall_info->actions_len)
375 size += nla_total_size(actions_attrlen);
377 /* OVS_PACKET_ATTR_MRU */
378 if (upcall_info->mru)
379 size += nla_total_size(sizeof(upcall_info->mru));
384 static void pad_packet(struct datapath *dp, struct sk_buff *skb)
386 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
387 size_t plen = NLA_ALIGN(skb->len) - skb->len;
390 skb_put_zero(skb, plen);
394 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
395 const struct sw_flow_key *key,
396 const struct dp_upcall_info *upcall_info,
399 struct ovs_header *upcall;
400 struct sk_buff *nskb = NULL;
401 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
408 dp_ifindex = get_dpifindex(dp);
412 if (skb_vlan_tag_present(skb)) {
413 nskb = skb_clone(skb, GFP_ATOMIC);
417 nskb = __vlan_hwaccel_push_inside(nskb);
424 if (nla_attr_size(skb->len) > USHRT_MAX) {
429 /* Complete checksum if needed */
430 if (skb->ip_summed == CHECKSUM_PARTIAL &&
431 (err = skb_csum_hwoffload_help(skb, 0)))
434 /* Older versions of OVS user space enforce alignment of the last
435 * Netlink attribute to NLA_ALIGNTO which would require extensive
436 * padding logic. Only perform zerocopy if padding is not required.
438 if (dp->user_features & OVS_DP_F_UNALIGNED)
439 hlen = skb_zerocopy_headlen(skb);
443 len = upcall_msg_size(upcall_info, hlen - cutlen,
444 OVS_CB(skb)->acts_origlen);
445 user_skb = genlmsg_new(len, GFP_ATOMIC);
451 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
452 0, upcall_info->cmd);
457 upcall->dp_ifindex = dp_ifindex;
459 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
463 if (upcall_info->userdata)
464 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
465 nla_len(upcall_info->userdata),
466 nla_data(upcall_info->userdata));
468 if (upcall_info->egress_tun_info) {
469 nla = nla_nest_start_noflag(user_skb,
470 OVS_PACKET_ATTR_EGRESS_TUN_KEY);
475 err = ovs_nla_put_tunnel_info(user_skb,
476 upcall_info->egress_tun_info);
480 nla_nest_end(user_skb, nla);
483 if (upcall_info->actions_len) {
484 nla = nla_nest_start_noflag(user_skb, OVS_PACKET_ATTR_ACTIONS);
489 err = ovs_nla_put_actions(upcall_info->actions,
490 upcall_info->actions_len,
493 nla_nest_end(user_skb, nla);
495 nla_nest_cancel(user_skb, nla);
498 /* Add OVS_PACKET_ATTR_MRU */
499 if (upcall_info->mru &&
500 nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU, upcall_info->mru)) {
505 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
507 nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN, skb->len)) {
512 /* Add OVS_PACKET_ATTR_HASH */
513 hash = skb_get_hash_raw(skb);
515 hash |= OVS_PACKET_HASH_SW_BIT;
518 hash |= OVS_PACKET_HASH_L4_BIT;
520 if (nla_put(user_skb, OVS_PACKET_ATTR_HASH, sizeof (u64), &hash)) {
525 /* Only reserve room for attribute header, packet data is added
526 * in skb_zerocopy() */
527 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
531 nla->nla_len = nla_attr_size(skb->len - cutlen);
533 err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
537 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
538 pad_packet(dp, user_skb);
540 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
542 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
547 consume_skb(user_skb);
553 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
555 struct ovs_header *ovs_header = info->userhdr;
556 struct net *net = sock_net(skb->sk);
557 struct nlattr **a = info->attrs;
558 struct sw_flow_actions *acts;
559 struct sk_buff *packet;
560 struct sw_flow *flow;
561 struct sw_flow_actions *sf_acts;
563 struct vport *input_vport;
568 bool log = !a[OVS_PACKET_ATTR_PROBE];
571 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
572 !a[OVS_PACKET_ATTR_ACTIONS])
575 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
576 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
580 skb_reserve(packet, NET_IP_ALIGN);
582 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
584 /* Set packet's mru */
585 if (a[OVS_PACKET_ATTR_MRU]) {
586 mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
587 packet->ignore_df = 1;
589 OVS_CB(packet)->mru = mru;
591 if (a[OVS_PACKET_ATTR_HASH]) {
592 hash = nla_get_u64(a[OVS_PACKET_ATTR_HASH]);
594 __skb_set_hash(packet, hash & 0xFFFFFFFFULL,
595 !!(hash & OVS_PACKET_HASH_SW_BIT),
596 !!(hash & OVS_PACKET_HASH_L4_BIT));
599 /* Build an sw_flow for sending this packet. */
600 flow = ovs_flow_alloc();
605 err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
606 packet, &flow->key, log);
610 err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
611 &flow->key, &acts, log);
615 rcu_assign_pointer(flow->sf_acts, acts);
616 packet->priority = flow->key.phy.priority;
617 packet->mark = flow->key.phy.skb_mark;
620 dp = get_dp_rcu(net, ovs_header->dp_ifindex);
625 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
627 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
632 packet->dev = input_vport->dev;
633 OVS_CB(packet)->input_vport = input_vport;
634 sf_acts = rcu_dereference(flow->sf_acts);
637 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
641 ovs_flow_free(flow, false);
647 ovs_flow_free(flow, false);
654 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
655 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
656 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
657 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
658 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
659 [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
660 [OVS_PACKET_ATTR_HASH] = { .type = NLA_U64 },
663 static const struct genl_small_ops dp_packet_genl_ops[] = {
664 { .cmd = OVS_PACKET_CMD_EXECUTE,
665 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
666 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
667 .doit = ovs_packet_cmd_execute
671 static struct genl_family dp_packet_genl_family __ro_after_init = {
672 .hdrsize = sizeof(struct ovs_header),
673 .name = OVS_PACKET_FAMILY,
674 .version = OVS_PACKET_VERSION,
675 .maxattr = OVS_PACKET_ATTR_MAX,
676 .policy = packet_policy,
678 .parallel_ops = true,
679 .small_ops = dp_packet_genl_ops,
680 .n_small_ops = ARRAY_SIZE(dp_packet_genl_ops),
681 .module = THIS_MODULE,
684 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
685 struct ovs_dp_megaflow_stats *mega_stats)
689 memset(mega_stats, 0, sizeof(*mega_stats));
691 stats->n_flows = ovs_flow_tbl_count(&dp->table);
692 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
694 stats->n_hit = stats->n_missed = stats->n_lost = 0;
696 for_each_possible_cpu(i) {
697 const struct dp_stats_percpu *percpu_stats;
698 struct dp_stats_percpu local_stats;
701 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
704 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
705 local_stats = *percpu_stats;
706 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
708 stats->n_hit += local_stats.n_hit;
709 stats->n_missed += local_stats.n_missed;
710 stats->n_lost += local_stats.n_lost;
711 mega_stats->n_mask_hit += local_stats.n_mask_hit;
712 mega_stats->n_cache_hit += local_stats.n_cache_hit;
716 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
718 return ovs_identifier_is_ufid(sfid) &&
719 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
722 static bool should_fill_mask(uint32_t ufid_flags)
724 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
727 static bool should_fill_actions(uint32_t ufid_flags)
729 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
732 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
733 const struct sw_flow_id *sfid,
736 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
738 /* OVS_FLOW_ATTR_UFID, or unmasked flow key as fallback
739 * see ovs_nla_put_identifier()
741 if (sfid && ovs_identifier_is_ufid(sfid))
742 len += nla_total_size(sfid->ufid_len);
744 len += nla_total_size(ovs_key_attr_size());
746 /* OVS_FLOW_ATTR_KEY */
747 if (!sfid || should_fill_key(sfid, ufid_flags))
748 len += nla_total_size(ovs_key_attr_size());
750 /* OVS_FLOW_ATTR_MASK */
751 if (should_fill_mask(ufid_flags))
752 len += nla_total_size(ovs_key_attr_size());
754 /* OVS_FLOW_ATTR_ACTIONS */
755 if (should_fill_actions(ufid_flags))
756 len += nla_total_size(acts->orig_len);
759 + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
760 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
761 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
764 /* Called with ovs_mutex or RCU read lock. */
765 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
768 struct ovs_flow_stats stats;
772 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
775 nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
779 if (stats.n_packets &&
780 nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
781 sizeof(struct ovs_flow_stats), &stats,
785 if ((u8)ntohs(tcp_flags) &&
786 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
792 /* Called with ovs_mutex or RCU read lock. */
793 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
794 struct sk_buff *skb, int skb_orig_len)
796 struct nlattr *start;
799 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
800 * this is the first flow to be dumped into 'skb'. This is unusual for
801 * Netlink but individual action lists can be longer than
802 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
803 * The userspace caller can always fetch the actions separately if it
804 * really wants them. (Most userspace callers in fact don't care.)
806 * This can only fail for dump operations because the skb is always
807 * properly sized for single flows.
809 start = nla_nest_start_noflag(skb, OVS_FLOW_ATTR_ACTIONS);
811 const struct sw_flow_actions *sf_acts;
813 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
814 err = ovs_nla_put_actions(sf_acts->actions,
815 sf_acts->actions_len, skb);
818 nla_nest_end(skb, start);
823 nla_nest_cancel(skb, start);
825 } else if (skb_orig_len) {
832 /* Called with ovs_mutex or RCU read lock. */
833 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
834 struct sk_buff *skb, u32 portid,
835 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
837 const int skb_orig_len = skb->len;
838 struct ovs_header *ovs_header;
841 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
846 ovs_header->dp_ifindex = dp_ifindex;
848 err = ovs_nla_put_identifier(flow, skb);
852 if (should_fill_key(&flow->id, ufid_flags)) {
853 err = ovs_nla_put_masked_key(flow, skb);
858 if (should_fill_mask(ufid_flags)) {
859 err = ovs_nla_put_mask(flow, skb);
864 err = ovs_flow_cmd_fill_stats(flow, skb);
868 if (should_fill_actions(ufid_flags)) {
869 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
874 genlmsg_end(skb, ovs_header);
878 genlmsg_cancel(skb, ovs_header);
882 /* May not be called with RCU read lock. */
883 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
884 const struct sw_flow_id *sfid,
885 struct genl_info *info,
892 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
895 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
896 skb = genlmsg_new(len, GFP_KERNEL);
898 return ERR_PTR(-ENOMEM);
903 /* Called with ovs_mutex. */
904 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
906 struct genl_info *info, u8 cmd,
907 bool always, u32 ufid_flags)
912 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
913 &flow->id, info, always, ufid_flags);
914 if (IS_ERR_OR_NULL(skb))
917 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
918 info->snd_portid, info->snd_seq, 0,
920 if (WARN_ON_ONCE(retval < 0)) {
922 skb = ERR_PTR(retval);
927 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
929 struct net *net = sock_net(skb->sk);
930 struct nlattr **a = info->attrs;
931 struct ovs_header *ovs_header = info->userhdr;
932 struct sw_flow *flow = NULL, *new_flow;
933 struct sw_flow_mask mask;
934 struct sk_buff *reply;
936 struct sw_flow_key *key;
937 struct sw_flow_actions *acts;
938 struct sw_flow_match match;
939 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
941 bool log = !a[OVS_FLOW_ATTR_PROBE];
943 /* Must have key and actions. */
945 if (!a[OVS_FLOW_ATTR_KEY]) {
946 OVS_NLERR(log, "Flow key attr not present in new flow.");
949 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
950 OVS_NLERR(log, "Flow actions attr not present in new flow.");
954 /* Most of the time we need to allocate a new flow, do it before
957 new_flow = ovs_flow_alloc();
958 if (IS_ERR(new_flow)) {
959 error = PTR_ERR(new_flow);
964 key = kzalloc(sizeof(*key), GFP_KERNEL);
970 ovs_match_init(&match, key, false, &mask);
971 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
972 a[OVS_FLOW_ATTR_MASK], log);
976 ovs_flow_mask_key(&new_flow->key, key, true, &mask);
978 /* Extract flow identifier. */
979 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
984 /* Validate actions. */
985 error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
986 &new_flow->key, &acts, log);
988 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
992 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
995 error = PTR_ERR(reply);
1000 dp = get_dp(net, ovs_header->dp_ifindex);
1001 if (unlikely(!dp)) {
1003 goto err_unlock_ovs;
1006 /* Check if this is a duplicate flow */
1007 if (ovs_identifier_is_ufid(&new_flow->id))
1008 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
1010 flow = ovs_flow_tbl_lookup(&dp->table, key);
1011 if (likely(!flow)) {
1012 rcu_assign_pointer(new_flow->sf_acts, acts);
1014 /* Put flow in bucket. */
1015 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
1016 if (unlikely(error)) {
1018 goto err_unlock_ovs;
1021 if (unlikely(reply)) {
1022 error = ovs_flow_cmd_fill_info(new_flow,
1023 ovs_header->dp_ifindex,
1024 reply, info->snd_portid,
1032 struct sw_flow_actions *old_acts;
1034 /* Bail out if we're not allowed to modify an existing flow.
1035 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1036 * because Generic Netlink treats the latter as a dump
1037 * request. We also accept NLM_F_EXCL in case that bug ever
1040 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
1043 goto err_unlock_ovs;
1045 /* The flow identifier has to be the same for flow updates.
1046 * Look for any overlapping flow.
1048 if (unlikely(!ovs_flow_cmp(flow, &match))) {
1049 if (ovs_identifier_is_key(&flow->id))
1050 flow = ovs_flow_tbl_lookup_exact(&dp->table,
1052 else /* UFID matches but key is different */
1056 goto err_unlock_ovs;
1059 /* Update actions. */
1060 old_acts = ovsl_dereference(flow->sf_acts);
1061 rcu_assign_pointer(flow->sf_acts, acts);
1063 if (unlikely(reply)) {
1064 error = ovs_flow_cmd_fill_info(flow,
1065 ovs_header->dp_ifindex,
1066 reply, info->snd_portid,
1074 ovs_nla_free_flow_actions_rcu(old_acts);
1075 ovs_flow_free(new_flow, false);
1079 ovs_notify(&dp_flow_genl_family, reply, info);
1088 ovs_nla_free_flow_actions(acts);
1092 ovs_flow_free(new_flow, false);
1097 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1098 static noinline_for_stack
1099 struct sw_flow_actions *get_flow_actions(struct net *net,
1100 const struct nlattr *a,
1101 const struct sw_flow_key *key,
1102 const struct sw_flow_mask *mask,
1105 struct sw_flow_actions *acts;
1106 struct sw_flow_key masked_key;
1109 ovs_flow_mask_key(&masked_key, key, true, mask);
1110 error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
1113 "Actions may not be safe on all matching packets");
1114 return ERR_PTR(error);
1120 /* Factor out match-init and action-copy to avoid
1121 * "Wframe-larger-than=1024" warning. Because mask is only
1122 * used to get actions, we new a function to save some
1125 * If there are not key and action attrs, we return 0
1126 * directly. In the case, the caller will also not use the
1127 * match as before. If there is action attr, we try to get
1128 * actions and save them to *acts. Before returning from
1129 * the function, we reset the match->mask pointer. Because
1130 * we should not to return match object with dangling reference
1133 static noinline_for_stack int
1134 ovs_nla_init_match_and_action(struct net *net,
1135 struct sw_flow_match *match,
1136 struct sw_flow_key *key,
1138 struct sw_flow_actions **acts,
1141 struct sw_flow_mask mask;
1144 if (a[OVS_FLOW_ATTR_KEY]) {
1145 ovs_match_init(match, key, true, &mask);
1146 error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
1147 a[OVS_FLOW_ATTR_MASK], log);
1152 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1153 if (!a[OVS_FLOW_ATTR_KEY]) {
1155 "Flow key attribute not present in set flow.");
1160 *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
1162 if (IS_ERR(*acts)) {
1163 error = PTR_ERR(*acts);
1168 /* On success, error is 0. */
1174 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1176 struct net *net = sock_net(skb->sk);
1177 struct nlattr **a = info->attrs;
1178 struct ovs_header *ovs_header = info->userhdr;
1179 struct sw_flow_key key;
1180 struct sw_flow *flow;
1181 struct sk_buff *reply = NULL;
1182 struct datapath *dp;
1183 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1184 struct sw_flow_match match;
1185 struct sw_flow_id sfid;
1186 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1188 bool log = !a[OVS_FLOW_ATTR_PROBE];
1191 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1192 if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
1194 "Flow set message rejected, Key attribute missing.");
1198 error = ovs_nla_init_match_and_action(net, &match, &key, a,
1204 /* Can allocate before locking if have acts. */
1205 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1207 if (IS_ERR(reply)) {
1208 error = PTR_ERR(reply);
1209 goto err_kfree_acts;
1214 dp = get_dp(net, ovs_header->dp_ifindex);
1215 if (unlikely(!dp)) {
1217 goto err_unlock_ovs;
1219 /* Check that the flow exists. */
1221 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1223 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1224 if (unlikely(!flow)) {
1226 goto err_unlock_ovs;
1229 /* Update actions, if present. */
1231 old_acts = ovsl_dereference(flow->sf_acts);
1232 rcu_assign_pointer(flow->sf_acts, acts);
1234 if (unlikely(reply)) {
1235 error = ovs_flow_cmd_fill_info(flow,
1236 ovs_header->dp_ifindex,
1237 reply, info->snd_portid,
1244 /* Could not alloc without acts before locking. */
1245 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1246 info, OVS_FLOW_CMD_SET, false,
1249 if (IS_ERR(reply)) {
1250 error = PTR_ERR(reply);
1251 goto err_unlock_ovs;
1256 if (a[OVS_FLOW_ATTR_CLEAR])
1257 ovs_flow_stats_clear(flow);
1261 ovs_notify(&dp_flow_genl_family, reply, info);
1263 ovs_nla_free_flow_actions_rcu(old_acts);
1271 ovs_nla_free_flow_actions(acts);
1276 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1278 struct nlattr **a = info->attrs;
1279 struct ovs_header *ovs_header = info->userhdr;
1280 struct net *net = sock_net(skb->sk);
1281 struct sw_flow_key key;
1282 struct sk_buff *reply;
1283 struct sw_flow *flow;
1284 struct datapath *dp;
1285 struct sw_flow_match match;
1286 struct sw_flow_id ufid;
1287 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1289 bool log = !a[OVS_FLOW_ATTR_PROBE];
1292 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1293 if (a[OVS_FLOW_ATTR_KEY]) {
1294 ovs_match_init(&match, &key, true, NULL);
1295 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
1297 } else if (!ufid_present) {
1299 "Flow get message rejected, Key attribute missing.");
1306 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1313 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1315 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1321 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1322 OVS_FLOW_CMD_GET, true, ufid_flags);
1323 if (IS_ERR(reply)) {
1324 err = PTR_ERR(reply);
1329 return genlmsg_reply(reply, info);
1335 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1337 struct nlattr **a = info->attrs;
1338 struct ovs_header *ovs_header = info->userhdr;
1339 struct net *net = sock_net(skb->sk);
1340 struct sw_flow_key key;
1341 struct sk_buff *reply;
1342 struct sw_flow *flow = NULL;
1343 struct datapath *dp;
1344 struct sw_flow_match match;
1345 struct sw_flow_id ufid;
1346 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1348 bool log = !a[OVS_FLOW_ATTR_PROBE];
1351 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1352 if (a[OVS_FLOW_ATTR_KEY]) {
1353 ovs_match_init(&match, &key, true, NULL);
1354 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1361 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1362 if (unlikely(!dp)) {
1367 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1368 err = ovs_flow_tbl_flush(&dp->table);
1373 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1375 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1376 if (unlikely(!flow)) {
1381 ovs_flow_tbl_remove(&dp->table, flow);
1384 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1385 &flow->id, info, false, ufid_flags);
1386 if (likely(reply)) {
1387 if (!IS_ERR(reply)) {
1388 rcu_read_lock(); /*To keep RCU checker happy. */
1389 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1390 reply, info->snd_portid,
1395 if (WARN_ON_ONCE(err < 0)) {
1400 ovs_notify(&dp_flow_genl_family, reply, info);
1402 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0,
1408 ovs_flow_free(flow, true);
1415 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1417 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1418 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1419 struct table_instance *ti;
1420 struct datapath *dp;
1424 err = genlmsg_parse_deprecated(cb->nlh, &dp_flow_genl_family, a,
1425 OVS_FLOW_ATTR_MAX, flow_policy, NULL);
1428 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1431 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1437 ti = rcu_dereference(dp->table.ti);
1439 struct sw_flow *flow;
1442 bucket = cb->args[0];
1444 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1448 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1449 NETLINK_CB(cb->skb).portid,
1450 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1451 OVS_FLOW_CMD_GET, ufid_flags) < 0)
1454 cb->args[0] = bucket;
1461 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1462 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1463 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1464 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1465 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1466 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1467 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1468 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1471 static const struct genl_small_ops dp_flow_genl_ops[] = {
1472 { .cmd = OVS_FLOW_CMD_NEW,
1473 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1474 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1475 .doit = ovs_flow_cmd_new
1477 { .cmd = OVS_FLOW_CMD_DEL,
1478 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1479 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1480 .doit = ovs_flow_cmd_del
1482 { .cmd = OVS_FLOW_CMD_GET,
1483 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1484 .flags = 0, /* OK for unprivileged users. */
1485 .doit = ovs_flow_cmd_get,
1486 .dumpit = ovs_flow_cmd_dump
1488 { .cmd = OVS_FLOW_CMD_SET,
1489 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1490 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1491 .doit = ovs_flow_cmd_set,
1495 static struct genl_family dp_flow_genl_family __ro_after_init = {
1496 .hdrsize = sizeof(struct ovs_header),
1497 .name = OVS_FLOW_FAMILY,
1498 .version = OVS_FLOW_VERSION,
1499 .maxattr = OVS_FLOW_ATTR_MAX,
1500 .policy = flow_policy,
1502 .parallel_ops = true,
1503 .small_ops = dp_flow_genl_ops,
1504 .n_small_ops = ARRAY_SIZE(dp_flow_genl_ops),
1505 .mcgrps = &ovs_dp_flow_multicast_group,
1507 .module = THIS_MODULE,
1510 static size_t ovs_dp_cmd_msg_size(void)
1512 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1514 msgsize += nla_total_size(IFNAMSIZ);
1515 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
1516 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
1517 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1518 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_MASKS_CACHE_SIZE */
1523 /* Called with ovs_mutex. */
1524 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1525 u32 portid, u32 seq, u32 flags, u8 cmd)
1527 struct ovs_header *ovs_header;
1528 struct ovs_dp_stats dp_stats;
1529 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1532 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1537 ovs_header->dp_ifindex = get_dpifindex(dp);
1539 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1541 goto nla_put_failure;
1543 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1544 if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1545 &dp_stats, OVS_DP_ATTR_PAD))
1546 goto nla_put_failure;
1548 if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1549 sizeof(struct ovs_dp_megaflow_stats),
1550 &dp_megaflow_stats, OVS_DP_ATTR_PAD))
1551 goto nla_put_failure;
1553 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1554 goto nla_put_failure;
1556 if (nla_put_u32(skb, OVS_DP_ATTR_MASKS_CACHE_SIZE,
1557 ovs_flow_tbl_masks_cache_size(&dp->table)))
1558 goto nla_put_failure;
1560 genlmsg_end(skb, ovs_header);
1564 genlmsg_cancel(skb, ovs_header);
1569 static struct sk_buff *ovs_dp_cmd_alloc_info(void)
1571 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1574 /* Called with rcu_read_lock or ovs_mutex. */
1575 static struct datapath *lookup_datapath(struct net *net,
1576 const struct ovs_header *ovs_header,
1577 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1579 struct datapath *dp;
1581 if (!a[OVS_DP_ATTR_NAME])
1582 dp = get_dp(net, ovs_header->dp_ifindex);
1584 struct vport *vport;
1586 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1587 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1589 return dp ? dp : ERR_PTR(-ENODEV);
1592 static void ovs_dp_reset_user_features(struct sk_buff *skb,
1593 struct genl_info *info)
1595 struct datapath *dp;
1597 dp = lookup_datapath(sock_net(skb->sk), info->userhdr,
1602 pr_warn("%s: Dropping previously announced user features\n",
1604 dp->user_features = 0;
1607 DEFINE_STATIC_KEY_FALSE(tc_recirc_sharing_support);
1609 static int ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1611 u32 user_features = 0;
1613 if (a[OVS_DP_ATTR_USER_FEATURES]) {
1614 user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1616 if (user_features & ~(OVS_DP_F_VPORT_PIDS |
1617 OVS_DP_F_UNALIGNED |
1618 OVS_DP_F_TC_RECIRC_SHARING))
1621 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1622 if (user_features & OVS_DP_F_TC_RECIRC_SHARING)
1627 if (a[OVS_DP_ATTR_MASKS_CACHE_SIZE]) {
1631 cache_size = nla_get_u32(a[OVS_DP_ATTR_MASKS_CACHE_SIZE]);
1632 err = ovs_flow_tbl_masks_cache_resize(&dp->table, cache_size);
1637 dp->user_features = user_features;
1639 if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING)
1640 static_branch_enable(&tc_recirc_sharing_support);
1642 static_branch_disable(&tc_recirc_sharing_support);
1647 static int ovs_dp_stats_init(struct datapath *dp)
1649 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1650 if (!dp->stats_percpu)
1656 static int ovs_dp_vport_init(struct datapath *dp)
1660 dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS,
1661 sizeof(struct hlist_head),
1666 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1667 INIT_HLIST_HEAD(&dp->ports[i]);
1672 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1674 struct nlattr **a = info->attrs;
1675 struct vport_parms parms;
1676 struct sk_buff *reply;
1677 struct datapath *dp;
1678 struct vport *vport;
1679 struct ovs_net *ovs_net;
1683 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1686 reply = ovs_dp_cmd_alloc_info();
1691 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1693 goto err_destroy_reply;
1695 ovs_dp_set_net(dp, sock_net(skb->sk));
1697 /* Allocate table. */
1698 err = ovs_flow_tbl_init(&dp->table);
1700 goto err_destroy_dp;
1702 err = ovs_dp_stats_init(dp);
1704 goto err_destroy_table;
1706 err = ovs_dp_vport_init(dp);
1708 goto err_destroy_stats;
1710 err = ovs_meters_init(dp);
1712 goto err_destroy_ports;
1714 /* Set up our datapath device. */
1715 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1716 parms.type = OVS_VPORT_TYPE_INTERNAL;
1717 parms.options = NULL;
1719 parms.port_no = OVSP_LOCAL;
1720 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1722 /* So far only local changes have been made, now need the lock. */
1725 err = ovs_dp_change(dp, a);
1727 goto err_unlock_and_destroy_meters;
1729 vport = new_vport(&parms);
1730 if (IS_ERR(vport)) {
1731 err = PTR_ERR(vport);
1735 if (err == -EEXIST) {
1736 /* An outdated user space instance that does not understand
1737 * the concept of user_features has attempted to create a new
1738 * datapath and is likely to reuse it. Drop all user features.
1740 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1741 ovs_dp_reset_user_features(skb, info);
1744 goto err_unlock_and_destroy_meters;
1747 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1748 info->snd_seq, 0, OVS_DP_CMD_NEW);
1751 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1752 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1756 ovs_notify(&dp_datapath_genl_family, reply, info);
1759 err_unlock_and_destroy_meters:
1761 ovs_meters_exit(dp);
1765 free_percpu(dp->stats_percpu);
1767 ovs_flow_tbl_destroy(&dp->table);
1776 /* Called with ovs_mutex. */
1777 static void __dp_destroy(struct datapath *dp)
1779 struct flow_table *table = &dp->table;
1782 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1783 struct vport *vport;
1784 struct hlist_node *n;
1786 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1787 if (vport->port_no != OVSP_LOCAL)
1788 ovs_dp_detach_port(vport);
1791 list_del_rcu(&dp->list_node);
1793 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1794 * all ports in datapath are destroyed first before freeing datapath.
1796 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1798 /* Flush sw_flow in the tables. RCU cb only releases resource
1799 * such as dp, ports and tables. That may avoid some issues
1800 * such as RCU usage warning.
1802 table_instance_flow_flush(table, ovsl_dereference(table->ti),
1803 ovsl_dereference(table->ufid_ti));
1805 /* RCU destroy the ports, meters and flow tables. */
1806 call_rcu(&dp->rcu, destroy_dp_rcu);
1809 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1811 struct sk_buff *reply;
1812 struct datapath *dp;
1815 reply = ovs_dp_cmd_alloc_info();
1820 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1823 goto err_unlock_free;
1825 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1826 info->snd_seq, 0, OVS_DP_CMD_DEL);
1832 ovs_notify(&dp_datapath_genl_family, reply, info);
1842 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1844 struct sk_buff *reply;
1845 struct datapath *dp;
1848 reply = ovs_dp_cmd_alloc_info();
1853 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1856 goto err_unlock_free;
1858 err = ovs_dp_change(dp, info->attrs);
1860 goto err_unlock_free;
1862 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1863 info->snd_seq, 0, OVS_DP_CMD_SET);
1867 ovs_notify(&dp_datapath_genl_family, reply, info);
1877 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1879 struct sk_buff *reply;
1880 struct datapath *dp;
1883 reply = ovs_dp_cmd_alloc_info();
1888 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1891 goto err_unlock_free;
1893 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1894 info->snd_seq, 0, OVS_DP_CMD_GET);
1898 return genlmsg_reply(reply, info);
1906 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1908 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1909 struct datapath *dp;
1910 int skip = cb->args[0];
1914 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1916 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1917 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1918 OVS_DP_CMD_GET) < 0)
1929 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1930 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1931 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1932 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1933 [OVS_DP_ATTR_MASKS_CACHE_SIZE] = NLA_POLICY_RANGE(NLA_U32, 0,
1934 PCPU_MIN_UNIT_SIZE / sizeof(struct mask_cache_entry)),
1937 static const struct genl_small_ops dp_datapath_genl_ops[] = {
1938 { .cmd = OVS_DP_CMD_NEW,
1939 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1940 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1941 .doit = ovs_dp_cmd_new
1943 { .cmd = OVS_DP_CMD_DEL,
1944 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1945 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1946 .doit = ovs_dp_cmd_del
1948 { .cmd = OVS_DP_CMD_GET,
1949 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1950 .flags = 0, /* OK for unprivileged users. */
1951 .doit = ovs_dp_cmd_get,
1952 .dumpit = ovs_dp_cmd_dump
1954 { .cmd = OVS_DP_CMD_SET,
1955 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1956 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1957 .doit = ovs_dp_cmd_set,
1961 static struct genl_family dp_datapath_genl_family __ro_after_init = {
1962 .hdrsize = sizeof(struct ovs_header),
1963 .name = OVS_DATAPATH_FAMILY,
1964 .version = OVS_DATAPATH_VERSION,
1965 .maxattr = OVS_DP_ATTR_MAX,
1966 .policy = datapath_policy,
1968 .parallel_ops = true,
1969 .small_ops = dp_datapath_genl_ops,
1970 .n_small_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1971 .mcgrps = &ovs_dp_datapath_multicast_group,
1973 .module = THIS_MODULE,
1976 /* Called with ovs_mutex or RCU read lock. */
1977 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1978 struct net *net, u32 portid, u32 seq,
1979 u32 flags, u8 cmd, gfp_t gfp)
1981 struct ovs_header *ovs_header;
1982 struct ovs_vport_stats vport_stats;
1985 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1990 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1992 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1993 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1994 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1995 ovs_vport_name(vport)) ||
1996 nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
1997 goto nla_put_failure;
1999 if (!net_eq(net, dev_net(vport->dev))) {
2000 int id = peernet2id_alloc(net, dev_net(vport->dev), gfp);
2002 if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
2003 goto nla_put_failure;
2006 ovs_vport_get_stats(vport, &vport_stats);
2007 if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
2008 sizeof(struct ovs_vport_stats), &vport_stats,
2009 OVS_VPORT_ATTR_PAD))
2010 goto nla_put_failure;
2012 if (ovs_vport_get_upcall_portids(vport, skb))
2013 goto nla_put_failure;
2015 err = ovs_vport_get_options(vport, skb);
2016 if (err == -EMSGSIZE)
2019 genlmsg_end(skb, ovs_header);
2025 genlmsg_cancel(skb, ovs_header);
2029 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
2031 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2034 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
2035 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
2036 u32 portid, u32 seq, u8 cmd)
2038 struct sk_buff *skb;
2041 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2043 return ERR_PTR(-ENOMEM);
2045 retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd,
2052 /* Called with ovs_mutex or RCU read lock. */
2053 static struct vport *lookup_vport(struct net *net,
2054 const struct ovs_header *ovs_header,
2055 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2057 struct datapath *dp;
2058 struct vport *vport;
2060 if (a[OVS_VPORT_ATTR_IFINDEX])
2061 return ERR_PTR(-EOPNOTSUPP);
2062 if (a[OVS_VPORT_ATTR_NAME]) {
2063 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
2065 return ERR_PTR(-ENODEV);
2066 if (ovs_header->dp_ifindex &&
2067 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2068 return ERR_PTR(-ENODEV);
2070 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2071 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2073 if (port_no >= DP_MAX_PORTS)
2074 return ERR_PTR(-EFBIG);
2076 dp = get_dp(net, ovs_header->dp_ifindex);
2078 return ERR_PTR(-ENODEV);
2080 vport = ovs_vport_ovsl_rcu(dp, port_no);
2082 return ERR_PTR(-ENODEV);
2085 return ERR_PTR(-EINVAL);
2089 static unsigned int ovs_get_max_headroom(struct datapath *dp)
2091 unsigned int dev_headroom, max_headroom = 0;
2092 struct net_device *dev;
2093 struct vport *vport;
2096 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2097 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node,
2098 lockdep_ovsl_is_held()) {
2100 dev_headroom = netdev_get_fwd_headroom(dev);
2101 if (dev_headroom > max_headroom)
2102 max_headroom = dev_headroom;
2106 return max_headroom;
2109 /* Called with ovs_mutex */
2110 static void ovs_update_headroom(struct datapath *dp, unsigned int new_headroom)
2112 struct vport *vport;
2115 dp->max_headroom = new_headroom;
2116 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2117 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node,
2118 lockdep_ovsl_is_held())
2119 netdev_set_rx_headroom(vport->dev, new_headroom);
2123 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2125 struct nlattr **a = info->attrs;
2126 struct ovs_header *ovs_header = info->userhdr;
2127 struct vport_parms parms;
2128 struct sk_buff *reply;
2129 struct vport *vport;
2130 struct datapath *dp;
2131 unsigned int new_headroom;
2135 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2136 !a[OVS_VPORT_ATTR_UPCALL_PID])
2138 if (a[OVS_VPORT_ATTR_IFINDEX])
2141 port_no = a[OVS_VPORT_ATTR_PORT_NO]
2142 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
2143 if (port_no >= DP_MAX_PORTS)
2146 reply = ovs_vport_cmd_alloc_info();
2152 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2155 goto exit_unlock_free;
2158 vport = ovs_vport_ovsl(dp, port_no);
2161 goto exit_unlock_free;
2163 for (port_no = 1; ; port_no++) {
2164 if (port_no >= DP_MAX_PORTS) {
2166 goto exit_unlock_free;
2168 vport = ovs_vport_ovsl(dp, port_no);
2174 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2175 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2176 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2178 parms.port_no = port_no;
2179 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
2181 vport = new_vport(&parms);
2182 err = PTR_ERR(vport);
2183 if (IS_ERR(vport)) {
2186 goto exit_unlock_free;
2189 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2190 info->snd_portid, info->snd_seq, 0,
2191 OVS_VPORT_CMD_NEW, GFP_KERNEL);
2193 new_headroom = netdev_get_fwd_headroom(vport->dev);
2195 if (new_headroom > dp->max_headroom)
2196 ovs_update_headroom(dp, new_headroom);
2198 netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2203 ovs_notify(&dp_vport_genl_family, reply, info);
2212 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2214 struct nlattr **a = info->attrs;
2215 struct sk_buff *reply;
2216 struct vport *vport;
2219 reply = ovs_vport_cmd_alloc_info();
2224 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2225 err = PTR_ERR(vport);
2227 goto exit_unlock_free;
2229 if (a[OVS_VPORT_ATTR_TYPE] &&
2230 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2232 goto exit_unlock_free;
2235 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2236 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2238 goto exit_unlock_free;
2242 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2243 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2245 err = ovs_vport_set_upcall_portids(vport, ids);
2247 goto exit_unlock_free;
2250 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2251 info->snd_portid, info->snd_seq, 0,
2252 OVS_VPORT_CMD_SET, GFP_KERNEL);
2256 ovs_notify(&dp_vport_genl_family, reply, info);
2265 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2267 bool update_headroom = false;
2268 struct nlattr **a = info->attrs;
2269 struct sk_buff *reply;
2270 struct datapath *dp;
2271 struct vport *vport;
2272 unsigned int new_headroom;
2275 reply = ovs_vport_cmd_alloc_info();
2280 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2281 err = PTR_ERR(vport);
2283 goto exit_unlock_free;
2285 if (vport->port_no == OVSP_LOCAL) {
2287 goto exit_unlock_free;
2290 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2291 info->snd_portid, info->snd_seq, 0,
2292 OVS_VPORT_CMD_DEL, GFP_KERNEL);
2295 /* the vport deletion may trigger dp headroom update */
2297 if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
2298 update_headroom = true;
2300 netdev_reset_rx_headroom(vport->dev);
2301 ovs_dp_detach_port(vport);
2303 if (update_headroom) {
2304 new_headroom = ovs_get_max_headroom(dp);
2306 if (new_headroom < dp->max_headroom)
2307 ovs_update_headroom(dp, new_headroom);
2311 ovs_notify(&dp_vport_genl_family, reply, info);
2320 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2322 struct nlattr **a = info->attrs;
2323 struct ovs_header *ovs_header = info->userhdr;
2324 struct sk_buff *reply;
2325 struct vport *vport;
2328 reply = ovs_vport_cmd_alloc_info();
2333 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2334 err = PTR_ERR(vport);
2336 goto exit_unlock_free;
2337 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2338 info->snd_portid, info->snd_seq, 0,
2339 OVS_VPORT_CMD_GET, GFP_ATOMIC);
2343 return genlmsg_reply(reply, info);
2351 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2353 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2354 struct datapath *dp;
2355 int bucket = cb->args[0], skip = cb->args[1];
2359 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2364 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2365 struct vport *vport;
2368 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2370 ovs_vport_cmd_fill_info(vport, skb,
2372 NETLINK_CB(cb->skb).portid,
2392 static void ovs_dp_masks_rebalance(struct work_struct *work)
2394 struct ovs_net *ovs_net = container_of(work, struct ovs_net,
2395 masks_rebalance.work);
2396 struct datapath *dp;
2400 list_for_each_entry(dp, &ovs_net->dps, list_node)
2401 ovs_flow_masks_rebalance(&dp->table);
2405 schedule_delayed_work(&ovs_net->masks_rebalance,
2406 msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL));
2409 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2410 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2411 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2412 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2413 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2414 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_UNSPEC },
2415 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2416 [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
2417 [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
2420 static const struct genl_small_ops dp_vport_genl_ops[] = {
2421 { .cmd = OVS_VPORT_CMD_NEW,
2422 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2423 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2424 .doit = ovs_vport_cmd_new
2426 { .cmd = OVS_VPORT_CMD_DEL,
2427 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2428 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2429 .doit = ovs_vport_cmd_del
2431 { .cmd = OVS_VPORT_CMD_GET,
2432 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2433 .flags = 0, /* OK for unprivileged users. */
2434 .doit = ovs_vport_cmd_get,
2435 .dumpit = ovs_vport_cmd_dump
2437 { .cmd = OVS_VPORT_CMD_SET,
2438 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2439 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2440 .doit = ovs_vport_cmd_set,
2444 struct genl_family dp_vport_genl_family __ro_after_init = {
2445 .hdrsize = sizeof(struct ovs_header),
2446 .name = OVS_VPORT_FAMILY,
2447 .version = OVS_VPORT_VERSION,
2448 .maxattr = OVS_VPORT_ATTR_MAX,
2449 .policy = vport_policy,
2451 .parallel_ops = true,
2452 .small_ops = dp_vport_genl_ops,
2453 .n_small_ops = ARRAY_SIZE(dp_vport_genl_ops),
2454 .mcgrps = &ovs_dp_vport_multicast_group,
2456 .module = THIS_MODULE,
2459 static struct genl_family * const dp_genl_families[] = {
2460 &dp_datapath_genl_family,
2461 &dp_vport_genl_family,
2462 &dp_flow_genl_family,
2463 &dp_packet_genl_family,
2464 &dp_meter_genl_family,
2465 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2466 &dp_ct_limit_genl_family,
2470 static void dp_unregister_genl(int n_families)
2474 for (i = 0; i < n_families; i++)
2475 genl_unregister_family(dp_genl_families[i]);
2478 static int __init dp_register_genl(void)
2483 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2485 err = genl_register_family(dp_genl_families[i]);
2493 dp_unregister_genl(i);
2497 static int __net_init ovs_init_net(struct net *net)
2499 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2502 INIT_LIST_HEAD(&ovs_net->dps);
2503 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2504 INIT_DELAYED_WORK(&ovs_net->masks_rebalance, ovs_dp_masks_rebalance);
2506 err = ovs_ct_init(net);
2510 schedule_delayed_work(&ovs_net->masks_rebalance,
2511 msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL));
2515 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2516 struct list_head *head)
2518 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2519 struct datapath *dp;
2521 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2524 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2525 struct vport *vport;
2527 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2528 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2531 if (dev_net(vport->dev) == dnet)
2532 list_add(&vport->detach_list, head);
2538 static void __net_exit ovs_exit_net(struct net *dnet)
2540 struct datapath *dp, *dp_next;
2541 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2542 struct vport *vport, *vport_next;
2550 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2553 down_read(&net_rwsem);
2555 list_vports_from_net(net, dnet, &head);
2556 up_read(&net_rwsem);
2558 /* Detach all vports from given namespace. */
2559 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2560 list_del(&vport->detach_list);
2561 ovs_dp_detach_port(vport);
2566 cancel_delayed_work_sync(&ovs_net->masks_rebalance);
2567 cancel_work_sync(&ovs_net->dp_notify_work);
2570 static struct pernet_operations ovs_net_ops = {
2571 .init = ovs_init_net,
2572 .exit = ovs_exit_net,
2574 .size = sizeof(struct ovs_net),
2577 static int __init dp_init(void)
2581 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) >
2582 sizeof_field(struct sk_buff, cb));
2584 pr_info("Open vSwitch switching datapath\n");
2586 err = action_fifos_init();
2590 err = ovs_internal_dev_rtnl_link_register();
2592 goto error_action_fifos_exit;
2594 err = ovs_flow_init();
2596 goto error_unreg_rtnl_link;
2598 err = ovs_vport_init();
2600 goto error_flow_exit;
2602 err = register_pernet_device(&ovs_net_ops);
2604 goto error_vport_exit;
2606 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2608 goto error_netns_exit;
2610 err = ovs_netdev_init();
2612 goto error_unreg_notifier;
2614 err = dp_register_genl();
2616 goto error_unreg_netdev;
2622 error_unreg_notifier:
2623 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2625 unregister_pernet_device(&ovs_net_ops);
2630 error_unreg_rtnl_link:
2631 ovs_internal_dev_rtnl_link_unregister();
2632 error_action_fifos_exit:
2633 action_fifos_exit();
2638 static void dp_cleanup(void)
2640 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2642 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2643 unregister_pernet_device(&ovs_net_ops);
2647 ovs_internal_dev_rtnl_link_unregister();
2648 action_fifos_exit();
2651 module_init(dp_init);
2652 module_exit(dp_cleanup);
2654 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2655 MODULE_LICENSE("GPL");
2656 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
2657 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
2658 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
2659 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
2660 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);
2661 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY);