2 * Copyright (c) 2007-2015 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.h>
27 #include <linux/jhash.h>
28 #include <linux/delay.h>
29 #include <linux/time.h>
30 #include <linux/etherdevice.h>
31 #include <linux/genetlink.h>
32 #include <linux/kernel.h>
33 #include <linux/kthread.h>
34 #include <linux/mutex.h>
35 #include <linux/percpu.h>
36 #include <linux/rcupdate.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/version.h>
40 #include <linux/ethtool.h>
41 #include <linux/wait.h>
42 #include <asm/div64.h>
43 #include <linux/highmem.h>
44 #include <linux/netfilter_bridge.h>
45 #include <linux/netfilter_ipv4.h>
46 #include <linux/inetdevice.h>
47 #include <linux/list.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <net/genetlink.h>
52 #include <net/net_namespace.h>
53 #include <net/netns/generic.h>
56 #include "conntrack.h"
58 #include "flow_table.h"
59 #include "flow_netlink.h"
61 #include "vport-internal_dev.h"
62 #include "vport-netdev.h"
64 int ovs_net_id __read_mostly;
65 EXPORT_SYMBOL_GPL(ovs_net_id);
67 static struct genl_family dp_packet_genl_family;
68 static struct genl_family dp_flow_genl_family;
69 static struct genl_family dp_datapath_genl_family;
71 static const struct nla_policy flow_policy[];
73 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
74 .name = OVS_FLOW_MCGROUP
77 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
78 .name = OVS_DATAPATH_MCGROUP
81 struct genl_multicast_group ovs_dp_vport_multicast_group = {
82 .name = OVS_VPORT_MCGROUP
85 /* Check if need to build a reply message.
86 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply.
88 static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
91 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
92 genl_has_listeners(family, genl_info_net(info), group);
95 static void ovs_notify(struct genl_family *family, struct genl_multicast_group *grp,
96 struct sk_buff *skb, struct genl_info *info)
98 genl_notify(family, skb, genl_info_net(info),
99 info->snd_portid, GROUP_ID(grp), info->nlhdr, GFP_KERNEL);
105 * All writes e.g. Writes to device state (add/remove datapath, port, set
106 * operations on vports, etc.), Writes to other state (flow table
107 * modifications, set miscellaneous datapath parameters, etc.) are protected
110 * Reads are protected by RCU.
112 * There are a few special cases (mostly stats) that have their own
113 * synchronization but they nest under all of above and don't interact with
116 * The RTNL lock nests inside ovs_mutex.
119 static DEFINE_MUTEX(ovs_mutex);
123 mutex_lock(&ovs_mutex);
126 void ovs_unlock(void)
128 mutex_unlock(&ovs_mutex);
131 #ifdef CONFIG_LOCKDEP
132 int lockdep_ovsl_is_held(void)
135 return lockdep_is_held(&ovs_mutex);
139 EXPORT_SYMBOL_GPL(lockdep_ovsl_is_held);
142 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
143 const struct sw_flow_key *,
144 const struct dp_upcall_info *);
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 ovs_vport_name(vport);
185 static int get_dpifindex(const struct datapath *dp)
192 local = ovs_vport_rcu(dp, OVSP_LOCAL);
194 ifindex = 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);
213 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
216 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
219 /* Called with ovs_mutex or RCU read lock. */
220 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
223 struct hlist_head *head;
225 head = vport_hash_bucket(dp, port_no);
226 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
227 if (vport->port_no == port_no)
233 /* Called with ovs_mutex. */
234 static struct vport *new_vport(const struct vport_parms *parms)
238 vport = ovs_vport_add(parms);
239 if (!IS_ERR(vport)) {
240 struct datapath *dp = parms->dp;
241 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
243 hlist_add_head_rcu(&vport->dp_hash_node, head);
248 void ovs_dp_detach_port(struct vport *p)
252 /* First drop references to device. */
253 hlist_del_rcu(&p->dp_hash_node);
255 /* Then destroy it. */
259 /* Must be called with rcu_read_lock. */
260 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
262 const struct vport *p = OVS_CB(skb)->input_vport;
263 struct datapath *dp = p->dp;
264 struct sw_flow *flow;
265 struct sw_flow_actions *sf_acts;
266 struct dp_stats_percpu *stats;
270 stats = this_cpu_ptr(dp->stats_percpu);
273 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
275 if (unlikely(!flow)) {
276 struct dp_upcall_info upcall;
279 memset(&upcall, 0, sizeof(upcall));
280 upcall.cmd = OVS_PACKET_CMD_MISS;
281 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
282 upcall.mru = OVS_CB(skb)->mru;
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);
408 /* OVS_PACKET_ATTR_MRU */
409 if (upcall_info->mru)
410 size += nla_total_size(sizeof(upcall_info->mru));
415 static void pad_packet(struct datapath *dp, struct sk_buff *skb)
417 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
418 size_t plen = NLA_ALIGN(skb->len) - skb->len;
421 memset(skb_put(skb, plen), 0, plen);
425 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
426 const struct sw_flow_key *key,
427 const struct dp_upcall_info *upcall_info)
429 struct ovs_header *upcall;
430 struct sk_buff *nskb = NULL;
431 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
433 struct genl_info info = {
434 #ifdef HAVE_GENLMSG_NEW_UNICAST
435 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
437 .snd_portid = upcall_info->portid,
443 dp_ifindex = get_dpifindex(dp);
447 if (skb_vlan_tag_present(skb)) {
448 nskb = skb_clone(skb, GFP_ATOMIC);
452 nskb = __vlan_hwaccel_push_inside(nskb);
459 if (nla_attr_size(skb->len) > USHRT_MAX) {
464 /* Complete checksum if needed */
465 if (skb->ip_summed == CHECKSUM_PARTIAL &&
466 (err = skb_checksum_help(skb)))
469 /* Older versions of OVS user space enforce alignment of the last
470 * Netlink attribute to NLA_ALIGNTO which would require extensive
471 * padding logic. Only perform zerocopy if padding is not required.
473 if (dp->user_features & OVS_DP_F_UNALIGNED)
474 hlen = skb_zerocopy_headlen(skb);
478 len = upcall_msg_size(upcall_info, hlen);
479 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
485 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
486 0, upcall_info->cmd);
487 upcall->dp_ifindex = dp_ifindex;
489 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
492 if (upcall_info->userdata)
493 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
494 nla_len(upcall_info->userdata),
495 nla_data(upcall_info->userdata));
498 if (upcall_info->egress_tun_info) {
499 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
500 err = ovs_nla_put_egress_tunnel_key(user_skb,
501 upcall_info->egress_tun_info,
502 upcall_info->egress_tun_opts);
504 nla_nest_end(user_skb, nla);
507 if (upcall_info->actions_len) {
508 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS);
509 err = ovs_nla_put_actions(upcall_info->actions,
510 upcall_info->actions_len,
513 nla_nest_end(user_skb, nla);
515 nla_nest_cancel(user_skb, nla);
518 /* Add OVS_PACKET_ATTR_MRU */
519 if (upcall_info->mru) {
520 if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
525 pad_packet(dp, user_skb);
528 /* Only reserve room for attribute header, packet data is added
531 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
535 nla->nla_len = nla_attr_size(skb->len);
537 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
541 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
542 pad_packet(dp, user_skb);
544 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
546 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
556 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
558 struct ovs_header *ovs_header = info->userhdr;
559 struct net *net = sock_net(skb->sk);
560 struct nlattr **a = info->attrs;
561 struct sw_flow_actions *acts;
562 struct sk_buff *packet;
563 struct sw_flow *flow;
564 struct sw_flow_actions *sf_acts;
567 struct vport *input_vport;
571 bool log = !a[OVS_PACKET_ATTR_PROBE];
574 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
575 !a[OVS_PACKET_ATTR_ACTIONS])
578 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
579 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
583 skb_reserve(packet, NET_IP_ALIGN);
585 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
587 skb_reset_mac_header(packet);
588 eth = eth_hdr(packet);
590 /* Normally, setting the skb 'protocol' field would be handled by a
591 * call to eth_type_trans(), but it assumes there's a sending
592 * device, which we may not have.
594 if (eth_proto_is_802_3(eth->h_proto))
595 packet->protocol = eth->h_proto;
597 packet->protocol = htons(ETH_P_802_2);
599 /* Set packet's mru */
600 if (a[OVS_PACKET_ATTR_MRU]) {
601 mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
602 packet->ignore_df = 1;
604 OVS_CB(packet)->mru = mru;
606 /* Build an sw_flow for sending this packet. */
607 flow = ovs_flow_alloc();
612 err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
613 packet, &flow->key, log);
617 err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
618 &flow->key, &acts, log);
622 rcu_assign_pointer(flow->sf_acts, acts);
623 packet->priority = flow->key.phy.priority;
624 packet->mark = flow->key.phy.skb_mark;
627 dp = get_dp_rcu(net, ovs_header->dp_ifindex);
632 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
634 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
639 packet->dev = input_vport->dev;
640 OVS_CB(packet)->input_vport = input_vport;
641 sf_acts = rcu_dereference(flow->sf_acts);
644 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
648 ovs_flow_free(flow, false);
654 ovs_flow_free(flow, false);
661 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
662 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
663 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
664 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
665 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
666 [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
669 static struct genl_ops dp_packet_genl_ops[] = {
670 { .cmd = OVS_PACKET_CMD_EXECUTE,
671 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
672 .policy = packet_policy,
673 .doit = ovs_packet_cmd_execute
677 static struct genl_family dp_packet_genl_family = {
678 .id = GENL_ID_GENERATE,
679 .hdrsize = sizeof(struct ovs_header),
680 .name = OVS_PACKET_FAMILY,
681 .version = OVS_PACKET_VERSION,
682 .maxattr = OVS_PACKET_ATTR_MAX,
684 .parallel_ops = true,
685 .ops = dp_packet_genl_ops,
686 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
689 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
690 struct ovs_dp_megaflow_stats *mega_stats)
694 memset(mega_stats, 0, sizeof(*mega_stats));
696 stats->n_flows = ovs_flow_tbl_count(&dp->table);
697 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
699 stats->n_hit = stats->n_missed = stats->n_lost = 0;
701 for_each_possible_cpu(i) {
702 const struct dp_stats_percpu *percpu_stats;
703 struct dp_stats_percpu local_stats;
706 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
709 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
710 local_stats = *percpu_stats;
711 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
713 stats->n_hit += local_stats.n_hit;
714 stats->n_missed += local_stats.n_missed;
715 stats->n_lost += local_stats.n_lost;
716 mega_stats->n_mask_hit += local_stats.n_mask_hit;
720 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
722 return ovs_identifier_is_ufid(sfid) &&
723 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
726 static bool should_fill_mask(uint32_t ufid_flags)
728 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
731 static bool should_fill_actions(uint32_t ufid_flags)
733 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
736 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
737 const struct sw_flow_id *sfid,
740 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
742 /* OVS_FLOW_ATTR_UFID */
743 if (sfid && ovs_identifier_is_ufid(sfid))
744 len += nla_total_size(sfid->ufid_len);
746 /* OVS_FLOW_ATTR_KEY */
747 if (!sfid || should_fill_key(sfid, ufid_flags))
748 len += nla_total_size(ovs_key_attr_size());
750 /* OVS_FLOW_ATTR_MASK */
751 if (should_fill_mask(ufid_flags))
752 len += nla_total_size(ovs_key_attr_size());
754 /* OVS_FLOW_ATTR_ACTIONS */
755 if (should_fill_actions(ufid_flags))
756 len += nla_total_size(acts->orig_len);
759 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
760 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
761 + nla_total_size(8); /* OVS_FLOW_ATTR_USED */
764 /* Called with ovs_mutex or RCU read lock. */
765 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
768 struct ovs_flow_stats stats;
772 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
775 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
778 if (stats.n_packets &&
779 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
782 if ((u8)ntohs(tcp_flags) &&
783 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
789 /* Called with ovs_mutex or RCU read lock. */
790 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
791 struct sk_buff *skb, int skb_orig_len)
793 struct nlattr *start;
796 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
797 * this is the first flow to be dumped into 'skb'. This is unusual for
798 * Netlink but individual action lists can be longer than
799 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
800 * The userspace caller can always fetch the actions separately if it
801 * really wants them. (Most userspace callers in fact don't care.)
803 * This can only fail for dump operations because the skb is always
804 * properly sized for single flows.
806 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
808 const struct sw_flow_actions *sf_acts;
810 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
811 err = ovs_nla_put_actions(sf_acts->actions,
812 sf_acts->actions_len, skb);
815 nla_nest_end(skb, start);
820 nla_nest_cancel(skb, start);
822 } else if (skb_orig_len) {
829 /* Called with ovs_mutex or RCU read lock. */
830 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
831 struct sk_buff *skb, u32 portid,
832 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
834 const int skb_orig_len = skb->len;
835 struct ovs_header *ovs_header;
838 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
843 ovs_header->dp_ifindex = dp_ifindex;
845 err = ovs_nla_put_identifier(flow, skb);
849 if (should_fill_key(&flow->id, ufid_flags)) {
850 err = ovs_nla_put_masked_key(flow, skb);
855 if (should_fill_mask(ufid_flags)) {
856 err = ovs_nla_put_mask(flow, skb);
861 err = ovs_flow_cmd_fill_stats(flow, skb);
865 if (should_fill_actions(ufid_flags)) {
866 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
871 genlmsg_end(skb, ovs_header);
875 genlmsg_cancel(skb, ovs_header);
879 /* May not be called with RCU read lock. */
880 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
881 const struct sw_flow_id *sfid,
882 struct genl_info *info,
889 if (!always && !ovs_must_notify(&dp_flow_genl_family, info,
890 GROUP_ID(&ovs_dp_flow_multicast_group)))
893 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
894 skb = genlmsg_new_unicast(len, info, GFP_KERNEL);
896 return ERR_PTR(-ENOMEM);
901 /* Called with ovs_mutex. */
902 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
904 struct genl_info *info, u8 cmd,
905 bool always, u32 ufid_flags)
910 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
911 &flow->id, info, always, ufid_flags);
912 if (IS_ERR_OR_NULL(skb))
915 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
916 info->snd_portid, info->snd_seq, 0,
922 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
924 struct net *net = sock_net(skb->sk);
925 struct nlattr **a = info->attrs;
926 struct ovs_header *ovs_header = info->userhdr;
927 struct sw_flow *flow = NULL, *new_flow;
928 struct sw_flow_mask mask;
929 struct sk_buff *reply;
931 struct sw_flow_key key;
932 struct sw_flow_actions *acts;
933 struct sw_flow_match match;
934 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
936 bool log = !a[OVS_FLOW_ATTR_PROBE];
938 /* Must have key and actions. */
940 if (!a[OVS_FLOW_ATTR_KEY]) {
941 OVS_NLERR(log, "Flow key attr not present in new flow.");
944 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
945 OVS_NLERR(log, "Flow actions attr not present in new flow.");
949 /* Most of the time we need to allocate a new flow, do it before
952 new_flow = ovs_flow_alloc();
953 if (IS_ERR(new_flow)) {
954 error = PTR_ERR(new_flow);
959 ovs_match_init(&match, &key, &mask);
960 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
961 a[OVS_FLOW_ATTR_MASK], log);
965 ovs_flow_mask_key(&new_flow->key, &key, true, &mask);
967 /* Extract flow identifier. */
968 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
973 /* Validate actions. */
974 error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
975 &new_flow->key, &acts, log);
977 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
981 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
984 error = PTR_ERR(reply);
989 dp = get_dp(net, ovs_header->dp_ifindex);
995 /* Check if this is a duplicate flow */
996 if (ovs_identifier_is_ufid(&new_flow->id))
997 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
999 flow = ovs_flow_tbl_lookup(&dp->table, &key);
1000 if (likely(!flow)) {
1001 rcu_assign_pointer(new_flow->sf_acts, acts);
1003 /* Put flow in bucket. */
1004 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
1005 if (unlikely(error)) {
1007 goto err_unlock_ovs;
1010 if (unlikely(reply)) {
1011 error = ovs_flow_cmd_fill_info(new_flow,
1012 ovs_header->dp_ifindex,
1013 reply, info->snd_portid,
1021 struct sw_flow_actions *old_acts;
1023 /* Bail out if we're not allowed to modify an existing flow.
1024 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1025 * because Generic Netlink treats the latter as a dump
1026 * request. We also accept NLM_F_EXCL in case that bug ever
1029 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
1032 goto err_unlock_ovs;
1034 /* The flow identifier has to be the same for flow updates.
1035 * Look for any overlapping flow.
1037 if (unlikely(!ovs_flow_cmp(flow, &match))) {
1038 if (ovs_identifier_is_key(&flow->id))
1039 flow = ovs_flow_tbl_lookup_exact(&dp->table,
1041 else /* UFID matches but key is different */
1045 goto err_unlock_ovs;
1048 /* Update actions. */
1049 old_acts = ovsl_dereference(flow->sf_acts);
1050 rcu_assign_pointer(flow->sf_acts, acts);
1052 if (unlikely(reply)) {
1053 error = ovs_flow_cmd_fill_info(flow,
1054 ovs_header->dp_ifindex,
1055 reply, info->snd_portid,
1063 ovs_nla_free_flow_actions_rcu(old_acts);
1064 ovs_flow_free(new_flow, false);
1068 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1075 ovs_nla_free_flow_actions(acts);
1077 ovs_flow_free(new_flow, false);
1082 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1083 static struct sw_flow_actions *get_flow_actions(struct net *net,
1084 const struct nlattr *a,
1085 const struct sw_flow_key *key,
1086 const struct sw_flow_mask *mask,
1089 struct sw_flow_actions *acts;
1090 struct sw_flow_key masked_key;
1093 ovs_flow_mask_key(&masked_key, key, true, mask);
1094 error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
1097 "Actions may not be safe on all matching packets");
1098 return ERR_PTR(error);
1104 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1106 struct net *net = sock_net(skb->sk);
1107 struct nlattr **a = info->attrs;
1108 struct ovs_header *ovs_header = info->userhdr;
1109 struct sw_flow_key key;
1110 struct sw_flow *flow;
1111 struct sw_flow_mask mask;
1112 struct sk_buff *reply = NULL;
1113 struct datapath *dp;
1114 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1115 struct sw_flow_match match;
1116 struct sw_flow_id sfid;
1117 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1119 bool log = !a[OVS_FLOW_ATTR_PROBE];
1124 if (!a[OVS_FLOW_ATTR_KEY]) {
1125 OVS_NLERR(log, "Flow key attribute not present in set flow.");
1129 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1130 ovs_match_init(&match, &key, &mask);
1131 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1132 a[OVS_FLOW_ATTR_MASK], log);
1136 /* Validate actions. */
1137 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1138 acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], &key,
1141 error = PTR_ERR(acts);
1145 /* Can allocate before locking if have acts. */
1146 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1148 if (IS_ERR(reply)) {
1149 error = PTR_ERR(reply);
1150 goto err_kfree_acts;
1155 dp = get_dp(net, ovs_header->dp_ifindex);
1156 if (unlikely(!dp)) {
1158 goto err_unlock_ovs;
1160 /* Check that the flow exists. */
1162 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1164 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1165 if (unlikely(!flow)) {
1167 goto err_unlock_ovs;
1170 /* Update actions, if present. */
1172 old_acts = ovsl_dereference(flow->sf_acts);
1173 rcu_assign_pointer(flow->sf_acts, acts);
1175 if (unlikely(reply)) {
1176 error = ovs_flow_cmd_fill_info(flow,
1177 ovs_header->dp_ifindex,
1178 reply, info->snd_portid,
1185 /* Could not alloc without acts before locking. */
1186 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1187 info, OVS_FLOW_CMD_NEW, false,
1190 if (unlikely(IS_ERR(reply))) {
1191 error = PTR_ERR(reply);
1192 goto err_unlock_ovs;
1197 if (a[OVS_FLOW_ATTR_CLEAR])
1198 ovs_flow_stats_clear(flow);
1202 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1204 ovs_nla_free_flow_actions_rcu(old_acts);
1212 ovs_nla_free_flow_actions(acts);
1217 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1219 struct nlattr **a = info->attrs;
1220 struct ovs_header *ovs_header = info->userhdr;
1221 struct net *net = sock_net(skb->sk);
1222 struct sw_flow_key key;
1223 struct sk_buff *reply;
1224 struct sw_flow *flow;
1225 struct datapath *dp;
1226 struct sw_flow_match match;
1227 struct sw_flow_id ufid;
1228 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1230 bool log = !a[OVS_FLOW_ATTR_PROBE];
1233 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1234 if (a[OVS_FLOW_ATTR_KEY]) {
1235 ovs_match_init(&match, &key, NULL);
1236 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
1238 } else if (!ufid_present) {
1240 "Flow get message rejected, Key attribute missing.");
1247 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1254 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1256 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1262 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1263 OVS_FLOW_CMD_NEW, true, ufid_flags);
1264 if (IS_ERR(reply)) {
1265 err = PTR_ERR(reply);
1270 return genlmsg_reply(reply, info);
1276 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1278 struct nlattr **a = info->attrs;
1279 struct ovs_header *ovs_header = info->userhdr;
1280 struct net *net = sock_net(skb->sk);
1281 struct sw_flow_key key;
1282 struct sk_buff *reply;
1283 struct sw_flow *flow = NULL;
1284 struct datapath *dp;
1285 struct sw_flow_match match;
1286 struct sw_flow_id ufid;
1287 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1289 bool log = !a[OVS_FLOW_ATTR_PROBE];
1292 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1293 if (a[OVS_FLOW_ATTR_KEY]) {
1294 ovs_match_init(&match, &key, NULL);
1295 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1302 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1303 if (unlikely(!dp)) {
1308 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1309 err = ovs_flow_tbl_flush(&dp->table);
1314 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1316 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1317 if (unlikely(!flow)) {
1322 ovs_flow_tbl_remove(&dp->table, flow);
1325 reply = ovs_flow_cmd_alloc_info(rcu_dereference_raw(flow->sf_acts),
1326 &flow->id, info, false, ufid_flags);
1328 if (likely(reply)) {
1329 if (likely(!IS_ERR(reply))) {
1330 rcu_read_lock(); /*To keep RCU checker happy. */
1331 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1332 reply, info->snd_portid,
1338 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1340 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1341 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1346 ovs_flow_free(flow, true);
1353 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1355 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1356 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1357 struct table_instance *ti;
1358 struct datapath *dp;
1362 err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
1363 OVS_FLOW_ATTR_MAX, flow_policy);
1366 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1369 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1375 ti = rcu_dereference(dp->table.ti);
1377 struct sw_flow *flow;
1380 bucket = cb->args[0];
1382 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1386 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1387 NETLINK_CB(cb->skb).portid,
1388 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1389 OVS_FLOW_CMD_NEW, ufid_flags) < 0)
1392 cb->args[0] = bucket;
1399 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1400 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1401 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1402 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1403 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1404 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1405 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1406 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1409 static struct genl_ops dp_flow_genl_ops[] = {
1410 { .cmd = OVS_FLOW_CMD_NEW,
1411 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1412 .policy = flow_policy,
1413 .doit = ovs_flow_cmd_new
1415 { .cmd = OVS_FLOW_CMD_DEL,
1416 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1417 .policy = flow_policy,
1418 .doit = ovs_flow_cmd_del
1420 { .cmd = OVS_FLOW_CMD_GET,
1421 .flags = 0, /* OK for unprivileged users. */
1422 .policy = flow_policy,
1423 .doit = ovs_flow_cmd_get,
1424 .dumpit = ovs_flow_cmd_dump
1426 { .cmd = OVS_FLOW_CMD_SET,
1427 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1428 .policy = flow_policy,
1429 .doit = ovs_flow_cmd_set,
1433 static struct genl_family dp_flow_genl_family = {
1434 .id = GENL_ID_GENERATE,
1435 .hdrsize = sizeof(struct ovs_header),
1436 .name = OVS_FLOW_FAMILY,
1437 .version = OVS_FLOW_VERSION,
1438 .maxattr = OVS_FLOW_ATTR_MAX,
1440 .parallel_ops = true,
1441 .ops = dp_flow_genl_ops,
1442 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1443 .mcgrps = &ovs_dp_flow_multicast_group,
1447 static size_t ovs_dp_cmd_msg_size(void)
1449 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1451 msgsize += nla_total_size(IFNAMSIZ);
1452 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1453 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1454 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1459 /* Called with ovs_mutex. */
1460 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1461 u32 portid, u32 seq, u32 flags, u8 cmd)
1463 struct ovs_header *ovs_header;
1464 struct ovs_dp_stats dp_stats;
1465 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1468 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1473 ovs_header->dp_ifindex = get_dpifindex(dp);
1475 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1477 goto nla_put_failure;
1479 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1480 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1482 goto nla_put_failure;
1484 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1485 sizeof(struct ovs_dp_megaflow_stats),
1486 &dp_megaflow_stats))
1487 goto nla_put_failure;
1489 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1490 goto nla_put_failure;
1492 genlmsg_end(skb, ovs_header);
1496 genlmsg_cancel(skb, ovs_header);
1501 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1503 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1506 /* Called with rcu_read_lock or ovs_mutex. */
1507 static struct datapath *lookup_datapath(struct net *net,
1508 const struct ovs_header *ovs_header,
1509 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1511 struct datapath *dp;
1513 if (!a[OVS_DP_ATTR_NAME])
1514 dp = get_dp(net, ovs_header->dp_ifindex);
1516 struct vport *vport;
1518 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1519 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1521 return dp ? dp : ERR_PTR(-ENODEV);
1524 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1526 struct datapath *dp;
1528 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1532 WARN(dp->user_features, "Dropping previously announced user features\n");
1533 dp->user_features = 0;
1536 static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1538 if (a[OVS_DP_ATTR_USER_FEATURES])
1539 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1542 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1544 struct nlattr **a = info->attrs;
1545 struct vport_parms parms;
1546 struct sk_buff *reply;
1547 struct datapath *dp;
1548 struct vport *vport;
1549 struct ovs_net *ovs_net;
1553 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1556 reply = ovs_dp_cmd_alloc_info(info);
1561 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1563 goto err_free_reply;
1565 ovs_dp_set_net(dp, sock_net(skb->sk));
1567 /* Allocate table. */
1568 err = ovs_flow_tbl_init(&dp->table);
1572 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1573 if (!dp->stats_percpu) {
1575 goto err_destroy_table;
1578 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1582 goto err_destroy_percpu;
1585 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1586 INIT_HLIST_HEAD(&dp->ports[i]);
1588 /* Set up our datapath device. */
1589 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1590 parms.type = OVS_VPORT_TYPE_INTERNAL;
1591 parms.options = NULL;
1593 parms.port_no = OVSP_LOCAL;
1594 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1596 ovs_dp_change(dp, a);
1598 /* So far only local changes have been made, now need the lock. */
1601 vport = new_vport(&parms);
1602 if (IS_ERR(vport)) {
1603 err = PTR_ERR(vport);
1607 if (err == -EEXIST) {
1608 /* An outdated user space instance that does not understand
1609 * the concept of user_features has attempted to create a new
1610 * datapath and is likely to reuse it. Drop all user features.
1612 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1613 ovs_dp_reset_user_features(skb, info);
1616 goto err_destroy_ports_array;
1619 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1620 info->snd_seq, 0, OVS_DP_CMD_NEW);
1623 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1624 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1628 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1631 err_destroy_ports_array:
1635 free_percpu(dp->stats_percpu);
1637 ovs_flow_tbl_destroy(&dp->table);
1646 /* Called with ovs_mutex. */
1647 static void __dp_destroy(struct datapath *dp)
1651 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1652 struct vport *vport;
1653 struct hlist_node *n;
1655 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1656 if (vport->port_no != OVSP_LOCAL)
1657 ovs_dp_detach_port(vport);
1660 list_del_rcu(&dp->list_node);
1662 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1663 * all ports in datapath are destroyed first before freeing datapath.
1665 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1667 /* RCU destroy the flow table */
1668 call_rcu(&dp->rcu, destroy_dp_rcu);
1671 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1673 struct sk_buff *reply;
1674 struct datapath *dp;
1677 reply = ovs_dp_cmd_alloc_info(info);
1682 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1685 goto err_unlock_free;
1687 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1688 info->snd_seq, 0, OVS_DP_CMD_DEL);
1694 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1703 static int ovs_dp_cmd_set(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 ovs_dp_change(dp, info->attrs);
1721 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1722 info->snd_seq, 0, OVS_DP_CMD_NEW);
1727 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1736 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1738 struct sk_buff *reply;
1739 struct datapath *dp;
1742 reply = ovs_dp_cmd_alloc_info(info);
1747 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1750 goto err_unlock_free;
1752 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1753 info->snd_seq, 0, OVS_DP_CMD_NEW);
1757 return genlmsg_reply(reply, info);
1765 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1767 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1768 struct datapath *dp;
1769 int skip = cb->args[0];
1773 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1775 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1776 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1777 OVS_DP_CMD_NEW) < 0)
1788 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1789 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1790 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1791 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1794 static struct genl_ops dp_datapath_genl_ops[] = {
1795 { .cmd = OVS_DP_CMD_NEW,
1796 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1797 .policy = datapath_policy,
1798 .doit = ovs_dp_cmd_new
1800 { .cmd = OVS_DP_CMD_DEL,
1801 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1802 .policy = datapath_policy,
1803 .doit = ovs_dp_cmd_del
1805 { .cmd = OVS_DP_CMD_GET,
1806 .flags = 0, /* OK for unprivileged users. */
1807 .policy = datapath_policy,
1808 .doit = ovs_dp_cmd_get,
1809 .dumpit = ovs_dp_cmd_dump
1811 { .cmd = OVS_DP_CMD_SET,
1812 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1813 .policy = datapath_policy,
1814 .doit = ovs_dp_cmd_set,
1818 static struct genl_family dp_datapath_genl_family = {
1819 .id = GENL_ID_GENERATE,
1820 .hdrsize = sizeof(struct ovs_header),
1821 .name = OVS_DATAPATH_FAMILY,
1822 .version = OVS_DATAPATH_VERSION,
1823 .maxattr = OVS_DP_ATTR_MAX,
1825 .parallel_ops = true,
1826 .ops = dp_datapath_genl_ops,
1827 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1828 .mcgrps = &ovs_dp_datapath_multicast_group,
1832 /* Called with ovs_mutex or RCU read lock. */
1833 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1834 u32 portid, u32 seq, u32 flags, u8 cmd)
1836 struct ovs_header *ovs_header;
1837 struct ovs_vport_stats vport_stats;
1840 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1845 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1847 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1848 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1849 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1850 ovs_vport_name(vport)))
1851 goto nla_put_failure;
1853 ovs_vport_get_stats(vport, &vport_stats);
1854 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1856 goto nla_put_failure;
1858 if (ovs_vport_get_upcall_portids(vport, skb))
1859 goto nla_put_failure;
1861 err = ovs_vport_get_options(vport, skb);
1862 if (err == -EMSGSIZE)
1865 genlmsg_end(skb, ovs_header);
1871 genlmsg_cancel(skb, ovs_header);
1875 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1877 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1880 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1881 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1884 struct sk_buff *skb;
1887 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1889 return ERR_PTR(-ENOMEM);
1891 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1897 /* Called with ovs_mutex or RCU read lock. */
1898 static struct vport *lookup_vport(struct net *net,
1899 const struct ovs_header *ovs_header,
1900 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1902 struct datapath *dp;
1903 struct vport *vport;
1905 if (a[OVS_VPORT_ATTR_NAME]) {
1906 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1908 return ERR_PTR(-ENODEV);
1909 if (ovs_header->dp_ifindex &&
1910 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1911 return ERR_PTR(-ENODEV);
1913 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1914 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1916 if (port_no >= DP_MAX_PORTS)
1917 return ERR_PTR(-EFBIG);
1919 dp = get_dp(net, ovs_header->dp_ifindex);
1921 return ERR_PTR(-ENODEV);
1923 vport = ovs_vport_ovsl_rcu(dp, port_no);
1925 return ERR_PTR(-ENODEV);
1928 return ERR_PTR(-EINVAL);
1931 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1933 struct nlattr **a = info->attrs;
1934 struct ovs_header *ovs_header = info->userhdr;
1935 struct vport_parms parms;
1936 struct sk_buff *reply;
1937 struct vport *vport;
1938 struct datapath *dp;
1942 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1943 !a[OVS_VPORT_ATTR_UPCALL_PID])
1946 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1947 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1948 if (port_no >= DP_MAX_PORTS)
1951 reply = ovs_vport_cmd_alloc_info();
1957 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1960 goto exit_unlock_free;
1963 vport = ovs_vport_ovsl(dp, port_no);
1966 goto exit_unlock_free;
1968 for (port_no = 1; ; port_no++) {
1969 if (port_no >= DP_MAX_PORTS) {
1971 goto exit_unlock_free;
1973 vport = ovs_vport_ovsl(dp, port_no);
1979 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1980 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1981 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1983 parms.port_no = port_no;
1984 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1986 vport = new_vport(&parms);
1987 err = PTR_ERR(vport);
1988 if (IS_ERR(vport)) {
1991 goto exit_unlock_free;
1994 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1995 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1999 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2008 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2010 struct nlattr **a = info->attrs;
2011 struct sk_buff *reply;
2012 struct vport *vport;
2015 reply = ovs_vport_cmd_alloc_info();
2020 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2021 err = PTR_ERR(vport);
2023 goto exit_unlock_free;
2025 if (a[OVS_VPORT_ATTR_TYPE] &&
2026 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2028 goto exit_unlock_free;
2031 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2032 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2034 goto exit_unlock_free;
2037 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2038 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2040 err = ovs_vport_set_upcall_portids(vport, ids);
2042 goto exit_unlock_free;
2045 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2046 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2050 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2059 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2061 struct nlattr **a = info->attrs;
2062 struct sk_buff *reply;
2063 struct vport *vport;
2066 reply = ovs_vport_cmd_alloc_info();
2071 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2072 err = PTR_ERR(vport);
2074 goto exit_unlock_free;
2076 if (vport->port_no == OVSP_LOCAL) {
2078 goto exit_unlock_free;
2081 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2082 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
2084 ovs_dp_detach_port(vport);
2087 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2096 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2098 struct nlattr **a = info->attrs;
2099 struct ovs_header *ovs_header = info->userhdr;
2100 struct sk_buff *reply;
2101 struct vport *vport;
2104 reply = ovs_vport_cmd_alloc_info();
2109 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2110 err = PTR_ERR(vport);
2112 goto exit_unlock_free;
2113 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2114 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2118 return genlmsg_reply(reply, info);
2126 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2128 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2129 struct datapath *dp;
2130 int bucket = cb->args[0], skip = cb->args[1];
2134 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2139 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2140 struct vport *vport;
2143 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2145 ovs_vport_cmd_fill_info(vport, skb,
2146 NETLINK_CB(cb->skb).portid,
2149 OVS_VPORT_CMD_NEW) < 0)
2165 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2166 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2167 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2168 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2169 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2170 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2171 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2174 static struct genl_ops dp_vport_genl_ops[] = {
2175 { .cmd = OVS_VPORT_CMD_NEW,
2176 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2177 .policy = vport_policy,
2178 .doit = ovs_vport_cmd_new
2180 { .cmd = OVS_VPORT_CMD_DEL,
2181 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2182 .policy = vport_policy,
2183 .doit = ovs_vport_cmd_del
2185 { .cmd = OVS_VPORT_CMD_GET,
2186 .flags = 0, /* OK for unprivileged users. */
2187 .policy = vport_policy,
2188 .doit = ovs_vport_cmd_get,
2189 .dumpit = ovs_vport_cmd_dump
2191 { .cmd = OVS_VPORT_CMD_SET,
2192 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2193 .policy = vport_policy,
2194 .doit = ovs_vport_cmd_set,
2198 struct genl_family dp_vport_genl_family = {
2199 .id = GENL_ID_GENERATE,
2200 .hdrsize = sizeof(struct ovs_header),
2201 .name = OVS_VPORT_FAMILY,
2202 .version = OVS_VPORT_VERSION,
2203 .maxattr = OVS_VPORT_ATTR_MAX,
2205 .parallel_ops = true,
2206 .ops = dp_vport_genl_ops,
2207 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2208 .mcgrps = &ovs_dp_vport_multicast_group,
2212 static struct genl_family *dp_genl_families[] = {
2213 &dp_datapath_genl_family,
2214 &dp_vport_genl_family,
2215 &dp_flow_genl_family,
2216 &dp_packet_genl_family,
2219 static void dp_unregister_genl(int n_families)
2223 for (i = 0; i < n_families; i++)
2224 genl_unregister_family(dp_genl_families[i]);
2227 static int dp_register_genl(void)
2232 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2234 err = genl_register_family(dp_genl_families[i]);
2242 dp_unregister_genl(i);
2246 static int __net_init ovs_init_net(struct net *net)
2248 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2250 INIT_LIST_HEAD(&ovs_net->dps);
2251 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2256 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2257 struct list_head *head)
2259 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2260 struct datapath *dp;
2262 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2265 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2266 struct vport *vport;
2268 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2270 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2273 if (dev_net(vport->dev) == dnet)
2274 list_add(&vport->detach_list, head);
2280 static void __net_exit ovs_exit_net(struct net *dnet)
2282 struct datapath *dp, *dp_next;
2283 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2284 struct vport *vport, *vport_next;
2290 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2295 list_vports_from_net(net, dnet, &head);
2298 /* Detach all vports from given namespace. */
2299 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2300 list_del(&vport->detach_list);
2301 ovs_dp_detach_port(vport);
2306 cancel_work_sync(&ovs_net->dp_notify_work);
2309 static struct pernet_operations ovs_net_ops = {
2310 .init = ovs_init_net,
2311 .exit = ovs_exit_net,
2313 .size = sizeof(struct ovs_net),
2316 static int __init dp_init(void)
2320 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2322 pr_info("Open vSwitch switching datapath %s\n", VERSION);
2324 err = compat_init();
2328 err = action_fifos_init();
2330 goto error_compat_exit;
2332 err = ovs_internal_dev_rtnl_link_register();
2334 goto error_action_fifos_exit;
2336 err = ovs_flow_init();
2338 goto error_unreg_rtnl_link;
2340 err = ovs_vport_init();
2342 goto error_flow_exit;
2344 err = register_pernet_device(&ovs_net_ops);
2346 goto error_vport_exit;
2348 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2350 goto error_netns_exit;
2352 err = ovs_netdev_init();
2354 goto error_unreg_notifier;
2356 err = dp_register_genl();
2358 goto error_unreg_netdev;
2364 error_unreg_notifier:
2365 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2367 unregister_pernet_device(&ovs_net_ops);
2372 error_unreg_rtnl_link:
2373 ovs_internal_dev_rtnl_link_unregister();
2374 error_action_fifos_exit:
2375 action_fifos_exit();
2382 static void dp_cleanup(void)
2384 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2386 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2387 unregister_pernet_device(&ovs_net_ops);
2391 ovs_internal_dev_rtnl_link_unregister();
2392 action_fifos_exit();
2396 module_init(dp_init);
2397 module_exit(dp_cleanup);
2399 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2400 MODULE_LICENSE("GPL");
2401 MODULE_VERSION(VERSION);