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);
256 stats_counter = &stats->n_missed;
260 ovs_flow_stats_update(flow, key->tp.flags, skb);
261 sf_acts = rcu_dereference(flow->sf_acts);
262 ovs_execute_actions(dp, skb, sf_acts, key);
264 stats_counter = &stats->n_hit;
267 /* Update datapath statistics. */
268 u64_stats_update_begin(&stats->syncp);
270 stats->n_mask_hit += n_mask_hit;
271 u64_stats_update_end(&stats->syncp);
274 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
275 const struct sw_flow_key *key,
276 const struct dp_upcall_info *upcall_info,
279 struct dp_stats_percpu *stats;
282 if (upcall_info->portid == 0) {
287 if (!skb_is_gso(skb))
288 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
290 err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
297 stats = this_cpu_ptr(dp->stats_percpu);
299 u64_stats_update_begin(&stats->syncp);
301 u64_stats_update_end(&stats->syncp);
306 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
307 const struct sw_flow_key *key,
308 const struct dp_upcall_info *upcall_info,
311 struct sk_buff *segs, *nskb;
314 BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_SGO_CB_OFFSET);
315 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
317 return PTR_ERR(segs);
321 /* Queue all of the segments. */
324 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
328 } while ((skb = skb->next));
330 /* Free all of the segments. */
338 } while ((skb = nskb));
342 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
343 unsigned int hdrlen, int actions_attrlen)
345 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
346 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
347 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
348 + nla_total_size(sizeof(unsigned int)); /* OVS_PACKET_ATTR_LEN */
350 /* OVS_PACKET_ATTR_USERDATA */
351 if (upcall_info->userdata)
352 size += NLA_ALIGN(upcall_info->userdata->nla_len);
354 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
355 if (upcall_info->egress_tun_info)
356 size += nla_total_size(ovs_tun_key_attr_size());
358 /* OVS_PACKET_ATTR_ACTIONS */
359 if (upcall_info->actions_len)
360 size += nla_total_size(actions_attrlen);
362 /* OVS_PACKET_ATTR_MRU */
363 if (upcall_info->mru)
364 size += nla_total_size(sizeof(upcall_info->mru));
369 static void pad_packet(struct datapath *dp, struct sk_buff *skb)
371 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
372 size_t plen = NLA_ALIGN(skb->len) - skb->len;
375 skb_put_zero(skb, plen);
379 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
380 const struct sw_flow_key *key,
381 const struct dp_upcall_info *upcall_info,
384 struct ovs_header *upcall;
385 struct sk_buff *nskb = NULL;
386 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
392 dp_ifindex = get_dpifindex(dp);
396 if (skb_vlan_tag_present(skb)) {
397 nskb = skb_clone(skb, GFP_ATOMIC);
401 nskb = __vlan_hwaccel_push_inside(nskb);
408 if (nla_attr_size(skb->len) > USHRT_MAX) {
413 /* Complete checksum if needed */
414 if (skb->ip_summed == CHECKSUM_PARTIAL &&
415 (err = skb_csum_hwoffload_help(skb, 0)))
418 /* Older versions of OVS user space enforce alignment of the last
419 * Netlink attribute to NLA_ALIGNTO which would require extensive
420 * padding logic. Only perform zerocopy if padding is not required.
422 if (dp->user_features & OVS_DP_F_UNALIGNED)
423 hlen = skb_zerocopy_headlen(skb);
427 len = upcall_msg_size(upcall_info, hlen - cutlen,
428 OVS_CB(skb)->acts_origlen);
429 user_skb = genlmsg_new(len, GFP_ATOMIC);
435 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
436 0, upcall_info->cmd);
437 upcall->dp_ifindex = dp_ifindex;
439 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
442 if (upcall_info->userdata)
443 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
444 nla_len(upcall_info->userdata),
445 nla_data(upcall_info->userdata));
447 if (upcall_info->egress_tun_info) {
448 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
449 err = ovs_nla_put_tunnel_info(user_skb,
450 upcall_info->egress_tun_info);
452 nla_nest_end(user_skb, nla);
455 if (upcall_info->actions_len) {
456 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS);
457 err = ovs_nla_put_actions(upcall_info->actions,
458 upcall_info->actions_len,
461 nla_nest_end(user_skb, nla);
463 nla_nest_cancel(user_skb, nla);
466 /* Add OVS_PACKET_ATTR_MRU */
467 if (upcall_info->mru) {
468 if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
473 pad_packet(dp, user_skb);
476 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
478 if (nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN,
483 pad_packet(dp, user_skb);
486 /* Only reserve room for attribute header, packet data is added
487 * in skb_zerocopy() */
488 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
492 nla->nla_len = nla_attr_size(skb->len - cutlen);
494 err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
498 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
499 pad_packet(dp, user_skb);
501 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
503 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
513 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
515 struct ovs_header *ovs_header = info->userhdr;
516 struct net *net = sock_net(skb->sk);
517 struct nlattr **a = info->attrs;
518 struct sw_flow_actions *acts;
519 struct sk_buff *packet;
520 struct sw_flow *flow;
521 struct sw_flow_actions *sf_acts;
523 struct vport *input_vport;
527 bool log = !a[OVS_PACKET_ATTR_PROBE];
530 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
531 !a[OVS_PACKET_ATTR_ACTIONS])
534 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
535 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
539 skb_reserve(packet, NET_IP_ALIGN);
541 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
543 /* Set packet's mru */
544 if (a[OVS_PACKET_ATTR_MRU]) {
545 mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
546 packet->ignore_df = 1;
548 OVS_CB(packet)->mru = mru;
550 /* Build an sw_flow for sending this packet. */
551 flow = ovs_flow_alloc();
556 err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
557 packet, &flow->key, log);
561 err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
562 &flow->key, &acts, log);
566 rcu_assign_pointer(flow->sf_acts, acts);
567 packet->priority = flow->key.phy.priority;
568 packet->mark = flow->key.phy.skb_mark;
571 dp = get_dp_rcu(net, ovs_header->dp_ifindex);
576 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
578 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
583 packet->dev = input_vport->dev;
584 OVS_CB(packet)->input_vport = input_vport;
585 sf_acts = rcu_dereference(flow->sf_acts);
588 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
592 ovs_flow_free(flow, false);
598 ovs_flow_free(flow, false);
605 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
606 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
607 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
608 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
609 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
610 [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
613 static const struct genl_ops dp_packet_genl_ops[] = {
614 { .cmd = OVS_PACKET_CMD_EXECUTE,
615 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
616 .policy = packet_policy,
617 .doit = ovs_packet_cmd_execute
621 static struct genl_family dp_packet_genl_family __ro_after_init = {
622 .hdrsize = sizeof(struct ovs_header),
623 .name = OVS_PACKET_FAMILY,
624 .version = OVS_PACKET_VERSION,
625 .maxattr = OVS_PACKET_ATTR_MAX,
627 .parallel_ops = true,
628 .ops = dp_packet_genl_ops,
629 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
630 .module = THIS_MODULE,
633 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
634 struct ovs_dp_megaflow_stats *mega_stats)
638 memset(mega_stats, 0, sizeof(*mega_stats));
640 stats->n_flows = ovs_flow_tbl_count(&dp->table);
641 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
643 stats->n_hit = stats->n_missed = stats->n_lost = 0;
645 for_each_possible_cpu(i) {
646 const struct dp_stats_percpu *percpu_stats;
647 struct dp_stats_percpu local_stats;
650 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
653 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
654 local_stats = *percpu_stats;
655 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
657 stats->n_hit += local_stats.n_hit;
658 stats->n_missed += local_stats.n_missed;
659 stats->n_lost += local_stats.n_lost;
660 mega_stats->n_mask_hit += local_stats.n_mask_hit;
664 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
666 return ovs_identifier_is_ufid(sfid) &&
667 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
670 static bool should_fill_mask(uint32_t ufid_flags)
672 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
675 static bool should_fill_actions(uint32_t ufid_flags)
677 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
680 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
681 const struct sw_flow_id *sfid,
684 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
686 /* OVS_FLOW_ATTR_UFID */
687 if (sfid && ovs_identifier_is_ufid(sfid))
688 len += nla_total_size(sfid->ufid_len);
690 /* OVS_FLOW_ATTR_KEY */
691 if (!sfid || should_fill_key(sfid, ufid_flags))
692 len += nla_total_size(ovs_key_attr_size());
694 /* OVS_FLOW_ATTR_MASK */
695 if (should_fill_mask(ufid_flags))
696 len += nla_total_size(ovs_key_attr_size());
698 /* OVS_FLOW_ATTR_ACTIONS */
699 if (should_fill_actions(ufid_flags))
700 len += nla_total_size(acts->orig_len);
703 + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
704 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
705 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
708 /* Called with ovs_mutex or RCU read lock. */
709 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
712 struct ovs_flow_stats stats;
716 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
719 nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
723 if (stats.n_packets &&
724 nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
725 sizeof(struct ovs_flow_stats), &stats,
729 if ((u8)ntohs(tcp_flags) &&
730 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
736 /* Called with ovs_mutex or RCU read lock. */
737 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
738 struct sk_buff *skb, int skb_orig_len)
740 struct nlattr *start;
743 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
744 * this is the first flow to be dumped into 'skb'. This is unusual for
745 * Netlink but individual action lists can be longer than
746 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
747 * The userspace caller can always fetch the actions separately if it
748 * really wants them. (Most userspace callers in fact don't care.)
750 * This can only fail for dump operations because the skb is always
751 * properly sized for single flows.
753 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
755 const struct sw_flow_actions *sf_acts;
757 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
758 err = ovs_nla_put_actions(sf_acts->actions,
759 sf_acts->actions_len, skb);
762 nla_nest_end(skb, start);
767 nla_nest_cancel(skb, start);
769 } else if (skb_orig_len) {
776 /* Called with ovs_mutex or RCU read lock. */
777 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
778 struct sk_buff *skb, u32 portid,
779 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
781 const int skb_orig_len = skb->len;
782 struct ovs_header *ovs_header;
785 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
790 ovs_header->dp_ifindex = dp_ifindex;
792 err = ovs_nla_put_identifier(flow, skb);
796 if (should_fill_key(&flow->id, ufid_flags)) {
797 err = ovs_nla_put_masked_key(flow, skb);
802 if (should_fill_mask(ufid_flags)) {
803 err = ovs_nla_put_mask(flow, skb);
808 err = ovs_flow_cmd_fill_stats(flow, skb);
812 if (should_fill_actions(ufid_flags)) {
813 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
818 genlmsg_end(skb, ovs_header);
822 genlmsg_cancel(skb, ovs_header);
826 /* May not be called with RCU read lock. */
827 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
828 const struct sw_flow_id *sfid,
829 struct genl_info *info,
836 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
839 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
840 skb = genlmsg_new(len, GFP_KERNEL);
842 return ERR_PTR(-ENOMEM);
847 /* Called with ovs_mutex. */
848 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
850 struct genl_info *info, u8 cmd,
851 bool always, u32 ufid_flags)
856 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
857 &flow->id, info, always, ufid_flags);
858 if (IS_ERR_OR_NULL(skb))
861 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
862 info->snd_portid, info->snd_seq, 0,
868 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
870 struct net *net = sock_net(skb->sk);
871 struct nlattr **a = info->attrs;
872 struct ovs_header *ovs_header = info->userhdr;
873 struct sw_flow *flow = NULL, *new_flow;
874 struct sw_flow_mask mask;
875 struct sk_buff *reply;
877 struct sw_flow_actions *acts;
878 struct sw_flow_match match;
879 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
881 bool log = !a[OVS_FLOW_ATTR_PROBE];
883 /* Must have key and actions. */
885 if (!a[OVS_FLOW_ATTR_KEY]) {
886 OVS_NLERR(log, "Flow key attr not present in new flow.");
889 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
890 OVS_NLERR(log, "Flow actions attr not present in new flow.");
894 /* Most of the time we need to allocate a new flow, do it before
897 new_flow = ovs_flow_alloc();
898 if (IS_ERR(new_flow)) {
899 error = PTR_ERR(new_flow);
904 ovs_match_init(&match, &new_flow->key, false, &mask);
905 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
906 a[OVS_FLOW_ATTR_MASK], log);
910 /* Extract flow identifier. */
911 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
912 &new_flow->key, log);
916 /* unmasked key is needed to match when ufid is not used. */
917 if (ovs_identifier_is_key(&new_flow->id))
918 match.key = new_flow->id.unmasked_key;
920 ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask);
922 /* Validate actions. */
923 error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
924 &new_flow->key, &acts, log);
926 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
930 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
933 error = PTR_ERR(reply);
938 dp = get_dp(net, ovs_header->dp_ifindex);
944 /* Check if this is a duplicate flow */
945 if (ovs_identifier_is_ufid(&new_flow->id))
946 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
948 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key);
950 rcu_assign_pointer(new_flow->sf_acts, acts);
952 /* Put flow in bucket. */
953 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
954 if (unlikely(error)) {
959 if (unlikely(reply)) {
960 error = ovs_flow_cmd_fill_info(new_flow,
961 ovs_header->dp_ifindex,
962 reply, info->snd_portid,
970 struct sw_flow_actions *old_acts;
972 /* Bail out if we're not allowed to modify an existing flow.
973 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
974 * because Generic Netlink treats the latter as a dump
975 * request. We also accept NLM_F_EXCL in case that bug ever
978 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
983 /* The flow identifier has to be the same for flow updates.
984 * Look for any overlapping flow.
986 if (unlikely(!ovs_flow_cmp(flow, &match))) {
987 if (ovs_identifier_is_key(&flow->id))
988 flow = ovs_flow_tbl_lookup_exact(&dp->table,
990 else /* UFID matches but key is different */
997 /* Update actions. */
998 old_acts = ovsl_dereference(flow->sf_acts);
999 rcu_assign_pointer(flow->sf_acts, acts);
1001 if (unlikely(reply)) {
1002 error = ovs_flow_cmd_fill_info(flow,
1003 ovs_header->dp_ifindex,
1004 reply, info->snd_portid,
1012 ovs_nla_free_flow_actions_rcu(old_acts);
1013 ovs_flow_free(new_flow, false);
1017 ovs_notify(&dp_flow_genl_family, reply, info);
1024 ovs_nla_free_flow_actions(acts);
1026 ovs_flow_free(new_flow, false);
1031 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1032 static struct sw_flow_actions *get_flow_actions(struct net *net,
1033 const struct nlattr *a,
1034 const struct sw_flow_key *key,
1035 const struct sw_flow_mask *mask,
1038 struct sw_flow_actions *acts;
1039 struct sw_flow_key masked_key;
1042 ovs_flow_mask_key(&masked_key, key, true, mask);
1043 error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
1046 "Actions may not be safe on all matching packets");
1047 return ERR_PTR(error);
1053 /* Factor out match-init and action-copy to avoid
1054 * "Wframe-larger-than=1024" warning. Because mask is only
1055 * used to get actions, we new a function to save some
1058 * If there are not key and action attrs, we return 0
1059 * directly. In the case, the caller will also not use the
1060 * match as before. If there is action attr, we try to get
1061 * actions and save them to *acts. Before returning from
1062 * the function, we reset the match->mask pointer. Because
1063 * we should not to return match object with dangling reference
1066 static int ovs_nla_init_match_and_action(struct net *net,
1067 struct sw_flow_match *match,
1068 struct sw_flow_key *key,
1070 struct sw_flow_actions **acts,
1073 struct sw_flow_mask mask;
1076 if (a[OVS_FLOW_ATTR_KEY]) {
1077 ovs_match_init(match, key, true, &mask);
1078 error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
1079 a[OVS_FLOW_ATTR_MASK], log);
1084 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1085 if (!a[OVS_FLOW_ATTR_KEY]) {
1087 "Flow key attribute not present in set flow.");
1092 *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
1094 if (IS_ERR(*acts)) {
1095 error = PTR_ERR(*acts);
1100 /* On success, error is 0. */
1106 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1108 struct net *net = sock_net(skb->sk);
1109 struct nlattr **a = info->attrs;
1110 struct ovs_header *ovs_header = info->userhdr;
1111 struct sw_flow_key key;
1112 struct sw_flow *flow;
1113 struct sk_buff *reply = NULL;
1114 struct datapath *dp;
1115 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1116 struct sw_flow_match match;
1117 struct sw_flow_id sfid;
1118 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1120 bool log = !a[OVS_FLOW_ATTR_PROBE];
1123 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1124 if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
1126 "Flow set message rejected, Key attribute missing.");
1130 error = ovs_nla_init_match_and_action(net, &match, &key, a,
1136 /* Can allocate before locking if have acts. */
1137 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1139 if (IS_ERR(reply)) {
1140 error = PTR_ERR(reply);
1141 goto err_kfree_acts;
1146 dp = get_dp(net, ovs_header->dp_ifindex);
1147 if (unlikely(!dp)) {
1149 goto err_unlock_ovs;
1151 /* Check that the flow exists. */
1153 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1155 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1156 if (unlikely(!flow)) {
1158 goto err_unlock_ovs;
1161 /* Update actions, if present. */
1163 old_acts = ovsl_dereference(flow->sf_acts);
1164 rcu_assign_pointer(flow->sf_acts, acts);
1166 if (unlikely(reply)) {
1167 error = ovs_flow_cmd_fill_info(flow,
1168 ovs_header->dp_ifindex,
1169 reply, info->snd_portid,
1176 /* Could not alloc without acts before locking. */
1177 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1178 info, OVS_FLOW_CMD_NEW, false,
1181 if (IS_ERR(reply)) {
1182 error = PTR_ERR(reply);
1183 goto err_unlock_ovs;
1188 if (a[OVS_FLOW_ATTR_CLEAR])
1189 ovs_flow_stats_clear(flow);
1193 ovs_notify(&dp_flow_genl_family, reply, info);
1195 ovs_nla_free_flow_actions_rcu(old_acts);
1203 ovs_nla_free_flow_actions(acts);
1208 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1210 struct nlattr **a = info->attrs;
1211 struct ovs_header *ovs_header = info->userhdr;
1212 struct net *net = sock_net(skb->sk);
1213 struct sw_flow_key key;
1214 struct sk_buff *reply;
1215 struct sw_flow *flow;
1216 struct datapath *dp;
1217 struct sw_flow_match match;
1218 struct sw_flow_id ufid;
1219 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1221 bool log = !a[OVS_FLOW_ATTR_PROBE];
1224 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1225 if (a[OVS_FLOW_ATTR_KEY]) {
1226 ovs_match_init(&match, &key, true, NULL);
1227 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
1229 } else if (!ufid_present) {
1231 "Flow get message rejected, Key attribute missing.");
1238 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1245 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1247 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1253 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1254 OVS_FLOW_CMD_NEW, true, ufid_flags);
1255 if (IS_ERR(reply)) {
1256 err = PTR_ERR(reply);
1261 return genlmsg_reply(reply, info);
1267 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1269 struct nlattr **a = info->attrs;
1270 struct ovs_header *ovs_header = info->userhdr;
1271 struct net *net = sock_net(skb->sk);
1272 struct sw_flow_key key;
1273 struct sk_buff *reply;
1274 struct sw_flow *flow = NULL;
1275 struct datapath *dp;
1276 struct sw_flow_match match;
1277 struct sw_flow_id ufid;
1278 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1280 bool log = !a[OVS_FLOW_ATTR_PROBE];
1283 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1284 if (a[OVS_FLOW_ATTR_KEY]) {
1285 ovs_match_init(&match, &key, true, NULL);
1286 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1293 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1294 if (unlikely(!dp)) {
1299 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1300 err = ovs_flow_tbl_flush(&dp->table);
1305 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1307 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1308 if (unlikely(!flow)) {
1313 ovs_flow_tbl_remove(&dp->table, flow);
1316 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1317 &flow->id, info, false, ufid_flags);
1318 if (likely(reply)) {
1319 if (likely(!IS_ERR(reply))) {
1320 rcu_read_lock(); /*To keep RCU checker happy. */
1321 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1322 reply, info->snd_portid,
1329 ovs_notify(&dp_flow_genl_family, reply, info);
1331 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1335 ovs_flow_free(flow, true);
1342 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1344 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1345 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1346 struct table_instance *ti;
1347 struct datapath *dp;
1351 err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
1352 OVS_FLOW_ATTR_MAX, flow_policy, NULL);
1355 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1358 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1364 ti = rcu_dereference(dp->table.ti);
1366 struct sw_flow *flow;
1369 bucket = cb->args[0];
1371 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1375 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1376 NETLINK_CB(cb->skb).portid,
1377 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1378 OVS_FLOW_CMD_NEW, ufid_flags) < 0)
1381 cb->args[0] = bucket;
1388 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1389 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1390 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1391 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1392 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1393 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1394 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1395 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1398 static const struct genl_ops dp_flow_genl_ops[] = {
1399 { .cmd = OVS_FLOW_CMD_NEW,
1400 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1401 .policy = flow_policy,
1402 .doit = ovs_flow_cmd_new
1404 { .cmd = OVS_FLOW_CMD_DEL,
1405 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1406 .policy = flow_policy,
1407 .doit = ovs_flow_cmd_del
1409 { .cmd = OVS_FLOW_CMD_GET,
1410 .flags = 0, /* OK for unprivileged users. */
1411 .policy = flow_policy,
1412 .doit = ovs_flow_cmd_get,
1413 .dumpit = ovs_flow_cmd_dump
1415 { .cmd = OVS_FLOW_CMD_SET,
1416 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1417 .policy = flow_policy,
1418 .doit = ovs_flow_cmd_set,
1422 static struct genl_family dp_flow_genl_family __ro_after_init = {
1423 .hdrsize = sizeof(struct ovs_header),
1424 .name = OVS_FLOW_FAMILY,
1425 .version = OVS_FLOW_VERSION,
1426 .maxattr = OVS_FLOW_ATTR_MAX,
1428 .parallel_ops = true,
1429 .ops = dp_flow_genl_ops,
1430 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1431 .mcgrps = &ovs_dp_flow_multicast_group,
1433 .module = THIS_MODULE,
1436 static size_t ovs_dp_cmd_msg_size(void)
1438 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1440 msgsize += nla_total_size(IFNAMSIZ);
1441 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
1442 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
1443 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1448 /* Called with ovs_mutex. */
1449 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1450 u32 portid, u32 seq, u32 flags, u8 cmd)
1452 struct ovs_header *ovs_header;
1453 struct ovs_dp_stats dp_stats;
1454 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1457 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1462 ovs_header->dp_ifindex = get_dpifindex(dp);
1464 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1466 goto nla_put_failure;
1468 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1469 if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1470 &dp_stats, OVS_DP_ATTR_PAD))
1471 goto nla_put_failure;
1473 if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1474 sizeof(struct ovs_dp_megaflow_stats),
1475 &dp_megaflow_stats, OVS_DP_ATTR_PAD))
1476 goto nla_put_failure;
1478 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1479 goto nla_put_failure;
1481 genlmsg_end(skb, ovs_header);
1485 genlmsg_cancel(skb, ovs_header);
1490 static struct sk_buff *ovs_dp_cmd_alloc_info(void)
1492 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1495 /* Called with rcu_read_lock or ovs_mutex. */
1496 static struct datapath *lookup_datapath(struct net *net,
1497 const struct ovs_header *ovs_header,
1498 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1500 struct datapath *dp;
1502 if (!a[OVS_DP_ATTR_NAME])
1503 dp = get_dp(net, ovs_header->dp_ifindex);
1505 struct vport *vport;
1507 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1508 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1510 return dp ? dp : ERR_PTR(-ENODEV);
1513 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1515 struct datapath *dp;
1517 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1521 WARN(dp->user_features, "Dropping previously announced user features\n");
1522 dp->user_features = 0;
1525 static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1527 if (a[OVS_DP_ATTR_USER_FEATURES])
1528 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1531 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1533 struct nlattr **a = info->attrs;
1534 struct vport_parms parms;
1535 struct sk_buff *reply;
1536 struct datapath *dp;
1537 struct vport *vport;
1538 struct ovs_net *ovs_net;
1542 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1545 reply = ovs_dp_cmd_alloc_info();
1550 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1552 goto err_free_reply;
1554 ovs_dp_set_net(dp, sock_net(skb->sk));
1556 /* Allocate table. */
1557 err = ovs_flow_tbl_init(&dp->table);
1561 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1562 if (!dp->stats_percpu) {
1564 goto err_destroy_table;
1567 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1571 goto err_destroy_percpu;
1574 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1575 INIT_HLIST_HEAD(&dp->ports[i]);
1577 err = ovs_meters_init(dp);
1579 goto err_destroy_ports_array;
1581 /* Set up our datapath device. */
1582 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1583 parms.type = OVS_VPORT_TYPE_INTERNAL;
1584 parms.options = NULL;
1586 parms.port_no = OVSP_LOCAL;
1587 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1589 ovs_dp_change(dp, a);
1591 /* So far only local changes have been made, now need the lock. */
1594 vport = new_vport(&parms);
1595 if (IS_ERR(vport)) {
1596 err = PTR_ERR(vport);
1600 if (err == -EEXIST) {
1601 /* An outdated user space instance that does not understand
1602 * the concept of user_features has attempted to create a new
1603 * datapath and is likely to reuse it. Drop all user features.
1605 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1606 ovs_dp_reset_user_features(skb, info);
1609 goto err_destroy_meters;
1612 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1613 info->snd_seq, 0, OVS_DP_CMD_NEW);
1616 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1617 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1621 ovs_notify(&dp_datapath_genl_family, reply, info);
1626 ovs_meters_exit(dp);
1627 err_destroy_ports_array:
1630 free_percpu(dp->stats_percpu);
1632 ovs_flow_tbl_destroy(&dp->table);
1641 /* Called with ovs_mutex. */
1642 static void __dp_destroy(struct datapath *dp)
1646 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1647 struct vport *vport;
1648 struct hlist_node *n;
1650 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1651 if (vport->port_no != OVSP_LOCAL)
1652 ovs_dp_detach_port(vport);
1655 list_del_rcu(&dp->list_node);
1657 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1658 * all ports in datapath are destroyed first before freeing datapath.
1660 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1662 /* RCU destroy the flow table */
1663 call_rcu(&dp->rcu, destroy_dp_rcu);
1666 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1668 struct sk_buff *reply;
1669 struct datapath *dp;
1672 reply = ovs_dp_cmd_alloc_info();
1677 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1680 goto err_unlock_free;
1682 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1683 info->snd_seq, 0, OVS_DP_CMD_DEL);
1689 ovs_notify(&dp_datapath_genl_family, reply, info);
1699 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1701 struct sk_buff *reply;
1702 struct datapath *dp;
1705 reply = ovs_dp_cmd_alloc_info();
1710 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1713 goto err_unlock_free;
1715 ovs_dp_change(dp, info->attrs);
1717 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1718 info->snd_seq, 0, OVS_DP_CMD_NEW);
1722 ovs_notify(&dp_datapath_genl_family, reply, info);
1732 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1734 struct sk_buff *reply;
1735 struct datapath *dp;
1738 reply = ovs_dp_cmd_alloc_info();
1743 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1746 goto err_unlock_free;
1748 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1749 info->snd_seq, 0, OVS_DP_CMD_NEW);
1753 return genlmsg_reply(reply, info);
1761 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1763 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1764 struct datapath *dp;
1765 int skip = cb->args[0];
1769 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1771 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1772 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1773 OVS_DP_CMD_NEW) < 0)
1784 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1785 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1786 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1787 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1790 static const struct genl_ops dp_datapath_genl_ops[] = {
1791 { .cmd = OVS_DP_CMD_NEW,
1792 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1793 .policy = datapath_policy,
1794 .doit = ovs_dp_cmd_new
1796 { .cmd = OVS_DP_CMD_DEL,
1797 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1798 .policy = datapath_policy,
1799 .doit = ovs_dp_cmd_del
1801 { .cmd = OVS_DP_CMD_GET,
1802 .flags = 0, /* OK for unprivileged users. */
1803 .policy = datapath_policy,
1804 .doit = ovs_dp_cmd_get,
1805 .dumpit = ovs_dp_cmd_dump
1807 { .cmd = OVS_DP_CMD_SET,
1808 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1809 .policy = datapath_policy,
1810 .doit = ovs_dp_cmd_set,
1814 static struct genl_family dp_datapath_genl_family __ro_after_init = {
1815 .hdrsize = sizeof(struct ovs_header),
1816 .name = OVS_DATAPATH_FAMILY,
1817 .version = OVS_DATAPATH_VERSION,
1818 .maxattr = OVS_DP_ATTR_MAX,
1820 .parallel_ops = true,
1821 .ops = dp_datapath_genl_ops,
1822 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1823 .mcgrps = &ovs_dp_datapath_multicast_group,
1825 .module = THIS_MODULE,
1828 /* Called with ovs_mutex or RCU read lock. */
1829 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1830 struct net *net, u32 portid, u32 seq,
1833 struct ovs_header *ovs_header;
1834 struct ovs_vport_stats vport_stats;
1837 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1842 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1844 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1845 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1846 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1847 ovs_vport_name(vport)) ||
1848 nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
1849 goto nla_put_failure;
1851 if (!net_eq(net, dev_net(vport->dev))) {
1852 int id = peernet2id_alloc(net, dev_net(vport->dev));
1854 if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
1855 goto nla_put_failure;
1858 ovs_vport_get_stats(vport, &vport_stats);
1859 if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
1860 sizeof(struct ovs_vport_stats), &vport_stats,
1861 OVS_VPORT_ATTR_PAD))
1862 goto nla_put_failure;
1864 if (ovs_vport_get_upcall_portids(vport, skb))
1865 goto nla_put_failure;
1867 err = ovs_vport_get_options(vport, skb);
1868 if (err == -EMSGSIZE)
1871 genlmsg_end(skb, ovs_header);
1877 genlmsg_cancel(skb, ovs_header);
1881 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1883 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1886 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1887 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
1888 u32 portid, u32 seq, u8 cmd)
1890 struct sk_buff *skb;
1893 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1895 return ERR_PTR(-ENOMEM);
1897 retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd);
1903 /* Called with ovs_mutex or RCU read lock. */
1904 static struct vport *lookup_vport(struct net *net,
1905 const struct ovs_header *ovs_header,
1906 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1908 struct datapath *dp;
1909 struct vport *vport;
1911 if (a[OVS_VPORT_ATTR_IFINDEX])
1912 return ERR_PTR(-EOPNOTSUPP);
1913 if (a[OVS_VPORT_ATTR_NAME]) {
1914 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1916 return ERR_PTR(-ENODEV);
1917 if (ovs_header->dp_ifindex &&
1918 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1919 return ERR_PTR(-ENODEV);
1921 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1922 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1924 if (port_no >= DP_MAX_PORTS)
1925 return ERR_PTR(-EFBIG);
1927 dp = get_dp(net, ovs_header->dp_ifindex);
1929 return ERR_PTR(-ENODEV);
1931 vport = ovs_vport_ovsl_rcu(dp, port_no);
1933 return ERR_PTR(-ENODEV);
1936 return ERR_PTR(-EINVAL);
1940 /* Called with ovs_mutex */
1941 static void update_headroom(struct datapath *dp)
1943 unsigned dev_headroom, max_headroom = 0;
1944 struct net_device *dev;
1945 struct vport *vport;
1948 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1949 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1951 dev_headroom = netdev_get_fwd_headroom(dev);
1952 if (dev_headroom > max_headroom)
1953 max_headroom = dev_headroom;
1957 dp->max_headroom = max_headroom;
1958 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1959 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
1960 netdev_set_rx_headroom(vport->dev, max_headroom);
1963 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1965 struct nlattr **a = info->attrs;
1966 struct ovs_header *ovs_header = info->userhdr;
1967 struct vport_parms parms;
1968 struct sk_buff *reply;
1969 struct vport *vport;
1970 struct datapath *dp;
1974 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1975 !a[OVS_VPORT_ATTR_UPCALL_PID])
1977 if (a[OVS_VPORT_ATTR_IFINDEX])
1980 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1981 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1982 if (port_no >= DP_MAX_PORTS)
1985 reply = ovs_vport_cmd_alloc_info();
1991 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1994 goto exit_unlock_free;
1997 vport = ovs_vport_ovsl(dp, port_no);
2000 goto exit_unlock_free;
2002 for (port_no = 1; ; port_no++) {
2003 if (port_no >= DP_MAX_PORTS) {
2005 goto exit_unlock_free;
2007 vport = ovs_vport_ovsl(dp, port_no);
2013 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2014 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2015 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2017 parms.port_no = port_no;
2018 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
2020 vport = new_vport(&parms);
2021 err = PTR_ERR(vport);
2022 if (IS_ERR(vport)) {
2025 goto exit_unlock_free;
2028 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2029 info->snd_portid, info->snd_seq, 0,
2032 if (netdev_get_fwd_headroom(vport->dev) > dp->max_headroom)
2033 update_headroom(dp);
2035 netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2040 ovs_notify(&dp_vport_genl_family, reply, info);
2049 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2051 struct nlattr **a = info->attrs;
2052 struct sk_buff *reply;
2053 struct vport *vport;
2056 reply = ovs_vport_cmd_alloc_info();
2061 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2062 err = PTR_ERR(vport);
2064 goto exit_unlock_free;
2066 if (a[OVS_VPORT_ATTR_TYPE] &&
2067 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2069 goto exit_unlock_free;
2072 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2073 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2075 goto exit_unlock_free;
2079 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2080 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2082 err = ovs_vport_set_upcall_portids(vport, ids);
2084 goto exit_unlock_free;
2087 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2088 info->snd_portid, info->snd_seq, 0,
2093 ovs_notify(&dp_vport_genl_family, reply, info);
2102 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2104 bool must_update_headroom = false;
2105 struct nlattr **a = info->attrs;
2106 struct sk_buff *reply;
2107 struct datapath *dp;
2108 struct vport *vport;
2111 reply = ovs_vport_cmd_alloc_info();
2116 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2117 err = PTR_ERR(vport);
2119 goto exit_unlock_free;
2121 if (vport->port_no == OVSP_LOCAL) {
2123 goto exit_unlock_free;
2126 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2127 info->snd_portid, info->snd_seq, 0,
2131 /* the vport deletion may trigger dp headroom update */
2133 if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
2134 must_update_headroom = true;
2135 netdev_reset_rx_headroom(vport->dev);
2136 ovs_dp_detach_port(vport);
2138 if (must_update_headroom)
2139 update_headroom(dp);
2142 ovs_notify(&dp_vport_genl_family, reply, info);
2151 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2153 struct nlattr **a = info->attrs;
2154 struct ovs_header *ovs_header = info->userhdr;
2155 struct sk_buff *reply;
2156 struct vport *vport;
2159 reply = ovs_vport_cmd_alloc_info();
2164 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2165 err = PTR_ERR(vport);
2167 goto exit_unlock_free;
2168 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2169 info->snd_portid, info->snd_seq, 0,
2174 return genlmsg_reply(reply, info);
2182 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2184 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2185 struct datapath *dp;
2186 int bucket = cb->args[0], skip = cb->args[1];
2190 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2195 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2196 struct vport *vport;
2199 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2201 ovs_vport_cmd_fill_info(vport, skb,
2203 NETLINK_CB(cb->skb).portid,
2206 OVS_VPORT_CMD_NEW) < 0)
2222 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2223 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2224 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2225 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2226 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2227 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2228 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2229 [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
2230 [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
2233 static const struct genl_ops dp_vport_genl_ops[] = {
2234 { .cmd = OVS_VPORT_CMD_NEW,
2235 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2236 .policy = vport_policy,
2237 .doit = ovs_vport_cmd_new
2239 { .cmd = OVS_VPORT_CMD_DEL,
2240 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2241 .policy = vport_policy,
2242 .doit = ovs_vport_cmd_del
2244 { .cmd = OVS_VPORT_CMD_GET,
2245 .flags = 0, /* OK for unprivileged users. */
2246 .policy = vport_policy,
2247 .doit = ovs_vport_cmd_get,
2248 .dumpit = ovs_vport_cmd_dump
2250 { .cmd = OVS_VPORT_CMD_SET,
2251 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2252 .policy = vport_policy,
2253 .doit = ovs_vport_cmd_set,
2257 struct genl_family dp_vport_genl_family __ro_after_init = {
2258 .hdrsize = sizeof(struct ovs_header),
2259 .name = OVS_VPORT_FAMILY,
2260 .version = OVS_VPORT_VERSION,
2261 .maxattr = OVS_VPORT_ATTR_MAX,
2263 .parallel_ops = true,
2264 .ops = dp_vport_genl_ops,
2265 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2266 .mcgrps = &ovs_dp_vport_multicast_group,
2268 .module = THIS_MODULE,
2271 static struct genl_family * const dp_genl_families[] = {
2272 &dp_datapath_genl_family,
2273 &dp_vport_genl_family,
2274 &dp_flow_genl_family,
2275 &dp_packet_genl_family,
2276 &dp_meter_genl_family,
2279 static void dp_unregister_genl(int n_families)
2283 for (i = 0; i < n_families; i++)
2284 genl_unregister_family(dp_genl_families[i]);
2287 static int __init dp_register_genl(void)
2292 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2294 err = genl_register_family(dp_genl_families[i]);
2302 dp_unregister_genl(i);
2306 static int __net_init ovs_init_net(struct net *net)
2308 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2310 INIT_LIST_HEAD(&ovs_net->dps);
2311 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2316 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2317 struct list_head *head)
2319 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2320 struct datapath *dp;
2322 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2325 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2326 struct vport *vport;
2328 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2329 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2332 if (dev_net(vport->dev) == dnet)
2333 list_add(&vport->detach_list, head);
2339 static void __net_exit ovs_exit_net(struct net *dnet)
2341 struct datapath *dp, *dp_next;
2342 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2343 struct vport *vport, *vport_next;
2349 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2354 list_vports_from_net(net, dnet, &head);
2357 /* Detach all vports from given namespace. */
2358 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2359 list_del(&vport->detach_list);
2360 ovs_dp_detach_port(vport);
2365 cancel_work_sync(&ovs_net->dp_notify_work);
2368 static struct pernet_operations ovs_net_ops = {
2369 .init = ovs_init_net,
2370 .exit = ovs_exit_net,
2372 .size = sizeof(struct ovs_net),
2375 static int __init dp_init(void)
2379 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2381 pr_info("Open vSwitch switching datapath\n");
2383 err = action_fifos_init();
2387 err = ovs_internal_dev_rtnl_link_register();
2389 goto error_action_fifos_exit;
2391 err = ovs_flow_init();
2393 goto error_unreg_rtnl_link;
2395 err = ovs_vport_init();
2397 goto error_flow_exit;
2399 err = register_pernet_device(&ovs_net_ops);
2401 goto error_vport_exit;
2403 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2405 goto error_netns_exit;
2407 err = ovs_netdev_init();
2409 goto error_unreg_notifier;
2411 err = dp_register_genl();
2413 goto error_unreg_netdev;
2419 error_unreg_notifier:
2420 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2422 unregister_pernet_device(&ovs_net_ops);
2427 error_unreg_rtnl_link:
2428 ovs_internal_dev_rtnl_link_unregister();
2429 error_action_fifos_exit:
2430 action_fifos_exit();
2435 static void dp_cleanup(void)
2437 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2439 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2440 unregister_pernet_device(&ovs_net_ops);
2444 ovs_internal_dev_rtnl_link_unregister();
2445 action_fifos_exit();
2448 module_init(dp_init);
2449 module_exit(dp_cleanup);
2451 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2452 MODULE_LICENSE("GPL");
2453 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
2454 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
2455 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
2456 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
2457 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);