2 * Copyright (c) 2007-2014 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.h>
27 #include <linux/jhash.h>
28 #include <linux/delay.h>
29 #include <linux/time.h>
30 #include <linux/etherdevice.h>
31 #include <linux/genetlink.h>
32 #include <linux/kernel.h>
33 #include <linux/kthread.h>
34 #include <linux/mutex.h>
35 #include <linux/percpu.h>
36 #include <linux/rcupdate.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/ethtool.h>
40 #include <linux/wait.h>
41 #include <asm/div64.h>
42 #include <linux/highmem.h>
43 #include <linux/netfilter_bridge.h>
44 #include <linux/netfilter_ipv4.h>
45 #include <linux/inetdevice.h>
46 #include <linux/list.h>
47 #include <linux/openvswitch.h>
48 #include <linux/rculist.h>
49 #include <linux/dmi.h>
50 #include <net/genetlink.h>
51 #include <net/net_namespace.h>
52 #include <net/netns/generic.h>
56 #include "flow_table.h"
57 #include "flow_netlink.h"
58 #include "vport-internal_dev.h"
59 #include "vport-netdev.h"
61 int ovs_net_id __read_mostly;
62 EXPORT_SYMBOL_GPL(ovs_net_id);
64 static struct genl_family dp_packet_genl_family;
65 static struct genl_family dp_flow_genl_family;
66 static struct genl_family dp_datapath_genl_family;
68 static const struct nla_policy flow_policy[];
70 static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
71 .name = OVS_FLOW_MCGROUP,
74 static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
75 .name = OVS_DATAPATH_MCGROUP,
78 static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
79 .name = OVS_VPORT_MCGROUP,
82 /* Check if need to build a reply message.
83 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
84 static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
87 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
88 genl_has_listeners(family, genl_info_net(info), group);
91 static void ovs_notify(struct genl_family *family,
92 struct sk_buff *skb, struct genl_info *info)
94 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
95 0, info->nlhdr, GFP_KERNEL);
101 * All writes e.g. Writes to device state (add/remove datapath, port, set
102 * operations on vports, etc.), Writes to other state (flow table
103 * modifications, set miscellaneous datapath parameters, etc.) are protected
106 * Reads are protected by RCU.
108 * There are a few special cases (mostly stats) that have their own
109 * synchronization but they nest under all of above and don't interact with
112 * The RTNL lock nests inside ovs_mutex.
115 static DEFINE_MUTEX(ovs_mutex);
119 mutex_lock(&ovs_mutex);
122 void ovs_unlock(void)
124 mutex_unlock(&ovs_mutex);
127 #ifdef CONFIG_LOCKDEP
128 int lockdep_ovsl_is_held(void)
131 return lockdep_is_held(&ovs_mutex);
135 EXPORT_SYMBOL_GPL(lockdep_ovsl_is_held);
138 static struct vport *new_vport(const struct vport_parms *);
139 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
140 const struct sw_flow_key *,
141 const struct dp_upcall_info *);
142 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
143 const struct sw_flow_key *,
144 const struct dp_upcall_info *);
146 /* Must be called with rcu_read_lock. */
147 static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
149 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
152 struct vport *vport = ovs_internal_dev_get_vport(dev);
160 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
161 * returned dp pointer valid.
163 static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
167 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
169 dp = get_dp_rcu(net, dp_ifindex);
175 /* Must be called with rcu_read_lock or ovs_mutex. */
176 const char *ovs_dp_name(const struct datapath *dp)
178 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
179 return vport->ops->get_name(vport);
182 static int get_dpifindex(const struct datapath *dp)
189 local = ovs_vport_rcu(dp, OVSP_LOCAL);
191 ifindex = netdev_vport_priv(local)->dev->ifindex;
200 static void destroy_dp_rcu(struct rcu_head *rcu)
202 struct datapath *dp = container_of(rcu, struct datapath, rcu);
204 ovs_flow_tbl_destroy(&dp->table);
205 free_percpu(dp->stats_percpu);
210 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
213 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
216 /* Called with ovs_mutex or RCU read lock. */
217 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
220 struct hlist_head *head;
222 head = vport_hash_bucket(dp, port_no);
223 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
224 if (vport->port_no == port_no)
230 /* Called with ovs_mutex. */
231 static struct vport *new_vport(const struct vport_parms *parms)
235 vport = ovs_vport_add(parms);
236 if (!IS_ERR(vport)) {
237 struct datapath *dp = parms->dp;
238 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
240 hlist_add_head_rcu(&vport->dp_hash_node, head);
245 void ovs_dp_detach_port(struct vport *p)
249 /* First drop references to device. */
250 hlist_del_rcu(&p->dp_hash_node);
252 /* Then destroy it. */
256 /* Must be called with rcu_read_lock. */
257 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
259 const struct vport *p = OVS_CB(skb)->input_vport;
260 struct datapath *dp = p->dp;
261 struct sw_flow *flow;
262 struct sw_flow_actions *sf_acts;
263 struct dp_stats_percpu *stats;
267 stats = this_cpu_ptr(dp->stats_percpu);
270 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
271 if (unlikely(!flow)) {
272 struct dp_upcall_info upcall;
275 memset(&upcall, 0, sizeof(upcall));
276 upcall.cmd = OVS_PACKET_CMD_MISS;
277 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
278 error = ovs_dp_upcall(dp, skb, key, &upcall);
283 stats_counter = &stats->n_missed;
287 ovs_flow_stats_update(flow, key->tp.flags, skb);
288 sf_acts = rcu_dereference(flow->sf_acts);
289 ovs_execute_actions(dp, skb, sf_acts, key);
291 stats_counter = &stats->n_hit;
294 /* Update datapath statistics. */
295 u64_stats_update_begin(&stats->syncp);
297 stats->n_mask_hit += n_mask_hit;
298 u64_stats_update_end(&stats->syncp);
301 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
302 const struct sw_flow_key *key,
303 const struct dp_upcall_info *upcall_info)
305 struct dp_stats_percpu *stats;
308 if (upcall_info->portid == 0) {
313 if (!skb_is_gso(skb))
314 err = queue_userspace_packet(dp, skb, key, upcall_info);
316 err = queue_gso_packets(dp, skb, key, upcall_info);
323 stats = this_cpu_ptr(dp->stats_percpu);
325 u64_stats_update_begin(&stats->syncp);
327 u64_stats_update_end(&stats->syncp);
332 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
333 const struct sw_flow_key *key,
334 const struct dp_upcall_info *upcall_info)
336 unsigned short gso_type = skb_shinfo(skb)->gso_type;
337 struct sw_flow_key later_key;
338 struct sk_buff *segs, *nskb;
339 struct ovs_skb_cb ovs_cb;
342 ovs_cb = *OVS_CB(skb);
343 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
344 *OVS_CB(skb) = ovs_cb;
346 return PTR_ERR(segs);
350 if (gso_type & SKB_GSO_UDP) {
351 /* The initial flow key extracted by ovs_flow_key_extract()
352 * in this case is for a first fragment, so we need to
353 * properly mark later fragments.
356 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
359 /* Queue all of the segments. */
362 *OVS_CB(skb) = ovs_cb;
363 if (gso_type & SKB_GSO_UDP && skb != segs)
366 err = queue_userspace_packet(dp, skb, key, upcall_info);
370 } while ((skb = skb->next));
372 /* Free all of the segments. */
380 } while ((skb = nskb));
384 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
387 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
388 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
389 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
391 /* OVS_PACKET_ATTR_USERDATA */
392 if (upcall_info->userdata)
393 size += NLA_ALIGN(upcall_info->userdata->nla_len);
395 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
396 if (upcall_info->egress_tun_info)
397 size += nla_total_size(ovs_tun_key_attr_size());
399 /* OVS_PACKET_ATTR_ACTIONS */
400 if (upcall_info->actions_len)
401 size += nla_total_size(upcall_info->actions_len);
406 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
407 const struct sw_flow_key *key,
408 const struct dp_upcall_info *upcall_info)
410 struct ovs_header *upcall;
411 struct sk_buff *nskb = NULL;
412 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
414 struct genl_info info = {
415 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
416 .snd_portid = upcall_info->portid,
422 dp_ifindex = get_dpifindex(dp);
426 if (skb_vlan_tag_present(skb)) {
427 nskb = skb_clone(skb, GFP_ATOMIC);
431 nskb = __vlan_hwaccel_push_inside(nskb);
438 if (nla_attr_size(skb->len) > USHRT_MAX) {
443 /* Complete checksum if needed */
444 if (skb->ip_summed == CHECKSUM_PARTIAL &&
445 (err = skb_checksum_help(skb)))
448 /* Older versions of OVS user space enforce alignment of the last
449 * Netlink attribute to NLA_ALIGNTO which would require extensive
450 * padding logic. Only perform zerocopy if padding is not required.
452 if (dp->user_features & OVS_DP_F_UNALIGNED)
453 hlen = skb_zerocopy_headlen(skb);
457 len = upcall_msg_size(upcall_info, hlen);
458 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
464 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
465 0, upcall_info->cmd);
466 upcall->dp_ifindex = dp_ifindex;
468 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
471 if (upcall_info->userdata)
472 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
473 nla_len(upcall_info->userdata),
474 nla_data(upcall_info->userdata));
476 if (upcall_info->egress_tun_info) {
477 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
478 err = ovs_nla_put_egress_tunnel_key(user_skb,
479 upcall_info->egress_tun_info);
481 nla_nest_end(user_skb, nla);
484 if (upcall_info->actions_len) {
485 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS);
486 err = ovs_nla_put_actions(upcall_info->actions,
487 upcall_info->actions_len,
490 nla_nest_end(user_skb, nla);
492 nla_nest_cancel(user_skb, nla);
495 /* Only reserve room for attribute header, packet data is added
496 * in skb_zerocopy() */
497 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
501 nla->nla_len = nla_attr_size(skb->len);
503 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
507 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
508 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
509 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
512 memset(skb_put(user_skb, plen), 0, plen);
515 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
517 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
527 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
529 struct ovs_header *ovs_header = info->userhdr;
530 struct nlattr **a = info->attrs;
531 struct sw_flow_actions *acts;
532 struct sk_buff *packet;
533 struct sw_flow *flow;
534 struct sw_flow_actions *sf_acts;
537 struct vport *input_vport;
540 bool log = !a[OVS_PACKET_ATTR_PROBE];
543 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
544 !a[OVS_PACKET_ATTR_ACTIONS])
547 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
548 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
552 skb_reserve(packet, NET_IP_ALIGN);
554 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
556 skb_reset_mac_header(packet);
557 eth = eth_hdr(packet);
559 /* Normally, setting the skb 'protocol' field would be handled by a
560 * call to eth_type_trans(), but it assumes there's a sending
561 * device, which we may not have. */
562 if (eth_proto_is_802_3(eth->h_proto))
563 packet->protocol = eth->h_proto;
565 packet->protocol = htons(ETH_P_802_2);
567 /* Build an sw_flow for sending this packet. */
568 flow = ovs_flow_alloc();
573 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
578 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
579 &flow->key, &acts, log);
583 rcu_assign_pointer(flow->sf_acts, acts);
584 OVS_CB(packet)->egress_tun_info = NULL;
585 packet->priority = flow->key.phy.priority;
586 packet->mark = flow->key.phy.skb_mark;
589 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
594 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
596 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
601 OVS_CB(packet)->input_vport = input_vport;
602 sf_acts = rcu_dereference(flow->sf_acts);
605 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
609 ovs_flow_free(flow, false);
615 ovs_flow_free(flow, false);
622 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
623 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
624 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
625 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
626 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
629 static const struct genl_ops dp_packet_genl_ops[] = {
630 { .cmd = OVS_PACKET_CMD_EXECUTE,
631 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
632 .policy = packet_policy,
633 .doit = ovs_packet_cmd_execute
637 static struct genl_family dp_packet_genl_family = {
638 .id = GENL_ID_GENERATE,
639 .hdrsize = sizeof(struct ovs_header),
640 .name = OVS_PACKET_FAMILY,
641 .version = OVS_PACKET_VERSION,
642 .maxattr = OVS_PACKET_ATTR_MAX,
644 .parallel_ops = true,
645 .ops = dp_packet_genl_ops,
646 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
649 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
650 struct ovs_dp_megaflow_stats *mega_stats)
654 memset(mega_stats, 0, sizeof(*mega_stats));
656 stats->n_flows = ovs_flow_tbl_count(&dp->table);
657 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
659 stats->n_hit = stats->n_missed = stats->n_lost = 0;
661 for_each_possible_cpu(i) {
662 const struct dp_stats_percpu *percpu_stats;
663 struct dp_stats_percpu local_stats;
666 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
669 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
670 local_stats = *percpu_stats;
671 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
673 stats->n_hit += local_stats.n_hit;
674 stats->n_missed += local_stats.n_missed;
675 stats->n_lost += local_stats.n_lost;
676 mega_stats->n_mask_hit += local_stats.n_mask_hit;
680 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
682 return ovs_identifier_is_ufid(sfid) &&
683 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
686 static bool should_fill_mask(uint32_t ufid_flags)
688 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
691 static bool should_fill_actions(uint32_t ufid_flags)
693 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
696 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
697 const struct sw_flow_id *sfid,
700 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
702 /* OVS_FLOW_ATTR_UFID */
703 if (sfid && ovs_identifier_is_ufid(sfid))
704 len += nla_total_size(sfid->ufid_len);
706 /* OVS_FLOW_ATTR_KEY */
707 if (!sfid || should_fill_key(sfid, ufid_flags))
708 len += nla_total_size(ovs_key_attr_size());
710 /* OVS_FLOW_ATTR_MASK */
711 if (should_fill_mask(ufid_flags))
712 len += nla_total_size(ovs_key_attr_size());
714 /* OVS_FLOW_ATTR_ACTIONS */
715 if (should_fill_actions(ufid_flags))
716 len += nla_total_size(acts->actions_len);
719 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
720 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
721 + nla_total_size(8); /* OVS_FLOW_ATTR_USED */
724 /* Called with ovs_mutex or RCU read lock. */
725 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
728 struct ovs_flow_stats stats;
732 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
735 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
738 if (stats.n_packets &&
739 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
742 if ((u8)ntohs(tcp_flags) &&
743 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
749 /* Called with ovs_mutex or RCU read lock. */
750 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
751 struct sk_buff *skb, int skb_orig_len)
753 struct nlattr *start;
756 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
757 * this is the first flow to be dumped into 'skb'. This is unusual for
758 * Netlink but individual action lists can be longer than
759 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
760 * The userspace caller can always fetch the actions separately if it
761 * really wants them. (Most userspace callers in fact don't care.)
763 * This can only fail for dump operations because the skb is always
764 * properly sized for single flows.
766 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
768 const struct sw_flow_actions *sf_acts;
770 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
771 err = ovs_nla_put_actions(sf_acts->actions,
772 sf_acts->actions_len, skb);
775 nla_nest_end(skb, start);
780 nla_nest_cancel(skb, start);
782 } else if (skb_orig_len) {
789 /* Called with ovs_mutex or RCU read lock. */
790 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
791 struct sk_buff *skb, u32 portid,
792 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
794 const int skb_orig_len = skb->len;
795 struct ovs_header *ovs_header;
798 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
803 ovs_header->dp_ifindex = dp_ifindex;
805 err = ovs_nla_put_identifier(flow, skb);
809 if (should_fill_key(&flow->id, ufid_flags)) {
810 err = ovs_nla_put_masked_key(flow, skb);
815 if (should_fill_mask(ufid_flags)) {
816 err = ovs_nla_put_mask(flow, skb);
821 err = ovs_flow_cmd_fill_stats(flow, skb);
825 if (should_fill_actions(ufid_flags)) {
826 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
831 genlmsg_end(skb, ovs_header);
835 genlmsg_cancel(skb, ovs_header);
839 /* May not be called with RCU read lock. */
840 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
841 const struct sw_flow_id *sfid,
842 struct genl_info *info,
849 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
852 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
853 skb = genlmsg_new_unicast(len, info, GFP_KERNEL);
855 return ERR_PTR(-ENOMEM);
860 /* Called with ovs_mutex. */
861 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
863 struct genl_info *info, u8 cmd,
864 bool always, u32 ufid_flags)
869 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
870 &flow->id, info, always, ufid_flags);
871 if (IS_ERR_OR_NULL(skb))
874 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
875 info->snd_portid, info->snd_seq, 0,
881 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
883 struct nlattr **a = info->attrs;
884 struct ovs_header *ovs_header = info->userhdr;
885 struct sw_flow *flow = NULL, *new_flow;
886 struct sw_flow_mask mask;
887 struct sk_buff *reply;
889 struct sw_flow_key key;
890 struct sw_flow_actions *acts;
891 struct sw_flow_match match;
892 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
894 bool log = !a[OVS_FLOW_ATTR_PROBE];
896 /* Must have key and actions. */
898 if (!a[OVS_FLOW_ATTR_KEY]) {
899 OVS_NLERR(log, "Flow key attr not present in new flow.");
902 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
903 OVS_NLERR(log, "Flow actions attr not present in new flow.");
907 /* Most of the time we need to allocate a new flow, do it before
910 new_flow = ovs_flow_alloc();
911 if (IS_ERR(new_flow)) {
912 error = PTR_ERR(new_flow);
917 ovs_match_init(&match, &key, &mask);
918 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
919 a[OVS_FLOW_ATTR_MASK], log);
923 ovs_flow_mask_key(&new_flow->key, &key, &mask);
925 /* Extract flow identifier. */
926 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
931 /* Validate actions. */
932 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
935 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
939 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
942 error = PTR_ERR(reply);
947 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
953 /* Check if this is a duplicate flow */
954 if (ovs_identifier_is_ufid(&new_flow->id))
955 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
957 flow = ovs_flow_tbl_lookup(&dp->table, &key);
959 rcu_assign_pointer(new_flow->sf_acts, acts);
961 /* Put flow in bucket. */
962 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
963 if (unlikely(error)) {
968 if (unlikely(reply)) {
969 error = ovs_flow_cmd_fill_info(new_flow,
970 ovs_header->dp_ifindex,
971 reply, info->snd_portid,
979 struct sw_flow_actions *old_acts;
981 /* Bail out if we're not allowed to modify an existing flow.
982 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
983 * because Generic Netlink treats the latter as a dump
984 * request. We also accept NLM_F_EXCL in case that bug ever
987 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
992 /* The flow identifier has to be the same for flow updates.
993 * Look for any overlapping flow.
995 if (unlikely(!ovs_flow_cmp(flow, &match))) {
996 if (ovs_identifier_is_key(&flow->id))
997 flow = ovs_flow_tbl_lookup_exact(&dp->table,
999 else /* UFID matches but key is different */
1003 goto err_unlock_ovs;
1006 /* Update actions. */
1007 old_acts = ovsl_dereference(flow->sf_acts);
1008 rcu_assign_pointer(flow->sf_acts, acts);
1010 if (unlikely(reply)) {
1011 error = ovs_flow_cmd_fill_info(flow,
1012 ovs_header->dp_ifindex,
1013 reply, info->snd_portid,
1021 ovs_nla_free_flow_actions(old_acts);
1022 ovs_flow_free(new_flow, false);
1026 ovs_notify(&dp_flow_genl_family, reply, info);
1035 ovs_flow_free(new_flow, false);
1040 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1041 static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
1042 const struct sw_flow_key *key,
1043 const struct sw_flow_mask *mask,
1046 struct sw_flow_actions *acts;
1047 struct sw_flow_key masked_key;
1050 ovs_flow_mask_key(&masked_key, key, mask);
1051 error = ovs_nla_copy_actions(a, &masked_key, &acts, log);
1054 "Actions may not be safe on all matching packets");
1055 return ERR_PTR(error);
1061 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1063 struct nlattr **a = info->attrs;
1064 struct ovs_header *ovs_header = info->userhdr;
1065 struct sw_flow_key key;
1066 struct sw_flow *flow;
1067 struct sw_flow_mask mask;
1068 struct sk_buff *reply = NULL;
1069 struct datapath *dp;
1070 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1071 struct sw_flow_match match;
1072 struct sw_flow_id sfid;
1073 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1075 bool log = !a[OVS_FLOW_ATTR_PROBE];
1080 if (!a[OVS_FLOW_ATTR_KEY]) {
1081 OVS_NLERR(log, "Flow key attribute not present in set flow.");
1085 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1086 ovs_match_init(&match, &key, &mask);
1087 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
1088 a[OVS_FLOW_ATTR_MASK], log);
1092 /* Validate actions. */
1093 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1094 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask,
1097 error = PTR_ERR(acts);
1101 /* Can allocate before locking if have acts. */
1102 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1104 if (IS_ERR(reply)) {
1105 error = PTR_ERR(reply);
1106 goto err_kfree_acts;
1111 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1112 if (unlikely(!dp)) {
1114 goto err_unlock_ovs;
1116 /* Check that the flow exists. */
1118 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1120 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1121 if (unlikely(!flow)) {
1123 goto err_unlock_ovs;
1126 /* Update actions, if present. */
1128 old_acts = ovsl_dereference(flow->sf_acts);
1129 rcu_assign_pointer(flow->sf_acts, acts);
1131 if (unlikely(reply)) {
1132 error = ovs_flow_cmd_fill_info(flow,
1133 ovs_header->dp_ifindex,
1134 reply, info->snd_portid,
1141 /* Could not alloc without acts before locking. */
1142 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1143 info, OVS_FLOW_CMD_NEW, false,
1146 if (unlikely(IS_ERR(reply))) {
1147 error = PTR_ERR(reply);
1148 goto err_unlock_ovs;
1153 if (a[OVS_FLOW_ATTR_CLEAR])
1154 ovs_flow_stats_clear(flow);
1158 ovs_notify(&dp_flow_genl_family, reply, info);
1160 ovs_nla_free_flow_actions(old_acts);
1173 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1175 struct nlattr **a = info->attrs;
1176 struct ovs_header *ovs_header = info->userhdr;
1177 struct sw_flow_key key;
1178 struct sk_buff *reply;
1179 struct sw_flow *flow;
1180 struct datapath *dp;
1181 struct sw_flow_match match;
1182 struct sw_flow_id ufid;
1183 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1185 bool log = !a[OVS_FLOW_ATTR_PROBE];
1188 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1189 if (a[OVS_FLOW_ATTR_KEY]) {
1190 ovs_match_init(&match, &key, NULL);
1191 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1193 } else if (!ufid_present) {
1195 "Flow get message rejected, Key attribute missing.");
1202 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1209 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1211 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1217 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1218 OVS_FLOW_CMD_NEW, true, ufid_flags);
1219 if (IS_ERR(reply)) {
1220 err = PTR_ERR(reply);
1225 return genlmsg_reply(reply, info);
1231 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1233 struct nlattr **a = info->attrs;
1234 struct ovs_header *ovs_header = info->userhdr;
1235 struct sw_flow_key key;
1236 struct sk_buff *reply;
1237 struct sw_flow *flow = NULL;
1238 struct datapath *dp;
1239 struct sw_flow_match match;
1240 struct sw_flow_id ufid;
1241 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1243 bool log = !a[OVS_FLOW_ATTR_PROBE];
1246 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1247 if (a[OVS_FLOW_ATTR_KEY]) {
1248 ovs_match_init(&match, &key, NULL);
1249 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1256 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1257 if (unlikely(!dp)) {
1262 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1263 err = ovs_flow_tbl_flush(&dp->table);
1268 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1270 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1271 if (unlikely(!flow)) {
1276 ovs_flow_tbl_remove(&dp->table, flow);
1279 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1280 &flow->id, info, false, ufid_flags);
1281 if (likely(reply)) {
1282 if (likely(!IS_ERR(reply))) {
1283 rcu_read_lock(); /*To keep RCU checker happy. */
1284 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1285 reply, info->snd_portid,
1292 ovs_notify(&dp_flow_genl_family, reply, info);
1294 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1298 ovs_flow_free(flow, true);
1305 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1307 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1308 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1309 struct table_instance *ti;
1310 struct datapath *dp;
1314 err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
1315 OVS_FLOW_ATTR_MAX, flow_policy);
1318 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1321 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1327 ti = rcu_dereference(dp->table.ti);
1329 struct sw_flow *flow;
1332 bucket = cb->args[0];
1334 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1338 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1339 NETLINK_CB(cb->skb).portid,
1340 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1341 OVS_FLOW_CMD_NEW, ufid_flags) < 0)
1344 cb->args[0] = bucket;
1351 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1352 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1353 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1354 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1355 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1356 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1357 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1358 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1361 static const struct genl_ops dp_flow_genl_ops[] = {
1362 { .cmd = OVS_FLOW_CMD_NEW,
1363 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1364 .policy = flow_policy,
1365 .doit = ovs_flow_cmd_new
1367 { .cmd = OVS_FLOW_CMD_DEL,
1368 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1369 .policy = flow_policy,
1370 .doit = ovs_flow_cmd_del
1372 { .cmd = OVS_FLOW_CMD_GET,
1373 .flags = 0, /* OK for unprivileged users. */
1374 .policy = flow_policy,
1375 .doit = ovs_flow_cmd_get,
1376 .dumpit = ovs_flow_cmd_dump
1378 { .cmd = OVS_FLOW_CMD_SET,
1379 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1380 .policy = flow_policy,
1381 .doit = ovs_flow_cmd_set,
1385 static struct genl_family dp_flow_genl_family = {
1386 .id = GENL_ID_GENERATE,
1387 .hdrsize = sizeof(struct ovs_header),
1388 .name = OVS_FLOW_FAMILY,
1389 .version = OVS_FLOW_VERSION,
1390 .maxattr = OVS_FLOW_ATTR_MAX,
1392 .parallel_ops = true,
1393 .ops = dp_flow_genl_ops,
1394 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1395 .mcgrps = &ovs_dp_flow_multicast_group,
1399 static size_t ovs_dp_cmd_msg_size(void)
1401 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1403 msgsize += nla_total_size(IFNAMSIZ);
1404 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1405 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1406 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1411 /* Called with ovs_mutex. */
1412 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1413 u32 portid, u32 seq, u32 flags, u8 cmd)
1415 struct ovs_header *ovs_header;
1416 struct ovs_dp_stats dp_stats;
1417 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1420 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1425 ovs_header->dp_ifindex = get_dpifindex(dp);
1427 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1429 goto nla_put_failure;
1431 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1432 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1434 goto nla_put_failure;
1436 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1437 sizeof(struct ovs_dp_megaflow_stats),
1438 &dp_megaflow_stats))
1439 goto nla_put_failure;
1441 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1442 goto nla_put_failure;
1444 genlmsg_end(skb, ovs_header);
1448 genlmsg_cancel(skb, ovs_header);
1453 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1455 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1458 /* Called with rcu_read_lock or ovs_mutex. */
1459 static struct datapath *lookup_datapath(struct net *net,
1460 const struct ovs_header *ovs_header,
1461 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1463 struct datapath *dp;
1465 if (!a[OVS_DP_ATTR_NAME])
1466 dp = get_dp(net, ovs_header->dp_ifindex);
1468 struct vport *vport;
1470 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1471 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1473 return dp ? dp : ERR_PTR(-ENODEV);
1476 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1478 struct datapath *dp;
1480 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1484 WARN(dp->user_features, "Dropping previously announced user features\n");
1485 dp->user_features = 0;
1488 static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1490 if (a[OVS_DP_ATTR_USER_FEATURES])
1491 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1494 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1496 struct nlattr **a = info->attrs;
1497 struct vport_parms parms;
1498 struct sk_buff *reply;
1499 struct datapath *dp;
1500 struct vport *vport;
1501 struct ovs_net *ovs_net;
1505 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1508 reply = ovs_dp_cmd_alloc_info(info);
1513 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1515 goto err_free_reply;
1517 ovs_dp_set_net(dp, sock_net(skb->sk));
1519 /* Allocate table. */
1520 err = ovs_flow_tbl_init(&dp->table);
1524 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1525 if (!dp->stats_percpu) {
1527 goto err_destroy_table;
1530 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1534 goto err_destroy_percpu;
1537 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1538 INIT_HLIST_HEAD(&dp->ports[i]);
1540 /* Set up our datapath device. */
1541 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1542 parms.type = OVS_VPORT_TYPE_INTERNAL;
1543 parms.options = NULL;
1545 parms.port_no = OVSP_LOCAL;
1546 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1548 ovs_dp_change(dp, a);
1550 /* So far only local changes have been made, now need the lock. */
1553 vport = new_vport(&parms);
1554 if (IS_ERR(vport)) {
1555 err = PTR_ERR(vport);
1559 if (err == -EEXIST) {
1560 /* An outdated user space instance that does not understand
1561 * the concept of user_features has attempted to create a new
1562 * datapath and is likely to reuse it. Drop all user features.
1564 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1565 ovs_dp_reset_user_features(skb, info);
1568 goto err_destroy_ports_array;
1571 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1572 info->snd_seq, 0, OVS_DP_CMD_NEW);
1575 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1576 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1580 ovs_notify(&dp_datapath_genl_family, reply, info);
1583 err_destroy_ports_array:
1587 free_percpu(dp->stats_percpu);
1589 ovs_flow_tbl_destroy(&dp->table);
1598 /* Called with ovs_mutex. */
1599 static void __dp_destroy(struct datapath *dp)
1603 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1604 struct vport *vport;
1605 struct hlist_node *n;
1607 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1608 if (vport->port_no != OVSP_LOCAL)
1609 ovs_dp_detach_port(vport);
1612 list_del_rcu(&dp->list_node);
1614 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1615 * all ports in datapath are destroyed first before freeing datapath.
1617 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1619 /* RCU destroy the flow table */
1620 call_rcu(&dp->rcu, destroy_dp_rcu);
1623 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1625 struct sk_buff *reply;
1626 struct datapath *dp;
1629 reply = ovs_dp_cmd_alloc_info(info);
1634 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1637 goto err_unlock_free;
1639 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1640 info->snd_seq, 0, OVS_DP_CMD_DEL);
1646 ovs_notify(&dp_datapath_genl_family, reply, info);
1656 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1658 struct sk_buff *reply;
1659 struct datapath *dp;
1662 reply = ovs_dp_cmd_alloc_info(info);
1667 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1670 goto err_unlock_free;
1672 ovs_dp_change(dp, info->attrs);
1674 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1675 info->snd_seq, 0, OVS_DP_CMD_NEW);
1679 ovs_notify(&dp_datapath_genl_family, reply, info);
1689 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1691 struct sk_buff *reply;
1692 struct datapath *dp;
1695 reply = ovs_dp_cmd_alloc_info(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_NEW);
1710 return genlmsg_reply(reply, info);
1718 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1720 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1721 struct datapath *dp;
1722 int skip = cb->args[0];
1726 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1728 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1729 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1730 OVS_DP_CMD_NEW) < 0)
1741 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1742 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1743 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1744 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1747 static const struct genl_ops dp_datapath_genl_ops[] = {
1748 { .cmd = OVS_DP_CMD_NEW,
1749 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1750 .policy = datapath_policy,
1751 .doit = ovs_dp_cmd_new
1753 { .cmd = OVS_DP_CMD_DEL,
1754 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1755 .policy = datapath_policy,
1756 .doit = ovs_dp_cmd_del
1758 { .cmd = OVS_DP_CMD_GET,
1759 .flags = 0, /* OK for unprivileged users. */
1760 .policy = datapath_policy,
1761 .doit = ovs_dp_cmd_get,
1762 .dumpit = ovs_dp_cmd_dump
1764 { .cmd = OVS_DP_CMD_SET,
1765 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1766 .policy = datapath_policy,
1767 .doit = ovs_dp_cmd_set,
1771 static struct genl_family dp_datapath_genl_family = {
1772 .id = GENL_ID_GENERATE,
1773 .hdrsize = sizeof(struct ovs_header),
1774 .name = OVS_DATAPATH_FAMILY,
1775 .version = OVS_DATAPATH_VERSION,
1776 .maxattr = OVS_DP_ATTR_MAX,
1778 .parallel_ops = true,
1779 .ops = dp_datapath_genl_ops,
1780 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1781 .mcgrps = &ovs_dp_datapath_multicast_group,
1785 /* Called with ovs_mutex or RCU read lock. */
1786 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1787 u32 portid, u32 seq, u32 flags, u8 cmd)
1789 struct ovs_header *ovs_header;
1790 struct ovs_vport_stats vport_stats;
1793 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1798 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1800 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1801 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1802 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1803 vport->ops->get_name(vport)))
1804 goto nla_put_failure;
1806 ovs_vport_get_stats(vport, &vport_stats);
1807 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1809 goto nla_put_failure;
1811 if (ovs_vport_get_upcall_portids(vport, skb))
1812 goto nla_put_failure;
1814 err = ovs_vport_get_options(vport, skb);
1815 if (err == -EMSGSIZE)
1818 genlmsg_end(skb, ovs_header);
1824 genlmsg_cancel(skb, ovs_header);
1828 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1830 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1833 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1834 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1837 struct sk_buff *skb;
1840 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1842 return ERR_PTR(-ENOMEM);
1844 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1850 /* Called with ovs_mutex or RCU read lock. */
1851 static struct vport *lookup_vport(struct net *net,
1852 const struct ovs_header *ovs_header,
1853 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1855 struct datapath *dp;
1856 struct vport *vport;
1858 if (a[OVS_VPORT_ATTR_NAME]) {
1859 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1861 return ERR_PTR(-ENODEV);
1862 if (ovs_header->dp_ifindex &&
1863 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1864 return ERR_PTR(-ENODEV);
1866 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1867 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1869 if (port_no >= DP_MAX_PORTS)
1870 return ERR_PTR(-EFBIG);
1872 dp = get_dp(net, ovs_header->dp_ifindex);
1874 return ERR_PTR(-ENODEV);
1876 vport = ovs_vport_ovsl_rcu(dp, port_no);
1878 return ERR_PTR(-ENODEV);
1881 return ERR_PTR(-EINVAL);
1884 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1886 struct nlattr **a = info->attrs;
1887 struct ovs_header *ovs_header = info->userhdr;
1888 struct vport_parms parms;
1889 struct sk_buff *reply;
1890 struct vport *vport;
1891 struct datapath *dp;
1895 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1896 !a[OVS_VPORT_ATTR_UPCALL_PID])
1899 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1900 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1901 if (port_no >= DP_MAX_PORTS)
1904 reply = ovs_vport_cmd_alloc_info();
1910 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1913 goto exit_unlock_free;
1916 vport = ovs_vport_ovsl(dp, port_no);
1919 goto exit_unlock_free;
1921 for (port_no = 1; ; port_no++) {
1922 if (port_no >= DP_MAX_PORTS) {
1924 goto exit_unlock_free;
1926 vport = ovs_vport_ovsl(dp, port_no);
1932 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1933 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1934 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1936 parms.port_no = port_no;
1937 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1939 vport = new_vport(&parms);
1940 err = PTR_ERR(vport);
1941 if (IS_ERR(vport)) {
1944 goto exit_unlock_free;
1947 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1948 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1952 ovs_notify(&dp_vport_genl_family, reply, info);
1961 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1963 struct nlattr **a = info->attrs;
1964 struct sk_buff *reply;
1965 struct vport *vport;
1968 reply = ovs_vport_cmd_alloc_info();
1973 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1974 err = PTR_ERR(vport);
1976 goto exit_unlock_free;
1978 if (a[OVS_VPORT_ATTR_TYPE] &&
1979 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1981 goto exit_unlock_free;
1984 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1985 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1987 goto exit_unlock_free;
1991 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1992 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1994 err = ovs_vport_set_upcall_portids(vport, ids);
1996 goto exit_unlock_free;
1999 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2000 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2004 ovs_notify(&dp_vport_genl_family, reply, info);
2013 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2015 struct nlattr **a = info->attrs;
2016 struct sk_buff *reply;
2017 struct vport *vport;
2020 reply = ovs_vport_cmd_alloc_info();
2025 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2026 err = PTR_ERR(vport);
2028 goto exit_unlock_free;
2030 if (vport->port_no == OVSP_LOCAL) {
2032 goto exit_unlock_free;
2035 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2036 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
2038 ovs_dp_detach_port(vport);
2041 ovs_notify(&dp_vport_genl_family, reply, info);
2050 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2052 struct nlattr **a = info->attrs;
2053 struct ovs_header *ovs_header = info->userhdr;
2054 struct sk_buff *reply;
2055 struct vport *vport;
2058 reply = ovs_vport_cmd_alloc_info();
2063 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2064 err = PTR_ERR(vport);
2066 goto exit_unlock_free;
2067 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2068 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2072 return genlmsg_reply(reply, info);
2080 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2082 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2083 struct datapath *dp;
2084 int bucket = cb->args[0], skip = cb->args[1];
2088 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2093 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2094 struct vport *vport;
2097 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2099 ovs_vport_cmd_fill_info(vport, skb,
2100 NETLINK_CB(cb->skb).portid,
2103 OVS_VPORT_CMD_NEW) < 0)
2119 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2120 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2121 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2122 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2123 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2124 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2125 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2128 static const struct genl_ops dp_vport_genl_ops[] = {
2129 { .cmd = OVS_VPORT_CMD_NEW,
2130 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2131 .policy = vport_policy,
2132 .doit = ovs_vport_cmd_new
2134 { .cmd = OVS_VPORT_CMD_DEL,
2135 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2136 .policy = vport_policy,
2137 .doit = ovs_vport_cmd_del
2139 { .cmd = OVS_VPORT_CMD_GET,
2140 .flags = 0, /* OK for unprivileged users. */
2141 .policy = vport_policy,
2142 .doit = ovs_vport_cmd_get,
2143 .dumpit = ovs_vport_cmd_dump
2145 { .cmd = OVS_VPORT_CMD_SET,
2146 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2147 .policy = vport_policy,
2148 .doit = ovs_vport_cmd_set,
2152 struct genl_family dp_vport_genl_family = {
2153 .id = GENL_ID_GENERATE,
2154 .hdrsize = sizeof(struct ovs_header),
2155 .name = OVS_VPORT_FAMILY,
2156 .version = OVS_VPORT_VERSION,
2157 .maxattr = OVS_VPORT_ATTR_MAX,
2159 .parallel_ops = true,
2160 .ops = dp_vport_genl_ops,
2161 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2162 .mcgrps = &ovs_dp_vport_multicast_group,
2166 static struct genl_family * const dp_genl_families[] = {
2167 &dp_datapath_genl_family,
2168 &dp_vport_genl_family,
2169 &dp_flow_genl_family,
2170 &dp_packet_genl_family,
2173 static void dp_unregister_genl(int n_families)
2177 for (i = 0; i < n_families; i++)
2178 genl_unregister_family(dp_genl_families[i]);
2181 static int dp_register_genl(void)
2186 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2188 err = genl_register_family(dp_genl_families[i]);
2196 dp_unregister_genl(i);
2200 static int __net_init ovs_init_net(struct net *net)
2202 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2204 INIT_LIST_HEAD(&ovs_net->dps);
2205 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2209 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2210 struct list_head *head)
2212 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2213 struct datapath *dp;
2215 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2218 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2219 struct vport *vport;
2221 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2222 struct netdev_vport *netdev_vport;
2224 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2227 netdev_vport = netdev_vport_priv(vport);
2228 if (dev_net(netdev_vport->dev) == dnet)
2229 list_add(&vport->detach_list, head);
2235 static void __net_exit ovs_exit_net(struct net *dnet)
2237 struct datapath *dp, *dp_next;
2238 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2239 struct vport *vport, *vport_next;
2244 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2249 list_vports_from_net(net, dnet, &head);
2252 /* Detach all vports from given namespace. */
2253 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2254 list_del(&vport->detach_list);
2255 ovs_dp_detach_port(vport);
2260 cancel_work_sync(&ovs_net->dp_notify_work);
2263 static struct pernet_operations ovs_net_ops = {
2264 .init = ovs_init_net,
2265 .exit = ovs_exit_net,
2267 .size = sizeof(struct ovs_net),
2270 static int __init dp_init(void)
2274 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2276 pr_info("Open vSwitch switching datapath\n");
2278 err = action_fifos_init();
2282 err = ovs_internal_dev_rtnl_link_register();
2284 goto error_action_fifos_exit;
2286 err = ovs_flow_init();
2288 goto error_unreg_rtnl_link;
2290 err = ovs_vport_init();
2292 goto error_flow_exit;
2294 err = register_pernet_device(&ovs_net_ops);
2296 goto error_vport_exit;
2298 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2300 goto error_netns_exit;
2302 err = ovs_netdev_init();
2304 goto error_unreg_notifier;
2306 err = dp_register_genl();
2308 goto error_unreg_netdev;
2314 error_unreg_notifier:
2315 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2317 unregister_pernet_device(&ovs_net_ops);
2322 error_unreg_rtnl_link:
2323 ovs_internal_dev_rtnl_link_unregister();
2324 error_action_fifos_exit:
2325 action_fifos_exit();
2330 static void dp_cleanup(void)
2332 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2334 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2335 unregister_pernet_device(&ovs_net_ops);
2339 ovs_internal_dev_rtnl_link_unregister();
2340 action_fifos_exit();
2343 module_init(dp_init);
2344 module_exit(dp_cleanup);
2346 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2347 MODULE_LICENSE("GPL");