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 /* Must be called with rcu_read_lock or ovs_mutex. */
134 const char *ovs_dp_name(const struct datapath *dp)
136 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
137 return ovs_vport_name(vport);
140 static int get_dpifindex(const struct datapath *dp)
147 local = ovs_vport_rcu(dp, OVSP_LOCAL);
149 ifindex = local->dev->ifindex;
158 static void destroy_dp_rcu(struct rcu_head *rcu)
160 struct datapath *dp = container_of(rcu, struct datapath, rcu);
162 ovs_flow_tbl_destroy(&dp->table);
163 free_percpu(dp->stats_percpu);
169 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
172 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
175 /* Called with ovs_mutex or RCU read lock. */
176 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
179 struct hlist_head *head;
181 head = vport_hash_bucket(dp, port_no);
182 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
183 if (vport->port_no == port_no)
189 /* Called with ovs_mutex. */
190 static struct vport *new_vport(const struct vport_parms *parms)
194 vport = ovs_vport_add(parms);
195 if (!IS_ERR(vport)) {
196 struct datapath *dp = parms->dp;
197 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
199 hlist_add_head_rcu(&vport->dp_hash_node, head);
204 void ovs_dp_detach_port(struct vport *p)
208 /* First drop references to device. */
209 hlist_del_rcu(&p->dp_hash_node);
211 /* Then destroy it. */
215 /* Must be called with rcu_read_lock. */
216 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
218 const struct vport *p = OVS_CB(skb)->input_vport;
219 struct datapath *dp = p->dp;
220 struct sw_flow *flow;
221 struct sw_flow_actions *sf_acts;
222 struct dp_stats_percpu *stats;
226 stats = this_cpu_ptr(dp->stats_percpu);
229 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
230 if (unlikely(!flow)) {
231 struct dp_upcall_info upcall;
234 memset(&upcall, 0, sizeof(upcall));
235 upcall.cmd = OVS_PACKET_CMD_MISS;
236 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
237 upcall.mru = OVS_CB(skb)->mru;
238 error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
243 stats_counter = &stats->n_missed;
247 ovs_flow_stats_update(flow, key->tp.flags, skb);
248 sf_acts = rcu_dereference(flow->sf_acts);
249 ovs_execute_actions(dp, skb, sf_acts, key);
251 stats_counter = &stats->n_hit;
254 /* Update datapath statistics. */
255 u64_stats_update_begin(&stats->syncp);
257 stats->n_mask_hit += n_mask_hit;
258 u64_stats_update_end(&stats->syncp);
261 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
262 const struct sw_flow_key *key,
263 const struct dp_upcall_info *upcall_info,
266 struct dp_stats_percpu *stats;
269 if (upcall_info->portid == 0) {
274 if (!skb_is_gso(skb))
275 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
277 err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
284 stats = this_cpu_ptr(dp->stats_percpu);
286 u64_stats_update_begin(&stats->syncp);
288 u64_stats_update_end(&stats->syncp);
293 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
294 const struct sw_flow_key *key,
295 const struct dp_upcall_info *upcall_info,
298 unsigned int gso_type = skb_shinfo(skb)->gso_type;
299 struct sw_flow_key later_key;
300 struct sk_buff *segs, *nskb;
303 BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_SGO_CB_OFFSET);
304 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
306 return PTR_ERR(segs);
310 if (gso_type & SKB_GSO_UDP) {
311 /* The initial flow key extracted by ovs_flow_key_extract()
312 * in this case is for a first fragment, so we need to
313 * properly mark later fragments.
316 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
319 /* Queue all of the segments. */
322 if (gso_type & SKB_GSO_UDP && skb != segs)
325 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
329 } while ((skb = skb->next));
331 /* Free all of the segments. */
339 } while ((skb = nskb));
343 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
344 unsigned int hdrlen, int actions_attrlen)
346 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
347 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
348 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
349 + nla_total_size(sizeof(unsigned int)); /* OVS_PACKET_ATTR_LEN */
351 /* OVS_PACKET_ATTR_USERDATA */
352 if (upcall_info->userdata)
353 size += NLA_ALIGN(upcall_info->userdata->nla_len);
355 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
356 if (upcall_info->egress_tun_info)
357 size += nla_total_size(ovs_tun_key_attr_size());
359 /* OVS_PACKET_ATTR_ACTIONS */
360 if (upcall_info->actions_len)
361 size += nla_total_size(actions_attrlen);
363 /* OVS_PACKET_ATTR_MRU */
364 if (upcall_info->mru)
365 size += nla_total_size(sizeof(upcall_info->mru));
370 static void pad_packet(struct datapath *dp, struct sk_buff *skb)
372 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
373 size_t plen = NLA_ALIGN(skb->len) - skb->len;
376 skb_put_zero(skb, plen);
380 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
381 const struct sw_flow_key *key,
382 const struct dp_upcall_info *upcall_info,
385 struct ovs_header *upcall;
386 struct sk_buff *nskb = NULL;
387 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
393 dp_ifindex = get_dpifindex(dp);
397 if (skb_vlan_tag_present(skb)) {
398 nskb = skb_clone(skb, GFP_ATOMIC);
402 nskb = __vlan_hwaccel_push_inside(nskb);
409 if (nla_attr_size(skb->len) > USHRT_MAX) {
414 /* Complete checksum if needed */
415 if (skb->ip_summed == CHECKSUM_PARTIAL &&
416 (err = skb_csum_hwoffload_help(skb, 0)))
419 /* Older versions of OVS user space enforce alignment of the last
420 * Netlink attribute to NLA_ALIGNTO which would require extensive
421 * padding logic. Only perform zerocopy if padding is not required.
423 if (dp->user_features & OVS_DP_F_UNALIGNED)
424 hlen = skb_zerocopy_headlen(skb);
428 len = upcall_msg_size(upcall_info, hlen - cutlen,
429 OVS_CB(skb)->acts_origlen);
430 user_skb = genlmsg_new(len, GFP_ATOMIC);
436 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
437 0, upcall_info->cmd);
442 upcall->dp_ifindex = dp_ifindex;
444 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
448 if (upcall_info->userdata)
449 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
450 nla_len(upcall_info->userdata),
451 nla_data(upcall_info->userdata));
453 if (upcall_info->egress_tun_info) {
454 nla = nla_nest_start_noflag(user_skb,
455 OVS_PACKET_ATTR_EGRESS_TUN_KEY);
460 err = ovs_nla_put_tunnel_info(user_skb,
461 upcall_info->egress_tun_info);
465 nla_nest_end(user_skb, nla);
468 if (upcall_info->actions_len) {
469 nla = nla_nest_start_noflag(user_skb, OVS_PACKET_ATTR_ACTIONS);
474 err = ovs_nla_put_actions(upcall_info->actions,
475 upcall_info->actions_len,
478 nla_nest_end(user_skb, nla);
480 nla_nest_cancel(user_skb, nla);
483 /* Add OVS_PACKET_ATTR_MRU */
484 if (upcall_info->mru) {
485 if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
490 pad_packet(dp, user_skb);
493 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
495 if (nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN,
500 pad_packet(dp, user_skb);
503 /* Only reserve room for attribute header, packet data is added
504 * in skb_zerocopy() */
505 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
509 nla->nla_len = nla_attr_size(skb->len - cutlen);
511 err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
515 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
516 pad_packet(dp, user_skb);
518 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
520 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
530 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
532 struct ovs_header *ovs_header = info->userhdr;
533 struct net *net = sock_net(skb->sk);
534 struct nlattr **a = info->attrs;
535 struct sw_flow_actions *acts;
536 struct sk_buff *packet;
537 struct sw_flow *flow;
538 struct sw_flow_actions *sf_acts;
540 struct vport *input_vport;
544 bool log = !a[OVS_PACKET_ATTR_PROBE];
547 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
548 !a[OVS_PACKET_ATTR_ACTIONS])
551 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
552 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
556 skb_reserve(packet, NET_IP_ALIGN);
558 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
560 /* Set packet's mru */
561 if (a[OVS_PACKET_ATTR_MRU]) {
562 mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
563 packet->ignore_df = 1;
565 OVS_CB(packet)->mru = mru;
567 /* Build an sw_flow for sending this packet. */
568 flow = ovs_flow_alloc();
573 err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
574 packet, &flow->key, log);
578 err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
579 &flow->key, &acts, log);
583 rcu_assign_pointer(flow->sf_acts, acts);
584 packet->priority = flow->key.phy.priority;
585 packet->mark = flow->key.phy.skb_mark;
588 dp = get_dp_rcu(net, ovs_header->dp_ifindex);
593 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
595 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
600 packet->dev = input_vport->dev;
601 OVS_CB(packet)->input_vport = input_vport;
602 sf_acts = rcu_dereference(flow->sf_acts);
605 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
609 ovs_flow_free(flow, false);
615 ovs_flow_free(flow, false);
622 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
623 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
624 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
625 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
626 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
627 [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
630 static const struct genl_ops dp_packet_genl_ops[] = {
631 { .cmd = OVS_PACKET_CMD_EXECUTE,
632 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
633 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
634 .doit = ovs_packet_cmd_execute
638 static struct genl_family dp_packet_genl_family __ro_after_init = {
639 .hdrsize = sizeof(struct ovs_header),
640 .name = OVS_PACKET_FAMILY,
641 .version = OVS_PACKET_VERSION,
642 .maxattr = OVS_PACKET_ATTR_MAX,
643 .policy = packet_policy,
645 .parallel_ops = true,
646 .ops = dp_packet_genl_ops,
647 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
648 .module = THIS_MODULE,
651 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
652 struct ovs_dp_megaflow_stats *mega_stats)
656 memset(mega_stats, 0, sizeof(*mega_stats));
658 stats->n_flows = ovs_flow_tbl_count(&dp->table);
659 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
661 stats->n_hit = stats->n_missed = stats->n_lost = 0;
663 for_each_possible_cpu(i) {
664 const struct dp_stats_percpu *percpu_stats;
665 struct dp_stats_percpu local_stats;
668 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
671 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
672 local_stats = *percpu_stats;
673 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
675 stats->n_hit += local_stats.n_hit;
676 stats->n_missed += local_stats.n_missed;
677 stats->n_lost += local_stats.n_lost;
678 mega_stats->n_mask_hit += local_stats.n_mask_hit;
682 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
684 return ovs_identifier_is_ufid(sfid) &&
685 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
688 static bool should_fill_mask(uint32_t ufid_flags)
690 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
693 static bool should_fill_actions(uint32_t ufid_flags)
695 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
698 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
699 const struct sw_flow_id *sfid,
702 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
704 /* OVS_FLOW_ATTR_UFID */
705 if (sfid && ovs_identifier_is_ufid(sfid))
706 len += nla_total_size(sfid->ufid_len);
708 /* OVS_FLOW_ATTR_KEY */
709 if (!sfid || should_fill_key(sfid, ufid_flags))
710 len += nla_total_size(ovs_key_attr_size());
712 /* OVS_FLOW_ATTR_MASK */
713 if (should_fill_mask(ufid_flags))
714 len += nla_total_size(ovs_key_attr_size());
716 /* OVS_FLOW_ATTR_ACTIONS */
717 if (should_fill_actions(ufid_flags))
718 len += nla_total_size(acts->orig_len);
721 + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
722 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
723 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
726 /* Called with ovs_mutex or RCU read lock. */
727 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
730 struct ovs_flow_stats stats;
734 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
737 nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
741 if (stats.n_packets &&
742 nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
743 sizeof(struct ovs_flow_stats), &stats,
747 if ((u8)ntohs(tcp_flags) &&
748 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
754 /* Called with ovs_mutex or RCU read lock. */
755 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
756 struct sk_buff *skb, int skb_orig_len)
758 struct nlattr *start;
761 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
762 * this is the first flow to be dumped into 'skb'. This is unusual for
763 * Netlink but individual action lists can be longer than
764 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
765 * The userspace caller can always fetch the actions separately if it
766 * really wants them. (Most userspace callers in fact don't care.)
768 * This can only fail for dump operations because the skb is always
769 * properly sized for single flows.
771 start = nla_nest_start_noflag(skb, OVS_FLOW_ATTR_ACTIONS);
773 const struct sw_flow_actions *sf_acts;
775 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
776 err = ovs_nla_put_actions(sf_acts->actions,
777 sf_acts->actions_len, skb);
780 nla_nest_end(skb, start);
785 nla_nest_cancel(skb, start);
787 } else if (skb_orig_len) {
794 /* Called with ovs_mutex or RCU read lock. */
795 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
796 struct sk_buff *skb, u32 portid,
797 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
799 const int skb_orig_len = skb->len;
800 struct ovs_header *ovs_header;
803 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
808 ovs_header->dp_ifindex = dp_ifindex;
810 err = ovs_nla_put_identifier(flow, skb);
814 if (should_fill_key(&flow->id, ufid_flags)) {
815 err = ovs_nla_put_masked_key(flow, skb);
820 if (should_fill_mask(ufid_flags)) {
821 err = ovs_nla_put_mask(flow, skb);
826 err = ovs_flow_cmd_fill_stats(flow, skb);
830 if (should_fill_actions(ufid_flags)) {
831 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
836 genlmsg_end(skb, ovs_header);
840 genlmsg_cancel(skb, ovs_header);
844 /* May not be called with RCU read lock. */
845 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
846 const struct sw_flow_id *sfid,
847 struct genl_info *info,
854 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
857 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
858 skb = genlmsg_new(len, GFP_KERNEL);
860 return ERR_PTR(-ENOMEM);
865 /* Called with ovs_mutex. */
866 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
868 struct genl_info *info, u8 cmd,
869 bool always, u32 ufid_flags)
874 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
875 &flow->id, info, always, ufid_flags);
876 if (IS_ERR_OR_NULL(skb))
879 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
880 info->snd_portid, info->snd_seq, 0,
886 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
888 struct net *net = sock_net(skb->sk);
889 struct nlattr **a = info->attrs;
890 struct ovs_header *ovs_header = info->userhdr;
891 struct sw_flow *flow = NULL, *new_flow;
892 struct sw_flow_mask mask;
893 struct sk_buff *reply;
895 struct sw_flow_actions *acts;
896 struct sw_flow_match match;
897 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
899 bool log = !a[OVS_FLOW_ATTR_PROBE];
901 /* Must have key and actions. */
903 if (!a[OVS_FLOW_ATTR_KEY]) {
904 OVS_NLERR(log, "Flow key attr not present in new flow.");
907 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
908 OVS_NLERR(log, "Flow actions attr not present in new flow.");
912 /* Most of the time we need to allocate a new flow, do it before
915 new_flow = ovs_flow_alloc();
916 if (IS_ERR(new_flow)) {
917 error = PTR_ERR(new_flow);
922 ovs_match_init(&match, &new_flow->key, false, &mask);
923 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
924 a[OVS_FLOW_ATTR_MASK], log);
928 /* Extract flow identifier. */
929 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
930 &new_flow->key, log);
934 /* unmasked key is needed to match when ufid is not used. */
935 if (ovs_identifier_is_key(&new_flow->id))
936 match.key = new_flow->id.unmasked_key;
938 ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask);
940 /* Validate actions. */
941 error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
942 &new_flow->key, &acts, log);
944 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
948 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
951 error = PTR_ERR(reply);
956 dp = get_dp(net, ovs_header->dp_ifindex);
962 /* Check if this is a duplicate flow */
963 if (ovs_identifier_is_ufid(&new_flow->id))
964 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
966 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key);
968 rcu_assign_pointer(new_flow->sf_acts, acts);
970 /* Put flow in bucket. */
971 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
972 if (unlikely(error)) {
977 if (unlikely(reply)) {
978 error = ovs_flow_cmd_fill_info(new_flow,
979 ovs_header->dp_ifindex,
980 reply, info->snd_portid,
988 struct sw_flow_actions *old_acts;
990 /* Bail out if we're not allowed to modify an existing flow.
991 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
992 * because Generic Netlink treats the latter as a dump
993 * request. We also accept NLM_F_EXCL in case that bug ever
996 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
1001 /* The flow identifier has to be the same for flow updates.
1002 * Look for any overlapping flow.
1004 if (unlikely(!ovs_flow_cmp(flow, &match))) {
1005 if (ovs_identifier_is_key(&flow->id))
1006 flow = ovs_flow_tbl_lookup_exact(&dp->table,
1008 else /* UFID matches but key is different */
1012 goto err_unlock_ovs;
1015 /* Update actions. */
1016 old_acts = ovsl_dereference(flow->sf_acts);
1017 rcu_assign_pointer(flow->sf_acts, acts);
1019 if (unlikely(reply)) {
1020 error = ovs_flow_cmd_fill_info(flow,
1021 ovs_header->dp_ifindex,
1022 reply, info->snd_portid,
1030 ovs_nla_free_flow_actions_rcu(old_acts);
1031 ovs_flow_free(new_flow, false);
1035 ovs_notify(&dp_flow_genl_family, reply, info);
1042 ovs_nla_free_flow_actions(acts);
1044 ovs_flow_free(new_flow, false);
1049 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1050 static noinline_for_stack struct sw_flow_actions *get_flow_actions(struct net *net,
1051 const struct nlattr *a,
1052 const struct sw_flow_key *key,
1053 const struct sw_flow_mask *mask,
1056 struct sw_flow_actions *acts;
1057 struct sw_flow_key masked_key;
1060 ovs_flow_mask_key(&masked_key, key, true, mask);
1061 error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
1064 "Actions may not be safe on all matching packets");
1065 return ERR_PTR(error);
1071 /* Factor out match-init and action-copy to avoid
1072 * "Wframe-larger-than=1024" warning. Because mask is only
1073 * used to get actions, we new a function to save some
1076 * If there are not key and action attrs, we return 0
1077 * directly. In the case, the caller will also not use the
1078 * match as before. If there is action attr, we try to get
1079 * actions and save them to *acts. Before returning from
1080 * the function, we reset the match->mask pointer. Because
1081 * we should not to return match object with dangling reference
1084 static noinline_for_stack int
1085 ovs_nla_init_match_and_action(struct net *net,
1086 struct sw_flow_match *match,
1087 struct sw_flow_key *key,
1089 struct sw_flow_actions **acts,
1092 struct sw_flow_mask mask;
1095 if (a[OVS_FLOW_ATTR_KEY]) {
1096 ovs_match_init(match, key, true, &mask);
1097 error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
1098 a[OVS_FLOW_ATTR_MASK], log);
1103 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1104 if (!a[OVS_FLOW_ATTR_KEY]) {
1106 "Flow key attribute not present in set flow.");
1111 *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
1113 if (IS_ERR(*acts)) {
1114 error = PTR_ERR(*acts);
1119 /* On success, error is 0. */
1125 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1127 struct net *net = sock_net(skb->sk);
1128 struct nlattr **a = info->attrs;
1129 struct ovs_header *ovs_header = info->userhdr;
1130 struct sw_flow_key key;
1131 struct sw_flow *flow;
1132 struct sk_buff *reply = NULL;
1133 struct datapath *dp;
1134 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1135 struct sw_flow_match match;
1136 struct sw_flow_id sfid;
1137 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1139 bool log = !a[OVS_FLOW_ATTR_PROBE];
1142 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1143 if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
1145 "Flow set message rejected, Key attribute missing.");
1149 error = ovs_nla_init_match_and_action(net, &match, &key, a,
1155 /* Can allocate before locking if have acts. */
1156 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1158 if (IS_ERR(reply)) {
1159 error = PTR_ERR(reply);
1160 goto err_kfree_acts;
1165 dp = get_dp(net, ovs_header->dp_ifindex);
1166 if (unlikely(!dp)) {
1168 goto err_unlock_ovs;
1170 /* Check that the flow exists. */
1172 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1174 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1175 if (unlikely(!flow)) {
1177 goto err_unlock_ovs;
1180 /* Update actions, if present. */
1182 old_acts = ovsl_dereference(flow->sf_acts);
1183 rcu_assign_pointer(flow->sf_acts, acts);
1185 if (unlikely(reply)) {
1186 error = ovs_flow_cmd_fill_info(flow,
1187 ovs_header->dp_ifindex,
1188 reply, info->snd_portid,
1195 /* Could not alloc without acts before locking. */
1196 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1197 info, OVS_FLOW_CMD_SET, false,
1200 if (IS_ERR(reply)) {
1201 error = PTR_ERR(reply);
1202 goto err_unlock_ovs;
1207 if (a[OVS_FLOW_ATTR_CLEAR])
1208 ovs_flow_stats_clear(flow);
1212 ovs_notify(&dp_flow_genl_family, reply, info);
1214 ovs_nla_free_flow_actions_rcu(old_acts);
1222 ovs_nla_free_flow_actions(acts);
1227 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1229 struct nlattr **a = info->attrs;
1230 struct ovs_header *ovs_header = info->userhdr;
1231 struct net *net = sock_net(skb->sk);
1232 struct sw_flow_key key;
1233 struct sk_buff *reply;
1234 struct sw_flow *flow;
1235 struct datapath *dp;
1236 struct sw_flow_match match;
1237 struct sw_flow_id ufid;
1238 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1240 bool log = !a[OVS_FLOW_ATTR_PROBE];
1243 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1244 if (a[OVS_FLOW_ATTR_KEY]) {
1245 ovs_match_init(&match, &key, true, NULL);
1246 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
1248 } else if (!ufid_present) {
1250 "Flow get message rejected, Key attribute missing.");
1257 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1264 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1266 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1272 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1273 OVS_FLOW_CMD_GET, true, ufid_flags);
1274 if (IS_ERR(reply)) {
1275 err = PTR_ERR(reply);
1280 return genlmsg_reply(reply, info);
1286 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1288 struct nlattr **a = info->attrs;
1289 struct ovs_header *ovs_header = info->userhdr;
1290 struct net *net = sock_net(skb->sk);
1291 struct sw_flow_key key;
1292 struct sk_buff *reply;
1293 struct sw_flow *flow = NULL;
1294 struct datapath *dp;
1295 struct sw_flow_match match;
1296 struct sw_flow_id ufid;
1297 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1299 bool log = !a[OVS_FLOW_ATTR_PROBE];
1302 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1303 if (a[OVS_FLOW_ATTR_KEY]) {
1304 ovs_match_init(&match, &key, true, NULL);
1305 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1312 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1313 if (unlikely(!dp)) {
1318 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1319 err = ovs_flow_tbl_flush(&dp->table);
1324 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1326 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1327 if (unlikely(!flow)) {
1332 ovs_flow_tbl_remove(&dp->table, flow);
1335 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1336 &flow->id, info, false, ufid_flags);
1337 if (likely(reply)) {
1338 if (!IS_ERR(reply)) {
1339 rcu_read_lock(); /*To keep RCU checker happy. */
1340 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1341 reply, info->snd_portid,
1348 ovs_notify(&dp_flow_genl_family, reply, info);
1350 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1354 ovs_flow_free(flow, true);
1361 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1363 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1364 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1365 struct table_instance *ti;
1366 struct datapath *dp;
1370 err = genlmsg_parse_deprecated(cb->nlh, &dp_flow_genl_family, a,
1371 OVS_FLOW_ATTR_MAX, flow_policy, NULL);
1374 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1377 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1383 ti = rcu_dereference(dp->table.ti);
1385 struct sw_flow *flow;
1388 bucket = cb->args[0];
1390 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1394 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1395 NETLINK_CB(cb->skb).portid,
1396 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1397 OVS_FLOW_CMD_GET, ufid_flags) < 0)
1400 cb->args[0] = bucket;
1407 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1408 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1409 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1410 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1411 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1412 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1413 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1414 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1417 static const struct genl_ops dp_flow_genl_ops[] = {
1418 { .cmd = OVS_FLOW_CMD_NEW,
1419 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1420 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1421 .doit = ovs_flow_cmd_new
1423 { .cmd = OVS_FLOW_CMD_DEL,
1424 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1425 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1426 .doit = ovs_flow_cmd_del
1428 { .cmd = OVS_FLOW_CMD_GET,
1429 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1430 .flags = 0, /* OK for unprivileged users. */
1431 .doit = ovs_flow_cmd_get,
1432 .dumpit = ovs_flow_cmd_dump
1434 { .cmd = OVS_FLOW_CMD_SET,
1435 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1436 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1437 .doit = ovs_flow_cmd_set,
1441 static struct genl_family dp_flow_genl_family __ro_after_init = {
1442 .hdrsize = sizeof(struct ovs_header),
1443 .name = OVS_FLOW_FAMILY,
1444 .version = OVS_FLOW_VERSION,
1445 .maxattr = OVS_FLOW_ATTR_MAX,
1446 .policy = flow_policy,
1448 .parallel_ops = true,
1449 .ops = dp_flow_genl_ops,
1450 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1451 .mcgrps = &ovs_dp_flow_multicast_group,
1453 .module = THIS_MODULE,
1456 static size_t ovs_dp_cmd_msg_size(void)
1458 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1460 msgsize += nla_total_size(IFNAMSIZ);
1461 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
1462 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
1463 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1468 /* Called with ovs_mutex. */
1469 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1470 u32 portid, u32 seq, u32 flags, u8 cmd)
1472 struct ovs_header *ovs_header;
1473 struct ovs_dp_stats dp_stats;
1474 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1477 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1482 ovs_header->dp_ifindex = get_dpifindex(dp);
1484 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1486 goto nla_put_failure;
1488 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1489 if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1490 &dp_stats, OVS_DP_ATTR_PAD))
1491 goto nla_put_failure;
1493 if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1494 sizeof(struct ovs_dp_megaflow_stats),
1495 &dp_megaflow_stats, OVS_DP_ATTR_PAD))
1496 goto nla_put_failure;
1498 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1499 goto nla_put_failure;
1501 genlmsg_end(skb, ovs_header);
1505 genlmsg_cancel(skb, ovs_header);
1510 static struct sk_buff *ovs_dp_cmd_alloc_info(void)
1512 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1515 /* Called with rcu_read_lock or ovs_mutex. */
1516 static struct datapath *lookup_datapath(struct net *net,
1517 const struct ovs_header *ovs_header,
1518 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1520 struct datapath *dp;
1522 if (!a[OVS_DP_ATTR_NAME])
1523 dp = get_dp(net, ovs_header->dp_ifindex);
1525 struct vport *vport;
1527 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1528 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1530 return dp ? dp : ERR_PTR(-ENODEV);
1533 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1535 struct datapath *dp;
1537 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1541 WARN(dp->user_features, "Dropping previously announced user features\n");
1542 dp->user_features = 0;
1545 static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1547 if (a[OVS_DP_ATTR_USER_FEATURES])
1548 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1551 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1553 struct nlattr **a = info->attrs;
1554 struct vport_parms parms;
1555 struct sk_buff *reply;
1556 struct datapath *dp;
1557 struct vport *vport;
1558 struct ovs_net *ovs_net;
1562 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1565 reply = ovs_dp_cmd_alloc_info();
1570 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1572 goto err_free_reply;
1574 ovs_dp_set_net(dp, sock_net(skb->sk));
1576 /* Allocate table. */
1577 err = ovs_flow_tbl_init(&dp->table);
1581 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1582 if (!dp->stats_percpu) {
1584 goto err_destroy_table;
1587 dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS,
1588 sizeof(struct hlist_head),
1592 goto err_destroy_percpu;
1595 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1596 INIT_HLIST_HEAD(&dp->ports[i]);
1598 err = ovs_meters_init(dp);
1600 goto err_destroy_ports_array;
1602 /* Set up our datapath device. */
1603 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1604 parms.type = OVS_VPORT_TYPE_INTERNAL;
1605 parms.options = NULL;
1607 parms.port_no = OVSP_LOCAL;
1608 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1610 ovs_dp_change(dp, a);
1612 /* So far only local changes have been made, now need the lock. */
1615 vport = new_vport(&parms);
1616 if (IS_ERR(vport)) {
1617 err = PTR_ERR(vport);
1621 if (err == -EEXIST) {
1622 /* An outdated user space instance that does not understand
1623 * the concept of user_features has attempted to create a new
1624 * datapath and is likely to reuse it. Drop all user features.
1626 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1627 ovs_dp_reset_user_features(skb, info);
1630 goto err_destroy_meters;
1633 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1634 info->snd_seq, 0, OVS_DP_CMD_NEW);
1637 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1638 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1642 ovs_notify(&dp_datapath_genl_family, reply, info);
1647 ovs_meters_exit(dp);
1648 err_destroy_ports_array:
1651 free_percpu(dp->stats_percpu);
1653 ovs_flow_tbl_destroy(&dp->table);
1662 /* Called with ovs_mutex. */
1663 static void __dp_destroy(struct datapath *dp)
1667 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1668 struct vport *vport;
1669 struct hlist_node *n;
1671 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1672 if (vport->port_no != OVSP_LOCAL)
1673 ovs_dp_detach_port(vport);
1676 list_del_rcu(&dp->list_node);
1678 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1679 * all ports in datapath are destroyed first before freeing datapath.
1681 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1683 /* RCU destroy the flow table */
1684 call_rcu(&dp->rcu, destroy_dp_rcu);
1687 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1689 struct sk_buff *reply;
1690 struct datapath *dp;
1693 reply = ovs_dp_cmd_alloc_info();
1698 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1701 goto err_unlock_free;
1703 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1704 info->snd_seq, 0, OVS_DP_CMD_DEL);
1710 ovs_notify(&dp_datapath_genl_family, reply, info);
1720 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1722 struct sk_buff *reply;
1723 struct datapath *dp;
1726 reply = ovs_dp_cmd_alloc_info();
1731 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1734 goto err_unlock_free;
1736 ovs_dp_change(dp, info->attrs);
1738 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1739 info->snd_seq, 0, OVS_DP_CMD_SET);
1743 ovs_notify(&dp_datapath_genl_family, reply, info);
1753 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1755 struct sk_buff *reply;
1756 struct datapath *dp;
1759 reply = ovs_dp_cmd_alloc_info();
1764 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1767 goto err_unlock_free;
1769 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1770 info->snd_seq, 0, OVS_DP_CMD_GET);
1774 return genlmsg_reply(reply, info);
1782 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1784 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1785 struct datapath *dp;
1786 int skip = cb->args[0];
1790 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1792 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1793 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1794 OVS_DP_CMD_GET) < 0)
1805 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1806 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1807 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1808 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1811 static const struct genl_ops dp_datapath_genl_ops[] = {
1812 { .cmd = OVS_DP_CMD_NEW,
1813 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1814 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1815 .doit = ovs_dp_cmd_new
1817 { .cmd = OVS_DP_CMD_DEL,
1818 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1819 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1820 .doit = ovs_dp_cmd_del
1822 { .cmd = OVS_DP_CMD_GET,
1823 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1824 .flags = 0, /* OK for unprivileged users. */
1825 .doit = ovs_dp_cmd_get,
1826 .dumpit = ovs_dp_cmd_dump
1828 { .cmd = OVS_DP_CMD_SET,
1829 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1830 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1831 .doit = ovs_dp_cmd_set,
1835 static struct genl_family dp_datapath_genl_family __ro_after_init = {
1836 .hdrsize = sizeof(struct ovs_header),
1837 .name = OVS_DATAPATH_FAMILY,
1838 .version = OVS_DATAPATH_VERSION,
1839 .maxattr = OVS_DP_ATTR_MAX,
1840 .policy = datapath_policy,
1842 .parallel_ops = true,
1843 .ops = dp_datapath_genl_ops,
1844 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1845 .mcgrps = &ovs_dp_datapath_multicast_group,
1847 .module = THIS_MODULE,
1850 /* Called with ovs_mutex or RCU read lock. */
1851 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1852 struct net *net, u32 portid, u32 seq,
1855 struct ovs_header *ovs_header;
1856 struct ovs_vport_stats vport_stats;
1859 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1864 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1866 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1867 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1868 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1869 ovs_vport_name(vport)) ||
1870 nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
1871 goto nla_put_failure;
1873 if (!net_eq(net, dev_net(vport->dev))) {
1874 int id = peernet2id_alloc(net, dev_net(vport->dev));
1876 if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
1877 goto nla_put_failure;
1880 ovs_vport_get_stats(vport, &vport_stats);
1881 if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
1882 sizeof(struct ovs_vport_stats), &vport_stats,
1883 OVS_VPORT_ATTR_PAD))
1884 goto nla_put_failure;
1886 if (ovs_vport_get_upcall_portids(vport, skb))
1887 goto nla_put_failure;
1889 err = ovs_vport_get_options(vport, skb);
1890 if (err == -EMSGSIZE)
1893 genlmsg_end(skb, ovs_header);
1899 genlmsg_cancel(skb, ovs_header);
1903 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1905 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1908 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1909 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
1910 u32 portid, u32 seq, u8 cmd)
1912 struct sk_buff *skb;
1915 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1917 return ERR_PTR(-ENOMEM);
1919 retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd);
1925 /* Called with ovs_mutex or RCU read lock. */
1926 static struct vport *lookup_vport(struct net *net,
1927 const struct ovs_header *ovs_header,
1928 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1930 struct datapath *dp;
1931 struct vport *vport;
1933 if (a[OVS_VPORT_ATTR_IFINDEX])
1934 return ERR_PTR(-EOPNOTSUPP);
1935 if (a[OVS_VPORT_ATTR_NAME]) {
1936 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1938 return ERR_PTR(-ENODEV);
1939 if (ovs_header->dp_ifindex &&
1940 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1941 return ERR_PTR(-ENODEV);
1943 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1944 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1946 if (port_no >= DP_MAX_PORTS)
1947 return ERR_PTR(-EFBIG);
1949 dp = get_dp(net, ovs_header->dp_ifindex);
1951 return ERR_PTR(-ENODEV);
1953 vport = ovs_vport_ovsl_rcu(dp, port_no);
1955 return ERR_PTR(-ENODEV);
1958 return ERR_PTR(-EINVAL);
1962 static unsigned int ovs_get_max_headroom(struct datapath *dp)
1964 unsigned int dev_headroom, max_headroom = 0;
1965 struct net_device *dev;
1966 struct vport *vport;
1969 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1970 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1972 dev_headroom = netdev_get_fwd_headroom(dev);
1973 if (dev_headroom > max_headroom)
1974 max_headroom = dev_headroom;
1978 return max_headroom;
1981 /* Called with ovs_mutex */
1982 static void ovs_update_headroom(struct datapath *dp, unsigned int new_headroom)
1984 struct vport *vport;
1987 dp->max_headroom = new_headroom;
1988 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1989 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
1990 netdev_set_rx_headroom(vport->dev, new_headroom);
1993 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1995 struct nlattr **a = info->attrs;
1996 struct ovs_header *ovs_header = info->userhdr;
1997 struct vport_parms parms;
1998 struct sk_buff *reply;
1999 struct vport *vport;
2000 struct datapath *dp;
2001 unsigned int new_headroom;
2005 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2006 !a[OVS_VPORT_ATTR_UPCALL_PID])
2008 if (a[OVS_VPORT_ATTR_IFINDEX])
2011 port_no = a[OVS_VPORT_ATTR_PORT_NO]
2012 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
2013 if (port_no >= DP_MAX_PORTS)
2016 reply = ovs_vport_cmd_alloc_info();
2022 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2025 goto exit_unlock_free;
2028 vport = ovs_vport_ovsl(dp, port_no);
2031 goto exit_unlock_free;
2033 for (port_no = 1; ; port_no++) {
2034 if (port_no >= DP_MAX_PORTS) {
2036 goto exit_unlock_free;
2038 vport = ovs_vport_ovsl(dp, port_no);
2044 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2045 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2046 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2048 parms.port_no = port_no;
2049 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
2051 vport = new_vport(&parms);
2052 err = PTR_ERR(vport);
2053 if (IS_ERR(vport)) {
2056 goto exit_unlock_free;
2059 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2060 info->snd_portid, info->snd_seq, 0,
2063 new_headroom = netdev_get_fwd_headroom(vport->dev);
2065 if (new_headroom > dp->max_headroom)
2066 ovs_update_headroom(dp, new_headroom);
2068 netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2073 ovs_notify(&dp_vport_genl_family, reply, info);
2082 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2084 struct nlattr **a = info->attrs;
2085 struct sk_buff *reply;
2086 struct vport *vport;
2089 reply = ovs_vport_cmd_alloc_info();
2094 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2095 err = PTR_ERR(vport);
2097 goto exit_unlock_free;
2099 if (a[OVS_VPORT_ATTR_TYPE] &&
2100 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2102 goto exit_unlock_free;
2105 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2106 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2108 goto exit_unlock_free;
2112 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2113 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2115 err = ovs_vport_set_upcall_portids(vport, ids);
2117 goto exit_unlock_free;
2120 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2121 info->snd_portid, info->snd_seq, 0,
2126 ovs_notify(&dp_vport_genl_family, reply, info);
2135 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2137 bool update_headroom = false;
2138 struct nlattr **a = info->attrs;
2139 struct sk_buff *reply;
2140 struct datapath *dp;
2141 struct vport *vport;
2142 unsigned int new_headroom;
2145 reply = ovs_vport_cmd_alloc_info();
2150 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2151 err = PTR_ERR(vport);
2153 goto exit_unlock_free;
2155 if (vport->port_no == OVSP_LOCAL) {
2157 goto exit_unlock_free;
2160 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2161 info->snd_portid, info->snd_seq, 0,
2165 /* the vport deletion may trigger dp headroom update */
2167 if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
2168 update_headroom = true;
2170 netdev_reset_rx_headroom(vport->dev);
2171 ovs_dp_detach_port(vport);
2173 if (update_headroom) {
2174 new_headroom = ovs_get_max_headroom(dp);
2176 if (new_headroom < dp->max_headroom)
2177 ovs_update_headroom(dp, new_headroom);
2181 ovs_notify(&dp_vport_genl_family, reply, info);
2190 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2192 struct nlattr **a = info->attrs;
2193 struct ovs_header *ovs_header = info->userhdr;
2194 struct sk_buff *reply;
2195 struct vport *vport;
2198 reply = ovs_vport_cmd_alloc_info();
2203 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2204 err = PTR_ERR(vport);
2206 goto exit_unlock_free;
2207 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2208 info->snd_portid, info->snd_seq, 0,
2213 return genlmsg_reply(reply, info);
2221 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2223 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2224 struct datapath *dp;
2225 int bucket = cb->args[0], skip = cb->args[1];
2229 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2234 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2235 struct vport *vport;
2238 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2240 ovs_vport_cmd_fill_info(vport, skb,
2242 NETLINK_CB(cb->skb).portid,
2245 OVS_VPORT_CMD_GET) < 0)
2261 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2262 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2263 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2264 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2265 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2266 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2267 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2268 [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
2269 [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
2272 static const struct genl_ops dp_vport_genl_ops[] = {
2273 { .cmd = OVS_VPORT_CMD_NEW,
2274 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2275 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2276 .doit = ovs_vport_cmd_new
2278 { .cmd = OVS_VPORT_CMD_DEL,
2279 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2280 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2281 .doit = ovs_vport_cmd_del
2283 { .cmd = OVS_VPORT_CMD_GET,
2284 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2285 .flags = 0, /* OK for unprivileged users. */
2286 .doit = ovs_vport_cmd_get,
2287 .dumpit = ovs_vport_cmd_dump
2289 { .cmd = OVS_VPORT_CMD_SET,
2290 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2291 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2292 .doit = ovs_vport_cmd_set,
2296 struct genl_family dp_vport_genl_family __ro_after_init = {
2297 .hdrsize = sizeof(struct ovs_header),
2298 .name = OVS_VPORT_FAMILY,
2299 .version = OVS_VPORT_VERSION,
2300 .maxattr = OVS_VPORT_ATTR_MAX,
2301 .policy = vport_policy,
2303 .parallel_ops = true,
2304 .ops = dp_vport_genl_ops,
2305 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2306 .mcgrps = &ovs_dp_vport_multicast_group,
2308 .module = THIS_MODULE,
2311 static struct genl_family * const dp_genl_families[] = {
2312 &dp_datapath_genl_family,
2313 &dp_vport_genl_family,
2314 &dp_flow_genl_family,
2315 &dp_packet_genl_family,
2316 &dp_meter_genl_family,
2317 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2318 &dp_ct_limit_genl_family,
2322 static void dp_unregister_genl(int n_families)
2326 for (i = 0; i < n_families; i++)
2327 genl_unregister_family(dp_genl_families[i]);
2330 static int __init dp_register_genl(void)
2335 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2337 err = genl_register_family(dp_genl_families[i]);
2345 dp_unregister_genl(i);
2349 static int __net_init ovs_init_net(struct net *net)
2351 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2353 INIT_LIST_HEAD(&ovs_net->dps);
2354 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2355 return ovs_ct_init(net);
2358 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2359 struct list_head *head)
2361 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2362 struct datapath *dp;
2364 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2367 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2368 struct vport *vport;
2370 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2371 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2374 if (dev_net(vport->dev) == dnet)
2375 list_add(&vport->detach_list, head);
2381 static void __net_exit ovs_exit_net(struct net *dnet)
2383 struct datapath *dp, *dp_next;
2384 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2385 struct vport *vport, *vport_next;
2391 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2394 down_read(&net_rwsem);
2396 list_vports_from_net(net, dnet, &head);
2397 up_read(&net_rwsem);
2399 /* Detach all vports from given namespace. */
2400 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2401 list_del(&vport->detach_list);
2402 ovs_dp_detach_port(vport);
2407 cancel_work_sync(&ovs_net->dp_notify_work);
2410 static struct pernet_operations ovs_net_ops = {
2411 .init = ovs_init_net,
2412 .exit = ovs_exit_net,
2414 .size = sizeof(struct ovs_net),
2417 static int __init dp_init(void)
2421 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2423 pr_info("Open vSwitch switching datapath\n");
2425 err = action_fifos_init();
2429 err = ovs_internal_dev_rtnl_link_register();
2431 goto error_action_fifos_exit;
2433 err = ovs_flow_init();
2435 goto error_unreg_rtnl_link;
2437 err = ovs_vport_init();
2439 goto error_flow_exit;
2441 err = register_pernet_device(&ovs_net_ops);
2443 goto error_vport_exit;
2445 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2447 goto error_netns_exit;
2449 err = ovs_netdev_init();
2451 goto error_unreg_notifier;
2453 err = dp_register_genl();
2455 goto error_unreg_netdev;
2461 error_unreg_notifier:
2462 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2464 unregister_pernet_device(&ovs_net_ops);
2469 error_unreg_rtnl_link:
2470 ovs_internal_dev_rtnl_link_unregister();
2471 error_action_fifos_exit:
2472 action_fifos_exit();
2477 static void dp_cleanup(void)
2479 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2481 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2482 unregister_pernet_device(&ovs_net_ops);
2486 ovs_internal_dev_rtnl_link_unregister();
2487 action_fifos_exit();
2490 module_init(dp_init);
2491 module_exit(dp_cleanup);
2493 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2494 MODULE_LICENSE("GPL");
2495 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
2496 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
2497 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
2498 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
2499 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);
2500 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY);