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 */
443 dp_ifindex = get_dpifindex(dp);
447 if (skb_vlan_tag_present(skb)) {
448 nskb = skb_clone(skb, GFP_ATOMIC);
452 nskb = __vlan_hwaccel_push_inside(nskb);
459 if (nla_attr_size(skb->len) > USHRT_MAX) {
464 /* Complete checksum if needed */
465 if (skb->ip_summed == CHECKSUM_PARTIAL &&
466 (err = skb_checksum_help(skb)))
469 /* Older versions of OVS user space enforce alignment of the last
470 * Netlink attribute to NLA_ALIGNTO which would require extensive
471 * padding logic. Only perform zerocopy if padding is not required.
473 if (dp->user_features & OVS_DP_F_UNALIGNED)
474 hlen = skb_zerocopy_headlen(skb);
478 len = upcall_msg_size(upcall_info, hlen - cutlen);
479 user_skb = genlmsg_new(len, GFP_ATOMIC);
485 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
486 0, upcall_info->cmd);
487 upcall->dp_ifindex = dp_ifindex;
489 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
492 if (upcall_info->userdata)
493 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
494 nla_len(upcall_info->userdata),
495 nla_data(upcall_info->userdata));
498 if (upcall_info->egress_tun_info) {
499 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
500 err = ovs_nla_put_tunnel_info(user_skb,
501 upcall_info->egress_tun_info);
503 nla_nest_end(user_skb, nla);
506 if (upcall_info->actions_len) {
507 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS);
508 err = ovs_nla_put_actions(upcall_info->actions,
509 upcall_info->actions_len,
512 nla_nest_end(user_skb, nla);
514 nla_nest_cancel(user_skb, nla);
517 /* Add OVS_PACKET_ATTR_MRU */
518 if (upcall_info->mru) {
519 if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
524 pad_packet(dp, user_skb);
527 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
529 if (nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN,
534 pad_packet(dp, user_skb);
537 /* Only reserve room for attribute header, packet data is added
540 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
544 nla->nla_len = nla_attr_size(skb->len - cutlen);
546 err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
550 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
551 pad_packet(dp, user_skb);
553 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
555 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
565 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
567 struct ovs_header *ovs_header = info->userhdr;
568 struct net *net = sock_net(skb->sk);
569 struct nlattr **a = info->attrs;
570 struct sw_flow_actions *acts;
571 struct sk_buff *packet;
572 struct sw_flow *flow;
573 struct sw_flow_actions *sf_acts;
576 struct vport *input_vport;
580 bool log = !a[OVS_PACKET_ATTR_PROBE];
583 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
584 !a[OVS_PACKET_ATTR_ACTIONS])
587 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
588 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
592 skb_reserve(packet, NET_IP_ALIGN);
594 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
596 skb_reset_mac_header(packet);
597 eth = eth_hdr(packet);
599 /* Normally, setting the skb 'protocol' field would be handled by a
600 * call to eth_type_trans(), but it assumes there's a sending
601 * device, which we may not have.
603 if (eth_proto_is_802_3(eth->h_proto))
604 packet->protocol = eth->h_proto;
606 packet->protocol = htons(ETH_P_802_2);
608 /* Set packet's mru */
609 if (a[OVS_PACKET_ATTR_MRU]) {
610 mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
611 packet->ignore_df = 1;
613 OVS_CB(packet)->mru = mru;
615 /* Build an sw_flow for sending this packet. */
616 flow = ovs_flow_alloc();
621 err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
622 packet, &flow->key, log);
626 err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
627 &flow->key, &acts, log);
631 rcu_assign_pointer(flow->sf_acts, acts);
632 packet->priority = flow->key.phy.priority;
633 packet->mark = flow->key.phy.skb_mark;
636 dp = get_dp_rcu(net, ovs_header->dp_ifindex);
641 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
643 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
648 packet->dev = input_vport->dev;
649 OVS_CB(packet)->input_vport = input_vport;
650 sf_acts = rcu_dereference(flow->sf_acts);
653 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
657 ovs_flow_free(flow, false);
663 ovs_flow_free(flow, false);
670 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
671 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
672 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
673 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
674 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
675 [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
678 static struct genl_ops dp_packet_genl_ops[] = {
679 { .cmd = OVS_PACKET_CMD_EXECUTE,
680 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
681 .policy = packet_policy,
682 .doit = ovs_packet_cmd_execute
686 static struct genl_family dp_packet_genl_family = {
687 .id = GENL_ID_GENERATE,
688 .hdrsize = sizeof(struct ovs_header),
689 .name = OVS_PACKET_FAMILY,
690 .version = OVS_PACKET_VERSION,
691 .maxattr = OVS_PACKET_ATTR_MAX,
693 .parallel_ops = true,
694 .ops = dp_packet_genl_ops,
695 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
698 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
699 struct ovs_dp_megaflow_stats *mega_stats)
703 memset(mega_stats, 0, sizeof(*mega_stats));
705 stats->n_flows = ovs_flow_tbl_count(&dp->table);
706 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
708 stats->n_hit = stats->n_missed = stats->n_lost = 0;
710 for_each_possible_cpu(i) {
711 const struct dp_stats_percpu *percpu_stats;
712 struct dp_stats_percpu local_stats;
715 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
718 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
719 local_stats = *percpu_stats;
720 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
722 stats->n_hit += local_stats.n_hit;
723 stats->n_missed += local_stats.n_missed;
724 stats->n_lost += local_stats.n_lost;
725 mega_stats->n_mask_hit += local_stats.n_mask_hit;
729 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
731 return ovs_identifier_is_ufid(sfid) &&
732 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
735 static bool should_fill_mask(uint32_t ufid_flags)
737 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
740 static bool should_fill_actions(uint32_t ufid_flags)
742 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
745 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
746 const struct sw_flow_id *sfid,
749 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
751 /* OVS_FLOW_ATTR_UFID */
752 if (sfid && ovs_identifier_is_ufid(sfid))
753 len += nla_total_size(sfid->ufid_len);
755 /* OVS_FLOW_ATTR_KEY */
756 if (!sfid || should_fill_key(sfid, ufid_flags))
757 len += nla_total_size(ovs_key_attr_size());
759 /* OVS_FLOW_ATTR_MASK */
760 if (should_fill_mask(ufid_flags))
761 len += nla_total_size(ovs_key_attr_size());
763 /* OVS_FLOW_ATTR_ACTIONS */
764 if (should_fill_actions(ufid_flags))
765 len += nla_total_size(acts->orig_len);
768 + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
769 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
770 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
773 /* Called with ovs_mutex or RCU read lock. */
774 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
777 struct ovs_flow_stats stats;
781 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
784 nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
788 if (stats.n_packets &&
789 nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
790 sizeof(struct ovs_flow_stats), &stats,
794 if ((u8)ntohs(tcp_flags) &&
795 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
801 /* Called with ovs_mutex or RCU read lock. */
802 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
803 struct sk_buff *skb, int skb_orig_len)
805 struct nlattr *start;
808 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
809 * this is the first flow to be dumped into 'skb'. This is unusual for
810 * Netlink but individual action lists can be longer than
811 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
812 * The userspace caller can always fetch the actions separately if it
813 * really wants them. (Most userspace callers in fact don't care.)
815 * This can only fail for dump operations because the skb is always
816 * properly sized for single flows.
818 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
820 const struct sw_flow_actions *sf_acts;
822 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
823 err = ovs_nla_put_actions(sf_acts->actions,
824 sf_acts->actions_len, skb);
827 nla_nest_end(skb, start);
832 nla_nest_cancel(skb, start);
834 } else if (skb_orig_len) {
841 /* Called with ovs_mutex or RCU read lock. */
842 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
843 struct sk_buff *skb, u32 portid,
844 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
846 const int skb_orig_len = skb->len;
847 struct ovs_header *ovs_header;
850 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
855 ovs_header->dp_ifindex = dp_ifindex;
857 err = ovs_nla_put_identifier(flow, skb);
861 if (should_fill_key(&flow->id, ufid_flags)) {
862 err = ovs_nla_put_masked_key(flow, skb);
867 if (should_fill_mask(ufid_flags)) {
868 err = ovs_nla_put_mask(flow, skb);
873 err = ovs_flow_cmd_fill_stats(flow, skb);
877 if (should_fill_actions(ufid_flags)) {
878 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
883 genlmsg_end(skb, ovs_header);
887 genlmsg_cancel(skb, ovs_header);
891 /* May not be called with RCU read lock. */
892 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
893 const struct sw_flow_id *sfid,
894 struct genl_info *info,
901 if (!always && !ovs_must_notify(&dp_flow_genl_family, info,
902 GROUP_ID(&ovs_dp_flow_multicast_group)))
905 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
906 skb = genlmsg_new(len, GFP_KERNEL);
908 return ERR_PTR(-ENOMEM);
913 /* Called with ovs_mutex. */
914 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
916 struct genl_info *info, u8 cmd,
917 bool always, u32 ufid_flags)
922 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
923 &flow->id, info, always, ufid_flags);
924 if (IS_ERR_OR_NULL(skb))
927 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
928 info->snd_portid, info->snd_seq, 0,
934 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
936 struct net *net = sock_net(skb->sk);
937 struct nlattr **a = info->attrs;
938 struct ovs_header *ovs_header = info->userhdr;
939 struct sw_flow *flow = NULL, *new_flow;
940 struct sw_flow_mask mask;
941 struct sk_buff *reply;
943 struct sw_flow_key key;
944 struct sw_flow_actions *acts;
945 struct sw_flow_match match;
946 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
948 bool log = !a[OVS_FLOW_ATTR_PROBE];
950 /* Must have key and actions. */
952 if (!a[OVS_FLOW_ATTR_KEY]) {
953 OVS_NLERR(log, "Flow key attr not present in new flow.");
956 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
957 OVS_NLERR(log, "Flow actions attr not present in new flow.");
961 /* Most of the time we need to allocate a new flow, do it before
964 new_flow = ovs_flow_alloc();
965 if (IS_ERR(new_flow)) {
966 error = PTR_ERR(new_flow);
971 ovs_match_init(&match, &key, &mask);
972 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
973 a[OVS_FLOW_ATTR_MASK], log);
977 ovs_flow_mask_key(&new_flow->key, &key, true, &mask);
979 /* Extract flow identifier. */
980 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
985 /* Validate actions. */
986 error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
987 &new_flow->key, &acts, log);
989 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
993 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
996 error = PTR_ERR(reply);
1001 dp = get_dp(net, ovs_header->dp_ifindex);
1002 if (unlikely(!dp)) {
1004 goto err_unlock_ovs;
1007 /* Check if this is a duplicate flow */
1008 if (ovs_identifier_is_ufid(&new_flow->id))
1009 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
1011 flow = ovs_flow_tbl_lookup(&dp->table, &key);
1012 if (likely(!flow)) {
1013 rcu_assign_pointer(new_flow->sf_acts, acts);
1015 /* Put flow in bucket. */
1016 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
1017 if (unlikely(error)) {
1019 goto err_unlock_ovs;
1022 if (unlikely(reply)) {
1023 error = ovs_flow_cmd_fill_info(new_flow,
1024 ovs_header->dp_ifindex,
1025 reply, info->snd_portid,
1033 struct sw_flow_actions *old_acts;
1035 /* Bail out if we're not allowed to modify an existing flow.
1036 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1037 * because Generic Netlink treats the latter as a dump
1038 * request. We also accept NLM_F_EXCL in case that bug ever
1041 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
1044 goto err_unlock_ovs;
1046 /* The flow identifier has to be the same for flow updates.
1047 * Look for any overlapping flow.
1049 if (unlikely(!ovs_flow_cmp(flow, &match))) {
1050 if (ovs_identifier_is_key(&flow->id))
1051 flow = ovs_flow_tbl_lookup_exact(&dp->table,
1053 else /* UFID matches but key is different */
1057 goto err_unlock_ovs;
1060 /* Update actions. */
1061 old_acts = ovsl_dereference(flow->sf_acts);
1062 rcu_assign_pointer(flow->sf_acts, acts);
1064 if (unlikely(reply)) {
1065 error = ovs_flow_cmd_fill_info(flow,
1066 ovs_header->dp_ifindex,
1067 reply, info->snd_portid,
1075 ovs_nla_free_flow_actions_rcu(old_acts);
1076 ovs_flow_free(new_flow, false);
1080 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1087 ovs_nla_free_flow_actions(acts);
1089 ovs_flow_free(new_flow, false);
1094 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1095 static struct sw_flow_actions *get_flow_actions(struct net *net,
1096 const struct nlattr *a,
1097 const struct sw_flow_key *key,
1098 const struct sw_flow_mask *mask,
1101 struct sw_flow_actions *acts;
1102 struct sw_flow_key masked_key;
1105 ovs_flow_mask_key(&masked_key, key, true, mask);
1106 error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
1109 "Actions may not be safe on all matching packets");
1110 return ERR_PTR(error);
1116 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1118 struct net *net = sock_net(skb->sk);
1119 struct nlattr **a = info->attrs;
1120 struct ovs_header *ovs_header = info->userhdr;
1121 struct sw_flow_key key;
1122 struct sw_flow *flow;
1123 struct sw_flow_mask mask;
1124 struct sk_buff *reply = NULL;
1125 struct datapath *dp;
1126 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1127 struct sw_flow_match match;
1128 struct sw_flow_id sfid;
1129 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1131 bool log = !a[OVS_FLOW_ATTR_PROBE];
1134 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1135 if (a[OVS_FLOW_ATTR_KEY]) {
1136 ovs_match_init(&match, &key, &mask);
1137 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1138 a[OVS_FLOW_ATTR_MASK], log);
1139 } else if (!ufid_present) {
1141 "Flow set message rejected, Key attribute missing.");
1147 /* Validate actions. */
1148 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1149 if (!a[OVS_FLOW_ATTR_KEY]) {
1151 "Flow key attribute not present in set flow.");
1156 acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], &key,
1159 error = PTR_ERR(acts);
1163 /* Can allocate before locking if have acts. */
1164 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1166 if (IS_ERR(reply)) {
1167 error = PTR_ERR(reply);
1168 goto err_kfree_acts;
1173 dp = get_dp(net, ovs_header->dp_ifindex);
1174 if (unlikely(!dp)) {
1176 goto err_unlock_ovs;
1178 /* Check that the flow exists. */
1180 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1182 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1183 if (unlikely(!flow)) {
1185 goto err_unlock_ovs;
1188 /* Update actions, if present. */
1190 old_acts = ovsl_dereference(flow->sf_acts);
1191 rcu_assign_pointer(flow->sf_acts, acts);
1193 if (unlikely(reply)) {
1194 error = ovs_flow_cmd_fill_info(flow,
1195 ovs_header->dp_ifindex,
1196 reply, info->snd_portid,
1203 /* Could not alloc without acts before locking. */
1204 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1205 info, OVS_FLOW_CMD_NEW, false,
1208 if (unlikely(IS_ERR(reply))) {
1209 error = PTR_ERR(reply);
1210 goto err_unlock_ovs;
1215 if (a[OVS_FLOW_ATTR_CLEAR])
1216 ovs_flow_stats_clear(flow);
1220 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1222 ovs_nla_free_flow_actions_rcu(old_acts);
1230 ovs_nla_free_flow_actions(acts);
1235 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1237 struct nlattr **a = info->attrs;
1238 struct ovs_header *ovs_header = info->userhdr;
1239 struct net *net = sock_net(skb->sk);
1240 struct sw_flow_key key;
1241 struct sk_buff *reply;
1242 struct sw_flow *flow;
1243 struct datapath *dp;
1244 struct sw_flow_match match;
1245 struct sw_flow_id ufid;
1246 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1248 bool log = !a[OVS_FLOW_ATTR_PROBE];
1251 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1252 if (a[OVS_FLOW_ATTR_KEY]) {
1253 ovs_match_init(&match, &key, NULL);
1254 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
1256 } else if (!ufid_present) {
1258 "Flow get message rejected, Key attribute missing.");
1265 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1272 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1274 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1280 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1281 OVS_FLOW_CMD_NEW, true, ufid_flags);
1282 if (IS_ERR(reply)) {
1283 err = PTR_ERR(reply);
1288 return genlmsg_reply(reply, info);
1294 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1296 struct nlattr **a = info->attrs;
1297 struct ovs_header *ovs_header = info->userhdr;
1298 struct net *net = sock_net(skb->sk);
1299 struct sw_flow_key key;
1300 struct sk_buff *reply;
1301 struct sw_flow *flow = NULL;
1302 struct datapath *dp;
1303 struct sw_flow_match match;
1304 struct sw_flow_id ufid;
1305 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1307 bool log = !a[OVS_FLOW_ATTR_PROBE];
1310 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1311 if (a[OVS_FLOW_ATTR_KEY]) {
1312 ovs_match_init(&match, &key, NULL);
1313 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1320 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1321 if (unlikely(!dp)) {
1326 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1327 err = ovs_flow_tbl_flush(&dp->table);
1332 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1334 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1335 if (unlikely(!flow)) {
1340 ovs_flow_tbl_remove(&dp->table, flow);
1343 reply = ovs_flow_cmd_alloc_info(rcu_dereference_raw(flow->sf_acts),
1344 &flow->id, info, false, ufid_flags);
1346 if (likely(reply)) {
1347 if (likely(!IS_ERR(reply))) {
1348 rcu_read_lock(); /*To keep RCU checker happy. */
1349 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1350 reply, info->snd_portid,
1356 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1358 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1359 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1364 ovs_flow_free(flow, true);
1371 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1373 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1374 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1375 struct table_instance *ti;
1376 struct datapath *dp;
1380 err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
1381 OVS_FLOW_ATTR_MAX, flow_policy);
1384 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1387 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1393 ti = rcu_dereference(dp->table.ti);
1395 struct sw_flow *flow;
1398 bucket = cb->args[0];
1400 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1404 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1405 NETLINK_CB(cb->skb).portid,
1406 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1407 OVS_FLOW_CMD_NEW, ufid_flags) < 0)
1410 cb->args[0] = bucket;
1417 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1418 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1419 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1420 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1421 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1422 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1423 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1424 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1427 static struct genl_ops dp_flow_genl_ops[] = {
1428 { .cmd = OVS_FLOW_CMD_NEW,
1429 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1430 .policy = flow_policy,
1431 .doit = ovs_flow_cmd_new
1433 { .cmd = OVS_FLOW_CMD_DEL,
1434 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1435 .policy = flow_policy,
1436 .doit = ovs_flow_cmd_del
1438 { .cmd = OVS_FLOW_CMD_GET,
1439 .flags = 0, /* OK for unprivileged users. */
1440 .policy = flow_policy,
1441 .doit = ovs_flow_cmd_get,
1442 .dumpit = ovs_flow_cmd_dump
1444 { .cmd = OVS_FLOW_CMD_SET,
1445 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1446 .policy = flow_policy,
1447 .doit = ovs_flow_cmd_set,
1451 static struct genl_family dp_flow_genl_family = {
1452 .id = GENL_ID_GENERATE,
1453 .hdrsize = sizeof(struct ovs_header),
1454 .name = OVS_FLOW_FAMILY,
1455 .version = OVS_FLOW_VERSION,
1456 .maxattr = OVS_FLOW_ATTR_MAX,
1458 .parallel_ops = true,
1459 .ops = dp_flow_genl_ops,
1460 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1461 .mcgrps = &ovs_dp_flow_multicast_group,
1465 static size_t ovs_dp_cmd_msg_size(void)
1467 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1469 msgsize += nla_total_size(IFNAMSIZ);
1470 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
1471 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
1472 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1477 /* Called with ovs_mutex. */
1478 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1479 u32 portid, u32 seq, u32 flags, u8 cmd)
1481 struct ovs_header *ovs_header;
1482 struct ovs_dp_stats dp_stats;
1483 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1486 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1491 ovs_header->dp_ifindex = get_dpifindex(dp);
1493 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1495 goto nla_put_failure;
1497 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1498 if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1499 &dp_stats, OVS_DP_ATTR_PAD))
1500 goto nla_put_failure;
1502 if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1503 sizeof(struct ovs_dp_megaflow_stats),
1504 &dp_megaflow_stats, OVS_DP_ATTR_PAD))
1505 goto nla_put_failure;
1507 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1508 goto nla_put_failure;
1510 genlmsg_end(skb, ovs_header);
1514 genlmsg_cancel(skb, ovs_header);
1519 static struct sk_buff *ovs_dp_cmd_alloc_info(void)
1521 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1524 /* Called with rcu_read_lock or ovs_mutex. */
1525 static struct datapath *lookup_datapath(struct net *net,
1526 const struct ovs_header *ovs_header,
1527 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1529 struct datapath *dp;
1531 if (!a[OVS_DP_ATTR_NAME])
1532 dp = get_dp(net, ovs_header->dp_ifindex);
1534 struct vport *vport;
1536 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1537 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1539 return dp ? dp : ERR_PTR(-ENODEV);
1542 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1544 struct datapath *dp;
1546 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1550 WARN(dp->user_features, "Dropping previously announced user features\n");
1551 dp->user_features = 0;
1554 static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1556 if (a[OVS_DP_ATTR_USER_FEATURES])
1557 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1560 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1562 struct nlattr **a = info->attrs;
1563 struct vport_parms parms;
1564 struct sk_buff *reply;
1565 struct datapath *dp;
1566 struct vport *vport;
1567 struct ovs_net *ovs_net;
1571 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1574 reply = ovs_dp_cmd_alloc_info();
1579 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1581 goto err_free_reply;
1583 ovs_dp_set_net(dp, sock_net(skb->sk));
1585 /* Allocate table. */
1586 err = ovs_flow_tbl_init(&dp->table);
1590 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1591 if (!dp->stats_percpu) {
1593 goto err_destroy_table;
1596 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1600 goto err_destroy_percpu;
1603 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1604 INIT_HLIST_HEAD(&dp->ports[i]);
1606 /* Set up our datapath device. */
1607 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1608 parms.type = OVS_VPORT_TYPE_INTERNAL;
1609 parms.options = NULL;
1611 parms.port_no = OVSP_LOCAL;
1612 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1614 ovs_dp_change(dp, a);
1616 /* So far only local changes have been made, now need the lock. */
1619 vport = new_vport(&parms);
1620 if (IS_ERR(vport)) {
1621 err = PTR_ERR(vport);
1625 if (err == -EEXIST) {
1626 /* An outdated user space instance that does not understand
1627 * the concept of user_features has attempted to create a new
1628 * datapath and is likely to reuse it. Drop all user features.
1630 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1631 ovs_dp_reset_user_features(skb, info);
1634 goto err_destroy_ports_array;
1637 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1638 info->snd_seq, 0, OVS_DP_CMD_NEW);
1641 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1642 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1646 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1649 err_destroy_ports_array:
1653 free_percpu(dp->stats_percpu);
1655 ovs_flow_tbl_destroy(&dp->table);
1664 /* Called with ovs_mutex. */
1665 static void __dp_destroy(struct datapath *dp)
1669 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1670 struct vport *vport;
1671 struct hlist_node *n;
1673 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1674 if (vport->port_no != OVSP_LOCAL)
1675 ovs_dp_detach_port(vport);
1678 list_del_rcu(&dp->list_node);
1680 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1681 * all ports in datapath are destroyed first before freeing datapath.
1683 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1685 /* RCU destroy the flow table */
1686 call_rcu(&dp->rcu, destroy_dp_rcu);
1689 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1691 struct sk_buff *reply;
1692 struct datapath *dp;
1695 reply = ovs_dp_cmd_alloc_info();
1700 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1703 goto err_unlock_free;
1705 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1706 info->snd_seq, 0, OVS_DP_CMD_DEL);
1712 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1721 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1723 struct sk_buff *reply;
1724 struct datapath *dp;
1727 reply = ovs_dp_cmd_alloc_info();
1732 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1735 goto err_unlock_free;
1737 ovs_dp_change(dp, info->attrs);
1739 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1740 info->snd_seq, 0, OVS_DP_CMD_NEW);
1745 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1754 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1756 struct sk_buff *reply;
1757 struct datapath *dp;
1760 reply = ovs_dp_cmd_alloc_info();
1765 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1768 goto err_unlock_free;
1770 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1771 info->snd_seq, 0, OVS_DP_CMD_NEW);
1775 return genlmsg_reply(reply, info);
1783 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1785 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1786 struct datapath *dp;
1787 int skip = cb->args[0];
1791 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1793 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1794 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1795 OVS_DP_CMD_NEW) < 0)
1806 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1807 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1808 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1809 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1812 static struct genl_ops dp_datapath_genl_ops[] = {
1813 { .cmd = OVS_DP_CMD_NEW,
1814 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1815 .policy = datapath_policy,
1816 .doit = ovs_dp_cmd_new
1818 { .cmd = OVS_DP_CMD_DEL,
1819 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1820 .policy = datapath_policy,
1821 .doit = ovs_dp_cmd_del
1823 { .cmd = OVS_DP_CMD_GET,
1824 .flags = 0, /* OK for unprivileged users. */
1825 .policy = datapath_policy,
1826 .doit = ovs_dp_cmd_get,
1827 .dumpit = ovs_dp_cmd_dump
1829 { .cmd = OVS_DP_CMD_SET,
1830 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1831 .policy = datapath_policy,
1832 .doit = ovs_dp_cmd_set,
1836 static struct genl_family dp_datapath_genl_family = {
1837 .id = GENL_ID_GENERATE,
1838 .hdrsize = sizeof(struct ovs_header),
1839 .name = OVS_DATAPATH_FAMILY,
1840 .version = OVS_DATAPATH_VERSION,
1841 .maxattr = OVS_DP_ATTR_MAX,
1843 .parallel_ops = true,
1844 .ops = dp_datapath_genl_ops,
1845 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1846 .mcgrps = &ovs_dp_datapath_multicast_group,
1850 /* Called with ovs_mutex or RCU read lock. */
1851 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1852 u32 portid, u32 seq, u32 flags, u8 cmd)
1854 struct ovs_header *ovs_header;
1855 struct ovs_vport_stats vport_stats;
1858 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1863 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1865 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1866 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1867 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1868 ovs_vport_name(vport)))
1869 goto nla_put_failure;
1871 ovs_vport_get_stats(vport, &vport_stats);
1872 if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
1873 sizeof(struct ovs_vport_stats), &vport_stats,
1874 OVS_VPORT_ATTR_PAD))
1875 goto nla_put_failure;
1877 if (ovs_vport_get_upcall_portids(vport, skb))
1878 goto nla_put_failure;
1880 err = ovs_vport_get_options(vport, skb);
1881 if (err == -EMSGSIZE)
1884 genlmsg_end(skb, ovs_header);
1890 genlmsg_cancel(skb, ovs_header);
1894 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1896 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1899 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1900 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1903 struct sk_buff *skb;
1906 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1908 return ERR_PTR(-ENOMEM);
1910 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1916 /* Called with ovs_mutex or RCU read lock. */
1917 static struct vport *lookup_vport(struct net *net,
1918 const struct ovs_header *ovs_header,
1919 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1921 struct datapath *dp;
1922 struct vport *vport;
1924 if (a[OVS_VPORT_ATTR_NAME]) {
1925 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1927 return ERR_PTR(-ENODEV);
1928 if (ovs_header->dp_ifindex &&
1929 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1930 return ERR_PTR(-ENODEV);
1932 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1933 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1935 if (port_no >= DP_MAX_PORTS)
1936 return ERR_PTR(-EFBIG);
1938 dp = get_dp(net, ovs_header->dp_ifindex);
1940 return ERR_PTR(-ENODEV);
1942 vport = ovs_vport_ovsl_rcu(dp, port_no);
1944 return ERR_PTR(-ENODEV);
1947 return ERR_PTR(-EINVAL);
1950 /* Called with ovs_mutex */
1951 static void update_headroom(struct datapath *dp)
1953 unsigned dev_headroom, max_headroom = 0;
1954 struct net_device *dev;
1955 struct vport *vport;
1958 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1959 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1961 dev_headroom = netdev_get_fwd_headroom(dev);
1962 if (dev_headroom > max_headroom)
1963 max_headroom = dev_headroom;
1967 dp->max_headroom = max_headroom;
1968 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1969 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
1970 netdev_set_rx_headroom(vport->dev, max_headroom);
1973 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1975 struct nlattr **a = info->attrs;
1976 struct ovs_header *ovs_header = info->userhdr;
1977 struct vport_parms parms;
1978 struct sk_buff *reply;
1979 struct vport *vport;
1980 struct datapath *dp;
1984 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1985 !a[OVS_VPORT_ATTR_UPCALL_PID])
1988 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1989 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1990 if (port_no >= DP_MAX_PORTS)
1993 reply = ovs_vport_cmd_alloc_info();
1999 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2002 goto exit_unlock_free;
2005 vport = ovs_vport_ovsl(dp, port_no);
2008 goto exit_unlock_free;
2010 for (port_no = 1; ; port_no++) {
2011 if (port_no >= DP_MAX_PORTS) {
2013 goto exit_unlock_free;
2015 vport = ovs_vport_ovsl(dp, port_no);
2021 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2022 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2023 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2025 parms.port_no = port_no;
2026 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
2028 vport = new_vport(&parms);
2029 err = PTR_ERR(vport);
2030 if (IS_ERR(vport)) {
2033 goto exit_unlock_free;
2036 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2037 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2040 if (netdev_get_fwd_headroom(vport->dev) > dp->max_headroom)
2041 update_headroom(dp);
2043 netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2047 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2056 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2058 struct nlattr **a = info->attrs;
2059 struct sk_buff *reply;
2060 struct vport *vport;
2063 reply = ovs_vport_cmd_alloc_info();
2068 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2069 err = PTR_ERR(vport);
2071 goto exit_unlock_free;
2073 if (a[OVS_VPORT_ATTR_TYPE] &&
2074 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2076 goto exit_unlock_free;
2079 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2080 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2082 goto exit_unlock_free;
2085 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2086 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2088 err = ovs_vport_set_upcall_portids(vport, ids);
2090 goto exit_unlock_free;
2093 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2094 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2098 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2107 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2109 bool must_update_headroom = false;
2110 struct nlattr **a = info->attrs;
2111 struct sk_buff *reply;
2112 struct datapath *dp;
2113 struct vport *vport;
2116 reply = ovs_vport_cmd_alloc_info();
2121 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2122 err = PTR_ERR(vport);
2124 goto exit_unlock_free;
2126 if (vport->port_no == OVSP_LOCAL) {
2128 goto exit_unlock_free;
2131 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2132 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
2135 /* the vport deletion may trigger dp headroom update */
2137 if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
2138 must_update_headroom = true;
2139 netdev_reset_rx_headroom(vport->dev);
2140 ovs_dp_detach_port(vport);
2142 if (must_update_headroom)
2143 update_headroom(dp);
2147 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2156 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2158 struct nlattr **a = info->attrs;
2159 struct ovs_header *ovs_header = info->userhdr;
2160 struct sk_buff *reply;
2161 struct vport *vport;
2164 reply = ovs_vport_cmd_alloc_info();
2169 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2170 err = PTR_ERR(vport);
2172 goto exit_unlock_free;
2173 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2174 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2178 return genlmsg_reply(reply, info);
2186 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2188 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2189 struct datapath *dp;
2190 int bucket = cb->args[0], skip = cb->args[1];
2194 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2199 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2200 struct vport *vport;
2203 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2205 ovs_vport_cmd_fill_info(vport, skb,
2206 NETLINK_CB(cb->skb).portid,
2209 OVS_VPORT_CMD_NEW) < 0)
2225 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2226 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2227 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2228 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2229 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2230 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2231 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2234 static struct genl_ops dp_vport_genl_ops[] = {
2235 { .cmd = OVS_VPORT_CMD_NEW,
2236 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2237 .policy = vport_policy,
2238 .doit = ovs_vport_cmd_new
2240 { .cmd = OVS_VPORT_CMD_DEL,
2241 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2242 .policy = vport_policy,
2243 .doit = ovs_vport_cmd_del
2245 { .cmd = OVS_VPORT_CMD_GET,
2246 .flags = 0, /* OK for unprivileged users. */
2247 .policy = vport_policy,
2248 .doit = ovs_vport_cmd_get,
2249 .dumpit = ovs_vport_cmd_dump
2251 { .cmd = OVS_VPORT_CMD_SET,
2252 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2253 .policy = vport_policy,
2254 .doit = ovs_vport_cmd_set,
2258 struct genl_family dp_vport_genl_family = {
2259 .id = GENL_ID_GENERATE,
2260 .hdrsize = sizeof(struct ovs_header),
2261 .name = OVS_VPORT_FAMILY,
2262 .version = OVS_VPORT_VERSION,
2263 .maxattr = OVS_VPORT_ATTR_MAX,
2265 .parallel_ops = true,
2266 .ops = dp_vport_genl_ops,
2267 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2268 .mcgrps = &ovs_dp_vport_multicast_group,
2272 static struct genl_family *dp_genl_families[] = {
2273 &dp_datapath_genl_family,
2274 &dp_vport_genl_family,
2275 &dp_flow_genl_family,
2276 &dp_packet_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 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) {
2330 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2333 if (dev_net(vport->dev) == dnet)
2334 list_add(&vport->detach_list, head);
2340 static void __net_exit ovs_exit_net(struct net *dnet)
2342 struct datapath *dp, *dp_next;
2343 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2344 struct vport *vport, *vport_next;
2350 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2355 list_vports_from_net(net, dnet, &head);
2358 /* Detach all vports from given namespace. */
2359 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2360 list_del(&vport->detach_list);
2361 ovs_dp_detach_port(vport);
2366 cancel_work_sync(&ovs_net->dp_notify_work);
2369 static struct pernet_operations ovs_net_ops = {
2370 .init = ovs_init_net,
2371 .exit = ovs_exit_net,
2373 .size = sizeof(struct ovs_net),
2376 static int __init dp_init(void)
2380 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2382 pr_info("Open vSwitch switching datapath %s\n", VERSION);
2384 err = compat_init();
2388 err = action_fifos_init();
2390 goto error_compat_exit;
2392 err = ovs_internal_dev_rtnl_link_register();
2394 goto error_action_fifos_exit;
2396 err = ovs_flow_init();
2398 goto error_unreg_rtnl_link;
2400 err = ovs_vport_init();
2402 goto error_flow_exit;
2404 err = register_pernet_device(&ovs_net_ops);
2406 goto error_vport_exit;
2408 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2410 goto error_netns_exit;
2412 err = ovs_netdev_init();
2414 goto error_unreg_notifier;
2416 err = dp_register_genl();
2418 goto error_unreg_netdev;
2424 error_unreg_notifier:
2425 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2427 unregister_pernet_device(&ovs_net_ops);
2432 error_unreg_rtnl_link:
2433 ovs_internal_dev_rtnl_link_unregister();
2434 error_action_fifos_exit:
2435 action_fifos_exit();
2442 static void dp_cleanup(void)
2444 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2446 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2447 unregister_pernet_device(&ovs_net_ops);
2451 ovs_internal_dev_rtnl_link_unregister();
2452 action_fifos_exit();
2456 module_init(dp_init);
2457 module_exit(dp_cleanup);
2459 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2460 MODULE_LICENSE("GPL");
2461 MODULE_VERSION(VERSION);