2 * Copyright (c) 2007-2014 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.h>
27 #include <linux/jhash.h>
28 #include <linux/delay.h>
29 #include <linux/time.h>
30 #include <linux/etherdevice.h>
31 #include <linux/genetlink.h>
32 #include <linux/kernel.h>
33 #include <linux/kthread.h>
34 #include <linux/mutex.h>
35 #include <linux/percpu.h>
36 #include <linux/rcupdate.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/ethtool.h>
40 #include <linux/wait.h>
41 #include <asm/div64.h>
42 #include <linux/highmem.h>
43 #include <linux/netfilter_bridge.h>
44 #include <linux/netfilter_ipv4.h>
45 #include <linux/inetdevice.h>
46 #include <linux/list.h>
47 #include <linux/openvswitch.h>
48 #include <linux/rculist.h>
49 #include <linux/dmi.h>
50 #include <net/genetlink.h>
51 #include <net/net_namespace.h>
52 #include <net/netns/generic.h>
56 #include "flow_table.h"
57 #include "flow_netlink.h"
59 #include "vport-internal_dev.h"
60 #include "vport-netdev.h"
62 unsigned int ovs_net_id __read_mostly;
64 static struct genl_family dp_packet_genl_family;
65 static struct genl_family dp_flow_genl_family;
66 static struct genl_family dp_datapath_genl_family;
68 static const struct nla_policy flow_policy[];
70 static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
71 .name = OVS_FLOW_MCGROUP,
74 static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
75 .name = OVS_DATAPATH_MCGROUP,
78 static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
79 .name = OVS_VPORT_MCGROUP,
82 /* Check if need to build a reply message.
83 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
84 static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
87 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
88 genl_has_listeners(family, genl_info_net(info), group);
91 static void ovs_notify(struct genl_family *family,
92 struct sk_buff *skb, struct genl_info *info)
94 genl_notify(family, skb, info, 0, GFP_KERNEL);
100 * All writes e.g. Writes to device state (add/remove datapath, port, set
101 * operations on vports, etc.), Writes to other state (flow table
102 * modifications, set miscellaneous datapath parameters, etc.) are protected
105 * Reads are protected by RCU.
107 * There are a few special cases (mostly stats) that have their own
108 * synchronization but they nest under all of above and don't interact with
111 * The RTNL lock nests inside ovs_mutex.
114 static DEFINE_MUTEX(ovs_mutex);
118 mutex_lock(&ovs_mutex);
121 void ovs_unlock(void)
123 mutex_unlock(&ovs_mutex);
126 #ifdef CONFIG_LOCKDEP
127 int lockdep_ovsl_is_held(void)
130 return lockdep_is_held(&ovs_mutex);
136 static struct vport *new_vport(const struct vport_parms *);
137 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
138 const struct sw_flow_key *,
139 const struct dp_upcall_info *,
141 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
142 const struct sw_flow_key *,
143 const struct dp_upcall_info *,
146 /* Must be called with rcu_read_lock or ovs_mutex. */
147 const char *ovs_dp_name(const struct datapath *dp)
149 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
150 return ovs_vport_name(vport);
153 static int get_dpifindex(const struct datapath *dp)
160 local = ovs_vport_rcu(dp, OVSP_LOCAL);
162 ifindex = local->dev->ifindex;
171 static void destroy_dp_rcu(struct rcu_head *rcu)
173 struct datapath *dp = container_of(rcu, struct datapath, rcu);
175 ovs_flow_tbl_destroy(&dp->table);
176 free_percpu(dp->stats_percpu);
182 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
185 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
188 /* Called with ovs_mutex or RCU read lock. */
189 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
192 struct hlist_head *head;
194 head = vport_hash_bucket(dp, port_no);
195 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
196 if (vport->port_no == port_no)
202 /* Called with ovs_mutex. */
203 static struct vport *new_vport(const struct vport_parms *parms)
207 vport = ovs_vport_add(parms);
208 if (!IS_ERR(vport)) {
209 struct datapath *dp = parms->dp;
210 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
212 hlist_add_head_rcu(&vport->dp_hash_node, head);
217 void ovs_dp_detach_port(struct vport *p)
221 /* First drop references to device. */
222 hlist_del_rcu(&p->dp_hash_node);
224 /* Then destroy it. */
228 /* Must be called with rcu_read_lock. */
229 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
231 const struct vport *p = OVS_CB(skb)->input_vport;
232 struct datapath *dp = p->dp;
233 struct sw_flow *flow;
234 struct sw_flow_actions *sf_acts;
235 struct dp_stats_percpu *stats;
239 stats = this_cpu_ptr(dp->stats_percpu);
242 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
243 if (unlikely(!flow)) {
244 struct dp_upcall_info upcall;
247 memset(&upcall, 0, sizeof(upcall));
248 upcall.cmd = OVS_PACKET_CMD_MISS;
249 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
250 upcall.mru = OVS_CB(skb)->mru;
251 error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
263 stats_counter = &stats->n_missed;
267 ovs_flow_stats_update(flow, key->tp.flags, skb);
268 sf_acts = rcu_dereference(flow->sf_acts);
269 ovs_execute_actions(dp, skb, sf_acts, key);
271 stats_counter = &stats->n_hit;
274 /* Update datapath statistics. */
275 u64_stats_update_begin(&stats->syncp);
277 stats->n_mask_hit += n_mask_hit;
278 u64_stats_update_end(&stats->syncp);
281 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
282 const struct sw_flow_key *key,
283 const struct dp_upcall_info *upcall_info,
286 struct dp_stats_percpu *stats;
289 if (upcall_info->portid == 0) {
294 if (!skb_is_gso(skb))
295 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
297 err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
304 stats = this_cpu_ptr(dp->stats_percpu);
306 u64_stats_update_begin(&stats->syncp);
308 u64_stats_update_end(&stats->syncp);
313 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
314 const struct sw_flow_key *key,
315 const struct dp_upcall_info *upcall_info,
318 unsigned int gso_type = skb_shinfo(skb)->gso_type;
319 struct sw_flow_key later_key;
320 struct sk_buff *segs, *nskb;
323 BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_SGO_CB_OFFSET);
324 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
326 return PTR_ERR(segs);
330 if (gso_type & SKB_GSO_UDP) {
331 /* The initial flow key extracted by ovs_flow_key_extract()
332 * in this case is for a first fragment, so we need to
333 * properly mark later fragments.
336 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
339 /* Queue all of the segments. */
342 if (gso_type & SKB_GSO_UDP && skb != segs)
345 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
349 } while ((skb = skb->next));
351 /* Free all of the segments. */
359 } while ((skb = nskb));
363 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
364 unsigned int hdrlen, int actions_attrlen)
366 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
367 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
368 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
369 + nla_total_size(sizeof(unsigned int)); /* OVS_PACKET_ATTR_LEN */
371 /* OVS_PACKET_ATTR_USERDATA */
372 if (upcall_info->userdata)
373 size += NLA_ALIGN(upcall_info->userdata->nla_len);
375 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
376 if (upcall_info->egress_tun_info)
377 size += nla_total_size(ovs_tun_key_attr_size());
379 /* OVS_PACKET_ATTR_ACTIONS */
380 if (upcall_info->actions_len)
381 size += nla_total_size(actions_attrlen);
383 /* OVS_PACKET_ATTR_MRU */
384 if (upcall_info->mru)
385 size += nla_total_size(sizeof(upcall_info->mru));
390 static void pad_packet(struct datapath *dp, struct sk_buff *skb)
392 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
393 size_t plen = NLA_ALIGN(skb->len) - skb->len;
396 skb_put_zero(skb, plen);
400 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
401 const struct sw_flow_key *key,
402 const struct dp_upcall_info *upcall_info,
405 struct ovs_header *upcall;
406 struct sk_buff *nskb = NULL;
407 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
413 dp_ifindex = get_dpifindex(dp);
417 if (skb_vlan_tag_present(skb)) {
418 nskb = skb_clone(skb, GFP_ATOMIC);
422 nskb = __vlan_hwaccel_push_inside(nskb);
429 if (nla_attr_size(skb->len) > USHRT_MAX) {
434 /* Complete checksum if needed */
435 if (skb->ip_summed == CHECKSUM_PARTIAL &&
436 (err = skb_csum_hwoffload_help(skb, 0)))
439 /* Older versions of OVS user space enforce alignment of the last
440 * Netlink attribute to NLA_ALIGNTO which would require extensive
441 * padding logic. Only perform zerocopy if padding is not required.
443 if (dp->user_features & OVS_DP_F_UNALIGNED)
444 hlen = skb_zerocopy_headlen(skb);
448 len = upcall_msg_size(upcall_info, hlen - cutlen,
449 OVS_CB(skb)->acts_origlen);
450 user_skb = genlmsg_new(len, GFP_ATOMIC);
456 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
457 0, upcall_info->cmd);
458 upcall->dp_ifindex = dp_ifindex;
460 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
463 if (upcall_info->userdata)
464 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
465 nla_len(upcall_info->userdata),
466 nla_data(upcall_info->userdata));
468 if (upcall_info->egress_tun_info) {
469 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
470 err = ovs_nla_put_tunnel_info(user_skb,
471 upcall_info->egress_tun_info);
473 nla_nest_end(user_skb, nla);
476 if (upcall_info->actions_len) {
477 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS);
478 err = ovs_nla_put_actions(upcall_info->actions,
479 upcall_info->actions_len,
482 nla_nest_end(user_skb, nla);
484 nla_nest_cancel(user_skb, nla);
487 /* Add OVS_PACKET_ATTR_MRU */
488 if (upcall_info->mru) {
489 if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
494 pad_packet(dp, user_skb);
497 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
499 if (nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN,
504 pad_packet(dp, user_skb);
507 /* Only reserve room for attribute header, packet data is added
508 * in skb_zerocopy() */
509 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
513 nla->nla_len = nla_attr_size(skb->len - cutlen);
515 err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
519 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
520 pad_packet(dp, user_skb);
522 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
524 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
529 consume_skb(user_skb);
535 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
537 struct ovs_header *ovs_header = info->userhdr;
538 struct net *net = sock_net(skb->sk);
539 struct nlattr **a = info->attrs;
540 struct sw_flow_actions *acts;
541 struct sk_buff *packet;
542 struct sw_flow *flow;
543 struct sw_flow_actions *sf_acts;
545 struct vport *input_vport;
549 bool log = !a[OVS_PACKET_ATTR_PROBE];
552 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
553 !a[OVS_PACKET_ATTR_ACTIONS])
556 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
557 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
561 skb_reserve(packet, NET_IP_ALIGN);
563 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
565 /* Set packet's mru */
566 if (a[OVS_PACKET_ATTR_MRU]) {
567 mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
568 packet->ignore_df = 1;
570 OVS_CB(packet)->mru = mru;
572 /* Build an sw_flow for sending this packet. */
573 flow = ovs_flow_alloc();
578 err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
579 packet, &flow->key, log);
583 err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
584 &flow->key, &acts, log);
588 rcu_assign_pointer(flow->sf_acts, acts);
589 packet->priority = flow->key.phy.priority;
590 packet->mark = flow->key.phy.skb_mark;
593 dp = get_dp_rcu(net, ovs_header->dp_ifindex);
598 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
600 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
605 packet->dev = input_vport->dev;
606 OVS_CB(packet)->input_vport = input_vport;
607 sf_acts = rcu_dereference(flow->sf_acts);
610 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
614 ovs_flow_free(flow, false);
620 ovs_flow_free(flow, false);
627 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
628 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
629 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
630 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
631 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
632 [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
635 static const struct genl_ops dp_packet_genl_ops[] = {
636 { .cmd = OVS_PACKET_CMD_EXECUTE,
637 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
638 .policy = packet_policy,
639 .doit = ovs_packet_cmd_execute
643 static struct genl_family dp_packet_genl_family __ro_after_init = {
644 .hdrsize = sizeof(struct ovs_header),
645 .name = OVS_PACKET_FAMILY,
646 .version = OVS_PACKET_VERSION,
647 .maxattr = OVS_PACKET_ATTR_MAX,
649 .parallel_ops = true,
650 .ops = dp_packet_genl_ops,
651 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
652 .module = THIS_MODULE,
655 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
656 struct ovs_dp_megaflow_stats *mega_stats)
660 memset(mega_stats, 0, sizeof(*mega_stats));
662 stats->n_flows = ovs_flow_tbl_count(&dp->table);
663 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
665 stats->n_hit = stats->n_missed = stats->n_lost = 0;
667 for_each_possible_cpu(i) {
668 const struct dp_stats_percpu *percpu_stats;
669 struct dp_stats_percpu local_stats;
672 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
675 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
676 local_stats = *percpu_stats;
677 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
679 stats->n_hit += local_stats.n_hit;
680 stats->n_missed += local_stats.n_missed;
681 stats->n_lost += local_stats.n_lost;
682 mega_stats->n_mask_hit += local_stats.n_mask_hit;
686 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
688 return ovs_identifier_is_ufid(sfid) &&
689 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
692 static bool should_fill_mask(uint32_t ufid_flags)
694 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
697 static bool should_fill_actions(uint32_t ufid_flags)
699 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
702 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
703 const struct sw_flow_id *sfid,
706 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
708 /* OVS_FLOW_ATTR_UFID, or unmasked flow key as fallback
709 * see ovs_nla_put_identifier()
711 if (sfid && ovs_identifier_is_ufid(sfid))
712 len += nla_total_size(sfid->ufid_len);
714 len += nla_total_size(ovs_key_attr_size());
716 /* OVS_FLOW_ATTR_KEY */
717 if (!sfid || should_fill_key(sfid, ufid_flags))
718 len += nla_total_size(ovs_key_attr_size());
720 /* OVS_FLOW_ATTR_MASK */
721 if (should_fill_mask(ufid_flags))
722 len += nla_total_size(ovs_key_attr_size());
724 /* OVS_FLOW_ATTR_ACTIONS */
725 if (should_fill_actions(ufid_flags))
726 len += nla_total_size(acts->orig_len);
729 + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
730 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
731 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
734 /* Called with ovs_mutex or RCU read lock. */
735 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
738 struct ovs_flow_stats stats;
742 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
745 nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
749 if (stats.n_packets &&
750 nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
751 sizeof(struct ovs_flow_stats), &stats,
755 if ((u8)ntohs(tcp_flags) &&
756 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
762 /* Called with ovs_mutex or RCU read lock. */
763 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
764 struct sk_buff *skb, int skb_orig_len)
766 struct nlattr *start;
769 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
770 * this is the first flow to be dumped into 'skb'. This is unusual for
771 * Netlink but individual action lists can be longer than
772 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
773 * The userspace caller can always fetch the actions separately if it
774 * really wants them. (Most userspace callers in fact don't care.)
776 * This can only fail for dump operations because the skb is always
777 * properly sized for single flows.
779 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
781 const struct sw_flow_actions *sf_acts;
783 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
784 err = ovs_nla_put_actions(sf_acts->actions,
785 sf_acts->actions_len, skb);
788 nla_nest_end(skb, start);
793 nla_nest_cancel(skb, start);
795 } else if (skb_orig_len) {
802 /* Called with ovs_mutex or RCU read lock. */
803 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
804 struct sk_buff *skb, u32 portid,
805 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
807 const int skb_orig_len = skb->len;
808 struct ovs_header *ovs_header;
811 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
816 ovs_header->dp_ifindex = dp_ifindex;
818 err = ovs_nla_put_identifier(flow, skb);
822 if (should_fill_key(&flow->id, ufid_flags)) {
823 err = ovs_nla_put_masked_key(flow, skb);
828 if (should_fill_mask(ufid_flags)) {
829 err = ovs_nla_put_mask(flow, skb);
834 err = ovs_flow_cmd_fill_stats(flow, skb);
838 if (should_fill_actions(ufid_flags)) {
839 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
844 genlmsg_end(skb, ovs_header);
848 genlmsg_cancel(skb, ovs_header);
852 /* May not be called with RCU read lock. */
853 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
854 const struct sw_flow_id *sfid,
855 struct genl_info *info,
862 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
865 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
866 skb = genlmsg_new(len, GFP_KERNEL);
868 return ERR_PTR(-ENOMEM);
873 /* Called with ovs_mutex. */
874 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
876 struct genl_info *info, u8 cmd,
877 bool always, u32 ufid_flags)
882 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
883 &flow->id, info, always, ufid_flags);
884 if (IS_ERR_OR_NULL(skb))
887 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
888 info->snd_portid, info->snd_seq, 0,
890 if (WARN_ON_ONCE(retval < 0)) {
892 skb = ERR_PTR(retval);
897 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
899 struct net *net = sock_net(skb->sk);
900 struct nlattr **a = info->attrs;
901 struct ovs_header *ovs_header = info->userhdr;
902 struct sw_flow *flow = NULL, *new_flow;
903 struct sw_flow_mask mask;
904 struct sk_buff *reply;
906 struct sw_flow_actions *acts;
907 struct sw_flow_match match;
908 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
910 bool log = !a[OVS_FLOW_ATTR_PROBE];
912 /* Must have key and actions. */
914 if (!a[OVS_FLOW_ATTR_KEY]) {
915 OVS_NLERR(log, "Flow key attr not present in new flow.");
918 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
919 OVS_NLERR(log, "Flow actions attr not present in new flow.");
923 /* Most of the time we need to allocate a new flow, do it before
926 new_flow = ovs_flow_alloc();
927 if (IS_ERR(new_flow)) {
928 error = PTR_ERR(new_flow);
933 ovs_match_init(&match, &new_flow->key, false, &mask);
934 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
935 a[OVS_FLOW_ATTR_MASK], log);
939 /* Extract flow identifier. */
940 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
941 &new_flow->key, log);
945 /* unmasked key is needed to match when ufid is not used. */
946 if (ovs_identifier_is_key(&new_flow->id))
947 match.key = new_flow->id.unmasked_key;
949 ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask);
951 /* Validate actions. */
952 error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
953 &new_flow->key, &acts, log);
955 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
959 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
962 error = PTR_ERR(reply);
967 dp = get_dp(net, ovs_header->dp_ifindex);
973 /* Check if this is a duplicate flow */
974 if (ovs_identifier_is_ufid(&new_flow->id))
975 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
977 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key);
979 rcu_assign_pointer(new_flow->sf_acts, acts);
981 /* Put flow in bucket. */
982 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
983 if (unlikely(error)) {
988 if (unlikely(reply)) {
989 error = ovs_flow_cmd_fill_info(new_flow,
990 ovs_header->dp_ifindex,
991 reply, info->snd_portid,
999 struct sw_flow_actions *old_acts;
1001 /* Bail out if we're not allowed to modify an existing flow.
1002 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1003 * because Generic Netlink treats the latter as a dump
1004 * request. We also accept NLM_F_EXCL in case that bug ever
1007 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
1010 goto err_unlock_ovs;
1012 /* The flow identifier has to be the same for flow updates.
1013 * Look for any overlapping flow.
1015 if (unlikely(!ovs_flow_cmp(flow, &match))) {
1016 if (ovs_identifier_is_key(&flow->id))
1017 flow = ovs_flow_tbl_lookup_exact(&dp->table,
1019 else /* UFID matches but key is different */
1023 goto err_unlock_ovs;
1026 /* Update actions. */
1027 old_acts = ovsl_dereference(flow->sf_acts);
1028 rcu_assign_pointer(flow->sf_acts, acts);
1030 if (unlikely(reply)) {
1031 error = ovs_flow_cmd_fill_info(flow,
1032 ovs_header->dp_ifindex,
1033 reply, info->snd_portid,
1041 ovs_nla_free_flow_actions_rcu(old_acts);
1042 ovs_flow_free(new_flow, false);
1046 ovs_notify(&dp_flow_genl_family, reply, info);
1053 ovs_nla_free_flow_actions(acts);
1055 ovs_flow_free(new_flow, false);
1060 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1061 static struct sw_flow_actions *get_flow_actions(struct net *net,
1062 const struct nlattr *a,
1063 const struct sw_flow_key *key,
1064 const struct sw_flow_mask *mask,
1067 struct sw_flow_actions *acts;
1068 struct sw_flow_key masked_key;
1071 ovs_flow_mask_key(&masked_key, key, true, mask);
1072 error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
1075 "Actions may not be safe on all matching packets");
1076 return ERR_PTR(error);
1082 /* Factor out match-init and action-copy to avoid
1083 * "Wframe-larger-than=1024" warning. Because mask is only
1084 * used to get actions, we new a function to save some
1087 * If there are not key and action attrs, we return 0
1088 * directly. In the case, the caller will also not use the
1089 * match as before. If there is action attr, we try to get
1090 * actions and save them to *acts. Before returning from
1091 * the function, we reset the match->mask pointer. Because
1092 * we should not to return match object with dangling reference
1095 static int ovs_nla_init_match_and_action(struct net *net,
1096 struct sw_flow_match *match,
1097 struct sw_flow_key *key,
1099 struct sw_flow_actions **acts,
1102 struct sw_flow_mask mask;
1105 if (a[OVS_FLOW_ATTR_KEY]) {
1106 ovs_match_init(match, key, true, &mask);
1107 error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
1108 a[OVS_FLOW_ATTR_MASK], log);
1113 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1114 if (!a[OVS_FLOW_ATTR_KEY]) {
1116 "Flow key attribute not present in set flow.");
1121 *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
1123 if (IS_ERR(*acts)) {
1124 error = PTR_ERR(*acts);
1129 /* On success, error is 0. */
1135 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1137 struct net *net = sock_net(skb->sk);
1138 struct nlattr **a = info->attrs;
1139 struct ovs_header *ovs_header = info->userhdr;
1140 struct sw_flow_key key;
1141 struct sw_flow *flow;
1142 struct sk_buff *reply = NULL;
1143 struct datapath *dp;
1144 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1145 struct sw_flow_match match;
1146 struct sw_flow_id sfid;
1147 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1149 bool log = !a[OVS_FLOW_ATTR_PROBE];
1152 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1153 if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
1155 "Flow set message rejected, Key attribute missing.");
1159 error = ovs_nla_init_match_and_action(net, &match, &key, a,
1165 /* Can allocate before locking if have acts. */
1166 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1168 if (IS_ERR(reply)) {
1169 error = PTR_ERR(reply);
1170 goto err_kfree_acts;
1175 dp = get_dp(net, ovs_header->dp_ifindex);
1176 if (unlikely(!dp)) {
1178 goto err_unlock_ovs;
1180 /* Check that the flow exists. */
1182 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1184 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1185 if (unlikely(!flow)) {
1187 goto err_unlock_ovs;
1190 /* Update actions, if present. */
1192 old_acts = ovsl_dereference(flow->sf_acts);
1193 rcu_assign_pointer(flow->sf_acts, acts);
1195 if (unlikely(reply)) {
1196 error = ovs_flow_cmd_fill_info(flow,
1197 ovs_header->dp_ifindex,
1198 reply, info->snd_portid,
1205 /* Could not alloc without acts before locking. */
1206 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1207 info, OVS_FLOW_CMD_NEW, false,
1210 if (IS_ERR(reply)) {
1211 error = PTR_ERR(reply);
1212 goto err_unlock_ovs;
1217 if (a[OVS_FLOW_ATTR_CLEAR])
1218 ovs_flow_stats_clear(flow);
1222 ovs_notify(&dp_flow_genl_family, reply, info);
1224 ovs_nla_free_flow_actions_rcu(old_acts);
1232 ovs_nla_free_flow_actions(acts);
1237 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1239 struct nlattr **a = info->attrs;
1240 struct ovs_header *ovs_header = info->userhdr;
1241 struct net *net = sock_net(skb->sk);
1242 struct sw_flow_key key;
1243 struct sk_buff *reply;
1244 struct sw_flow *flow;
1245 struct datapath *dp;
1246 struct sw_flow_match match;
1247 struct sw_flow_id ufid;
1248 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1250 bool log = !a[OVS_FLOW_ATTR_PROBE];
1253 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1254 if (a[OVS_FLOW_ATTR_KEY]) {
1255 ovs_match_init(&match, &key, true, NULL);
1256 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
1258 } else if (!ufid_present) {
1260 "Flow get message rejected, Key attribute missing.");
1267 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1274 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1276 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1282 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1283 OVS_FLOW_CMD_NEW, true, ufid_flags);
1284 if (IS_ERR(reply)) {
1285 err = PTR_ERR(reply);
1290 return genlmsg_reply(reply, info);
1296 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1298 struct nlattr **a = info->attrs;
1299 struct ovs_header *ovs_header = info->userhdr;
1300 struct net *net = sock_net(skb->sk);
1301 struct sw_flow_key key;
1302 struct sk_buff *reply;
1303 struct sw_flow *flow = NULL;
1304 struct datapath *dp;
1305 struct sw_flow_match match;
1306 struct sw_flow_id ufid;
1307 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1309 bool log = !a[OVS_FLOW_ATTR_PROBE];
1312 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1313 if (a[OVS_FLOW_ATTR_KEY]) {
1314 ovs_match_init(&match, &key, true, NULL);
1315 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1322 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1323 if (unlikely(!dp)) {
1328 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1329 err = ovs_flow_tbl_flush(&dp->table);
1334 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1336 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1337 if (unlikely(!flow)) {
1342 ovs_flow_tbl_remove(&dp->table, flow);
1345 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1346 &flow->id, info, false, ufid_flags);
1347 if (likely(reply)) {
1348 if (likely(!IS_ERR(reply))) {
1349 rcu_read_lock(); /*To keep RCU checker happy. */
1350 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1351 reply, info->snd_portid,
1356 if (WARN_ON_ONCE(err < 0)) {
1361 ovs_notify(&dp_flow_genl_family, reply, info);
1363 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1368 ovs_flow_free(flow, true);
1375 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1377 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1378 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1379 struct table_instance *ti;
1380 struct datapath *dp;
1384 err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
1385 OVS_FLOW_ATTR_MAX, flow_policy, NULL);
1388 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1391 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1397 ti = rcu_dereference(dp->table.ti);
1399 struct sw_flow *flow;
1402 bucket = cb->args[0];
1404 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1408 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1409 NETLINK_CB(cb->skb).portid,
1410 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1411 OVS_FLOW_CMD_NEW, ufid_flags) < 0)
1414 cb->args[0] = bucket;
1421 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1422 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1423 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1424 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1425 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1426 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1427 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1428 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1431 static const struct genl_ops dp_flow_genl_ops[] = {
1432 { .cmd = OVS_FLOW_CMD_NEW,
1433 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1434 .policy = flow_policy,
1435 .doit = ovs_flow_cmd_new
1437 { .cmd = OVS_FLOW_CMD_DEL,
1438 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1439 .policy = flow_policy,
1440 .doit = ovs_flow_cmd_del
1442 { .cmd = OVS_FLOW_CMD_GET,
1443 .flags = 0, /* OK for unprivileged users. */
1444 .policy = flow_policy,
1445 .doit = ovs_flow_cmd_get,
1446 .dumpit = ovs_flow_cmd_dump
1448 { .cmd = OVS_FLOW_CMD_SET,
1449 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1450 .policy = flow_policy,
1451 .doit = ovs_flow_cmd_set,
1455 static struct genl_family dp_flow_genl_family __ro_after_init = {
1456 .hdrsize = sizeof(struct ovs_header),
1457 .name = OVS_FLOW_FAMILY,
1458 .version = OVS_FLOW_VERSION,
1459 .maxattr = OVS_FLOW_ATTR_MAX,
1461 .parallel_ops = true,
1462 .ops = dp_flow_genl_ops,
1463 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1464 .mcgrps = &ovs_dp_flow_multicast_group,
1466 .module = THIS_MODULE,
1469 static size_t ovs_dp_cmd_msg_size(void)
1471 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1473 msgsize += nla_total_size(IFNAMSIZ);
1474 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
1475 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
1476 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1481 /* Called with ovs_mutex. */
1482 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1483 u32 portid, u32 seq, u32 flags, u8 cmd)
1485 struct ovs_header *ovs_header;
1486 struct ovs_dp_stats dp_stats;
1487 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1490 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1495 ovs_header->dp_ifindex = get_dpifindex(dp);
1497 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1499 goto nla_put_failure;
1501 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1502 if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1503 &dp_stats, OVS_DP_ATTR_PAD))
1504 goto nla_put_failure;
1506 if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1507 sizeof(struct ovs_dp_megaflow_stats),
1508 &dp_megaflow_stats, OVS_DP_ATTR_PAD))
1509 goto nla_put_failure;
1511 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1512 goto nla_put_failure;
1514 genlmsg_end(skb, ovs_header);
1518 genlmsg_cancel(skb, ovs_header);
1523 static struct sk_buff *ovs_dp_cmd_alloc_info(void)
1525 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1528 /* Called with rcu_read_lock or ovs_mutex. */
1529 static struct datapath *lookup_datapath(struct net *net,
1530 const struct ovs_header *ovs_header,
1531 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1533 struct datapath *dp;
1535 if (!a[OVS_DP_ATTR_NAME])
1536 dp = get_dp(net, ovs_header->dp_ifindex);
1538 struct vport *vport;
1540 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1541 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1543 return dp ? dp : ERR_PTR(-ENODEV);
1546 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1548 struct datapath *dp;
1550 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1554 pr_warn("%s: Dropping previously announced user features\n",
1556 dp->user_features = 0;
1559 static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1561 if (a[OVS_DP_ATTR_USER_FEATURES])
1562 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1565 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1567 struct nlattr **a = info->attrs;
1568 struct vport_parms parms;
1569 struct sk_buff *reply;
1570 struct datapath *dp;
1571 struct vport *vport;
1572 struct ovs_net *ovs_net;
1576 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1579 reply = ovs_dp_cmd_alloc_info();
1584 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1586 goto err_free_reply;
1588 ovs_dp_set_net(dp, sock_net(skb->sk));
1590 /* Allocate table. */
1591 err = ovs_flow_tbl_init(&dp->table);
1595 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1596 if (!dp->stats_percpu) {
1598 goto err_destroy_table;
1601 dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS,
1602 sizeof(struct hlist_head),
1606 goto err_destroy_percpu;
1609 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1610 INIT_HLIST_HEAD(&dp->ports[i]);
1612 err = ovs_meters_init(dp);
1614 goto err_destroy_ports_array;
1616 /* Set up our datapath device. */
1617 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1618 parms.type = OVS_VPORT_TYPE_INTERNAL;
1619 parms.options = NULL;
1621 parms.port_no = OVSP_LOCAL;
1622 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1624 ovs_dp_change(dp, a);
1626 /* So far only local changes have been made, now need the lock. */
1629 vport = new_vport(&parms);
1630 if (IS_ERR(vport)) {
1631 err = PTR_ERR(vport);
1635 if (err == -EEXIST) {
1636 /* An outdated user space instance that does not understand
1637 * the concept of user_features has attempted to create a new
1638 * datapath and is likely to reuse it. Drop all user features.
1640 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1641 ovs_dp_reset_user_features(skb, info);
1644 goto err_destroy_meters;
1647 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1648 info->snd_seq, 0, OVS_DP_CMD_NEW);
1651 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1652 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1656 ovs_notify(&dp_datapath_genl_family, reply, info);
1661 ovs_meters_exit(dp);
1662 err_destroy_ports_array:
1665 free_percpu(dp->stats_percpu);
1667 ovs_flow_tbl_destroy(&dp->table);
1676 /* Called with ovs_mutex. */
1677 static void __dp_destroy(struct datapath *dp)
1681 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1682 struct vport *vport;
1683 struct hlist_node *n;
1685 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1686 if (vport->port_no != OVSP_LOCAL)
1687 ovs_dp_detach_port(vport);
1690 list_del_rcu(&dp->list_node);
1692 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1693 * all ports in datapath are destroyed first before freeing datapath.
1695 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1697 /* RCU destroy the flow table */
1698 call_rcu(&dp->rcu, destroy_dp_rcu);
1701 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1703 struct sk_buff *reply;
1704 struct datapath *dp;
1707 reply = ovs_dp_cmd_alloc_info();
1712 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1715 goto err_unlock_free;
1717 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1718 info->snd_seq, 0, OVS_DP_CMD_DEL);
1724 ovs_notify(&dp_datapath_genl_family, reply, info);
1734 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1736 struct sk_buff *reply;
1737 struct datapath *dp;
1740 reply = ovs_dp_cmd_alloc_info();
1745 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1748 goto err_unlock_free;
1750 ovs_dp_change(dp, info->attrs);
1752 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1753 info->snd_seq, 0, OVS_DP_CMD_NEW);
1757 ovs_notify(&dp_datapath_genl_family, reply, info);
1767 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1769 struct sk_buff *reply;
1770 struct datapath *dp;
1773 reply = ovs_dp_cmd_alloc_info();
1778 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1781 goto err_unlock_free;
1783 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1784 info->snd_seq, 0, OVS_DP_CMD_NEW);
1788 return genlmsg_reply(reply, info);
1796 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1798 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1799 struct datapath *dp;
1800 int skip = cb->args[0];
1804 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1806 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1807 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1808 OVS_DP_CMD_NEW) < 0)
1819 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1820 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1821 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1822 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1825 static const struct genl_ops dp_datapath_genl_ops[] = {
1826 { .cmd = OVS_DP_CMD_NEW,
1827 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1828 .policy = datapath_policy,
1829 .doit = ovs_dp_cmd_new
1831 { .cmd = OVS_DP_CMD_DEL,
1832 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1833 .policy = datapath_policy,
1834 .doit = ovs_dp_cmd_del
1836 { .cmd = OVS_DP_CMD_GET,
1837 .flags = 0, /* OK for unprivileged users. */
1838 .policy = datapath_policy,
1839 .doit = ovs_dp_cmd_get,
1840 .dumpit = ovs_dp_cmd_dump
1842 { .cmd = OVS_DP_CMD_SET,
1843 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1844 .policy = datapath_policy,
1845 .doit = ovs_dp_cmd_set,
1849 static struct genl_family dp_datapath_genl_family __ro_after_init = {
1850 .hdrsize = sizeof(struct ovs_header),
1851 .name = OVS_DATAPATH_FAMILY,
1852 .version = OVS_DATAPATH_VERSION,
1853 .maxattr = OVS_DP_ATTR_MAX,
1855 .parallel_ops = true,
1856 .ops = dp_datapath_genl_ops,
1857 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1858 .mcgrps = &ovs_dp_datapath_multicast_group,
1860 .module = THIS_MODULE,
1863 /* Called with ovs_mutex or RCU read lock. */
1864 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1865 struct net *net, u32 portid, u32 seq,
1866 u32 flags, u8 cmd, gfp_t gfp)
1868 struct ovs_header *ovs_header;
1869 struct ovs_vport_stats vport_stats;
1872 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1877 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1879 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1880 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1881 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1882 ovs_vport_name(vport)) ||
1883 nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
1884 goto nla_put_failure;
1886 if (!net_eq(net, dev_net(vport->dev))) {
1887 int id = peernet2id_alloc(net, dev_net(vport->dev), gfp);
1889 if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
1890 goto nla_put_failure;
1893 ovs_vport_get_stats(vport, &vport_stats);
1894 if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
1895 sizeof(struct ovs_vport_stats), &vport_stats,
1896 OVS_VPORT_ATTR_PAD))
1897 goto nla_put_failure;
1899 if (ovs_vport_get_upcall_portids(vport, skb))
1900 goto nla_put_failure;
1902 err = ovs_vport_get_options(vport, skb);
1903 if (err == -EMSGSIZE)
1906 genlmsg_end(skb, ovs_header);
1912 genlmsg_cancel(skb, ovs_header);
1916 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1918 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1921 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1922 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
1923 u32 portid, u32 seq, u8 cmd)
1925 struct sk_buff *skb;
1928 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1930 return ERR_PTR(-ENOMEM);
1932 retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd,
1939 /* Called with ovs_mutex or RCU read lock. */
1940 static struct vport *lookup_vport(struct net *net,
1941 const struct ovs_header *ovs_header,
1942 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1944 struct datapath *dp;
1945 struct vport *vport;
1947 if (a[OVS_VPORT_ATTR_IFINDEX])
1948 return ERR_PTR(-EOPNOTSUPP);
1949 if (a[OVS_VPORT_ATTR_NAME]) {
1950 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1952 return ERR_PTR(-ENODEV);
1953 if (ovs_header->dp_ifindex &&
1954 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1955 return ERR_PTR(-ENODEV);
1957 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1958 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1960 if (port_no >= DP_MAX_PORTS)
1961 return ERR_PTR(-EFBIG);
1963 dp = get_dp(net, ovs_header->dp_ifindex);
1965 return ERR_PTR(-ENODEV);
1967 vport = ovs_vport_ovsl_rcu(dp, port_no);
1969 return ERR_PTR(-ENODEV);
1972 return ERR_PTR(-EINVAL);
1976 /* Called with ovs_mutex */
1977 static void update_headroom(struct datapath *dp)
1979 unsigned dev_headroom, max_headroom = 0;
1980 struct net_device *dev;
1981 struct vport *vport;
1984 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1985 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1987 dev_headroom = netdev_get_fwd_headroom(dev);
1988 if (dev_headroom > max_headroom)
1989 max_headroom = dev_headroom;
1993 dp->max_headroom = max_headroom;
1994 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1995 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
1996 netdev_set_rx_headroom(vport->dev, max_headroom);
1999 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2001 struct nlattr **a = info->attrs;
2002 struct ovs_header *ovs_header = info->userhdr;
2003 struct vport_parms parms;
2004 struct sk_buff *reply;
2005 struct vport *vport;
2006 struct datapath *dp;
2010 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2011 !a[OVS_VPORT_ATTR_UPCALL_PID])
2013 if (a[OVS_VPORT_ATTR_IFINDEX])
2016 port_no = a[OVS_VPORT_ATTR_PORT_NO]
2017 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
2018 if (port_no >= DP_MAX_PORTS)
2021 reply = ovs_vport_cmd_alloc_info();
2027 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2030 goto exit_unlock_free;
2033 vport = ovs_vport_ovsl(dp, port_no);
2036 goto exit_unlock_free;
2038 for (port_no = 1; ; port_no++) {
2039 if (port_no >= DP_MAX_PORTS) {
2041 goto exit_unlock_free;
2043 vport = ovs_vport_ovsl(dp, port_no);
2049 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2050 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2051 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2053 parms.port_no = port_no;
2054 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
2056 vport = new_vport(&parms);
2057 err = PTR_ERR(vport);
2058 if (IS_ERR(vport)) {
2061 goto exit_unlock_free;
2064 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2065 info->snd_portid, info->snd_seq, 0,
2066 OVS_VPORT_CMD_NEW, GFP_KERNEL);
2068 if (netdev_get_fwd_headroom(vport->dev) > dp->max_headroom)
2069 update_headroom(dp);
2071 netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2076 ovs_notify(&dp_vport_genl_family, reply, info);
2085 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2087 struct nlattr **a = info->attrs;
2088 struct sk_buff *reply;
2089 struct vport *vport;
2092 reply = ovs_vport_cmd_alloc_info();
2097 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2098 err = PTR_ERR(vport);
2100 goto exit_unlock_free;
2102 if (a[OVS_VPORT_ATTR_TYPE] &&
2103 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2105 goto exit_unlock_free;
2108 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2109 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2111 goto exit_unlock_free;
2115 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2116 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2118 err = ovs_vport_set_upcall_portids(vport, ids);
2120 goto exit_unlock_free;
2123 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2124 info->snd_portid, info->snd_seq, 0,
2125 OVS_VPORT_CMD_NEW, GFP_ATOMIC);
2129 ovs_notify(&dp_vport_genl_family, reply, info);
2138 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2140 bool must_update_headroom = false;
2141 struct nlattr **a = info->attrs;
2142 struct sk_buff *reply;
2143 struct datapath *dp;
2144 struct vport *vport;
2147 reply = ovs_vport_cmd_alloc_info();
2152 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2153 err = PTR_ERR(vport);
2155 goto exit_unlock_free;
2157 if (vport->port_no == OVSP_LOCAL) {
2159 goto exit_unlock_free;
2162 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2163 info->snd_portid, info->snd_seq, 0,
2164 OVS_VPORT_CMD_DEL, GFP_KERNEL);
2167 /* the vport deletion may trigger dp headroom update */
2169 if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
2170 must_update_headroom = true;
2171 netdev_reset_rx_headroom(vport->dev);
2172 ovs_dp_detach_port(vport);
2174 if (must_update_headroom)
2175 update_headroom(dp);
2178 ovs_notify(&dp_vport_genl_family, reply, info);
2187 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2189 struct nlattr **a = info->attrs;
2190 struct ovs_header *ovs_header = info->userhdr;
2191 struct sk_buff *reply;
2192 struct vport *vport;
2195 reply = ovs_vport_cmd_alloc_info();
2200 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2201 err = PTR_ERR(vport);
2203 goto exit_unlock_free;
2204 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2205 info->snd_portid, info->snd_seq, 0,
2206 OVS_VPORT_CMD_NEW, GFP_ATOMIC);
2210 return genlmsg_reply(reply, info);
2218 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2220 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2221 struct datapath *dp;
2222 int bucket = cb->args[0], skip = cb->args[1];
2226 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2231 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2232 struct vport *vport;
2235 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2237 ovs_vport_cmd_fill_info(vport, skb,
2239 NETLINK_CB(cb->skb).portid,
2259 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2260 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2261 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2262 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2263 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2264 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_UNSPEC },
2265 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2266 [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
2267 [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
2270 static const struct genl_ops dp_vport_genl_ops[] = {
2271 { .cmd = OVS_VPORT_CMD_NEW,
2272 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2273 .policy = vport_policy,
2274 .doit = ovs_vport_cmd_new
2276 { .cmd = OVS_VPORT_CMD_DEL,
2277 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2278 .policy = vport_policy,
2279 .doit = ovs_vport_cmd_del
2281 { .cmd = OVS_VPORT_CMD_GET,
2282 .flags = 0, /* OK for unprivileged users. */
2283 .policy = vport_policy,
2284 .doit = ovs_vport_cmd_get,
2285 .dumpit = ovs_vport_cmd_dump
2287 { .cmd = OVS_VPORT_CMD_SET,
2288 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2289 .policy = vport_policy,
2290 .doit = ovs_vport_cmd_set,
2294 struct genl_family dp_vport_genl_family __ro_after_init = {
2295 .hdrsize = sizeof(struct ovs_header),
2296 .name = OVS_VPORT_FAMILY,
2297 .version = OVS_VPORT_VERSION,
2298 .maxattr = OVS_VPORT_ATTR_MAX,
2300 .parallel_ops = true,
2301 .ops = dp_vport_genl_ops,
2302 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2303 .mcgrps = &ovs_dp_vport_multicast_group,
2305 .module = THIS_MODULE,
2308 static struct genl_family * const dp_genl_families[] = {
2309 &dp_datapath_genl_family,
2310 &dp_vport_genl_family,
2311 &dp_flow_genl_family,
2312 &dp_packet_genl_family,
2313 &dp_meter_genl_family,
2314 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2315 &dp_ct_limit_genl_family,
2319 static void dp_unregister_genl(int n_families)
2323 for (i = 0; i < n_families; i++)
2324 genl_unregister_family(dp_genl_families[i]);
2327 static int __init dp_register_genl(void)
2332 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2334 err = genl_register_family(dp_genl_families[i]);
2342 dp_unregister_genl(i);
2346 static int __net_init ovs_init_net(struct net *net)
2348 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2350 INIT_LIST_HEAD(&ovs_net->dps);
2351 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2352 return ovs_ct_init(net);
2355 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2356 struct list_head *head)
2358 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2359 struct datapath *dp;
2361 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2364 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2365 struct vport *vport;
2367 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2368 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2371 if (dev_net(vport->dev) == dnet)
2372 list_add(&vport->detach_list, head);
2378 static void __net_exit ovs_exit_net(struct net *dnet)
2380 struct datapath *dp, *dp_next;
2381 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2382 struct vport *vport, *vport_next;
2388 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2391 down_read(&net_rwsem);
2393 list_vports_from_net(net, dnet, &head);
2394 up_read(&net_rwsem);
2396 /* Detach all vports from given namespace. */
2397 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2398 list_del(&vport->detach_list);
2399 ovs_dp_detach_port(vport);
2404 cancel_work_sync(&ovs_net->dp_notify_work);
2407 static struct pernet_operations ovs_net_ops = {
2408 .init = ovs_init_net,
2409 .exit = ovs_exit_net,
2411 .size = sizeof(struct ovs_net),
2414 static int __init dp_init(void)
2418 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2420 pr_info("Open vSwitch switching datapath\n");
2422 err = action_fifos_init();
2426 err = ovs_internal_dev_rtnl_link_register();
2428 goto error_action_fifos_exit;
2430 err = ovs_flow_init();
2432 goto error_unreg_rtnl_link;
2434 err = ovs_vport_init();
2436 goto error_flow_exit;
2438 err = register_pernet_device(&ovs_net_ops);
2440 goto error_vport_exit;
2442 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2444 goto error_netns_exit;
2446 err = ovs_netdev_init();
2448 goto error_unreg_notifier;
2450 err = dp_register_genl();
2452 goto error_unreg_netdev;
2458 error_unreg_notifier:
2459 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2461 unregister_pernet_device(&ovs_net_ops);
2466 error_unreg_rtnl_link:
2467 ovs_internal_dev_rtnl_link_unregister();
2468 error_action_fifos_exit:
2469 action_fifos_exit();
2474 static void dp_cleanup(void)
2476 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2478 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2479 unregister_pernet_device(&ovs_net_ops);
2483 ovs_internal_dev_rtnl_link_unregister();
2484 action_fifos_exit();
2487 module_init(dp_init);
2488 module_exit(dp_cleanup);
2490 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2491 MODULE_LICENSE("GPL");
2492 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
2493 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
2494 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
2495 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
2496 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);
2497 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY);