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_actions *acts;
937 struct sw_flow_match match;
938 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
940 bool log = !a[OVS_FLOW_ATTR_PROBE];
942 /* Must have key and actions. */
944 if (!a[OVS_FLOW_ATTR_KEY]) {
945 OVS_NLERR(log, "Flow key attr not present in new flow.");
948 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
949 OVS_NLERR(log, "Flow actions attr not present in new flow.");
953 /* Most of the time we need to allocate a new flow, do it before
956 new_flow = ovs_flow_alloc();
957 if (IS_ERR(new_flow)) {
958 error = PTR_ERR(new_flow);
963 ovs_match_init(&match, &new_flow->key, false, &mask);
964 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
965 a[OVS_FLOW_ATTR_MASK], log);
969 /* Extract flow identifier. */
970 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
971 &new_flow->key, log);
975 /* unmasked key is needed to match when ufid is not used. */
976 if (ovs_identifier_is_key(&new_flow->id))
977 match.key = new_flow->id.unmasked_key;
979 ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask);
981 /* Validate actions. */
982 error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
983 &new_flow->key, &acts, log);
985 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
989 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
992 error = PTR_ERR(reply);
997 dp = get_dp(net, ovs_header->dp_ifindex);
1000 goto err_unlock_ovs;
1003 /* Check if this is a duplicate flow */
1004 if (ovs_identifier_is_ufid(&new_flow->id))
1005 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
1007 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key);
1008 if (likely(!flow)) {
1009 rcu_assign_pointer(new_flow->sf_acts, acts);
1011 /* Put flow in bucket. */
1012 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
1013 if (unlikely(error)) {
1015 goto err_unlock_ovs;
1018 if (unlikely(reply)) {
1019 error = ovs_flow_cmd_fill_info(new_flow,
1020 ovs_header->dp_ifindex,
1021 reply, info->snd_portid,
1029 struct sw_flow_actions *old_acts;
1031 /* Bail out if we're not allowed to modify an existing flow.
1032 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1033 * because Generic Netlink treats the latter as a dump
1034 * request. We also accept NLM_F_EXCL in case that bug ever
1037 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
1040 goto err_unlock_ovs;
1042 /* The flow identifier has to be the same for flow updates.
1043 * Look for any overlapping flow.
1045 if (unlikely(!ovs_flow_cmp(flow, &match))) {
1046 if (ovs_identifier_is_key(&flow->id))
1047 flow = ovs_flow_tbl_lookup_exact(&dp->table,
1049 else /* UFID matches but key is different */
1053 goto err_unlock_ovs;
1056 /* Update actions. */
1057 old_acts = ovsl_dereference(flow->sf_acts);
1058 rcu_assign_pointer(flow->sf_acts, acts);
1060 if (unlikely(reply)) {
1061 error = ovs_flow_cmd_fill_info(flow,
1062 ovs_header->dp_ifindex,
1063 reply, info->snd_portid,
1071 ovs_nla_free_flow_actions_rcu(old_acts);
1072 ovs_flow_free(new_flow, false);
1076 ovs_notify(&dp_flow_genl_family, reply, info);
1083 ovs_nla_free_flow_actions(acts);
1085 ovs_flow_free(new_flow, false);
1090 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1091 static noinline_for_stack
1092 struct sw_flow_actions *get_flow_actions(struct net *net,
1093 const struct nlattr *a,
1094 const struct sw_flow_key *key,
1095 const struct sw_flow_mask *mask,
1098 struct sw_flow_actions *acts;
1099 struct sw_flow_key masked_key;
1102 ovs_flow_mask_key(&masked_key, key, true, mask);
1103 error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
1106 "Actions may not be safe on all matching packets");
1107 return ERR_PTR(error);
1113 /* Factor out match-init and action-copy to avoid
1114 * "Wframe-larger-than=1024" warning. Because mask is only
1115 * used to get actions, we new a function to save some
1118 * If there are not key and action attrs, we return 0
1119 * directly. In the case, the caller will also not use the
1120 * match as before. If there is action attr, we try to get
1121 * actions and save them to *acts. Before returning from
1122 * the function, we reset the match->mask pointer. Because
1123 * we should not to return match object with dangling reference
1126 static noinline_for_stack int
1127 ovs_nla_init_match_and_action(struct net *net,
1128 struct sw_flow_match *match,
1129 struct sw_flow_key *key,
1131 struct sw_flow_actions **acts,
1134 struct sw_flow_mask mask;
1137 if (a[OVS_FLOW_ATTR_KEY]) {
1138 ovs_match_init(match, key, true, &mask);
1139 error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
1140 a[OVS_FLOW_ATTR_MASK], log);
1145 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1146 if (!a[OVS_FLOW_ATTR_KEY]) {
1148 "Flow key attribute not present in set flow.");
1153 *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
1155 if (IS_ERR(*acts)) {
1156 error = PTR_ERR(*acts);
1161 /* On success, error is 0. */
1167 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1169 struct net *net = sock_net(skb->sk);
1170 struct nlattr **a = info->attrs;
1171 struct ovs_header *ovs_header = info->userhdr;
1172 struct sw_flow_key key;
1173 struct sw_flow *flow;
1174 struct sk_buff *reply = NULL;
1175 struct datapath *dp;
1176 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1177 struct sw_flow_match match;
1178 struct sw_flow_id sfid;
1179 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1181 bool log = !a[OVS_FLOW_ATTR_PROBE];
1184 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1185 if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
1187 "Flow set message rejected, Key attribute missing.");
1191 error = ovs_nla_init_match_and_action(net, &match, &key, a,
1197 /* Can allocate before locking if have acts. */
1198 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1200 if (IS_ERR(reply)) {
1201 error = PTR_ERR(reply);
1202 goto err_kfree_acts;
1207 dp = get_dp(net, ovs_header->dp_ifindex);
1208 if (unlikely(!dp)) {
1210 goto err_unlock_ovs;
1212 /* Check that the flow exists. */
1214 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1216 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1217 if (unlikely(!flow)) {
1219 goto err_unlock_ovs;
1222 /* Update actions, if present. */
1224 old_acts = ovsl_dereference(flow->sf_acts);
1225 rcu_assign_pointer(flow->sf_acts, acts);
1227 if (unlikely(reply)) {
1228 error = ovs_flow_cmd_fill_info(flow,
1229 ovs_header->dp_ifindex,
1230 reply, info->snd_portid,
1237 /* Could not alloc without acts before locking. */
1238 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1239 info, OVS_FLOW_CMD_SET, false,
1242 if (IS_ERR(reply)) {
1243 error = PTR_ERR(reply);
1244 goto err_unlock_ovs;
1249 if (a[OVS_FLOW_ATTR_CLEAR])
1250 ovs_flow_stats_clear(flow);
1254 ovs_notify(&dp_flow_genl_family, reply, info);
1256 ovs_nla_free_flow_actions_rcu(old_acts);
1264 ovs_nla_free_flow_actions(acts);
1269 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1271 struct nlattr **a = info->attrs;
1272 struct ovs_header *ovs_header = info->userhdr;
1273 struct net *net = sock_net(skb->sk);
1274 struct sw_flow_key key;
1275 struct sk_buff *reply;
1276 struct sw_flow *flow;
1277 struct datapath *dp;
1278 struct sw_flow_match match;
1279 struct sw_flow_id ufid;
1280 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1282 bool log = !a[OVS_FLOW_ATTR_PROBE];
1285 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1286 if (a[OVS_FLOW_ATTR_KEY]) {
1287 ovs_match_init(&match, &key, true, NULL);
1288 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
1290 } else if (!ufid_present) {
1292 "Flow get message rejected, Key attribute missing.");
1299 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1306 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1308 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1314 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1315 OVS_FLOW_CMD_GET, true, ufid_flags);
1316 if (IS_ERR(reply)) {
1317 err = PTR_ERR(reply);
1322 return genlmsg_reply(reply, info);
1328 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1330 struct nlattr **a = info->attrs;
1331 struct ovs_header *ovs_header = info->userhdr;
1332 struct net *net = sock_net(skb->sk);
1333 struct sw_flow_key key;
1334 struct sk_buff *reply;
1335 struct sw_flow *flow = NULL;
1336 struct datapath *dp;
1337 struct sw_flow_match match;
1338 struct sw_flow_id ufid;
1339 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1341 bool log = !a[OVS_FLOW_ATTR_PROBE];
1344 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1345 if (a[OVS_FLOW_ATTR_KEY]) {
1346 ovs_match_init(&match, &key, true, NULL);
1347 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1354 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1355 if (unlikely(!dp)) {
1360 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1361 err = ovs_flow_tbl_flush(&dp->table);
1366 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1368 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1369 if (unlikely(!flow)) {
1374 ovs_flow_tbl_remove(&dp->table, flow);
1377 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1378 &flow->id, info, false, ufid_flags);
1379 if (likely(reply)) {
1380 if (!IS_ERR(reply)) {
1381 rcu_read_lock(); /*To keep RCU checker happy. */
1382 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1383 reply, info->snd_portid,
1388 if (WARN_ON_ONCE(err < 0)) {
1393 ovs_notify(&dp_flow_genl_family, reply, info);
1395 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0,
1401 ovs_flow_free(flow, true);
1408 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1410 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1411 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1412 struct table_instance *ti;
1413 struct datapath *dp;
1417 err = genlmsg_parse_deprecated(cb->nlh, &dp_flow_genl_family, a,
1418 OVS_FLOW_ATTR_MAX, flow_policy, NULL);
1421 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1424 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1430 ti = rcu_dereference(dp->table.ti);
1432 struct sw_flow *flow;
1435 bucket = cb->args[0];
1437 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1441 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1442 NETLINK_CB(cb->skb).portid,
1443 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1444 OVS_FLOW_CMD_GET, ufid_flags) < 0)
1447 cb->args[0] = bucket;
1454 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1455 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1456 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1457 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1458 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1459 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1460 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1461 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1464 static const struct genl_small_ops dp_flow_genl_ops[] = {
1465 { .cmd = OVS_FLOW_CMD_NEW,
1466 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1467 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1468 .doit = ovs_flow_cmd_new
1470 { .cmd = OVS_FLOW_CMD_DEL,
1471 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1472 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1473 .doit = ovs_flow_cmd_del
1475 { .cmd = OVS_FLOW_CMD_GET,
1476 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1477 .flags = 0, /* OK for unprivileged users. */
1478 .doit = ovs_flow_cmd_get,
1479 .dumpit = ovs_flow_cmd_dump
1481 { .cmd = OVS_FLOW_CMD_SET,
1482 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1483 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1484 .doit = ovs_flow_cmd_set,
1488 static struct genl_family dp_flow_genl_family __ro_after_init = {
1489 .hdrsize = sizeof(struct ovs_header),
1490 .name = OVS_FLOW_FAMILY,
1491 .version = OVS_FLOW_VERSION,
1492 .maxattr = OVS_FLOW_ATTR_MAX,
1493 .policy = flow_policy,
1495 .parallel_ops = true,
1496 .small_ops = dp_flow_genl_ops,
1497 .n_small_ops = ARRAY_SIZE(dp_flow_genl_ops),
1498 .mcgrps = &ovs_dp_flow_multicast_group,
1500 .module = THIS_MODULE,
1503 static size_t ovs_dp_cmd_msg_size(void)
1505 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1507 msgsize += nla_total_size(IFNAMSIZ);
1508 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
1509 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
1510 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1511 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_MASKS_CACHE_SIZE */
1516 /* Called with ovs_mutex. */
1517 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1518 u32 portid, u32 seq, u32 flags, u8 cmd)
1520 struct ovs_header *ovs_header;
1521 struct ovs_dp_stats dp_stats;
1522 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1525 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1530 ovs_header->dp_ifindex = get_dpifindex(dp);
1532 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1534 goto nla_put_failure;
1536 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1537 if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1538 &dp_stats, OVS_DP_ATTR_PAD))
1539 goto nla_put_failure;
1541 if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1542 sizeof(struct ovs_dp_megaflow_stats),
1543 &dp_megaflow_stats, OVS_DP_ATTR_PAD))
1544 goto nla_put_failure;
1546 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1547 goto nla_put_failure;
1549 if (nla_put_u32(skb, OVS_DP_ATTR_MASKS_CACHE_SIZE,
1550 ovs_flow_tbl_masks_cache_size(&dp->table)))
1551 goto nla_put_failure;
1553 genlmsg_end(skb, ovs_header);
1557 genlmsg_cancel(skb, ovs_header);
1562 static struct sk_buff *ovs_dp_cmd_alloc_info(void)
1564 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1567 /* Called with rcu_read_lock or ovs_mutex. */
1568 static struct datapath *lookup_datapath(struct net *net,
1569 const struct ovs_header *ovs_header,
1570 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1572 struct datapath *dp;
1574 if (!a[OVS_DP_ATTR_NAME])
1575 dp = get_dp(net, ovs_header->dp_ifindex);
1577 struct vport *vport;
1579 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1580 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1582 return dp ? dp : ERR_PTR(-ENODEV);
1585 static void ovs_dp_reset_user_features(struct sk_buff *skb,
1586 struct genl_info *info)
1588 struct datapath *dp;
1590 dp = lookup_datapath(sock_net(skb->sk), info->userhdr,
1595 pr_warn("%s: Dropping previously announced user features\n",
1597 dp->user_features = 0;
1600 DEFINE_STATIC_KEY_FALSE(tc_recirc_sharing_support);
1602 static int ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1604 u32 user_features = 0;
1606 if (a[OVS_DP_ATTR_USER_FEATURES]) {
1607 user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1609 if (user_features & ~(OVS_DP_F_VPORT_PIDS |
1610 OVS_DP_F_UNALIGNED |
1611 OVS_DP_F_TC_RECIRC_SHARING))
1614 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1615 if (user_features & OVS_DP_F_TC_RECIRC_SHARING)
1620 if (a[OVS_DP_ATTR_MASKS_CACHE_SIZE]) {
1624 cache_size = nla_get_u32(a[OVS_DP_ATTR_MASKS_CACHE_SIZE]);
1625 err = ovs_flow_tbl_masks_cache_resize(&dp->table, cache_size);
1630 dp->user_features = user_features;
1632 if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING)
1633 static_branch_enable(&tc_recirc_sharing_support);
1635 static_branch_disable(&tc_recirc_sharing_support);
1640 static int ovs_dp_stats_init(struct datapath *dp)
1642 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1643 if (!dp->stats_percpu)
1649 static int ovs_dp_vport_init(struct datapath *dp)
1653 dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS,
1654 sizeof(struct hlist_head),
1659 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1660 INIT_HLIST_HEAD(&dp->ports[i]);
1665 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1667 struct nlattr **a = info->attrs;
1668 struct vport_parms parms;
1669 struct sk_buff *reply;
1670 struct datapath *dp;
1671 struct vport *vport;
1672 struct ovs_net *ovs_net;
1676 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1679 reply = ovs_dp_cmd_alloc_info();
1684 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1686 goto err_destroy_reply;
1688 ovs_dp_set_net(dp, sock_net(skb->sk));
1690 /* Allocate table. */
1691 err = ovs_flow_tbl_init(&dp->table);
1693 goto err_destroy_dp;
1695 err = ovs_dp_stats_init(dp);
1697 goto err_destroy_table;
1699 err = ovs_dp_vport_init(dp);
1701 goto err_destroy_stats;
1703 err = ovs_meters_init(dp);
1705 goto err_destroy_ports;
1707 /* Set up our datapath device. */
1708 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1709 parms.type = OVS_VPORT_TYPE_INTERNAL;
1710 parms.options = NULL;
1712 parms.port_no = OVSP_LOCAL;
1713 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1715 /* So far only local changes have been made, now need the lock. */
1718 err = ovs_dp_change(dp, a);
1720 goto err_unlock_and_destroy_meters;
1722 vport = new_vport(&parms);
1723 if (IS_ERR(vport)) {
1724 err = PTR_ERR(vport);
1728 if (err == -EEXIST) {
1729 /* An outdated user space instance that does not understand
1730 * the concept of user_features has attempted to create a new
1731 * datapath and is likely to reuse it. Drop all user features.
1733 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1734 ovs_dp_reset_user_features(skb, info);
1737 goto err_unlock_and_destroy_meters;
1740 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1741 info->snd_seq, 0, OVS_DP_CMD_NEW);
1744 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1745 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1749 ovs_notify(&dp_datapath_genl_family, reply, info);
1752 err_unlock_and_destroy_meters:
1754 ovs_meters_exit(dp);
1758 free_percpu(dp->stats_percpu);
1760 ovs_flow_tbl_destroy(&dp->table);
1769 /* Called with ovs_mutex. */
1770 static void __dp_destroy(struct datapath *dp)
1772 struct flow_table *table = &dp->table;
1775 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1776 struct vport *vport;
1777 struct hlist_node *n;
1779 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1780 if (vport->port_no != OVSP_LOCAL)
1781 ovs_dp_detach_port(vport);
1784 list_del_rcu(&dp->list_node);
1786 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1787 * all ports in datapath are destroyed first before freeing datapath.
1789 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1791 /* Flush sw_flow in the tables. RCU cb only releases resource
1792 * such as dp, ports and tables. That may avoid some issues
1793 * such as RCU usage warning.
1795 table_instance_flow_flush(table, ovsl_dereference(table->ti),
1796 ovsl_dereference(table->ufid_ti));
1798 /* RCU destroy the ports, meters and flow tables. */
1799 call_rcu(&dp->rcu, destroy_dp_rcu);
1802 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1804 struct sk_buff *reply;
1805 struct datapath *dp;
1808 reply = ovs_dp_cmd_alloc_info();
1813 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1816 goto err_unlock_free;
1818 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1819 info->snd_seq, 0, OVS_DP_CMD_DEL);
1825 ovs_notify(&dp_datapath_genl_family, reply, info);
1835 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1837 struct sk_buff *reply;
1838 struct datapath *dp;
1841 reply = ovs_dp_cmd_alloc_info();
1846 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1849 goto err_unlock_free;
1851 err = ovs_dp_change(dp, info->attrs);
1853 goto err_unlock_free;
1855 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1856 info->snd_seq, 0, OVS_DP_CMD_SET);
1860 ovs_notify(&dp_datapath_genl_family, reply, info);
1870 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1872 struct sk_buff *reply;
1873 struct datapath *dp;
1876 reply = ovs_dp_cmd_alloc_info();
1881 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1884 goto err_unlock_free;
1886 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1887 info->snd_seq, 0, OVS_DP_CMD_GET);
1891 return genlmsg_reply(reply, info);
1899 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1901 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1902 struct datapath *dp;
1903 int skip = cb->args[0];
1907 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1909 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1910 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1911 OVS_DP_CMD_GET) < 0)
1922 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1923 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1924 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1925 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1926 [OVS_DP_ATTR_MASKS_CACHE_SIZE] = NLA_POLICY_RANGE(NLA_U32, 0,
1927 PCPU_MIN_UNIT_SIZE / sizeof(struct mask_cache_entry)),
1930 static const struct genl_small_ops dp_datapath_genl_ops[] = {
1931 { .cmd = OVS_DP_CMD_NEW,
1932 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1933 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1934 .doit = ovs_dp_cmd_new
1936 { .cmd = OVS_DP_CMD_DEL,
1937 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1938 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1939 .doit = ovs_dp_cmd_del
1941 { .cmd = OVS_DP_CMD_GET,
1942 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1943 .flags = 0, /* OK for unprivileged users. */
1944 .doit = ovs_dp_cmd_get,
1945 .dumpit = ovs_dp_cmd_dump
1947 { .cmd = OVS_DP_CMD_SET,
1948 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1949 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1950 .doit = ovs_dp_cmd_set,
1954 static struct genl_family dp_datapath_genl_family __ro_after_init = {
1955 .hdrsize = sizeof(struct ovs_header),
1956 .name = OVS_DATAPATH_FAMILY,
1957 .version = OVS_DATAPATH_VERSION,
1958 .maxattr = OVS_DP_ATTR_MAX,
1959 .policy = datapath_policy,
1961 .parallel_ops = true,
1962 .small_ops = dp_datapath_genl_ops,
1963 .n_small_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1964 .mcgrps = &ovs_dp_datapath_multicast_group,
1966 .module = THIS_MODULE,
1969 /* Called with ovs_mutex or RCU read lock. */
1970 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1971 struct net *net, u32 portid, u32 seq,
1972 u32 flags, u8 cmd, gfp_t gfp)
1974 struct ovs_header *ovs_header;
1975 struct ovs_vport_stats vport_stats;
1978 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1983 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1985 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1986 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1987 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1988 ovs_vport_name(vport)) ||
1989 nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
1990 goto nla_put_failure;
1992 if (!net_eq(net, dev_net(vport->dev))) {
1993 int id = peernet2id_alloc(net, dev_net(vport->dev), gfp);
1995 if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
1996 goto nla_put_failure;
1999 ovs_vport_get_stats(vport, &vport_stats);
2000 if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
2001 sizeof(struct ovs_vport_stats), &vport_stats,
2002 OVS_VPORT_ATTR_PAD))
2003 goto nla_put_failure;
2005 if (ovs_vport_get_upcall_portids(vport, skb))
2006 goto nla_put_failure;
2008 err = ovs_vport_get_options(vport, skb);
2009 if (err == -EMSGSIZE)
2012 genlmsg_end(skb, ovs_header);
2018 genlmsg_cancel(skb, ovs_header);
2022 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
2024 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2027 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
2028 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
2029 u32 portid, u32 seq, u8 cmd)
2031 struct sk_buff *skb;
2034 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2036 return ERR_PTR(-ENOMEM);
2038 retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd,
2045 /* Called with ovs_mutex or RCU read lock. */
2046 static struct vport *lookup_vport(struct net *net,
2047 const struct ovs_header *ovs_header,
2048 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2050 struct datapath *dp;
2051 struct vport *vport;
2053 if (a[OVS_VPORT_ATTR_IFINDEX])
2054 return ERR_PTR(-EOPNOTSUPP);
2055 if (a[OVS_VPORT_ATTR_NAME]) {
2056 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
2058 return ERR_PTR(-ENODEV);
2059 if (ovs_header->dp_ifindex &&
2060 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2061 return ERR_PTR(-ENODEV);
2063 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2064 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2066 if (port_no >= DP_MAX_PORTS)
2067 return ERR_PTR(-EFBIG);
2069 dp = get_dp(net, ovs_header->dp_ifindex);
2071 return ERR_PTR(-ENODEV);
2073 vport = ovs_vport_ovsl_rcu(dp, port_no);
2075 return ERR_PTR(-ENODEV);
2078 return ERR_PTR(-EINVAL);
2082 static unsigned int ovs_get_max_headroom(struct datapath *dp)
2084 unsigned int dev_headroom, max_headroom = 0;
2085 struct net_device *dev;
2086 struct vport *vport;
2089 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2090 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node,
2091 lockdep_ovsl_is_held()) {
2093 dev_headroom = netdev_get_fwd_headroom(dev);
2094 if (dev_headroom > max_headroom)
2095 max_headroom = dev_headroom;
2099 return max_headroom;
2102 /* Called with ovs_mutex */
2103 static void ovs_update_headroom(struct datapath *dp, unsigned int new_headroom)
2105 struct vport *vport;
2108 dp->max_headroom = new_headroom;
2109 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2110 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node,
2111 lockdep_ovsl_is_held())
2112 netdev_set_rx_headroom(vport->dev, new_headroom);
2116 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2118 struct nlattr **a = info->attrs;
2119 struct ovs_header *ovs_header = info->userhdr;
2120 struct vport_parms parms;
2121 struct sk_buff *reply;
2122 struct vport *vport;
2123 struct datapath *dp;
2124 unsigned int new_headroom;
2128 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2129 !a[OVS_VPORT_ATTR_UPCALL_PID])
2131 if (a[OVS_VPORT_ATTR_IFINDEX])
2134 port_no = a[OVS_VPORT_ATTR_PORT_NO]
2135 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
2136 if (port_no >= DP_MAX_PORTS)
2139 reply = ovs_vport_cmd_alloc_info();
2145 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2148 goto exit_unlock_free;
2151 vport = ovs_vport_ovsl(dp, port_no);
2154 goto exit_unlock_free;
2156 for (port_no = 1; ; port_no++) {
2157 if (port_no >= DP_MAX_PORTS) {
2159 goto exit_unlock_free;
2161 vport = ovs_vport_ovsl(dp, port_no);
2167 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2168 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2169 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2171 parms.port_no = port_no;
2172 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
2174 vport = new_vport(&parms);
2175 err = PTR_ERR(vport);
2176 if (IS_ERR(vport)) {
2179 goto exit_unlock_free;
2182 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2183 info->snd_portid, info->snd_seq, 0,
2184 OVS_VPORT_CMD_NEW, GFP_KERNEL);
2186 new_headroom = netdev_get_fwd_headroom(vport->dev);
2188 if (new_headroom > dp->max_headroom)
2189 ovs_update_headroom(dp, new_headroom);
2191 netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2196 ovs_notify(&dp_vport_genl_family, reply, info);
2205 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2207 struct nlattr **a = info->attrs;
2208 struct sk_buff *reply;
2209 struct vport *vport;
2212 reply = ovs_vport_cmd_alloc_info();
2217 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2218 err = PTR_ERR(vport);
2220 goto exit_unlock_free;
2222 if (a[OVS_VPORT_ATTR_TYPE] &&
2223 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2225 goto exit_unlock_free;
2228 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2229 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2231 goto exit_unlock_free;
2235 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2236 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2238 err = ovs_vport_set_upcall_portids(vport, ids);
2240 goto exit_unlock_free;
2243 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2244 info->snd_portid, info->snd_seq, 0,
2245 OVS_VPORT_CMD_SET, GFP_KERNEL);
2249 ovs_notify(&dp_vport_genl_family, reply, info);
2258 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2260 bool update_headroom = false;
2261 struct nlattr **a = info->attrs;
2262 struct sk_buff *reply;
2263 struct datapath *dp;
2264 struct vport *vport;
2265 unsigned int new_headroom;
2268 reply = ovs_vport_cmd_alloc_info();
2273 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2274 err = PTR_ERR(vport);
2276 goto exit_unlock_free;
2278 if (vport->port_no == OVSP_LOCAL) {
2280 goto exit_unlock_free;
2283 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2284 info->snd_portid, info->snd_seq, 0,
2285 OVS_VPORT_CMD_DEL, GFP_KERNEL);
2288 /* the vport deletion may trigger dp headroom update */
2290 if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
2291 update_headroom = true;
2293 netdev_reset_rx_headroom(vport->dev);
2294 ovs_dp_detach_port(vport);
2296 if (update_headroom) {
2297 new_headroom = ovs_get_max_headroom(dp);
2299 if (new_headroom < dp->max_headroom)
2300 ovs_update_headroom(dp, new_headroom);
2304 ovs_notify(&dp_vport_genl_family, reply, info);
2313 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2315 struct nlattr **a = info->attrs;
2316 struct ovs_header *ovs_header = info->userhdr;
2317 struct sk_buff *reply;
2318 struct vport *vport;
2321 reply = ovs_vport_cmd_alloc_info();
2326 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2327 err = PTR_ERR(vport);
2329 goto exit_unlock_free;
2330 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2331 info->snd_portid, info->snd_seq, 0,
2332 OVS_VPORT_CMD_GET, GFP_ATOMIC);
2336 return genlmsg_reply(reply, info);
2344 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2346 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2347 struct datapath *dp;
2348 int bucket = cb->args[0], skip = cb->args[1];
2352 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2357 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2358 struct vport *vport;
2361 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2363 ovs_vport_cmd_fill_info(vport, skb,
2365 NETLINK_CB(cb->skb).portid,
2385 static void ovs_dp_masks_rebalance(struct work_struct *work)
2387 struct ovs_net *ovs_net = container_of(work, struct ovs_net,
2388 masks_rebalance.work);
2389 struct datapath *dp;
2393 list_for_each_entry(dp, &ovs_net->dps, list_node)
2394 ovs_flow_masks_rebalance(&dp->table);
2398 schedule_delayed_work(&ovs_net->masks_rebalance,
2399 msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL));
2402 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2403 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2404 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2405 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2406 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2407 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_UNSPEC },
2408 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2409 [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
2410 [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
2413 static const struct genl_small_ops dp_vport_genl_ops[] = {
2414 { .cmd = OVS_VPORT_CMD_NEW,
2415 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2416 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2417 .doit = ovs_vport_cmd_new
2419 { .cmd = OVS_VPORT_CMD_DEL,
2420 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2421 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2422 .doit = ovs_vport_cmd_del
2424 { .cmd = OVS_VPORT_CMD_GET,
2425 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2426 .flags = 0, /* OK for unprivileged users. */
2427 .doit = ovs_vport_cmd_get,
2428 .dumpit = ovs_vport_cmd_dump
2430 { .cmd = OVS_VPORT_CMD_SET,
2431 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2432 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2433 .doit = ovs_vport_cmd_set,
2437 struct genl_family dp_vport_genl_family __ro_after_init = {
2438 .hdrsize = sizeof(struct ovs_header),
2439 .name = OVS_VPORT_FAMILY,
2440 .version = OVS_VPORT_VERSION,
2441 .maxattr = OVS_VPORT_ATTR_MAX,
2442 .policy = vport_policy,
2444 .parallel_ops = true,
2445 .small_ops = dp_vport_genl_ops,
2446 .n_small_ops = ARRAY_SIZE(dp_vport_genl_ops),
2447 .mcgrps = &ovs_dp_vport_multicast_group,
2449 .module = THIS_MODULE,
2452 static struct genl_family * const dp_genl_families[] = {
2453 &dp_datapath_genl_family,
2454 &dp_vport_genl_family,
2455 &dp_flow_genl_family,
2456 &dp_packet_genl_family,
2457 &dp_meter_genl_family,
2458 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2459 &dp_ct_limit_genl_family,
2463 static void dp_unregister_genl(int n_families)
2467 for (i = 0; i < n_families; i++)
2468 genl_unregister_family(dp_genl_families[i]);
2471 static int __init dp_register_genl(void)
2476 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2478 err = genl_register_family(dp_genl_families[i]);
2486 dp_unregister_genl(i);
2490 static int __net_init ovs_init_net(struct net *net)
2492 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2495 INIT_LIST_HEAD(&ovs_net->dps);
2496 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2497 INIT_DELAYED_WORK(&ovs_net->masks_rebalance, ovs_dp_masks_rebalance);
2499 err = ovs_ct_init(net);
2503 schedule_delayed_work(&ovs_net->masks_rebalance,
2504 msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL));
2508 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2509 struct list_head *head)
2511 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2512 struct datapath *dp;
2514 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2517 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2518 struct vport *vport;
2520 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2521 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2524 if (dev_net(vport->dev) == dnet)
2525 list_add(&vport->detach_list, head);
2531 static void __net_exit ovs_exit_net(struct net *dnet)
2533 struct datapath *dp, *dp_next;
2534 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2535 struct vport *vport, *vport_next;
2543 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2546 down_read(&net_rwsem);
2548 list_vports_from_net(net, dnet, &head);
2549 up_read(&net_rwsem);
2551 /* Detach all vports from given namespace. */
2552 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2553 list_del(&vport->detach_list);
2554 ovs_dp_detach_port(vport);
2559 cancel_delayed_work_sync(&ovs_net->masks_rebalance);
2560 cancel_work_sync(&ovs_net->dp_notify_work);
2563 static struct pernet_operations ovs_net_ops = {
2564 .init = ovs_init_net,
2565 .exit = ovs_exit_net,
2567 .size = sizeof(struct ovs_net),
2570 static int __init dp_init(void)
2574 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) >
2575 sizeof_field(struct sk_buff, cb));
2577 pr_info("Open vSwitch switching datapath\n");
2579 err = action_fifos_init();
2583 err = ovs_internal_dev_rtnl_link_register();
2585 goto error_action_fifos_exit;
2587 err = ovs_flow_init();
2589 goto error_unreg_rtnl_link;
2591 err = ovs_vport_init();
2593 goto error_flow_exit;
2595 err = register_pernet_device(&ovs_net_ops);
2597 goto error_vport_exit;
2599 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2601 goto error_netns_exit;
2603 err = ovs_netdev_init();
2605 goto error_unreg_notifier;
2607 err = dp_register_genl();
2609 goto error_unreg_netdev;
2615 error_unreg_notifier:
2616 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2618 unregister_pernet_device(&ovs_net_ops);
2623 error_unreg_rtnl_link:
2624 ovs_internal_dev_rtnl_link_unregister();
2625 error_action_fifos_exit:
2626 action_fifos_exit();
2631 static void dp_cleanup(void)
2633 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2635 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2636 unregister_pernet_device(&ovs_net_ops);
2640 ovs_internal_dev_rtnl_link_unregister();
2641 action_fifos_exit();
2644 module_init(dp_init);
2645 module_exit(dp_cleanup);
2647 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2648 MODULE_LICENSE("GPL");
2649 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
2650 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
2651 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
2652 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
2653 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);
2654 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY);