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/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>
57 #include "flow_table.h"
58 #include "flow_netlink.h"
60 #include "vport-internal_dev.h"
61 #include "vport-netdev.h"
63 int ovs_net_id __read_mostly;
64 EXPORT_SYMBOL_GPL(ovs_net_id);
66 static struct genl_family dp_packet_genl_family;
67 static struct genl_family dp_flow_genl_family;
68 static struct genl_family dp_datapath_genl_family;
70 static const struct nla_policy flow_policy[];
72 static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
73 .name = OVS_FLOW_MCGROUP,
76 static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
77 .name = OVS_DATAPATH_MCGROUP,
80 const struct genl_multicast_group ovs_dp_vport_multicast_group = {
81 .name = OVS_VPORT_MCGROUP,
84 /* Check if need to build a reply message.
85 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply.
87 static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
90 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
91 genl_has_listeners(family, genl_info_net(info), group);
94 static void ovs_notify(struct genl_family *family,
95 const 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 *);
145 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
146 const struct sw_flow_key *,
147 const struct dp_upcall_info *);
149 /* Must be called with rcu_read_lock. */
150 static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
152 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
155 struct vport *vport = ovs_internal_dev_get_vport(dev);
163 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
164 * returned dp pointer valid.
166 static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
170 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
172 dp = get_dp_rcu(net, dp_ifindex);
178 /* Must be called with rcu_read_lock or ovs_mutex. */
179 const char *ovs_dp_name(const struct datapath *dp)
181 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
182 return vport->ops->get_name(vport);
185 static int get_dpifindex(const struct datapath *dp)
192 local = ovs_vport_rcu(dp, OVSP_LOCAL);
194 ifindex = netdev_vport_priv(local)->dev->ifindex;
203 static void destroy_dp_rcu(struct rcu_head *rcu)
205 struct datapath *dp = container_of(rcu, struct datapath, rcu);
207 ovs_flow_tbl_destroy(&dp->table);
208 free_percpu(dp->stats_percpu);
209 release_net(ovs_dp_get_net(dp));
214 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
217 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
220 /* Called with ovs_mutex or RCU read lock. */
221 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
224 struct hlist_head *head;
226 head = vport_hash_bucket(dp, port_no);
227 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
228 if (vport->port_no == port_no)
234 /* Called with ovs_mutex. */
235 static struct vport *new_vport(const struct vport_parms *parms)
239 vport = ovs_vport_add(parms);
240 if (!IS_ERR(vport)) {
241 struct datapath *dp = parms->dp;
242 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
244 hlist_add_head_rcu(&vport->dp_hash_node, head);
249 void ovs_dp_detach_port(struct vport *p)
253 /* First drop references to device. */
254 hlist_del_rcu(&p->dp_hash_node);
256 /* Then destroy it. */
260 /* Must be called with rcu_read_lock. */
261 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
263 const struct vport *p = OVS_CB(skb)->input_vport;
264 struct datapath *dp = p->dp;
265 struct sw_flow *flow;
266 struct sw_flow_actions *sf_acts;
267 struct dp_stats_percpu *stats;
271 stats = this_cpu_ptr(dp->stats_percpu);
274 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
276 if (unlikely(!flow)) {
277 struct dp_upcall_info upcall;
280 memset(&upcall, 0, sizeof(upcall));
281 upcall.cmd = OVS_PACKET_CMD_MISS;
282 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
283 error = ovs_dp_upcall(dp, skb, key, &upcall);
288 stats_counter = &stats->n_missed;
292 ovs_flow_stats_update(flow, key->tp.flags, skb);
293 sf_acts = rcu_dereference(flow->sf_acts);
294 ovs_execute_actions(dp, skb, sf_acts, key);
296 stats_counter = &stats->n_hit;
299 /* Update datapath statistics. */
300 u64_stats_update_begin(&stats->syncp);
302 stats->n_mask_hit += n_mask_hit;
303 u64_stats_update_end(&stats->syncp);
306 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
307 const struct sw_flow_key *key,
308 const struct dp_upcall_info *upcall_info)
310 struct dp_stats_percpu *stats;
313 if (upcall_info->portid == 0) {
318 if (!skb_is_gso(skb))
319 err = queue_userspace_packet(dp, skb, key, upcall_info);
321 err = queue_gso_packets(dp, skb, key, upcall_info);
328 stats = this_cpu_ptr(dp->stats_percpu);
330 u64_stats_update_begin(&stats->syncp);
332 u64_stats_update_end(&stats->syncp);
337 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
338 const struct sw_flow_key *key,
339 const struct dp_upcall_info *upcall_info)
341 unsigned short gso_type = skb_shinfo(skb)->gso_type;
342 struct sw_flow_key later_key;
343 struct sk_buff *segs, *nskb;
344 struct ovs_skb_cb ovs_cb;
347 ovs_cb = *OVS_CB(skb);
348 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
349 *OVS_CB(skb) = ovs_cb;
351 return PTR_ERR(segs);
355 if (gso_type & SKB_GSO_UDP) {
356 /* The initial flow key extracted by ovs_flow_key_extract()
357 * in this case is for a first fragment, so we need to
358 * properly mark later fragments.
361 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
364 /* Queue all of the segments. */
367 *OVS_CB(skb) = ovs_cb;
368 if (gso_type & SKB_GSO_UDP && skb != segs)
371 err = queue_userspace_packet(dp, skb, key, upcall_info);
375 } while ((skb = skb->next));
377 /* Free all of the segments. */
385 } while ((skb = nskb));
389 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
392 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
393 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
394 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
396 /* OVS_PACKET_ATTR_USERDATA */
397 if (upcall_info->userdata)
398 size += NLA_ALIGN(upcall_info->userdata->nla_len);
400 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
401 if (upcall_info->egress_tun_info)
402 size += nla_total_size(ovs_tun_key_attr_size());
404 /* OVS_PACKET_ATTR_ACTIONS */
405 if (upcall_info->actions_len)
406 size += nla_total_size(upcall_info->actions_len);
411 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
412 const struct sw_flow_key *key,
413 const struct dp_upcall_info *upcall_info)
415 struct ovs_header *upcall;
416 struct sk_buff *nskb = NULL;
417 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
419 struct genl_info info = {
420 #ifdef HAVE_GENLMSG_NEW_UNICAST
421 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
423 .snd_portid = upcall_info->portid,
429 dp_ifindex = get_dpifindex(dp);
433 if (skb_vlan_tag_present(skb)) {
434 nskb = skb_clone(skb, GFP_ATOMIC);
438 nskb = vlan_insert_tag_set_proto(nskb, nskb->vlan_proto, skb_vlan_tag_get(nskb));
442 vlan_set_tci(nskb, 0);
447 if (nla_attr_size(skb->len) > USHRT_MAX) {
452 /* Complete checksum if needed */
453 if (skb->ip_summed == CHECKSUM_PARTIAL &&
454 (err = skb_checksum_help(skb)))
457 /* Older versions of OVS user space enforce alignment of the last
458 * Netlink attribute to NLA_ALIGNTO which would require extensive
459 * padding logic. Only perform zerocopy if padding is not required.
461 if (dp->user_features & OVS_DP_F_UNALIGNED)
462 hlen = skb_zerocopy_headlen(skb);
466 len = upcall_msg_size(upcall_info, hlen);
467 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
473 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
474 0, upcall_info->cmd);
475 upcall->dp_ifindex = dp_ifindex;
477 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
480 if (upcall_info->userdata)
481 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
482 nla_len(upcall_info->userdata),
483 nla_data(upcall_info->userdata));
485 if (upcall_info->egress_tun_info) {
486 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
487 err = ovs_nla_put_egress_tunnel_key(user_skb,
488 upcall_info->egress_tun_info);
490 nla_nest_end(user_skb, nla);
493 if (upcall_info->actions_len) {
494 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS);
495 err = ovs_nla_put_actions(upcall_info->actions,
496 upcall_info->actions_len,
499 nla_nest_end(user_skb, nla);
501 nla_nest_cancel(user_skb, nla);
504 /* Only reserve room for attribute header, packet data is added
507 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
511 nla->nla_len = nla_attr_size(skb->len);
513 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
517 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
518 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
519 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
522 memset(skb_put(user_skb, plen), 0, plen);
525 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
527 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
537 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
539 struct ovs_header *ovs_header = info->userhdr;
540 struct nlattr **a = info->attrs;
541 struct sw_flow_actions *acts;
542 struct sk_buff *packet;
543 struct sw_flow *flow;
544 struct sw_flow_actions *sf_acts;
547 struct vport *input_vport;
550 bool log = !a[OVS_PACKET_ATTR_PROBE];
553 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
554 !a[OVS_PACKET_ATTR_ACTIONS])
557 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
558 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
562 skb_reserve(packet, NET_IP_ALIGN);
564 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
566 skb_reset_mac_header(packet);
567 eth = eth_hdr(packet);
569 /* Normally, setting the skb 'protocol' field would be handled by a
570 * call to eth_type_trans(), but it assumes there's a sending
571 * device, which we may not have.
573 if (eth_proto_is_802_3(eth->h_proto))
574 packet->protocol = eth->h_proto;
576 packet->protocol = htons(ETH_P_802_2);
578 /* Build an sw_flow for sending this packet. */
579 flow = ovs_flow_alloc();
584 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
589 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
590 &flow->key, &acts, log);
594 rcu_assign_pointer(flow->sf_acts, acts);
595 OVS_CB(packet)->egress_tun_info = NULL;
596 packet->priority = flow->key.phy.priority;
597 packet->mark = flow->key.phy.skb_mark;
600 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
605 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
607 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
612 OVS_CB(packet)->input_vport = input_vport;
613 sf_acts = rcu_dereference(flow->sf_acts);
616 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
620 ovs_flow_free(flow, false);
626 ovs_flow_free(flow, false);
633 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
634 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
635 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
636 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
637 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
640 static const struct genl_ops dp_packet_genl_ops[] = {
641 { .cmd = OVS_PACKET_CMD_EXECUTE,
642 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
643 .policy = packet_policy,
644 .doit = ovs_packet_cmd_execute
648 static struct genl_family dp_packet_genl_family = {
649 .id = GENL_ID_GENERATE,
650 .hdrsize = sizeof(struct ovs_header),
651 .name = OVS_PACKET_FAMILY,
652 .version = OVS_PACKET_VERSION,
653 .maxattr = OVS_PACKET_ATTR_MAX,
655 .parallel_ops = true,
656 .ops = dp_packet_genl_ops,
657 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
660 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
661 struct ovs_dp_megaflow_stats *mega_stats)
665 memset(mega_stats, 0, sizeof(*mega_stats));
667 stats->n_flows = ovs_flow_tbl_count(&dp->table);
668 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
670 stats->n_hit = stats->n_missed = stats->n_lost = 0;
672 for_each_possible_cpu(i) {
673 const struct dp_stats_percpu *percpu_stats;
674 struct dp_stats_percpu local_stats;
677 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
680 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
681 local_stats = *percpu_stats;
682 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
684 stats->n_hit += local_stats.n_hit;
685 stats->n_missed += local_stats.n_missed;
686 stats->n_lost += local_stats.n_lost;
687 mega_stats->n_mask_hit += local_stats.n_mask_hit;
691 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
693 return ovs_identifier_is_ufid(sfid) &&
694 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
697 static bool should_fill_mask(uint32_t ufid_flags)
699 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
702 static bool should_fill_actions(uint32_t ufid_flags)
704 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
707 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
708 const struct sw_flow_id *sfid,
711 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
713 /* OVS_FLOW_ATTR_UFID */
714 if (sfid && ovs_identifier_is_ufid(sfid))
715 len += nla_total_size(sfid->ufid_len);
717 /* OVS_FLOW_ATTR_KEY */
718 if (!sfid || should_fill_key(sfid, ufid_flags))
719 len += nla_total_size(ovs_key_attr_size());
721 /* OVS_FLOW_ATTR_MASK */
722 if (should_fill_mask(ufid_flags))
723 len += nla_total_size(ovs_key_attr_size());
725 /* OVS_FLOW_ATTR_ACTIONS */
726 if (should_fill_actions(ufid_flags))
727 len += nla_total_size(acts->actions_len);
730 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
731 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
732 + nla_total_size(8); /* OVS_FLOW_ATTR_USED */
735 /* Called with ovs_mutex or RCU read lock. */
736 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
739 struct ovs_flow_stats stats;
743 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
746 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
749 if (stats.n_packets &&
750 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
753 if ((u8)ntohs(tcp_flags) &&
754 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
760 /* Called with ovs_mutex or RCU read lock. */
761 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
762 struct sk_buff *skb, int skb_orig_len)
764 struct nlattr *start;
767 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
768 * this is the first flow to be dumped into 'skb'. This is unusual for
769 * Netlink but individual action lists can be longer than
770 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
771 * The userspace caller can always fetch the actions separately if it
772 * really wants them. (Most userspace callers in fact don't care.)
774 * This can only fail for dump operations because the skb is always
775 * properly sized for single flows.
777 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
779 const struct sw_flow_actions *sf_acts;
781 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
782 err = ovs_nla_put_actions(sf_acts->actions,
783 sf_acts->actions_len, skb);
786 nla_nest_end(skb, start);
791 nla_nest_cancel(skb, start);
793 } else if (skb_orig_len) {
800 /* Called with ovs_mutex or RCU read lock. */
801 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
802 struct sk_buff *skb, u32 portid,
803 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
805 const int skb_orig_len = skb->len;
806 struct ovs_header *ovs_header;
809 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
814 ovs_header->dp_ifindex = dp_ifindex;
816 err = ovs_nla_put_identifier(flow, skb);
820 if (should_fill_key(&flow->id, ufid_flags)) {
821 err = ovs_nla_put_masked_key(flow, skb);
826 if (should_fill_mask(ufid_flags)) {
827 err = ovs_nla_put_mask(flow, skb);
832 err = ovs_flow_cmd_fill_stats(flow, skb);
836 if (should_fill_actions(ufid_flags)) {
837 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
842 genlmsg_end(skb, ovs_header);
846 genlmsg_cancel(skb, ovs_header);
850 /* May not be called with RCU read lock. */
851 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
852 const struct sw_flow_id *sfid,
853 struct genl_info *info,
860 if (!always && !ovs_must_notify(&dp_flow_genl_family, info,
861 GROUP_ID(&ovs_dp_flow_multicast_group)))
864 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
865 skb = genlmsg_new_unicast(len, info, GFP_KERNEL);
867 return ERR_PTR(-ENOMEM);
872 /* Called with ovs_mutex. */
873 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
875 struct genl_info *info, u8 cmd,
876 bool always, u32 ufid_flags)
881 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
882 &flow->id, info, always, ufid_flags);
883 if (IS_ERR_OR_NULL(skb))
886 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
887 info->snd_portid, info->snd_seq, 0,
893 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
895 struct nlattr **a = info->attrs;
896 struct ovs_header *ovs_header = info->userhdr;
897 struct sw_flow *flow = NULL, *new_flow;
898 struct sw_flow_mask mask;
899 struct sk_buff *reply;
901 struct sw_flow_key key;
902 struct sw_flow_actions *acts;
903 struct sw_flow_match match;
904 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
906 bool log = !a[OVS_FLOW_ATTR_PROBE];
908 /* Must have key and actions. */
910 if (!a[OVS_FLOW_ATTR_KEY]) {
911 OVS_NLERR(log, "Flow key attr not present in new flow.");
914 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
915 OVS_NLERR(log, "Flow actions attr not present in new flow.");
919 /* Most of the time we need to allocate a new flow, do it before
922 new_flow = ovs_flow_alloc();
923 if (IS_ERR(new_flow)) {
924 error = PTR_ERR(new_flow);
929 ovs_match_init(&match, &key, &mask);
930 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
931 a[OVS_FLOW_ATTR_MASK], log);
935 ovs_flow_mask_key(&new_flow->key, &key, &mask);
937 /* Extract flow identifier. */
938 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
943 /* Validate actions. */
944 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
947 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
951 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
954 error = PTR_ERR(reply);
959 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
965 /* Check if this is a duplicate flow */
966 if (ovs_identifier_is_ufid(&new_flow->id))
967 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
969 flow = ovs_flow_tbl_lookup(&dp->table, &key);
971 rcu_assign_pointer(new_flow->sf_acts, acts);
973 /* Put flow in bucket. */
974 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
975 if (unlikely(error)) {
980 if (unlikely(reply)) {
981 error = ovs_flow_cmd_fill_info(new_flow,
982 ovs_header->dp_ifindex,
983 reply, info->snd_portid,
991 struct sw_flow_actions *old_acts;
993 /* Bail out if we're not allowed to modify an existing flow.
994 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
995 * because Generic Netlink treats the latter as a dump
996 * request. We also accept NLM_F_EXCL in case that bug ever
999 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
1002 goto err_unlock_ovs;
1004 /* The flow identifier has to be the same for flow updates.
1005 * Look for any overlapping flow.
1007 if (unlikely(!ovs_flow_cmp(flow, &match))) {
1008 if (ovs_identifier_is_key(&flow->id))
1009 flow = ovs_flow_tbl_lookup_exact(&dp->table,
1011 else /* UFID matches but key is different */
1015 goto err_unlock_ovs;
1018 /* Update actions. */
1019 old_acts = ovsl_dereference(flow->sf_acts);
1020 rcu_assign_pointer(flow->sf_acts, acts);
1022 if (unlikely(reply)) {
1023 error = ovs_flow_cmd_fill_info(flow,
1024 ovs_header->dp_ifindex,
1025 reply, info->snd_portid,
1033 ovs_nla_free_flow_actions(old_acts);
1034 ovs_flow_free(new_flow, false);
1038 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1047 ovs_flow_free(new_flow, false);
1052 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1053 static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
1054 const struct sw_flow_key *key,
1055 const struct sw_flow_mask *mask,
1058 struct sw_flow_actions *acts;
1059 struct sw_flow_key masked_key;
1062 ovs_flow_mask_key(&masked_key, key, mask);
1063 error = ovs_nla_copy_actions(a, &masked_key, &acts, log);
1066 "Actions may not be safe on all matching packets");
1067 return ERR_PTR(error);
1073 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1075 struct nlattr **a = info->attrs;
1076 struct ovs_header *ovs_header = info->userhdr;
1077 struct sw_flow_key key;
1078 struct sw_flow *flow;
1079 struct sw_flow_mask mask;
1080 struct sk_buff *reply = NULL;
1081 struct datapath *dp;
1082 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1083 struct sw_flow_match match;
1084 struct sw_flow_id sfid;
1085 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1087 bool log = !a[OVS_FLOW_ATTR_PROBE];
1092 if (!a[OVS_FLOW_ATTR_KEY]) {
1093 OVS_NLERR(log, "Flow key attribute not present in set flow.");
1097 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1098 ovs_match_init(&match, &key, &mask);
1099 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
1100 a[OVS_FLOW_ATTR_MASK], log);
1104 /* Validate actions. */
1105 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1106 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask,
1109 error = PTR_ERR(acts);
1113 /* Can allocate before locking if have acts. */
1114 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1116 if (IS_ERR(reply)) {
1117 error = PTR_ERR(reply);
1118 goto err_kfree_acts;
1123 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1124 if (unlikely(!dp)) {
1126 goto err_unlock_ovs;
1128 /* Check that the flow exists. */
1130 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1132 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1133 if (unlikely(!flow)) {
1135 goto err_unlock_ovs;
1138 /* Update actions, if present. */
1140 old_acts = ovsl_dereference(flow->sf_acts);
1141 rcu_assign_pointer(flow->sf_acts, acts);
1143 if (unlikely(reply)) {
1144 error = ovs_flow_cmd_fill_info(flow,
1145 ovs_header->dp_ifindex,
1146 reply, info->snd_portid,
1153 /* Could not alloc without acts before locking. */
1154 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1155 info, OVS_FLOW_CMD_NEW, false,
1158 if (unlikely(IS_ERR(reply))) {
1159 error = PTR_ERR(reply);
1160 goto err_unlock_ovs;
1165 if (a[OVS_FLOW_ATTR_CLEAR])
1166 ovs_flow_stats_clear(flow);
1170 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1172 ovs_nla_free_flow_actions(old_acts);
1185 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1187 struct nlattr **a = info->attrs;
1188 struct ovs_header *ovs_header = info->userhdr;
1189 struct sw_flow_key key;
1190 struct sk_buff *reply;
1191 struct sw_flow *flow;
1192 struct datapath *dp;
1193 struct sw_flow_match match;
1194 struct sw_flow_id ufid;
1195 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1197 bool log = !a[OVS_FLOW_ATTR_PROBE];
1200 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1201 if (a[OVS_FLOW_ATTR_KEY]) {
1202 ovs_match_init(&match, &key, NULL);
1203 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1205 } else if (!ufid_present) {
1207 "Flow get message rejected, Key attribute missing.");
1214 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1221 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1223 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1229 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1230 OVS_FLOW_CMD_NEW, true, ufid_flags);
1231 if (IS_ERR(reply)) {
1232 err = PTR_ERR(reply);
1237 return genlmsg_reply(reply, info);
1243 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1245 struct nlattr **a = info->attrs;
1246 struct ovs_header *ovs_header = info->userhdr;
1247 struct sw_flow_key key;
1248 struct sk_buff *reply;
1249 struct sw_flow *flow = NULL;
1250 struct datapath *dp;
1251 struct sw_flow_match match;
1252 struct sw_flow_id ufid;
1253 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1255 bool log = !a[OVS_FLOW_ATTR_PROBE];
1258 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1259 if (a[OVS_FLOW_ATTR_KEY]) {
1260 ovs_match_init(&match, &key, NULL);
1261 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1268 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1269 if (unlikely(!dp)) {
1274 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1275 err = ovs_flow_tbl_flush(&dp->table);
1280 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1282 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1283 if (unlikely(!flow)) {
1288 ovs_flow_tbl_remove(&dp->table, flow);
1291 reply = ovs_flow_cmd_alloc_info(rcu_dereference_raw(flow->sf_acts),
1292 &flow->id, info, false, ufid_flags);
1294 if (likely(reply)) {
1295 if (likely(!IS_ERR(reply))) {
1296 rcu_read_lock(); /*To keep RCU checker happy. */
1297 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1298 reply, info->snd_portid,
1304 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1306 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1307 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1312 ovs_flow_free(flow, true);
1319 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1321 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1322 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1323 struct table_instance *ti;
1324 struct datapath *dp;
1328 err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
1329 OVS_FLOW_ATTR_MAX, flow_policy);
1332 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1335 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1341 ti = rcu_dereference(dp->table.ti);
1343 struct sw_flow *flow;
1346 bucket = cb->args[0];
1348 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1352 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1353 NETLINK_CB(cb->skb).portid,
1354 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1355 OVS_FLOW_CMD_NEW, ufid_flags) < 0)
1358 cb->args[0] = bucket;
1365 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1366 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1367 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1368 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1369 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1370 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1371 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1372 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1375 static const struct genl_ops dp_flow_genl_ops[] = {
1376 { .cmd = OVS_FLOW_CMD_NEW,
1377 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1378 .policy = flow_policy,
1379 .doit = ovs_flow_cmd_new
1381 { .cmd = OVS_FLOW_CMD_DEL,
1382 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1383 .policy = flow_policy,
1384 .doit = ovs_flow_cmd_del
1386 { .cmd = OVS_FLOW_CMD_GET,
1387 .flags = 0, /* OK for unprivileged users. */
1388 .policy = flow_policy,
1389 .doit = ovs_flow_cmd_get,
1390 .dumpit = ovs_flow_cmd_dump
1392 { .cmd = OVS_FLOW_CMD_SET,
1393 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1394 .policy = flow_policy,
1395 .doit = ovs_flow_cmd_set,
1399 static struct genl_family dp_flow_genl_family = {
1400 .id = GENL_ID_GENERATE,
1401 .hdrsize = sizeof(struct ovs_header),
1402 .name = OVS_FLOW_FAMILY,
1403 .version = OVS_FLOW_VERSION,
1404 .maxattr = OVS_FLOW_ATTR_MAX,
1406 .parallel_ops = true,
1407 .ops = dp_flow_genl_ops,
1408 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1409 .mcgrps = &ovs_dp_flow_multicast_group,
1413 static size_t ovs_dp_cmd_msg_size(void)
1415 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1417 msgsize += nla_total_size(IFNAMSIZ);
1418 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1419 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1420 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1425 /* Called with ovs_mutex. */
1426 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1427 u32 portid, u32 seq, u32 flags, u8 cmd)
1429 struct ovs_header *ovs_header;
1430 struct ovs_dp_stats dp_stats;
1431 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1434 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1439 ovs_header->dp_ifindex = get_dpifindex(dp);
1441 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1443 goto nla_put_failure;
1445 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1446 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1448 goto nla_put_failure;
1450 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1451 sizeof(struct ovs_dp_megaflow_stats),
1452 &dp_megaflow_stats))
1453 goto nla_put_failure;
1455 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1456 goto nla_put_failure;
1458 genlmsg_end(skb, ovs_header);
1462 genlmsg_cancel(skb, ovs_header);
1467 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1469 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1472 /* Called with rcu_read_lock or ovs_mutex. */
1473 static struct datapath *lookup_datapath(struct net *net,
1474 const struct ovs_header *ovs_header,
1475 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1477 struct datapath *dp;
1479 if (!a[OVS_DP_ATTR_NAME])
1480 dp = get_dp(net, ovs_header->dp_ifindex);
1482 struct vport *vport;
1484 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1485 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1487 return dp ? dp : ERR_PTR(-ENODEV);
1490 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1492 struct datapath *dp;
1494 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1498 WARN(dp->user_features, "Dropping previously announced user features\n");
1499 dp->user_features = 0;
1502 static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1504 if (a[OVS_DP_ATTR_USER_FEATURES])
1505 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1508 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1510 struct nlattr **a = info->attrs;
1511 struct vport_parms parms;
1512 struct sk_buff *reply;
1513 struct datapath *dp;
1514 struct vport *vport;
1515 struct ovs_net *ovs_net;
1519 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1522 reply = ovs_dp_cmd_alloc_info(info);
1527 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1529 goto err_free_reply;
1531 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1533 /* Allocate table. */
1534 err = ovs_flow_tbl_init(&dp->table);
1538 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1539 if (!dp->stats_percpu) {
1541 goto err_destroy_table;
1544 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1548 goto err_destroy_percpu;
1551 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1552 INIT_HLIST_HEAD(&dp->ports[i]);
1554 /* Set up our datapath device. */
1555 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1556 parms.type = OVS_VPORT_TYPE_INTERNAL;
1557 parms.options = NULL;
1559 parms.port_no = OVSP_LOCAL;
1560 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1562 ovs_dp_change(dp, a);
1564 /* So far only local changes have been made, now need the lock. */
1567 vport = new_vport(&parms);
1568 if (IS_ERR(vport)) {
1569 err = PTR_ERR(vport);
1573 if (err == -EEXIST) {
1574 /* An outdated user space instance that does not understand
1575 * the concept of user_features has attempted to create a new
1576 * datapath and is likely to reuse it. Drop all user features.
1578 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1579 ovs_dp_reset_user_features(skb, info);
1582 goto err_destroy_ports_array;
1585 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1586 info->snd_seq, 0, OVS_DP_CMD_NEW);
1589 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1590 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1594 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1597 err_destroy_ports_array:
1601 free_percpu(dp->stats_percpu);
1603 ovs_flow_tbl_destroy(&dp->table);
1605 release_net(ovs_dp_get_net(dp));
1613 /* Called with ovs_mutex. */
1614 static void __dp_destroy(struct datapath *dp)
1618 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1619 struct vport *vport;
1620 struct hlist_node *n;
1622 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1623 if (vport->port_no != OVSP_LOCAL)
1624 ovs_dp_detach_port(vport);
1627 list_del_rcu(&dp->list_node);
1629 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1630 * all ports in datapath are destroyed first before freeing datapath.
1632 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1634 /* RCU destroy the flow table */
1635 call_rcu(&dp->rcu, destroy_dp_rcu);
1638 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1640 struct sk_buff *reply;
1641 struct datapath *dp;
1644 reply = ovs_dp_cmd_alloc_info(info);
1649 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1652 goto err_unlock_free;
1654 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1655 info->snd_seq, 0, OVS_DP_CMD_DEL);
1661 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1670 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1672 struct sk_buff *reply;
1673 struct datapath *dp;
1676 reply = ovs_dp_cmd_alloc_info(info);
1681 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1684 goto err_unlock_free;
1686 ovs_dp_change(dp, info->attrs);
1688 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1689 info->snd_seq, 0, OVS_DP_CMD_NEW);
1694 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1703 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1705 struct sk_buff *reply;
1706 struct datapath *dp;
1709 reply = ovs_dp_cmd_alloc_info(info);
1714 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1717 goto err_unlock_free;
1719 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1720 info->snd_seq, 0, OVS_DP_CMD_NEW);
1724 return genlmsg_reply(reply, info);
1732 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1734 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1735 struct datapath *dp;
1736 int skip = cb->args[0];
1740 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1742 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1743 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1744 OVS_DP_CMD_NEW) < 0)
1755 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1756 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1757 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1758 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1761 static const struct genl_ops dp_datapath_genl_ops[] = {
1762 { .cmd = OVS_DP_CMD_NEW,
1763 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1764 .policy = datapath_policy,
1765 .doit = ovs_dp_cmd_new
1767 { .cmd = OVS_DP_CMD_DEL,
1768 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1769 .policy = datapath_policy,
1770 .doit = ovs_dp_cmd_del
1772 { .cmd = OVS_DP_CMD_GET,
1773 .flags = 0, /* OK for unprivileged users. */
1774 .policy = datapath_policy,
1775 .doit = ovs_dp_cmd_get,
1776 .dumpit = ovs_dp_cmd_dump
1778 { .cmd = OVS_DP_CMD_SET,
1779 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1780 .policy = datapath_policy,
1781 .doit = ovs_dp_cmd_set,
1785 static struct genl_family dp_datapath_genl_family = {
1786 .id = GENL_ID_GENERATE,
1787 .hdrsize = sizeof(struct ovs_header),
1788 .name = OVS_DATAPATH_FAMILY,
1789 .version = OVS_DATAPATH_VERSION,
1790 .maxattr = OVS_DP_ATTR_MAX,
1792 .parallel_ops = true,
1793 .ops = dp_datapath_genl_ops,
1794 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1795 .mcgrps = &ovs_dp_datapath_multicast_group,
1799 /* Called with ovs_mutex or RCU read lock. */
1800 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1801 u32 portid, u32 seq, u32 flags, u8 cmd)
1803 struct ovs_header *ovs_header;
1804 struct ovs_vport_stats vport_stats;
1807 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1812 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1814 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1815 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1816 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)))
1817 goto nla_put_failure;
1819 ovs_vport_get_stats(vport, &vport_stats);
1820 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1822 goto nla_put_failure;
1824 if (ovs_vport_get_upcall_portids(vport, skb))
1825 goto nla_put_failure;
1827 err = ovs_vport_get_options(vport, skb);
1828 if (err == -EMSGSIZE)
1831 genlmsg_end(skb, ovs_header);
1837 genlmsg_cancel(skb, ovs_header);
1841 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1843 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1846 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1847 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1850 struct sk_buff *skb;
1853 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1855 return ERR_PTR(-ENOMEM);
1857 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1863 /* Called with ovs_mutex or RCU read lock. */
1864 static struct vport *lookup_vport(struct net *net,
1865 const struct ovs_header *ovs_header,
1866 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1868 struct datapath *dp;
1869 struct vport *vport;
1871 if (a[OVS_VPORT_ATTR_NAME]) {
1872 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1874 return ERR_PTR(-ENODEV);
1875 if (ovs_header->dp_ifindex &&
1876 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1877 return ERR_PTR(-ENODEV);
1879 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1880 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1882 if (port_no >= DP_MAX_PORTS)
1883 return ERR_PTR(-EFBIG);
1885 dp = get_dp(net, ovs_header->dp_ifindex);
1887 return ERR_PTR(-ENODEV);
1889 vport = ovs_vport_ovsl_rcu(dp, port_no);
1891 return ERR_PTR(-ENODEV);
1894 return ERR_PTR(-EINVAL);
1897 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1899 struct nlattr **a = info->attrs;
1900 struct ovs_header *ovs_header = info->userhdr;
1901 struct vport_parms parms;
1902 struct sk_buff *reply;
1903 struct vport *vport;
1904 struct datapath *dp;
1908 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1909 !a[OVS_VPORT_ATTR_UPCALL_PID])
1912 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1913 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1914 if (port_no >= DP_MAX_PORTS)
1917 reply = ovs_vport_cmd_alloc_info();
1923 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1926 goto exit_unlock_free;
1929 vport = ovs_vport_ovsl(dp, port_no);
1932 goto exit_unlock_free;
1934 for (port_no = 1; ; port_no++) {
1935 if (port_no >= DP_MAX_PORTS) {
1937 goto exit_unlock_free;
1939 vport = ovs_vport_ovsl(dp, port_no);
1945 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1946 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1947 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1949 parms.port_no = port_no;
1950 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1952 vport = new_vport(&parms);
1953 err = PTR_ERR(vport);
1954 if (IS_ERR(vport)) {
1957 goto exit_unlock_free;
1960 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1961 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1965 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1974 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1976 struct nlattr **a = info->attrs;
1977 struct sk_buff *reply;
1978 struct vport *vport;
1981 reply = ovs_vport_cmd_alloc_info();
1986 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1987 err = PTR_ERR(vport);
1989 goto exit_unlock_free;
1991 if (a[OVS_VPORT_ATTR_TYPE] &&
1992 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1994 goto exit_unlock_free;
1997 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1998 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2000 goto exit_unlock_free;
2003 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2004 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2006 err = ovs_vport_set_upcall_portids(vport, ids);
2008 goto exit_unlock_free;
2011 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2012 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2016 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2025 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2027 struct nlattr **a = info->attrs;
2028 struct sk_buff *reply;
2029 struct vport *vport;
2032 reply = ovs_vport_cmd_alloc_info();
2037 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2038 err = PTR_ERR(vport);
2040 goto exit_unlock_free;
2042 if (vport->port_no == OVSP_LOCAL) {
2044 goto exit_unlock_free;
2047 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2048 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
2050 ovs_dp_detach_port(vport);
2053 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2062 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2064 struct nlattr **a = info->attrs;
2065 struct ovs_header *ovs_header = info->userhdr;
2066 struct sk_buff *reply;
2067 struct vport *vport;
2070 reply = ovs_vport_cmd_alloc_info();
2075 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2076 err = PTR_ERR(vport);
2078 goto exit_unlock_free;
2079 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2080 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2084 return genlmsg_reply(reply, info);
2092 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2094 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2095 struct datapath *dp;
2096 int bucket = cb->args[0], skip = cb->args[1];
2100 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2105 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2106 struct vport *vport;
2109 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2111 ovs_vport_cmd_fill_info(vport, skb,
2112 NETLINK_CB(cb->skb).portid,
2115 OVS_VPORT_CMD_NEW) < 0)
2131 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2132 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2133 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2134 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2135 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2136 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2137 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2140 static const struct genl_ops dp_vport_genl_ops[] = {
2141 { .cmd = OVS_VPORT_CMD_NEW,
2142 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2143 .policy = vport_policy,
2144 .doit = ovs_vport_cmd_new
2146 { .cmd = OVS_VPORT_CMD_DEL,
2147 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2148 .policy = vport_policy,
2149 .doit = ovs_vport_cmd_del
2151 { .cmd = OVS_VPORT_CMD_GET,
2152 .flags = 0, /* OK for unprivileged users. */
2153 .policy = vport_policy,
2154 .doit = ovs_vport_cmd_get,
2155 .dumpit = ovs_vport_cmd_dump
2157 { .cmd = OVS_VPORT_CMD_SET,
2158 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2159 .policy = vport_policy,
2160 .doit = ovs_vport_cmd_set,
2164 struct genl_family dp_vport_genl_family = {
2165 .id = GENL_ID_GENERATE,
2166 .hdrsize = sizeof(struct ovs_header),
2167 .name = OVS_VPORT_FAMILY,
2168 .version = OVS_VPORT_VERSION,
2169 .maxattr = OVS_VPORT_ATTR_MAX,
2171 .parallel_ops = true,
2172 .ops = dp_vport_genl_ops,
2173 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2174 .mcgrps = &ovs_dp_vport_multicast_group,
2178 static struct genl_family * const dp_genl_families[] = {
2179 &dp_datapath_genl_family,
2180 &dp_vport_genl_family,
2181 &dp_flow_genl_family,
2182 &dp_packet_genl_family,
2185 static void dp_unregister_genl(int n_families)
2189 for (i = 0; i < n_families; i++)
2190 genl_unregister_family(dp_genl_families[i]);
2193 static int dp_register_genl(void)
2198 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2200 err = genl_register_family(dp_genl_families[i]);
2208 dp_unregister_genl(i);
2212 static int __net_init ovs_init_net(struct net *net)
2214 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2216 INIT_LIST_HEAD(&ovs_net->dps);
2217 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2221 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2222 struct list_head *head)
2224 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2225 struct datapath *dp;
2227 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2230 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2231 struct vport *vport;
2233 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2234 struct netdev_vport *netdev_vport;
2236 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2239 netdev_vport = netdev_vport_priv(vport);
2240 if (dev_net(netdev_vport->dev) == dnet)
2241 list_add(&vport->detach_list, head);
2247 static void __net_exit ovs_exit_net(struct net *dnet)
2249 struct datapath *dp, *dp_next;
2250 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2251 struct vport *vport, *vport_next;
2256 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2261 list_vports_from_net(net, dnet, &head);
2264 /* Detach all vports from given namespace. */
2265 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2266 list_del(&vport->detach_list);
2267 ovs_dp_detach_port(vport);
2272 cancel_work_sync(&ovs_net->dp_notify_work);
2275 static struct pernet_operations ovs_net_ops = {
2276 .init = ovs_init_net,
2277 .exit = ovs_exit_net,
2279 .size = sizeof(struct ovs_net),
2282 DEFINE_COMPAT_PNET_REG_FUNC(device);
2284 static int __init dp_init(void)
2288 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2290 pr_info("Open vSwitch switching datapath %s\n", VERSION);
2292 err = action_fifos_init();
2296 err = ovs_internal_dev_rtnl_link_register();
2298 goto error_action_fifos_exit;
2300 err = ovs_flow_init();
2302 goto error_unreg_rtnl_link;
2304 err = ovs_vport_init();
2306 goto error_flow_exit;
2308 err = register_pernet_device(&ovs_net_ops);
2310 goto error_vport_exit;
2312 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2314 goto error_netns_exit;
2316 err = ovs_netdev_init();
2318 goto error_unreg_notifier;
2320 err = dp_register_genl();
2322 goto error_unreg_netdev;
2328 error_unreg_notifier:
2329 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2331 unregister_pernet_device(&ovs_net_ops);
2336 error_unreg_rtnl_link:
2337 ovs_internal_dev_rtnl_link_unregister();
2338 error_action_fifos_exit:
2339 action_fifos_exit();
2344 static void dp_cleanup(void)
2346 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2348 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2349 unregister_pernet_device(&ovs_net_ops);
2353 ovs_internal_dev_rtnl_link_unregister();
2354 action_fifos_exit();
2357 module_init(dp_init);
2358 module_exit(dp_cleanup);
2360 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2361 MODULE_LICENSE("GPL");
2362 MODULE_VERSION(VERSION);