2 * Copyright (c) 2007-2015 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/version.h>
40 #include <linux/ethtool.h>
41 #include <linux/wait.h>
42 #include <asm/div64.h>
43 #include <linux/highmem.h>
44 #include <linux/netfilter_bridge.h>
45 #include <linux/netfilter_ipv4.h>
46 #include <linux/inetdevice.h>
47 #include <linux/list.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <net/genetlink.h>
52 #include <net/net_namespace.h>
53 #include <net/netns/generic.h>
56 #include "conntrack.h"
58 #include "flow_table.h"
59 #include "flow_netlink.h"
61 #include "vport-internal_dev.h"
62 #include "vport-netdev.h"
64 int ovs_net_id __read_mostly;
65 EXPORT_SYMBOL_GPL(ovs_net_id);
67 static struct genl_family dp_packet_genl_family;
68 static struct genl_family dp_flow_genl_family;
69 static struct genl_family dp_datapath_genl_family;
71 static const struct nla_policy flow_policy[];
73 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
74 .name = OVS_FLOW_MCGROUP
77 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
78 .name = OVS_DATAPATH_MCGROUP
81 struct genl_multicast_group ovs_dp_vport_multicast_group = {
82 .name = OVS_VPORT_MCGROUP
85 /* Check if need to build a reply message.
86 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply.
88 static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
91 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
92 genl_has_listeners(family, genl_info_net(info), group);
95 static void ovs_notify(struct genl_family *family, struct genl_multicast_group *grp,
96 struct sk_buff *skb, struct genl_info *info)
98 genl_notify(family, skb, genl_info_net(info),
99 info->snd_portid, GROUP_ID(grp), info->nlhdr, GFP_KERNEL);
105 * All writes e.g. Writes to device state (add/remove datapath, port, set
106 * operations on vports, etc.), Writes to other state (flow table
107 * modifications, set miscellaneous datapath parameters, etc.) are protected
110 * Reads are protected by RCU.
112 * There are a few special cases (mostly stats) that have their own
113 * synchronization but they nest under all of above and don't interact with
116 * The RTNL lock nests inside ovs_mutex.
119 static DEFINE_MUTEX(ovs_mutex);
123 mutex_lock(&ovs_mutex);
126 void ovs_unlock(void)
128 mutex_unlock(&ovs_mutex);
131 #ifdef CONFIG_LOCKDEP
132 int lockdep_ovsl_is_held(void)
135 return lockdep_is_held(&ovs_mutex);
139 EXPORT_SYMBOL_GPL(lockdep_ovsl_is_held);
142 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
143 const struct sw_flow_key *,
144 const struct dp_upcall_info *,
146 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
147 const struct sw_flow_key *,
148 const struct dp_upcall_info *,
151 /* Must be called with rcu_read_lock. */
152 static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
154 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
157 struct vport *vport = ovs_internal_dev_get_vport(dev);
165 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
166 * returned dp pointer valid.
168 static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
172 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
174 dp = get_dp_rcu(net, dp_ifindex);
180 /* Must be called with rcu_read_lock or ovs_mutex. */
181 const char *ovs_dp_name(const struct datapath *dp)
183 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
184 return ovs_vport_name(vport);
187 static int get_dpifindex(const struct datapath *dp)
194 local = ovs_vport_rcu(dp, OVSP_LOCAL);
196 ifindex = local->dev->ifindex;
205 static void destroy_dp_rcu(struct rcu_head *rcu)
207 struct datapath *dp = container_of(rcu, struct datapath, rcu);
209 ovs_flow_tbl_destroy(&dp->table);
210 free_percpu(dp->stats_percpu);
215 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
218 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
221 /* Called with ovs_mutex or RCU read lock. */
222 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
225 struct hlist_head *head;
227 head = vport_hash_bucket(dp, port_no);
228 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
229 if (vport->port_no == port_no)
235 /* Called with ovs_mutex. */
236 static struct vport *new_vport(const struct vport_parms *parms)
240 vport = ovs_vport_add(parms);
241 if (!IS_ERR(vport)) {
242 struct datapath *dp = parms->dp;
243 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
245 hlist_add_head_rcu(&vport->dp_hash_node, head);
250 void ovs_dp_detach_port(struct vport *p)
254 /* First drop references to device. */
255 hlist_del_rcu(&p->dp_hash_node);
257 /* Then destroy it. */
261 /* Must be called with rcu_read_lock. */
262 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
264 const struct vport *p = OVS_CB(skb)->input_vport;
265 struct datapath *dp = p->dp;
266 struct sw_flow *flow;
267 struct sw_flow_actions *sf_acts;
268 struct dp_stats_percpu *stats;
272 stats = this_cpu_ptr(dp->stats_percpu);
275 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
277 if (unlikely(!flow)) {
278 struct dp_upcall_info upcall;
281 memset(&upcall, 0, sizeof(upcall));
282 upcall.cmd = OVS_PACKET_CMD_MISS;
283 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
284 upcall.mru = OVS_CB(skb)->mru;
285 error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
290 stats_counter = &stats->n_missed;
294 ovs_flow_stats_update(flow, key->tp.flags, skb);
295 sf_acts = rcu_dereference(flow->sf_acts);
296 ovs_execute_actions(dp, skb, sf_acts, key);
298 stats_counter = &stats->n_hit;
301 /* Update datapath statistics. */
302 u64_stats_update_begin(&stats->syncp);
304 stats->n_mask_hit += n_mask_hit;
305 u64_stats_update_end(&stats->syncp);
308 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
309 const struct sw_flow_key *key,
310 const struct dp_upcall_info *upcall_info,
313 struct dp_stats_percpu *stats;
316 if (upcall_info->portid == 0) {
321 if (!skb_is_gso(skb))
322 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
324 err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
331 stats = this_cpu_ptr(dp->stats_percpu);
333 u64_stats_update_begin(&stats->syncp);
335 u64_stats_update_end(&stats->syncp);
340 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
341 const struct sw_flow_key *key,
342 const struct dp_upcall_info *upcall_info,
345 unsigned short gso_type = skb_shinfo(skb)->gso_type;
346 struct sw_flow_key later_key;
347 struct sk_buff *segs, *nskb;
348 struct ovs_skb_cb ovs_cb;
351 ovs_cb = *OVS_CB(skb);
352 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
353 *OVS_CB(skb) = ovs_cb;
355 return PTR_ERR(segs);
359 if (gso_type & SKB_GSO_UDP) {
360 /* The initial flow key extracted by ovs_flow_key_extract()
361 * in this case is for a first fragment, so we need to
362 * properly mark later fragments.
365 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
368 /* Queue all of the segments. */
371 *OVS_CB(skb) = ovs_cb;
372 if (gso_type & SKB_GSO_UDP && skb != segs)
375 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
379 } while ((skb = skb->next));
381 /* Free all of the segments. */
389 } while ((skb = nskb));
393 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
396 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
397 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
398 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
399 + nla_total_size(sizeof(unsigned int)); /* OVS_PACKET_ATTR_LEN */
401 /* OVS_PACKET_ATTR_USERDATA */
402 if (upcall_info->userdata)
403 size += NLA_ALIGN(upcall_info->userdata->nla_len);
405 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
406 if (upcall_info->egress_tun_info)
407 size += nla_total_size(ovs_tun_key_attr_size());
409 /* OVS_PACKET_ATTR_ACTIONS */
410 if (upcall_info->actions_len)
411 size += nla_total_size(upcall_info->actions_len);
413 /* OVS_PACKET_ATTR_MRU */
414 if (upcall_info->mru)
415 size += nla_total_size(sizeof(upcall_info->mru));
420 static void pad_packet(struct datapath *dp, struct sk_buff *skb)
422 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
423 size_t plen = NLA_ALIGN(skb->len) - skb->len;
426 memset(skb_put(skb, plen), 0, plen);
430 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
431 const struct sw_flow_key *key,
432 const struct dp_upcall_info *upcall_info,
435 struct ovs_header *upcall;
436 struct sk_buff *nskb = NULL;
437 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
439 struct genl_info info = {
440 #ifdef HAVE_GENLMSG_NEW_UNICAST
441 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
443 .snd_portid = upcall_info->portid,
449 dp_ifindex = get_dpifindex(dp);
453 if (skb_vlan_tag_present(skb)) {
454 nskb = skb_clone(skb, GFP_ATOMIC);
458 nskb = __vlan_hwaccel_push_inside(nskb);
465 if (nla_attr_size(skb->len) > USHRT_MAX) {
470 /* Complete checksum if needed */
471 if (skb->ip_summed == CHECKSUM_PARTIAL &&
472 (err = skb_checksum_help(skb)))
475 /* Older versions of OVS user space enforce alignment of the last
476 * Netlink attribute to NLA_ALIGNTO which would require extensive
477 * padding logic. Only perform zerocopy if padding is not required.
479 if (dp->user_features & OVS_DP_F_UNALIGNED)
480 hlen = skb_zerocopy_headlen(skb);
484 len = upcall_msg_size(upcall_info, hlen - cutlen);
485 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
491 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
492 0, upcall_info->cmd);
493 upcall->dp_ifindex = dp_ifindex;
495 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
498 if (upcall_info->userdata)
499 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
500 nla_len(upcall_info->userdata),
501 nla_data(upcall_info->userdata));
504 if (upcall_info->egress_tun_info) {
505 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
506 err = ovs_nla_put_tunnel_info(user_skb,
507 upcall_info->egress_tun_info);
509 nla_nest_end(user_skb, nla);
512 if (upcall_info->actions_len) {
513 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS);
514 err = ovs_nla_put_actions(upcall_info->actions,
515 upcall_info->actions_len,
518 nla_nest_end(user_skb, nla);
520 nla_nest_cancel(user_skb, nla);
523 /* Add OVS_PACKET_ATTR_MRU */
524 if (upcall_info->mru) {
525 if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
530 pad_packet(dp, user_skb);
533 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
535 if (nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN,
540 pad_packet(dp, user_skb);
543 /* Only reserve room for attribute header, packet data is added
546 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
550 nla->nla_len = nla_attr_size(skb->len - cutlen);
552 err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
556 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
557 pad_packet(dp, user_skb);
559 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
561 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
571 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
573 struct ovs_header *ovs_header = info->userhdr;
574 struct net *net = sock_net(skb->sk);
575 struct nlattr **a = info->attrs;
576 struct sw_flow_actions *acts;
577 struct sk_buff *packet;
578 struct sw_flow *flow;
579 struct sw_flow_actions *sf_acts;
582 struct vport *input_vport;
586 bool log = !a[OVS_PACKET_ATTR_PROBE];
589 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
590 !a[OVS_PACKET_ATTR_ACTIONS])
593 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
594 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
598 skb_reserve(packet, NET_IP_ALIGN);
600 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
602 skb_reset_mac_header(packet);
603 eth = eth_hdr(packet);
605 /* Normally, setting the skb 'protocol' field would be handled by a
606 * call to eth_type_trans(), but it assumes there's a sending
607 * device, which we may not have.
609 if (eth_proto_is_802_3(eth->h_proto))
610 packet->protocol = eth->h_proto;
612 packet->protocol = htons(ETH_P_802_2);
614 /* Set packet's mru */
615 if (a[OVS_PACKET_ATTR_MRU]) {
616 mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
617 packet->ignore_df = 1;
619 OVS_CB(packet)->mru = mru;
621 /* Build an sw_flow for sending this packet. */
622 flow = ovs_flow_alloc();
627 err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
628 packet, &flow->key, log);
632 err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
633 &flow->key, &acts, log);
637 rcu_assign_pointer(flow->sf_acts, acts);
638 packet->priority = flow->key.phy.priority;
639 packet->mark = flow->key.phy.skb_mark;
642 dp = get_dp_rcu(net, ovs_header->dp_ifindex);
647 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
649 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
654 packet->dev = input_vport->dev;
655 OVS_CB(packet)->input_vport = input_vport;
656 sf_acts = rcu_dereference(flow->sf_acts);
659 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
663 ovs_flow_free(flow, false);
669 ovs_flow_free(flow, false);
676 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
677 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
678 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
679 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
680 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
681 [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
684 static struct genl_ops dp_packet_genl_ops[] = {
685 { .cmd = OVS_PACKET_CMD_EXECUTE,
686 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
687 .policy = packet_policy,
688 .doit = ovs_packet_cmd_execute
692 static struct genl_family dp_packet_genl_family = {
693 .id = GENL_ID_GENERATE,
694 .hdrsize = sizeof(struct ovs_header),
695 .name = OVS_PACKET_FAMILY,
696 .version = OVS_PACKET_VERSION,
697 .maxattr = OVS_PACKET_ATTR_MAX,
699 .parallel_ops = true,
700 .ops = dp_packet_genl_ops,
701 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
704 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
705 struct ovs_dp_megaflow_stats *mega_stats)
709 memset(mega_stats, 0, sizeof(*mega_stats));
711 stats->n_flows = ovs_flow_tbl_count(&dp->table);
712 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
714 stats->n_hit = stats->n_missed = stats->n_lost = 0;
716 for_each_possible_cpu(i) {
717 const struct dp_stats_percpu *percpu_stats;
718 struct dp_stats_percpu local_stats;
721 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
724 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
725 local_stats = *percpu_stats;
726 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
728 stats->n_hit += local_stats.n_hit;
729 stats->n_missed += local_stats.n_missed;
730 stats->n_lost += local_stats.n_lost;
731 mega_stats->n_mask_hit += local_stats.n_mask_hit;
735 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
737 return ovs_identifier_is_ufid(sfid) &&
738 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
741 static bool should_fill_mask(uint32_t ufid_flags)
743 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
746 static bool should_fill_actions(uint32_t ufid_flags)
748 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
751 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
752 const struct sw_flow_id *sfid,
755 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
757 /* OVS_FLOW_ATTR_UFID */
758 if (sfid && ovs_identifier_is_ufid(sfid))
759 len += nla_total_size(sfid->ufid_len);
761 /* OVS_FLOW_ATTR_KEY */
762 if (!sfid || should_fill_key(sfid, ufid_flags))
763 len += nla_total_size(ovs_key_attr_size());
765 /* OVS_FLOW_ATTR_MASK */
766 if (should_fill_mask(ufid_flags))
767 len += nla_total_size(ovs_key_attr_size());
769 /* OVS_FLOW_ATTR_ACTIONS */
770 if (should_fill_actions(ufid_flags))
771 len += nla_total_size(acts->orig_len);
774 + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
775 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
776 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
779 /* Called with ovs_mutex or RCU read lock. */
780 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
783 struct ovs_flow_stats stats;
787 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
790 nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
794 if (stats.n_packets &&
795 nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
796 sizeof(struct ovs_flow_stats), &stats,
800 if ((u8)ntohs(tcp_flags) &&
801 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
807 /* Called with ovs_mutex or RCU read lock. */
808 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
809 struct sk_buff *skb, int skb_orig_len)
811 struct nlattr *start;
814 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
815 * this is the first flow to be dumped into 'skb'. This is unusual for
816 * Netlink but individual action lists can be longer than
817 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
818 * The userspace caller can always fetch the actions separately if it
819 * really wants them. (Most userspace callers in fact don't care.)
821 * This can only fail for dump operations because the skb is always
822 * properly sized for single flows.
824 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
826 const struct sw_flow_actions *sf_acts;
828 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
829 err = ovs_nla_put_actions(sf_acts->actions,
830 sf_acts->actions_len, skb);
833 nla_nest_end(skb, start);
838 nla_nest_cancel(skb, start);
840 } else if (skb_orig_len) {
847 /* Called with ovs_mutex or RCU read lock. */
848 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
849 struct sk_buff *skb, u32 portid,
850 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
852 const int skb_orig_len = skb->len;
853 struct ovs_header *ovs_header;
856 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
861 ovs_header->dp_ifindex = dp_ifindex;
863 err = ovs_nla_put_identifier(flow, skb);
867 if (should_fill_key(&flow->id, ufid_flags)) {
868 err = ovs_nla_put_masked_key(flow, skb);
873 if (should_fill_mask(ufid_flags)) {
874 err = ovs_nla_put_mask(flow, skb);
879 err = ovs_flow_cmd_fill_stats(flow, skb);
883 if (should_fill_actions(ufid_flags)) {
884 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
889 genlmsg_end(skb, ovs_header);
893 genlmsg_cancel(skb, ovs_header);
897 /* May not be called with RCU read lock. */
898 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
899 const struct sw_flow_id *sfid,
900 struct genl_info *info,
907 if (!always && !ovs_must_notify(&dp_flow_genl_family, info,
908 GROUP_ID(&ovs_dp_flow_multicast_group)))
911 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
912 skb = genlmsg_new_unicast(len, info, GFP_KERNEL);
914 return ERR_PTR(-ENOMEM);
919 /* Called with ovs_mutex. */
920 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
922 struct genl_info *info, u8 cmd,
923 bool always, u32 ufid_flags)
928 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
929 &flow->id, info, always, ufid_flags);
930 if (IS_ERR_OR_NULL(skb))
933 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
934 info->snd_portid, info->snd_seq, 0,
940 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
942 struct net *net = sock_net(skb->sk);
943 struct nlattr **a = info->attrs;
944 struct ovs_header *ovs_header = info->userhdr;
945 struct sw_flow *flow = NULL, *new_flow;
946 struct sw_flow_mask mask;
947 struct sk_buff *reply;
949 struct sw_flow_key key;
950 struct sw_flow_actions *acts;
951 struct sw_flow_match match;
952 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
954 bool log = !a[OVS_FLOW_ATTR_PROBE];
956 /* Must have key and actions. */
958 if (!a[OVS_FLOW_ATTR_KEY]) {
959 OVS_NLERR(log, "Flow key attr not present in new flow.");
962 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
963 OVS_NLERR(log, "Flow actions attr not present in new flow.");
967 /* Most of the time we need to allocate a new flow, do it before
970 new_flow = ovs_flow_alloc();
971 if (IS_ERR(new_flow)) {
972 error = PTR_ERR(new_flow);
977 ovs_match_init(&match, &key, &mask);
978 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
979 a[OVS_FLOW_ATTR_MASK], log);
983 ovs_flow_mask_key(&new_flow->key, &key, true, &mask);
985 /* Extract flow identifier. */
986 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
991 /* Validate actions. */
992 error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
993 &new_flow->key, &acts, log);
995 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
999 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
1001 if (IS_ERR(reply)) {
1002 error = PTR_ERR(reply);
1003 goto err_kfree_acts;
1007 dp = get_dp(net, ovs_header->dp_ifindex);
1008 if (unlikely(!dp)) {
1010 goto err_unlock_ovs;
1013 /* Check if this is a duplicate flow */
1014 if (ovs_identifier_is_ufid(&new_flow->id))
1015 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
1017 flow = ovs_flow_tbl_lookup(&dp->table, &key);
1018 if (likely(!flow)) {
1019 rcu_assign_pointer(new_flow->sf_acts, acts);
1021 /* Put flow in bucket. */
1022 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
1023 if (unlikely(error)) {
1025 goto err_unlock_ovs;
1028 if (unlikely(reply)) {
1029 error = ovs_flow_cmd_fill_info(new_flow,
1030 ovs_header->dp_ifindex,
1031 reply, info->snd_portid,
1039 struct sw_flow_actions *old_acts;
1041 /* Bail out if we're not allowed to modify an existing flow.
1042 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1043 * because Generic Netlink treats the latter as a dump
1044 * request. We also accept NLM_F_EXCL in case that bug ever
1047 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
1050 goto err_unlock_ovs;
1052 /* The flow identifier has to be the same for flow updates.
1053 * Look for any overlapping flow.
1055 if (unlikely(!ovs_flow_cmp(flow, &match))) {
1056 if (ovs_identifier_is_key(&flow->id))
1057 flow = ovs_flow_tbl_lookup_exact(&dp->table,
1059 else /* UFID matches but key is different */
1063 goto err_unlock_ovs;
1066 /* Update actions. */
1067 old_acts = ovsl_dereference(flow->sf_acts);
1068 rcu_assign_pointer(flow->sf_acts, acts);
1070 if (unlikely(reply)) {
1071 error = ovs_flow_cmd_fill_info(flow,
1072 ovs_header->dp_ifindex,
1073 reply, info->snd_portid,
1081 ovs_nla_free_flow_actions_rcu(old_acts);
1082 ovs_flow_free(new_flow, false);
1086 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1093 ovs_nla_free_flow_actions(acts);
1095 ovs_flow_free(new_flow, false);
1100 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1101 static struct sw_flow_actions *get_flow_actions(struct net *net,
1102 const struct nlattr *a,
1103 const struct sw_flow_key *key,
1104 const struct sw_flow_mask *mask,
1107 struct sw_flow_actions *acts;
1108 struct sw_flow_key masked_key;
1111 ovs_flow_mask_key(&masked_key, key, true, mask);
1112 error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
1115 "Actions may not be safe on all matching packets");
1116 return ERR_PTR(error);
1122 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1124 struct net *net = sock_net(skb->sk);
1125 struct nlattr **a = info->attrs;
1126 struct ovs_header *ovs_header = info->userhdr;
1127 struct sw_flow_key key;
1128 struct sw_flow *flow;
1129 struct sw_flow_mask mask;
1130 struct sk_buff *reply = NULL;
1131 struct datapath *dp;
1132 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1133 struct sw_flow_match match;
1134 struct sw_flow_id sfid;
1135 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1137 bool log = !a[OVS_FLOW_ATTR_PROBE];
1140 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1141 if (a[OVS_FLOW_ATTR_KEY]) {
1142 ovs_match_init(&match, &key, &mask);
1143 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1144 a[OVS_FLOW_ATTR_MASK], log);
1145 } else if (!ufid_present) {
1147 "Flow set message rejected, Key attribute missing.");
1153 /* Validate actions. */
1154 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1155 if (!a[OVS_FLOW_ATTR_KEY]) {
1157 "Flow key attribute not present in set flow.");
1162 acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], &key,
1165 error = PTR_ERR(acts);
1169 /* Can allocate before locking if have acts. */
1170 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1172 if (IS_ERR(reply)) {
1173 error = PTR_ERR(reply);
1174 goto err_kfree_acts;
1179 dp = get_dp(net, ovs_header->dp_ifindex);
1180 if (unlikely(!dp)) {
1182 goto err_unlock_ovs;
1184 /* Check that the flow exists. */
1186 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1188 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1189 if (unlikely(!flow)) {
1191 goto err_unlock_ovs;
1194 /* Update actions, if present. */
1196 old_acts = ovsl_dereference(flow->sf_acts);
1197 rcu_assign_pointer(flow->sf_acts, acts);
1199 if (unlikely(reply)) {
1200 error = ovs_flow_cmd_fill_info(flow,
1201 ovs_header->dp_ifindex,
1202 reply, info->snd_portid,
1209 /* Could not alloc without acts before locking. */
1210 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1211 info, OVS_FLOW_CMD_NEW, false,
1214 if (unlikely(IS_ERR(reply))) {
1215 error = PTR_ERR(reply);
1216 goto err_unlock_ovs;
1221 if (a[OVS_FLOW_ATTR_CLEAR])
1222 ovs_flow_stats_clear(flow);
1226 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1228 ovs_nla_free_flow_actions_rcu(old_acts);
1236 ovs_nla_free_flow_actions(acts);
1241 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1243 struct nlattr **a = info->attrs;
1244 struct ovs_header *ovs_header = info->userhdr;
1245 struct net *net = sock_net(skb->sk);
1246 struct sw_flow_key key;
1247 struct sk_buff *reply;
1248 struct sw_flow *flow;
1249 struct datapath *dp;
1250 struct sw_flow_match match;
1251 struct sw_flow_id ufid;
1252 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1254 bool log = !a[OVS_FLOW_ATTR_PROBE];
1257 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1258 if (a[OVS_FLOW_ATTR_KEY]) {
1259 ovs_match_init(&match, &key, NULL);
1260 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
1262 } else if (!ufid_present) {
1264 "Flow get message rejected, Key attribute missing.");
1271 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1278 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1280 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1286 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1287 OVS_FLOW_CMD_NEW, true, ufid_flags);
1288 if (IS_ERR(reply)) {
1289 err = PTR_ERR(reply);
1294 return genlmsg_reply(reply, info);
1300 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1302 struct nlattr **a = info->attrs;
1303 struct ovs_header *ovs_header = info->userhdr;
1304 struct net *net = sock_net(skb->sk);
1305 struct sw_flow_key key;
1306 struct sk_buff *reply;
1307 struct sw_flow *flow = NULL;
1308 struct datapath *dp;
1309 struct sw_flow_match match;
1310 struct sw_flow_id ufid;
1311 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1313 bool log = !a[OVS_FLOW_ATTR_PROBE];
1316 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1317 if (a[OVS_FLOW_ATTR_KEY]) {
1318 ovs_match_init(&match, &key, NULL);
1319 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1326 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1327 if (unlikely(!dp)) {
1332 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1333 err = ovs_flow_tbl_flush(&dp->table);
1338 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1340 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1341 if (unlikely(!flow)) {
1346 ovs_flow_tbl_remove(&dp->table, flow);
1349 reply = ovs_flow_cmd_alloc_info(rcu_dereference_raw(flow->sf_acts),
1350 &flow->id, info, false, ufid_flags);
1352 if (likely(reply)) {
1353 if (likely(!IS_ERR(reply))) {
1354 rcu_read_lock(); /*To keep RCU checker happy. */
1355 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1356 reply, info->snd_portid,
1362 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1364 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1365 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1370 ovs_flow_free(flow, true);
1377 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1379 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1380 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1381 struct table_instance *ti;
1382 struct datapath *dp;
1386 err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
1387 OVS_FLOW_ATTR_MAX, flow_policy);
1390 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1393 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1399 ti = rcu_dereference(dp->table.ti);
1401 struct sw_flow *flow;
1404 bucket = cb->args[0];
1406 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1410 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1411 NETLINK_CB(cb->skb).portid,
1412 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1413 OVS_FLOW_CMD_NEW, ufid_flags) < 0)
1416 cb->args[0] = bucket;
1423 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1424 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1425 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1426 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1427 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1428 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1429 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1430 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1433 static struct genl_ops dp_flow_genl_ops[] = {
1434 { .cmd = OVS_FLOW_CMD_NEW,
1435 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1436 .policy = flow_policy,
1437 .doit = ovs_flow_cmd_new
1439 { .cmd = OVS_FLOW_CMD_DEL,
1440 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1441 .policy = flow_policy,
1442 .doit = ovs_flow_cmd_del
1444 { .cmd = OVS_FLOW_CMD_GET,
1445 .flags = 0, /* OK for unprivileged users. */
1446 .policy = flow_policy,
1447 .doit = ovs_flow_cmd_get,
1448 .dumpit = ovs_flow_cmd_dump
1450 { .cmd = OVS_FLOW_CMD_SET,
1451 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1452 .policy = flow_policy,
1453 .doit = ovs_flow_cmd_set,
1457 static struct genl_family dp_flow_genl_family = {
1458 .id = GENL_ID_GENERATE,
1459 .hdrsize = sizeof(struct ovs_header),
1460 .name = OVS_FLOW_FAMILY,
1461 .version = OVS_FLOW_VERSION,
1462 .maxattr = OVS_FLOW_ATTR_MAX,
1464 .parallel_ops = true,
1465 .ops = dp_flow_genl_ops,
1466 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1467 .mcgrps = &ovs_dp_flow_multicast_group,
1471 static size_t ovs_dp_cmd_msg_size(void)
1473 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1475 msgsize += nla_total_size(IFNAMSIZ);
1476 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
1477 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
1478 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1483 /* Called with ovs_mutex. */
1484 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1485 u32 portid, u32 seq, u32 flags, u8 cmd)
1487 struct ovs_header *ovs_header;
1488 struct ovs_dp_stats dp_stats;
1489 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1492 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1497 ovs_header->dp_ifindex = get_dpifindex(dp);
1499 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1501 goto nla_put_failure;
1503 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1504 if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1505 &dp_stats, OVS_DP_ATTR_PAD))
1506 goto nla_put_failure;
1508 if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1509 sizeof(struct ovs_dp_megaflow_stats),
1510 &dp_megaflow_stats, OVS_DP_ATTR_PAD))
1511 goto nla_put_failure;
1513 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1514 goto nla_put_failure;
1516 genlmsg_end(skb, ovs_header);
1520 genlmsg_cancel(skb, ovs_header);
1525 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1527 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1530 /* Called with rcu_read_lock or ovs_mutex. */
1531 static struct datapath *lookup_datapath(struct net *net,
1532 const struct ovs_header *ovs_header,
1533 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1535 struct datapath *dp;
1537 if (!a[OVS_DP_ATTR_NAME])
1538 dp = get_dp(net, ovs_header->dp_ifindex);
1540 struct vport *vport;
1542 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1543 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1545 return dp ? dp : ERR_PTR(-ENODEV);
1548 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1550 struct datapath *dp;
1552 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1556 WARN(dp->user_features, "Dropping previously announced user features\n");
1557 dp->user_features = 0;
1560 static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1562 if (a[OVS_DP_ATTR_USER_FEATURES])
1563 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1566 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1568 struct nlattr **a = info->attrs;
1569 struct vport_parms parms;
1570 struct sk_buff *reply;
1571 struct datapath *dp;
1572 struct vport *vport;
1573 struct ovs_net *ovs_net;
1577 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1580 reply = ovs_dp_cmd_alloc_info(info);
1585 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1587 goto err_free_reply;
1589 ovs_dp_set_net(dp, sock_net(skb->sk));
1591 /* Allocate table. */
1592 err = ovs_flow_tbl_init(&dp->table);
1596 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1597 if (!dp->stats_percpu) {
1599 goto err_destroy_table;
1602 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * 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 /* Set up our datapath device. */
1613 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1614 parms.type = OVS_VPORT_TYPE_INTERNAL;
1615 parms.options = NULL;
1617 parms.port_no = OVSP_LOCAL;
1618 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1620 ovs_dp_change(dp, a);
1622 /* So far only local changes have been made, now need the lock. */
1625 vport = new_vport(&parms);
1626 if (IS_ERR(vport)) {
1627 err = PTR_ERR(vport);
1631 if (err == -EEXIST) {
1632 /* An outdated user space instance that does not understand
1633 * the concept of user_features has attempted to create a new
1634 * datapath and is likely to reuse it. Drop all user features.
1636 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1637 ovs_dp_reset_user_features(skb, info);
1640 goto err_destroy_ports_array;
1643 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1644 info->snd_seq, 0, OVS_DP_CMD_NEW);
1647 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1648 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1652 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1655 err_destroy_ports_array:
1659 free_percpu(dp->stats_percpu);
1661 ovs_flow_tbl_destroy(&dp->table);
1670 /* Called with ovs_mutex. */
1671 static void __dp_destroy(struct datapath *dp)
1675 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1676 struct vport *vport;
1677 struct hlist_node *n;
1679 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1680 if (vport->port_no != OVSP_LOCAL)
1681 ovs_dp_detach_port(vport);
1684 list_del_rcu(&dp->list_node);
1686 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1687 * all ports in datapath are destroyed first before freeing datapath.
1689 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1691 /* RCU destroy the flow table */
1692 call_rcu(&dp->rcu, destroy_dp_rcu);
1695 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1697 struct sk_buff *reply;
1698 struct datapath *dp;
1701 reply = ovs_dp_cmd_alloc_info(info);
1706 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1709 goto err_unlock_free;
1711 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1712 info->snd_seq, 0, OVS_DP_CMD_DEL);
1718 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1727 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1729 struct sk_buff *reply;
1730 struct datapath *dp;
1733 reply = ovs_dp_cmd_alloc_info(info);
1738 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1741 goto err_unlock_free;
1743 ovs_dp_change(dp, info->attrs);
1745 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1746 info->snd_seq, 0, OVS_DP_CMD_NEW);
1751 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1760 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1762 struct sk_buff *reply;
1763 struct datapath *dp;
1766 reply = ovs_dp_cmd_alloc_info(info);
1771 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1774 goto err_unlock_free;
1776 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1777 info->snd_seq, 0, OVS_DP_CMD_NEW);
1781 return genlmsg_reply(reply, info);
1789 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1791 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1792 struct datapath *dp;
1793 int skip = cb->args[0];
1797 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1799 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1800 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1801 OVS_DP_CMD_NEW) < 0)
1812 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1813 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1814 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1815 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1818 static struct genl_ops dp_datapath_genl_ops[] = {
1819 { .cmd = OVS_DP_CMD_NEW,
1820 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1821 .policy = datapath_policy,
1822 .doit = ovs_dp_cmd_new
1824 { .cmd = OVS_DP_CMD_DEL,
1825 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1826 .policy = datapath_policy,
1827 .doit = ovs_dp_cmd_del
1829 { .cmd = OVS_DP_CMD_GET,
1830 .flags = 0, /* OK for unprivileged users. */
1831 .policy = datapath_policy,
1832 .doit = ovs_dp_cmd_get,
1833 .dumpit = ovs_dp_cmd_dump
1835 { .cmd = OVS_DP_CMD_SET,
1836 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1837 .policy = datapath_policy,
1838 .doit = ovs_dp_cmd_set,
1842 static struct genl_family dp_datapath_genl_family = {
1843 .id = GENL_ID_GENERATE,
1844 .hdrsize = sizeof(struct ovs_header),
1845 .name = OVS_DATAPATH_FAMILY,
1846 .version = OVS_DATAPATH_VERSION,
1847 .maxattr = OVS_DP_ATTR_MAX,
1849 .parallel_ops = true,
1850 .ops = dp_datapath_genl_ops,
1851 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1852 .mcgrps = &ovs_dp_datapath_multicast_group,
1856 /* Called with ovs_mutex or RCU read lock. */
1857 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1858 u32 portid, u32 seq, u32 flags, u8 cmd)
1860 struct ovs_header *ovs_header;
1861 struct ovs_vport_stats vport_stats;
1864 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1869 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1871 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1872 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1873 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1874 ovs_vport_name(vport)))
1875 goto nla_put_failure;
1877 ovs_vport_get_stats(vport, &vport_stats);
1878 if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
1879 sizeof(struct ovs_vport_stats), &vport_stats,
1880 OVS_VPORT_ATTR_PAD))
1881 goto nla_put_failure;
1883 if (ovs_vport_get_upcall_portids(vport, skb))
1884 goto nla_put_failure;
1886 err = ovs_vport_get_options(vport, skb);
1887 if (err == -EMSGSIZE)
1890 genlmsg_end(skb, ovs_header);
1896 genlmsg_cancel(skb, ovs_header);
1900 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1902 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1905 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1906 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1909 struct sk_buff *skb;
1912 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1914 return ERR_PTR(-ENOMEM);
1916 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1922 /* Called with ovs_mutex or RCU read lock. */
1923 static struct vport *lookup_vport(struct net *net,
1924 const struct ovs_header *ovs_header,
1925 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1927 struct datapath *dp;
1928 struct vport *vport;
1930 if (a[OVS_VPORT_ATTR_NAME]) {
1931 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1933 return ERR_PTR(-ENODEV);
1934 if (ovs_header->dp_ifindex &&
1935 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1936 return ERR_PTR(-ENODEV);
1938 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1939 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1941 if (port_no >= DP_MAX_PORTS)
1942 return ERR_PTR(-EFBIG);
1944 dp = get_dp(net, ovs_header->dp_ifindex);
1946 return ERR_PTR(-ENODEV);
1948 vport = ovs_vport_ovsl_rcu(dp, port_no);
1950 return ERR_PTR(-ENODEV);
1953 return ERR_PTR(-EINVAL);
1956 /* Called with ovs_mutex */
1957 static void update_headroom(struct datapath *dp)
1959 unsigned dev_headroom, max_headroom = 0;
1960 struct net_device *dev;
1961 struct vport *vport;
1964 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1965 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1967 dev_headroom = netdev_get_fwd_headroom(dev);
1968 if (dev_headroom > max_headroom)
1969 max_headroom = dev_headroom;
1973 dp->max_headroom = max_headroom;
1974 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1975 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
1976 netdev_set_rx_headroom(vport->dev, max_headroom);
1979 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1981 struct nlattr **a = info->attrs;
1982 struct ovs_header *ovs_header = info->userhdr;
1983 struct vport_parms parms;
1984 struct sk_buff *reply;
1985 struct vport *vport;
1986 struct datapath *dp;
1990 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1991 !a[OVS_VPORT_ATTR_UPCALL_PID])
1994 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1995 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1996 if (port_no >= DP_MAX_PORTS)
1999 reply = ovs_vport_cmd_alloc_info();
2005 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2008 goto exit_unlock_free;
2011 vport = ovs_vport_ovsl(dp, port_no);
2014 goto exit_unlock_free;
2016 for (port_no = 1; ; port_no++) {
2017 if (port_no >= DP_MAX_PORTS) {
2019 goto exit_unlock_free;
2021 vport = ovs_vport_ovsl(dp, port_no);
2027 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2028 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2029 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2031 parms.port_no = port_no;
2032 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
2034 vport = new_vport(&parms);
2035 err = PTR_ERR(vport);
2036 if (IS_ERR(vport)) {
2039 goto exit_unlock_free;
2042 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2043 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2046 if (netdev_get_fwd_headroom(vport->dev) > dp->max_headroom)
2047 update_headroom(dp);
2049 netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2053 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2062 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2064 struct nlattr **a = info->attrs;
2065 struct sk_buff *reply;
2066 struct vport *vport;
2069 reply = ovs_vport_cmd_alloc_info();
2074 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2075 err = PTR_ERR(vport);
2077 goto exit_unlock_free;
2079 if (a[OVS_VPORT_ATTR_TYPE] &&
2080 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2082 goto exit_unlock_free;
2085 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2086 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2088 goto exit_unlock_free;
2091 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2092 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2094 err = ovs_vport_set_upcall_portids(vport, ids);
2096 goto exit_unlock_free;
2099 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2100 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2104 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2113 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2115 bool must_update_headroom = false;
2116 struct nlattr **a = info->attrs;
2117 struct sk_buff *reply;
2118 struct datapath *dp;
2119 struct vport *vport;
2122 reply = ovs_vport_cmd_alloc_info();
2127 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2128 err = PTR_ERR(vport);
2130 goto exit_unlock_free;
2132 if (vport->port_no == OVSP_LOCAL) {
2134 goto exit_unlock_free;
2137 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2138 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
2141 /* the vport deletion may trigger dp headroom update */
2143 if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
2144 must_update_headroom = true;
2145 netdev_reset_rx_headroom(vport->dev);
2146 ovs_dp_detach_port(vport);
2148 if (must_update_headroom)
2149 update_headroom(dp);
2153 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2162 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2164 struct nlattr **a = info->attrs;
2165 struct ovs_header *ovs_header = info->userhdr;
2166 struct sk_buff *reply;
2167 struct vport *vport;
2170 reply = ovs_vport_cmd_alloc_info();
2175 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2176 err = PTR_ERR(vport);
2178 goto exit_unlock_free;
2179 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2180 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2184 return genlmsg_reply(reply, info);
2192 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2194 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2195 struct datapath *dp;
2196 int bucket = cb->args[0], skip = cb->args[1];
2200 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2205 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2206 struct vport *vport;
2209 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2211 ovs_vport_cmd_fill_info(vport, skb,
2212 NETLINK_CB(cb->skb).portid,
2215 OVS_VPORT_CMD_NEW) < 0)
2231 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2232 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2233 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2234 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2235 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2236 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2237 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2240 static struct genl_ops dp_vport_genl_ops[] = {
2241 { .cmd = OVS_VPORT_CMD_NEW,
2242 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2243 .policy = vport_policy,
2244 .doit = ovs_vport_cmd_new
2246 { .cmd = OVS_VPORT_CMD_DEL,
2247 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2248 .policy = vport_policy,
2249 .doit = ovs_vport_cmd_del
2251 { .cmd = OVS_VPORT_CMD_GET,
2252 .flags = 0, /* OK for unprivileged users. */
2253 .policy = vport_policy,
2254 .doit = ovs_vport_cmd_get,
2255 .dumpit = ovs_vport_cmd_dump
2257 { .cmd = OVS_VPORT_CMD_SET,
2258 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2259 .policy = vport_policy,
2260 .doit = ovs_vport_cmd_set,
2264 struct genl_family dp_vport_genl_family = {
2265 .id = GENL_ID_GENERATE,
2266 .hdrsize = sizeof(struct ovs_header),
2267 .name = OVS_VPORT_FAMILY,
2268 .version = OVS_VPORT_VERSION,
2269 .maxattr = OVS_VPORT_ATTR_MAX,
2271 .parallel_ops = true,
2272 .ops = dp_vport_genl_ops,
2273 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2274 .mcgrps = &ovs_dp_vport_multicast_group,
2278 static struct genl_family *dp_genl_families[] = {
2279 &dp_datapath_genl_family,
2280 &dp_vport_genl_family,
2281 &dp_flow_genl_family,
2282 &dp_packet_genl_family,
2285 static void dp_unregister_genl(int n_families)
2289 for (i = 0; i < n_families; i++)
2290 genl_unregister_family(dp_genl_families[i]);
2293 static int dp_register_genl(void)
2298 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2300 err = genl_register_family(dp_genl_families[i]);
2308 dp_unregister_genl(i);
2312 static int __net_init ovs_init_net(struct net *net)
2314 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2316 INIT_LIST_HEAD(&ovs_net->dps);
2317 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2322 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2323 struct list_head *head)
2325 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2326 struct datapath *dp;
2328 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2331 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2332 struct vport *vport;
2334 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2336 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2339 if (dev_net(vport->dev) == dnet)
2340 list_add(&vport->detach_list, head);
2346 static void __net_exit ovs_exit_net(struct net *dnet)
2348 struct datapath *dp, *dp_next;
2349 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2350 struct vport *vport, *vport_next;
2356 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2361 list_vports_from_net(net, dnet, &head);
2364 /* Detach all vports from given namespace. */
2365 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2366 list_del(&vport->detach_list);
2367 ovs_dp_detach_port(vport);
2372 cancel_work_sync(&ovs_net->dp_notify_work);
2375 static struct pernet_operations ovs_net_ops = {
2376 .init = ovs_init_net,
2377 .exit = ovs_exit_net,
2379 .size = sizeof(struct ovs_net),
2382 static int __init dp_init(void)
2386 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2388 pr_info("Open vSwitch switching datapath %s\n", VERSION);
2390 err = compat_init();
2394 err = action_fifos_init();
2396 goto error_compat_exit;
2398 err = ovs_internal_dev_rtnl_link_register();
2400 goto error_action_fifos_exit;
2402 err = ovs_flow_init();
2404 goto error_unreg_rtnl_link;
2406 err = ovs_vport_init();
2408 goto error_flow_exit;
2410 err = register_pernet_device(&ovs_net_ops);
2412 goto error_vport_exit;
2414 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2416 goto error_netns_exit;
2418 err = ovs_netdev_init();
2420 goto error_unreg_notifier;
2422 err = dp_register_genl();
2424 goto error_unreg_netdev;
2430 error_unreg_notifier:
2431 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2433 unregister_pernet_device(&ovs_net_ops);
2438 error_unreg_rtnl_link:
2439 ovs_internal_dev_rtnl_link_unregister();
2440 error_action_fifos_exit:
2441 action_fifos_exit();
2448 static void dp_cleanup(void)
2450 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2452 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2453 unregister_pernet_device(&ovs_net_ops);
2457 ovs_internal_dev_rtnl_link_unregister();
2458 action_fifos_exit();
2462 module_init(dp_init);
2463 module_exit(dp_cleanup);
2465 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2466 MODULE_LICENSE("GPL");
2467 MODULE_VERSION(VERSION);