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 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 int ovs_nla_init_match_and_action(struct net *net,
1085 struct sw_flow_match *match,
1086 struct sw_flow_key *key,
1088 struct sw_flow_actions **acts,
1091 struct sw_flow_mask mask;
1094 if (a[OVS_FLOW_ATTR_KEY]) {
1095 ovs_match_init(match, key, true, &mask);
1096 error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
1097 a[OVS_FLOW_ATTR_MASK], log);
1102 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1103 if (!a[OVS_FLOW_ATTR_KEY]) {
1105 "Flow key attribute not present in set flow.");
1110 *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
1112 if (IS_ERR(*acts)) {
1113 error = PTR_ERR(*acts);
1118 /* On success, error is 0. */
1124 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1126 struct net *net = sock_net(skb->sk);
1127 struct nlattr **a = info->attrs;
1128 struct ovs_header *ovs_header = info->userhdr;
1129 struct sw_flow_key key;
1130 struct sw_flow *flow;
1131 struct sk_buff *reply = NULL;
1132 struct datapath *dp;
1133 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1134 struct sw_flow_match match;
1135 struct sw_flow_id sfid;
1136 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1138 bool log = !a[OVS_FLOW_ATTR_PROBE];
1141 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1142 if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
1144 "Flow set message rejected, Key attribute missing.");
1148 error = ovs_nla_init_match_and_action(net, &match, &key, a,
1154 /* Can allocate before locking if have acts. */
1155 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1157 if (IS_ERR(reply)) {
1158 error = PTR_ERR(reply);
1159 goto err_kfree_acts;
1164 dp = get_dp(net, ovs_header->dp_ifindex);
1165 if (unlikely(!dp)) {
1167 goto err_unlock_ovs;
1169 /* Check that the flow exists. */
1171 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1173 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1174 if (unlikely(!flow)) {
1176 goto err_unlock_ovs;
1179 /* Update actions, if present. */
1181 old_acts = ovsl_dereference(flow->sf_acts);
1182 rcu_assign_pointer(flow->sf_acts, acts);
1184 if (unlikely(reply)) {
1185 error = ovs_flow_cmd_fill_info(flow,
1186 ovs_header->dp_ifindex,
1187 reply, info->snd_portid,
1194 /* Could not alloc without acts before locking. */
1195 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1196 info, OVS_FLOW_CMD_SET, false,
1199 if (IS_ERR(reply)) {
1200 error = PTR_ERR(reply);
1201 goto err_unlock_ovs;
1206 if (a[OVS_FLOW_ATTR_CLEAR])
1207 ovs_flow_stats_clear(flow);
1211 ovs_notify(&dp_flow_genl_family, reply, info);
1213 ovs_nla_free_flow_actions_rcu(old_acts);
1221 ovs_nla_free_flow_actions(acts);
1226 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1228 struct nlattr **a = info->attrs;
1229 struct ovs_header *ovs_header = info->userhdr;
1230 struct net *net = sock_net(skb->sk);
1231 struct sw_flow_key key;
1232 struct sk_buff *reply;
1233 struct sw_flow *flow;
1234 struct datapath *dp;
1235 struct sw_flow_match match;
1236 struct sw_flow_id ufid;
1237 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1239 bool log = !a[OVS_FLOW_ATTR_PROBE];
1242 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1243 if (a[OVS_FLOW_ATTR_KEY]) {
1244 ovs_match_init(&match, &key, true, NULL);
1245 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
1247 } else if (!ufid_present) {
1249 "Flow get message rejected, Key attribute missing.");
1256 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1263 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1265 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1271 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1272 OVS_FLOW_CMD_GET, true, ufid_flags);
1273 if (IS_ERR(reply)) {
1274 err = PTR_ERR(reply);
1279 return genlmsg_reply(reply, info);
1285 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1287 struct nlattr **a = info->attrs;
1288 struct ovs_header *ovs_header = info->userhdr;
1289 struct net *net = sock_net(skb->sk);
1290 struct sw_flow_key key;
1291 struct sk_buff *reply;
1292 struct sw_flow *flow = NULL;
1293 struct datapath *dp;
1294 struct sw_flow_match match;
1295 struct sw_flow_id ufid;
1296 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1298 bool log = !a[OVS_FLOW_ATTR_PROBE];
1301 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1302 if (a[OVS_FLOW_ATTR_KEY]) {
1303 ovs_match_init(&match, &key, true, NULL);
1304 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1311 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1312 if (unlikely(!dp)) {
1317 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1318 err = ovs_flow_tbl_flush(&dp->table);
1323 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1325 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1326 if (unlikely(!flow)) {
1331 ovs_flow_tbl_remove(&dp->table, flow);
1334 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1335 &flow->id, info, false, ufid_flags);
1336 if (likely(reply)) {
1337 if (!IS_ERR(reply)) {
1338 rcu_read_lock(); /*To keep RCU checker happy. */
1339 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1340 reply, info->snd_portid,
1347 ovs_notify(&dp_flow_genl_family, reply, info);
1349 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1353 ovs_flow_free(flow, true);
1360 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1362 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1363 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1364 struct table_instance *ti;
1365 struct datapath *dp;
1369 err = genlmsg_parse_deprecated(cb->nlh, &dp_flow_genl_family, a,
1370 OVS_FLOW_ATTR_MAX, flow_policy, NULL);
1373 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1376 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1382 ti = rcu_dereference(dp->table.ti);
1384 struct sw_flow *flow;
1387 bucket = cb->args[0];
1389 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1393 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1394 NETLINK_CB(cb->skb).portid,
1395 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1396 OVS_FLOW_CMD_GET, ufid_flags) < 0)
1399 cb->args[0] = bucket;
1406 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1407 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1408 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1409 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1410 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1411 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1412 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1413 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1416 static const struct genl_ops dp_flow_genl_ops[] = {
1417 { .cmd = OVS_FLOW_CMD_NEW,
1418 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1419 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1420 .doit = ovs_flow_cmd_new
1422 { .cmd = OVS_FLOW_CMD_DEL,
1423 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1424 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1425 .doit = ovs_flow_cmd_del
1427 { .cmd = OVS_FLOW_CMD_GET,
1428 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1429 .flags = 0, /* OK for unprivileged users. */
1430 .doit = ovs_flow_cmd_get,
1431 .dumpit = ovs_flow_cmd_dump
1433 { .cmd = OVS_FLOW_CMD_SET,
1434 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1435 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1436 .doit = ovs_flow_cmd_set,
1440 static struct genl_family dp_flow_genl_family __ro_after_init = {
1441 .hdrsize = sizeof(struct ovs_header),
1442 .name = OVS_FLOW_FAMILY,
1443 .version = OVS_FLOW_VERSION,
1444 .maxattr = OVS_FLOW_ATTR_MAX,
1445 .policy = flow_policy,
1447 .parallel_ops = true,
1448 .ops = dp_flow_genl_ops,
1449 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1450 .mcgrps = &ovs_dp_flow_multicast_group,
1452 .module = THIS_MODULE,
1455 static size_t ovs_dp_cmd_msg_size(void)
1457 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1459 msgsize += nla_total_size(IFNAMSIZ);
1460 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
1461 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
1462 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1467 /* Called with ovs_mutex. */
1468 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1469 u32 portid, u32 seq, u32 flags, u8 cmd)
1471 struct ovs_header *ovs_header;
1472 struct ovs_dp_stats dp_stats;
1473 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1476 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1481 ovs_header->dp_ifindex = get_dpifindex(dp);
1483 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1485 goto nla_put_failure;
1487 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1488 if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1489 &dp_stats, OVS_DP_ATTR_PAD))
1490 goto nla_put_failure;
1492 if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1493 sizeof(struct ovs_dp_megaflow_stats),
1494 &dp_megaflow_stats, OVS_DP_ATTR_PAD))
1495 goto nla_put_failure;
1497 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1498 goto nla_put_failure;
1500 genlmsg_end(skb, ovs_header);
1504 genlmsg_cancel(skb, ovs_header);
1509 static struct sk_buff *ovs_dp_cmd_alloc_info(void)
1511 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1514 /* Called with rcu_read_lock or ovs_mutex. */
1515 static struct datapath *lookup_datapath(struct net *net,
1516 const struct ovs_header *ovs_header,
1517 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1519 struct datapath *dp;
1521 if (!a[OVS_DP_ATTR_NAME])
1522 dp = get_dp(net, ovs_header->dp_ifindex);
1524 struct vport *vport;
1526 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1527 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1529 return dp ? dp : ERR_PTR(-ENODEV);
1532 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1534 struct datapath *dp;
1536 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1540 WARN(dp->user_features, "Dropping previously announced user features\n");
1541 dp->user_features = 0;
1544 static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1546 if (a[OVS_DP_ATTR_USER_FEATURES])
1547 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1550 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1552 struct nlattr **a = info->attrs;
1553 struct vport_parms parms;
1554 struct sk_buff *reply;
1555 struct datapath *dp;
1556 struct vport *vport;
1557 struct ovs_net *ovs_net;
1561 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1564 reply = ovs_dp_cmd_alloc_info();
1569 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1571 goto err_free_reply;
1573 ovs_dp_set_net(dp, sock_net(skb->sk));
1575 /* Allocate table. */
1576 err = ovs_flow_tbl_init(&dp->table);
1580 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1581 if (!dp->stats_percpu) {
1583 goto err_destroy_table;
1586 dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS,
1587 sizeof(struct hlist_head),
1591 goto err_destroy_percpu;
1594 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1595 INIT_HLIST_HEAD(&dp->ports[i]);
1597 err = ovs_meters_init(dp);
1599 goto err_destroy_ports_array;
1601 /* Set up our datapath device. */
1602 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1603 parms.type = OVS_VPORT_TYPE_INTERNAL;
1604 parms.options = NULL;
1606 parms.port_no = OVSP_LOCAL;
1607 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1609 ovs_dp_change(dp, a);
1611 /* So far only local changes have been made, now need the lock. */
1614 vport = new_vport(&parms);
1615 if (IS_ERR(vport)) {
1616 err = PTR_ERR(vport);
1620 if (err == -EEXIST) {
1621 /* An outdated user space instance that does not understand
1622 * the concept of user_features has attempted to create a new
1623 * datapath and is likely to reuse it. Drop all user features.
1625 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1626 ovs_dp_reset_user_features(skb, info);
1629 goto err_destroy_meters;
1632 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1633 info->snd_seq, 0, OVS_DP_CMD_NEW);
1636 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1637 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1641 ovs_notify(&dp_datapath_genl_family, reply, info);
1646 ovs_meters_exit(dp);
1647 err_destroy_ports_array:
1650 free_percpu(dp->stats_percpu);
1652 ovs_flow_tbl_destroy(&dp->table);
1661 /* Called with ovs_mutex. */
1662 static void __dp_destroy(struct datapath *dp)
1666 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1667 struct vport *vport;
1668 struct hlist_node *n;
1670 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1671 if (vport->port_no != OVSP_LOCAL)
1672 ovs_dp_detach_port(vport);
1675 list_del_rcu(&dp->list_node);
1677 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1678 * all ports in datapath are destroyed first before freeing datapath.
1680 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1682 /* RCU destroy the flow table */
1683 call_rcu(&dp->rcu, destroy_dp_rcu);
1686 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1688 struct sk_buff *reply;
1689 struct datapath *dp;
1692 reply = ovs_dp_cmd_alloc_info();
1697 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1700 goto err_unlock_free;
1702 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1703 info->snd_seq, 0, OVS_DP_CMD_DEL);
1709 ovs_notify(&dp_datapath_genl_family, reply, info);
1719 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1721 struct sk_buff *reply;
1722 struct datapath *dp;
1725 reply = ovs_dp_cmd_alloc_info();
1730 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1733 goto err_unlock_free;
1735 ovs_dp_change(dp, info->attrs);
1737 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1738 info->snd_seq, 0, OVS_DP_CMD_SET);
1742 ovs_notify(&dp_datapath_genl_family, reply, info);
1752 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1754 struct sk_buff *reply;
1755 struct datapath *dp;
1758 reply = ovs_dp_cmd_alloc_info();
1763 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1766 goto err_unlock_free;
1768 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1769 info->snd_seq, 0, OVS_DP_CMD_GET);
1773 return genlmsg_reply(reply, info);
1781 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1783 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1784 struct datapath *dp;
1785 int skip = cb->args[0];
1789 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1791 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1792 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1793 OVS_DP_CMD_GET) < 0)
1804 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1805 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1806 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1807 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1810 static const struct genl_ops dp_datapath_genl_ops[] = {
1811 { .cmd = OVS_DP_CMD_NEW,
1812 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1813 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1814 .doit = ovs_dp_cmd_new
1816 { .cmd = OVS_DP_CMD_DEL,
1817 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1818 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1819 .doit = ovs_dp_cmd_del
1821 { .cmd = OVS_DP_CMD_GET,
1822 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1823 .flags = 0, /* OK for unprivileged users. */
1824 .doit = ovs_dp_cmd_get,
1825 .dumpit = ovs_dp_cmd_dump
1827 { .cmd = OVS_DP_CMD_SET,
1828 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1829 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1830 .doit = ovs_dp_cmd_set,
1834 static struct genl_family dp_datapath_genl_family __ro_after_init = {
1835 .hdrsize = sizeof(struct ovs_header),
1836 .name = OVS_DATAPATH_FAMILY,
1837 .version = OVS_DATAPATH_VERSION,
1838 .maxattr = OVS_DP_ATTR_MAX,
1839 .policy = datapath_policy,
1841 .parallel_ops = true,
1842 .ops = dp_datapath_genl_ops,
1843 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1844 .mcgrps = &ovs_dp_datapath_multicast_group,
1846 .module = THIS_MODULE,
1849 /* Called with ovs_mutex or RCU read lock. */
1850 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1851 struct net *net, u32 portid, u32 seq,
1854 struct ovs_header *ovs_header;
1855 struct ovs_vport_stats vport_stats;
1858 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1863 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1865 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1866 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1867 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1868 ovs_vport_name(vport)) ||
1869 nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
1870 goto nla_put_failure;
1872 if (!net_eq(net, dev_net(vport->dev))) {
1873 int id = peernet2id_alloc(net, dev_net(vport->dev));
1875 if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
1876 goto nla_put_failure;
1879 ovs_vport_get_stats(vport, &vport_stats);
1880 if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
1881 sizeof(struct ovs_vport_stats), &vport_stats,
1882 OVS_VPORT_ATTR_PAD))
1883 goto nla_put_failure;
1885 if (ovs_vport_get_upcall_portids(vport, skb))
1886 goto nla_put_failure;
1888 err = ovs_vport_get_options(vport, skb);
1889 if (err == -EMSGSIZE)
1892 genlmsg_end(skb, ovs_header);
1898 genlmsg_cancel(skb, ovs_header);
1902 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1904 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1907 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1908 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
1909 u32 portid, u32 seq, u8 cmd)
1911 struct sk_buff *skb;
1914 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1916 return ERR_PTR(-ENOMEM);
1918 retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd);
1924 /* Called with ovs_mutex or RCU read lock. */
1925 static struct vport *lookup_vport(struct net *net,
1926 const struct ovs_header *ovs_header,
1927 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1929 struct datapath *dp;
1930 struct vport *vport;
1932 if (a[OVS_VPORT_ATTR_IFINDEX])
1933 return ERR_PTR(-EOPNOTSUPP);
1934 if (a[OVS_VPORT_ATTR_NAME]) {
1935 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1937 return ERR_PTR(-ENODEV);
1938 if (ovs_header->dp_ifindex &&
1939 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1940 return ERR_PTR(-ENODEV);
1942 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1943 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1945 if (port_no >= DP_MAX_PORTS)
1946 return ERR_PTR(-EFBIG);
1948 dp = get_dp(net, ovs_header->dp_ifindex);
1950 return ERR_PTR(-ENODEV);
1952 vport = ovs_vport_ovsl_rcu(dp, port_no);
1954 return ERR_PTR(-ENODEV);
1957 return ERR_PTR(-EINVAL);
1961 static unsigned int ovs_get_max_headroom(struct datapath *dp)
1963 unsigned int dev_headroom, max_headroom = 0;
1964 struct net_device *dev;
1965 struct vport *vport;
1968 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1969 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1971 dev_headroom = netdev_get_fwd_headroom(dev);
1972 if (dev_headroom > max_headroom)
1973 max_headroom = dev_headroom;
1977 return max_headroom;
1980 /* Called with ovs_mutex */
1981 static void ovs_update_headroom(struct datapath *dp, unsigned int new_headroom)
1983 struct vport *vport;
1986 dp->max_headroom = new_headroom;
1987 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1988 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
1989 netdev_set_rx_headroom(vport->dev, new_headroom);
1992 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1994 struct nlattr **a = info->attrs;
1995 struct ovs_header *ovs_header = info->userhdr;
1996 struct vport_parms parms;
1997 struct sk_buff *reply;
1998 struct vport *vport;
1999 struct datapath *dp;
2000 unsigned int new_headroom;
2004 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2005 !a[OVS_VPORT_ATTR_UPCALL_PID])
2007 if (a[OVS_VPORT_ATTR_IFINDEX])
2010 port_no = a[OVS_VPORT_ATTR_PORT_NO]
2011 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
2012 if (port_no >= DP_MAX_PORTS)
2015 reply = ovs_vport_cmd_alloc_info();
2021 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2024 goto exit_unlock_free;
2027 vport = ovs_vport_ovsl(dp, port_no);
2030 goto exit_unlock_free;
2032 for (port_no = 1; ; port_no++) {
2033 if (port_no >= DP_MAX_PORTS) {
2035 goto exit_unlock_free;
2037 vport = ovs_vport_ovsl(dp, port_no);
2043 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2044 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2045 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2047 parms.port_no = port_no;
2048 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
2050 vport = new_vport(&parms);
2051 err = PTR_ERR(vport);
2052 if (IS_ERR(vport)) {
2055 goto exit_unlock_free;
2058 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2059 info->snd_portid, info->snd_seq, 0,
2062 new_headroom = netdev_get_fwd_headroom(vport->dev);
2064 if (new_headroom > dp->max_headroom)
2065 ovs_update_headroom(dp, new_headroom);
2067 netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2072 ovs_notify(&dp_vport_genl_family, reply, info);
2081 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2083 struct nlattr **a = info->attrs;
2084 struct sk_buff *reply;
2085 struct vport *vport;
2088 reply = ovs_vport_cmd_alloc_info();
2093 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2094 err = PTR_ERR(vport);
2096 goto exit_unlock_free;
2098 if (a[OVS_VPORT_ATTR_TYPE] &&
2099 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2101 goto exit_unlock_free;
2104 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2105 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2107 goto exit_unlock_free;
2111 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2112 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2114 err = ovs_vport_set_upcall_portids(vport, ids);
2116 goto exit_unlock_free;
2119 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2120 info->snd_portid, info->snd_seq, 0,
2125 ovs_notify(&dp_vport_genl_family, reply, info);
2134 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2136 bool update_headroom = false;
2137 struct nlattr **a = info->attrs;
2138 struct sk_buff *reply;
2139 struct datapath *dp;
2140 struct vport *vport;
2141 unsigned int new_headroom;
2144 reply = ovs_vport_cmd_alloc_info();
2149 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2150 err = PTR_ERR(vport);
2152 goto exit_unlock_free;
2154 if (vport->port_no == OVSP_LOCAL) {
2156 goto exit_unlock_free;
2159 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2160 info->snd_portid, info->snd_seq, 0,
2164 /* the vport deletion may trigger dp headroom update */
2166 if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
2167 update_headroom = true;
2169 netdev_reset_rx_headroom(vport->dev);
2170 ovs_dp_detach_port(vport);
2172 if (update_headroom) {
2173 new_headroom = ovs_get_max_headroom(dp);
2175 if (new_headroom < dp->max_headroom)
2176 ovs_update_headroom(dp, new_headroom);
2180 ovs_notify(&dp_vport_genl_family, reply, info);
2189 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2191 struct nlattr **a = info->attrs;
2192 struct ovs_header *ovs_header = info->userhdr;
2193 struct sk_buff *reply;
2194 struct vport *vport;
2197 reply = ovs_vport_cmd_alloc_info();
2202 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2203 err = PTR_ERR(vport);
2205 goto exit_unlock_free;
2206 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2207 info->snd_portid, info->snd_seq, 0,
2212 return genlmsg_reply(reply, info);
2220 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2222 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2223 struct datapath *dp;
2224 int bucket = cb->args[0], skip = cb->args[1];
2228 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2233 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2234 struct vport *vport;
2237 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2239 ovs_vport_cmd_fill_info(vport, skb,
2241 NETLINK_CB(cb->skb).portid,
2244 OVS_VPORT_CMD_GET) < 0)
2260 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2261 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2262 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2263 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2264 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2265 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2266 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2267 [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
2268 [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
2271 static const struct genl_ops dp_vport_genl_ops[] = {
2272 { .cmd = OVS_VPORT_CMD_NEW,
2273 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2274 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2275 .doit = ovs_vport_cmd_new
2277 { .cmd = OVS_VPORT_CMD_DEL,
2278 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2279 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2280 .doit = ovs_vport_cmd_del
2282 { .cmd = OVS_VPORT_CMD_GET,
2283 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2284 .flags = 0, /* OK for unprivileged users. */
2285 .doit = ovs_vport_cmd_get,
2286 .dumpit = ovs_vport_cmd_dump
2288 { .cmd = OVS_VPORT_CMD_SET,
2289 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2290 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2291 .doit = ovs_vport_cmd_set,
2295 struct genl_family dp_vport_genl_family __ro_after_init = {
2296 .hdrsize = sizeof(struct ovs_header),
2297 .name = OVS_VPORT_FAMILY,
2298 .version = OVS_VPORT_VERSION,
2299 .maxattr = OVS_VPORT_ATTR_MAX,
2300 .policy = vport_policy,
2302 .parallel_ops = true,
2303 .ops = dp_vport_genl_ops,
2304 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2305 .mcgrps = &ovs_dp_vport_multicast_group,
2307 .module = THIS_MODULE,
2310 static struct genl_family * const dp_genl_families[] = {
2311 &dp_datapath_genl_family,
2312 &dp_vport_genl_family,
2313 &dp_flow_genl_family,
2314 &dp_packet_genl_family,
2315 &dp_meter_genl_family,
2316 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2317 &dp_ct_limit_genl_family,
2321 static void dp_unregister_genl(int n_families)
2325 for (i = 0; i < n_families; i++)
2326 genl_unregister_family(dp_genl_families[i]);
2329 static int __init dp_register_genl(void)
2334 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2336 err = genl_register_family(dp_genl_families[i]);
2344 dp_unregister_genl(i);
2348 static int __net_init ovs_init_net(struct net *net)
2350 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2352 INIT_LIST_HEAD(&ovs_net->dps);
2353 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2354 return ovs_ct_init(net);
2357 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2358 struct list_head *head)
2360 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2361 struct datapath *dp;
2363 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2366 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2367 struct vport *vport;
2369 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2370 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2373 if (dev_net(vport->dev) == dnet)
2374 list_add(&vport->detach_list, head);
2380 static void __net_exit ovs_exit_net(struct net *dnet)
2382 struct datapath *dp, *dp_next;
2383 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2384 struct vport *vport, *vport_next;
2390 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2393 down_read(&net_rwsem);
2395 list_vports_from_net(net, dnet, &head);
2396 up_read(&net_rwsem);
2398 /* Detach all vports from given namespace. */
2399 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2400 list_del(&vport->detach_list);
2401 ovs_dp_detach_port(vport);
2406 cancel_work_sync(&ovs_net->dp_notify_work);
2409 static struct pernet_operations ovs_net_ops = {
2410 .init = ovs_init_net,
2411 .exit = ovs_exit_net,
2413 .size = sizeof(struct ovs_net),
2416 static int __init dp_init(void)
2420 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2422 pr_info("Open vSwitch switching datapath\n");
2424 err = action_fifos_init();
2428 err = ovs_internal_dev_rtnl_link_register();
2430 goto error_action_fifos_exit;
2432 err = ovs_flow_init();
2434 goto error_unreg_rtnl_link;
2436 err = ovs_vport_init();
2438 goto error_flow_exit;
2440 err = register_pernet_device(&ovs_net_ops);
2442 goto error_vport_exit;
2444 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2446 goto error_netns_exit;
2448 err = ovs_netdev_init();
2450 goto error_unreg_notifier;
2452 err = dp_register_genl();
2454 goto error_unreg_netdev;
2460 error_unreg_notifier:
2461 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2463 unregister_pernet_device(&ovs_net_ops);
2468 error_unreg_rtnl_link:
2469 ovs_internal_dev_rtnl_link_unregister();
2470 error_action_fifos_exit:
2471 action_fifos_exit();
2476 static void dp_cleanup(void)
2478 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2480 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2481 unregister_pernet_device(&ovs_net_ops);
2485 ovs_internal_dev_rtnl_link_unregister();
2486 action_fifos_exit();
2489 module_init(dp_init);
2490 module_exit(dp_cleanup);
2492 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2493 MODULE_LICENSE("GPL");
2494 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
2495 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
2496 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
2497 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
2498 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);
2499 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY);