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 <linux/genetlink.h>
52 #include <net/genetlink.h>
53 #include <net/genetlink.h>
54 #include <net/net_namespace.h>
55 #include <net/netns/generic.h>
59 #include "flow_table.h"
60 #include "flow_netlink.h"
62 #include "vport-internal_dev.h"
63 #include "vport-netdev.h"
65 int ovs_net_id __read_mostly;
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 struct genl_multicast_group ovs_dp_flow_multicast_group = {
72 .name = OVS_FLOW_MCGROUP
75 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
76 .name = OVS_DATAPATH_MCGROUP
79 struct genl_multicast_group ovs_dp_vport_multicast_group = {
80 .name = OVS_VPORT_MCGROUP
83 /* Check if need to build a reply message.
84 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
85 static bool ovs_must_notify(struct genl_info *info,
86 const struct genl_multicast_group *grp)
88 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
89 netlink_has_listeners(genl_info_net(info)->genl_sock, GROUP_ID(grp));
92 static void ovs_notify(struct genl_family *family, struct genl_multicast_group *grp,
93 struct sk_buff *skb, struct genl_info *info)
95 genl_notify(family, skb, genl_info_net(info),
96 info->snd_portid, GROUP_ID(grp), info->nlhdr, GFP_KERNEL);
102 * All writes e.g. Writes to device state (add/remove datapath, port, set
103 * operations on vports, etc.), Writes to other state (flow table
104 * modifications, set miscellaneous datapath parameters, etc.) are protected
107 * Reads are protected by RCU.
109 * There are a few special cases (mostly stats) that have their own
110 * synchronization but they nest under all of above and don't interact with
113 * The RTNL lock nests inside ovs_mutex.
116 static DEFINE_MUTEX(ovs_mutex);
120 mutex_lock(&ovs_mutex);
123 void ovs_unlock(void)
125 mutex_unlock(&ovs_mutex);
128 #ifdef CONFIG_LOCKDEP
129 int lockdep_ovsl_is_held(void)
132 return lockdep_is_held(&ovs_mutex);
138 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
139 const struct dp_upcall_info *);
140 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
141 const struct dp_upcall_info *);
143 /* Must be called with rcu_read_lock. */
144 static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
146 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
149 struct vport *vport = ovs_internal_dev_get_vport(dev);
157 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
158 * returned dp pointer valid. */
159 static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
163 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
165 dp = get_dp_rcu(net, dp_ifindex);
171 /* Must be called with rcu_read_lock or ovs_mutex. */
172 const char *ovs_dp_name(const struct datapath *dp)
174 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
175 return vport->ops->get_name(vport);
178 static int get_dpifindex(struct datapath *dp)
185 local = ovs_vport_rcu(dp, OVSP_LOCAL);
187 ifindex = netdev_vport_priv(local)->dev->ifindex;
196 static void destroy_dp_rcu(struct rcu_head *rcu)
198 struct datapath *dp = container_of(rcu, struct datapath, rcu);
200 ovs_flow_tbl_destroy(&dp->table);
201 free_percpu(dp->stats_percpu);
202 release_net(ovs_dp_get_net(dp));
207 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
210 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
213 /* Called with ovs_mutex or RCU read lock. */
214 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
217 struct hlist_head *head;
219 head = vport_hash_bucket(dp, port_no);
220 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
221 if (vport->port_no == port_no)
227 /* Called with ovs_mutex. */
228 static struct vport *new_vport(const struct vport_parms *parms)
232 vport = ovs_vport_add(parms);
233 if (!IS_ERR(vport)) {
234 struct datapath *dp = parms->dp;
235 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
237 hlist_add_head_rcu(&vport->dp_hash_node, head);
242 void ovs_dp_detach_port(struct vport *p)
246 /* First drop references to device. */
247 hlist_del_rcu(&p->dp_hash_node);
249 /* Then destroy it. */
253 /* Must be called with rcu_read_lock. */
254 void ovs_dp_process_packet(struct sk_buff *skb, bool recirc)
256 const struct vport *p = OVS_CB(skb)->input_vport;
257 struct sw_flow_key *pkt_key = OVS_CB(skb)->pkt_key;
258 struct datapath *dp = p->dp;
259 struct sw_flow *flow;
260 struct dp_stats_percpu *stats;
264 stats = this_cpu_ptr(dp->stats_percpu);
267 flow = ovs_flow_tbl_lookup_stats(&dp->table, pkt_key, skb_get_hash(skb),
269 if (unlikely(!flow)) {
270 struct dp_upcall_info upcall;
272 upcall.cmd = OVS_PACKET_CMD_MISS;
273 upcall.userdata = NULL;
274 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
275 ovs_dp_upcall(dp, skb, &upcall);
277 stats_counter = &stats->n_missed;
281 OVS_CB(skb)->flow = flow;
283 ovs_flow_stats_update(OVS_CB(skb)->flow, pkt_key->tp.flags, skb);
284 ovs_execute_actions(dp, skb, recirc);
285 stats_counter = &stats->n_hit;
288 /* Update datapath statistics. */
289 u64_stats_update_begin(&stats->sync);
291 stats->n_mask_hit += n_mask_hit;
292 u64_stats_update_end(&stats->sync);
295 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
296 const struct dp_upcall_info *upcall_info)
298 struct dp_stats_percpu *stats;
301 BUG_ON(!OVS_CB(skb)->pkt_key);
303 if (upcall_info->portid == 0) {
308 if (!skb_is_gso(skb))
309 err = queue_userspace_packet(dp, skb, upcall_info);
311 err = queue_gso_packets(dp, skb, upcall_info);
318 stats = this_cpu_ptr(dp->stats_percpu);
320 u64_stats_update_begin(&stats->sync);
322 u64_stats_update_end(&stats->sync);
327 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
328 const struct dp_upcall_info *upcall_info)
330 unsigned short gso_type = skb_shinfo(skb)->gso_type;
331 struct sw_flow_key later_key;
332 struct sk_buff *segs, *nskb;
335 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
337 return PTR_ERR(segs);
339 if (gso_type & SKB_GSO_UDP) {
340 /* The initial flow key extracted by ovs_flow_key_extract()
341 * in this case is for a first fragment, so we need to
342 * properly mark later fragments.
344 later_key = *OVS_CB(skb)->pkt_key;
345 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
348 /* Queue all of the segments. */
351 if (gso_type & SKB_GSO_UDP && skb != segs)
352 OVS_CB(skb)->pkt_key = &later_key;
354 err = queue_userspace_packet(dp, skb, upcall_info);
358 } while ((skb = skb->next));
360 /* Free all of the segments. */
368 } while ((skb = nskb));
372 static size_t key_attr_size(void)
374 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
375 * updating this function. */
376 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 22);
378 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
379 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
380 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
381 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
382 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
383 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
384 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
385 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
386 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
387 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
388 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
389 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
390 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
391 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
392 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
393 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
394 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
395 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
396 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
397 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
398 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
399 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
400 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
403 static size_t upcall_msg_size(const struct nlattr *userdata,
406 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
407 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
408 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
410 /* OVS_PACKET_ATTR_USERDATA */
412 size += NLA_ALIGN(userdata->nla_len);
417 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
418 const struct dp_upcall_info *upcall_info)
420 struct ovs_header *upcall;
421 struct sk_buff *nskb = NULL;
422 struct sk_buff *user_skb; /* to be queued to userspace */
423 struct sw_flow_key *pkt_key = OVS_CB(skb)->pkt_key;
425 struct genl_info info = {
426 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
427 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
429 .snd_portid = upcall_info->portid,
435 dp_ifindex = get_dpifindex(dp);
439 if (vlan_tx_tag_present(skb)) {
440 nskb = skb_clone(skb, GFP_ATOMIC);
444 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
448 vlan_set_tci(nskb, 0);
453 if (nla_attr_size(skb->len) > USHRT_MAX) {
458 /* Complete checksum if needed */
459 if (skb->ip_summed == CHECKSUM_PARTIAL &&
460 (err = skb_checksum_help(skb)))
463 /* Older versions of OVS user space enforce alignment of the last
464 * Netlink attribute to NLA_ALIGNTO which would require extensive
465 * padding logic. Only perform zerocopy if padding is not required.
467 if (dp->user_features & OVS_DP_F_UNALIGNED)
468 hlen = skb_zerocopy_headlen(skb);
472 len = upcall_msg_size(upcall_info->userdata, hlen);
473 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
479 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
480 0, upcall_info->cmd);
481 upcall->dp_ifindex = dp_ifindex;
483 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
484 err = ovs_nla_put_flow(dp, pkt_key, pkt_key, user_skb);
486 nla_nest_end(user_skb, nla);
488 if (upcall_info->userdata)
489 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
490 nla_len(upcall_info->userdata),
491 nla_data(upcall_info->userdata));
493 /* Only reserve room for attribute header, packet data is added
494 * in skb_zerocopy() */
495 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
499 nla->nla_len = nla_attr_size(skb->len);
501 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
505 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
506 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
507 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
510 memset(skb_put(user_skb, plen), 0, plen);
513 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
515 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
523 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
525 struct ovs_header *ovs_header = info->userhdr;
526 struct nlattr **a = info->attrs;
527 struct sw_flow_actions *acts;
528 struct sk_buff *packet;
529 struct sw_flow *flow;
532 struct vport *input_vport;
537 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
538 !a[OVS_PACKET_ATTR_ACTIONS])
541 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
542 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
546 skb_reserve(packet, NET_IP_ALIGN);
548 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
550 skb_reset_mac_header(packet);
551 eth = eth_hdr(packet);
553 /* Normally, setting the skb 'protocol' field would be handled by a
554 * call to eth_type_trans(), but it assumes there's a sending
555 * device, which we may not have. */
556 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
557 packet->protocol = eth->h_proto;
559 packet->protocol = htons(ETH_P_802_2);
561 /* Build an sw_flow for sending this packet. */
562 flow = ovs_flow_alloc();
567 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
572 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
577 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
579 rcu_assign_pointer(flow->sf_acts, acts);
583 OVS_CB(packet)->flow = flow;
584 OVS_CB(packet)->pkt_key = &flow->key;
585 OVS_CB(skb)->egress_tun_info = NULL;
586 packet->priority = flow->key.phy.priority;
587 packet->mark = flow->key.phy.skb_mark;
590 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
595 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
597 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
602 OVS_CB(packet)->input_vport = input_vport;
605 err = ovs_execute_actions(dp, packet, false);
609 ovs_flow_free(flow, false);
615 ovs_flow_free(flow, false);
622 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
623 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
624 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
625 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
628 static struct genl_ops dp_packet_genl_ops[] = {
629 { .cmd = OVS_PACKET_CMD_EXECUTE,
630 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
631 .policy = packet_policy,
632 .doit = ovs_packet_cmd_execute
636 static struct genl_family dp_packet_genl_family = {
637 .id = GENL_ID_GENERATE,
638 .hdrsize = sizeof(struct ovs_header),
639 .name = OVS_PACKET_FAMILY,
640 .version = OVS_PACKET_VERSION,
641 .maxattr = OVS_PACKET_ATTR_MAX,
643 .parallel_ops = true,
644 .ops = dp_packet_genl_ops,
645 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
648 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
649 struct ovs_dp_megaflow_stats *mega_stats)
653 memset(mega_stats, 0, sizeof(*mega_stats));
655 stats->n_flows = ovs_flow_tbl_count(&dp->table);
656 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
658 stats->n_hit = stats->n_missed = stats->n_lost = 0;
660 for_each_possible_cpu(i) {
661 const struct dp_stats_percpu *percpu_stats;
662 struct dp_stats_percpu local_stats;
665 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
668 start = u64_stats_fetch_begin_irq(&percpu_stats->sync);
669 local_stats = *percpu_stats;
670 } while (u64_stats_fetch_retry_irq(&percpu_stats->sync, start));
672 stats->n_hit += local_stats.n_hit;
673 stats->n_missed += local_stats.n_missed;
674 stats->n_lost += local_stats.n_lost;
675 mega_stats->n_mask_hit += local_stats.n_mask_hit;
679 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
681 return NLMSG_ALIGN(sizeof(struct ovs_header))
682 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
683 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
684 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
685 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
686 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
687 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
690 /* Called with ovs_mutex or RCU read lock. */
691 static int ovs_flow_cmd_fill_match(struct datapath *dp,
692 const struct sw_flow *flow,
699 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
703 err = ovs_nla_put_flow(dp, &flow->unmasked_key,
704 &flow->unmasked_key, skb);
707 nla_nest_end(skb, nla);
709 /* Fill flow mask. */
710 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
714 err = ovs_nla_put_flow(dp, &flow->key, &flow->mask->key, skb);
717 nla_nest_end(skb, nla);
722 /* Called with ovs_mutex or RCU read lock. */
723 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
726 struct ovs_flow_stats stats;
730 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
733 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
736 if (stats.n_packets &&
737 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
740 if ((u8)ntohs(tcp_flags) &&
741 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
747 /* Called with ovs_mutex or RCU read lock. */
748 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
749 struct sk_buff *skb, int skb_orig_len)
751 struct nlattr *start;
754 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
755 * this is the first flow to be dumped into 'skb'. This is unusual for
756 * Netlink but individual action lists can be longer than
757 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
758 * The userspace caller can always fetch the actions separately if it
759 * really wants them. (Most userspace callers in fact don't care.)
761 * This can only fail for dump operations because the skb is always
762 * properly sized for single flows.
764 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
766 const struct sw_flow_actions *sf_acts;
768 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
769 err = ovs_nla_put_actions(sf_acts->actions,
770 sf_acts->actions_len, skb);
773 nla_nest_end(skb, start);
778 nla_nest_cancel(skb, start);
780 } else if (skb_orig_len) {
787 /* Called with ovs_mutex or RCU read lock. */
788 static int ovs_flow_cmd_fill_info(struct datapath *dp,
789 const struct sw_flow *flow, int dp_ifindex,
790 struct sk_buff *skb, u32 portid,
791 u32 seq, u32 flags, u8 cmd)
793 const int skb_orig_len = skb->len;
794 struct ovs_header *ovs_header;
797 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
800 ovs_header->dp_ifindex = dp_ifindex;
802 err = ovs_flow_cmd_fill_match(dp, flow, skb);
806 err = ovs_flow_cmd_fill_stats(flow, skb);
810 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
814 return genlmsg_end(skb, ovs_header);
817 genlmsg_cancel(skb, ovs_header);
821 /* May not be called with RCU read lock. */
822 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
823 struct genl_info *info,
828 if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
831 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
834 return ERR_PTR(-ENOMEM);
839 /* Called with ovs_mutex. */
840 static struct sk_buff *ovs_flow_cmd_build_info(struct datapath *dp,
841 const struct sw_flow *flow,
843 struct genl_info *info, u8 cmd,
849 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
851 if (!skb || IS_ERR(skb))
854 retval = ovs_flow_cmd_fill_info(dp, flow, dp_ifindex, skb,
855 info->snd_portid, info->snd_seq, 0,
861 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
863 struct nlattr **a = info->attrs;
864 struct ovs_header *ovs_header = info->userhdr;
865 struct sw_flow *flow, *new_flow;
866 struct sw_flow_mask mask;
867 struct sk_buff *reply;
869 struct sw_flow_actions *acts;
870 struct sw_flow_match match;
873 /* Must have key and actions. */
875 if (!a[OVS_FLOW_ATTR_KEY]) {
876 OVS_NLERR("Flow key attribute not present in new flow.\n");
879 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
880 OVS_NLERR("Flow actions attribute not present in new flow.\n");
884 /* Most of the time we need to allocate a new flow, do it before
886 new_flow = ovs_flow_alloc();
887 if (IS_ERR(new_flow)) {
888 error = PTR_ERR(new_flow);
893 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
894 error = ovs_nla_get_match(&match,
895 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
899 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
901 /* Validate actions. */
902 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
903 error = PTR_ERR(acts);
907 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
910 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
914 reply = ovs_flow_cmd_alloc_info(acts, info, false);
916 error = PTR_ERR(reply);
921 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
926 /* Check if this is a duplicate flow */
927 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
929 rcu_assign_pointer(new_flow->sf_acts, acts);
931 /* Put flow in bucket. */
932 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
933 if (unlikely(error)) {
938 if (unlikely(reply)) {
939 error = ovs_flow_cmd_fill_info(dp, new_flow,
940 ovs_header->dp_ifindex,
941 reply, info->snd_portid,
948 struct sw_flow_actions *old_acts;
950 /* Bail out if we're not allowed to modify an existing flow.
951 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
952 * because Generic Netlink treats the latter as a dump
953 * request. We also accept NLM_F_EXCL in case that bug ever
956 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
961 /* The unmasked key has to be the same for flow updates. */
962 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
963 /* Look for any overlapping flow. */
964 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
970 /* Update actions. */
971 old_acts = ovsl_dereference(flow->sf_acts);
972 rcu_assign_pointer(flow->sf_acts, acts);
974 if (unlikely(reply)) {
975 error = ovs_flow_cmd_fill_info(dp, flow,
976 ovs_header->dp_ifindex,
977 reply, info->snd_portid,
984 ovs_nla_free_flow_actions(old_acts);
985 ovs_flow_free(new_flow, false);
989 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
998 ovs_flow_free(new_flow, false);
1003 static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
1004 const struct sw_flow_key *key,
1005 const struct sw_flow_mask *mask)
1007 struct sw_flow_actions *acts;
1008 struct sw_flow_key masked_key;
1011 acts = ovs_nla_alloc_flow_actions(nla_len(a));
1015 ovs_flow_mask_key(&masked_key, key, mask);
1016 error = ovs_nla_copy_actions(a, &masked_key, &acts);
1018 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
1020 return ERR_PTR(error);
1026 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1028 struct nlattr **a = info->attrs;
1029 struct ovs_header *ovs_header = info->userhdr;
1030 struct sw_flow_key key;
1031 struct sw_flow *flow;
1032 struct sw_flow_mask mask;
1033 struct sk_buff *reply = NULL;
1034 struct datapath *dp;
1035 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1036 struct sw_flow_match match;
1041 if (!a[OVS_FLOW_ATTR_KEY]) {
1042 OVS_NLERR("Flow key attribute not present in set flow.\n");
1046 ovs_match_init(&match, &key, &mask);
1047 error = ovs_nla_get_match(&match,
1048 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
1052 /* Validate actions. */
1053 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1054 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
1056 error = PTR_ERR(acts);
1061 /* Can allocate before locking if have acts. */
1063 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1064 if (IS_ERR(reply)) {
1065 error = PTR_ERR(reply);
1066 goto err_kfree_acts;
1071 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1072 if (unlikely(!dp)) {
1074 goto err_unlock_ovs;
1076 /* Check that the flow exists. */
1077 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1078 if (unlikely(!flow)) {
1080 goto err_unlock_ovs;
1083 /* Update actions, if present. */
1085 old_acts = ovsl_dereference(flow->sf_acts);
1086 rcu_assign_pointer(flow->sf_acts, acts);
1088 if (unlikely(reply)) {
1089 error = ovs_flow_cmd_fill_info(dp, flow,
1090 ovs_header->dp_ifindex,
1091 reply, info->snd_portid,
1097 /* Could not alloc without acts before locking. */
1098 reply = ovs_flow_cmd_build_info(dp, flow,
1099 ovs_header->dp_ifindex,
1100 info, OVS_FLOW_CMD_NEW, false);
1101 if (unlikely(IS_ERR(reply))) {
1102 error = PTR_ERR(reply);
1103 goto err_unlock_ovs;
1108 if (a[OVS_FLOW_ATTR_CLEAR])
1109 ovs_flow_stats_clear(flow);
1113 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1115 ovs_nla_free_flow_actions(old_acts);
1127 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1129 struct nlattr **a = info->attrs;
1130 struct ovs_header *ovs_header = info->userhdr;
1131 struct sw_flow_key key;
1132 struct sk_buff *reply;
1133 struct sw_flow *flow;
1134 struct datapath *dp;
1135 struct sw_flow_match match;
1138 if (!a[OVS_FLOW_ATTR_KEY]) {
1139 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1143 ovs_match_init(&match, &key, NULL);
1144 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1149 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1155 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1161 reply = ovs_flow_cmd_build_info(dp, flow, ovs_header->dp_ifindex, info,
1162 OVS_FLOW_CMD_NEW, true);
1163 if (IS_ERR(reply)) {
1164 err = PTR_ERR(reply);
1169 return genlmsg_reply(reply, info);
1175 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1177 struct nlattr **a = info->attrs;
1178 struct ovs_header *ovs_header = info->userhdr;
1179 struct sw_flow_key key;
1180 struct sk_buff *reply;
1181 struct sw_flow *flow;
1182 struct datapath *dp;
1183 struct sw_flow_match match;
1186 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1187 ovs_match_init(&match, &key, NULL);
1188 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1194 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1195 if (unlikely(!dp)) {
1199 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1200 err = ovs_flow_tbl_flush(&dp->table);
1203 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1204 if (unlikely(!flow)) {
1209 ovs_flow_tbl_remove(&dp->table, flow);
1212 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *)flow->sf_acts,
1215 if (likely(reply)) {
1216 if (likely(!IS_ERR(reply))) {
1217 rcu_read_lock(); /* Keep RCU checker happy. */
1218 err = ovs_flow_cmd_fill_info(dp, flow,
1219 ovs_header->dp_ifindex,
1220 reply, info->snd_portid,
1225 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1227 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1228 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1233 ovs_flow_free(flow, true);
1240 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1242 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1243 struct table_instance *ti;
1244 struct datapath *dp;
1247 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1253 ti = rcu_dereference(dp->table.ti);
1255 struct sw_flow *flow;
1258 bucket = cb->args[0];
1260 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1264 if (ovs_flow_cmd_fill_info(dp, flow, ovs_header->dp_ifindex, skb,
1265 NETLINK_CB(cb->skb).portid,
1266 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1267 OVS_FLOW_CMD_NEW) < 0)
1270 cb->args[0] = bucket;
1277 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1278 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1279 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1280 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1283 static struct genl_ops dp_flow_genl_ops[] = {
1284 { .cmd = OVS_FLOW_CMD_NEW,
1285 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1286 .policy = flow_policy,
1287 .doit = ovs_flow_cmd_new
1289 { .cmd = OVS_FLOW_CMD_DEL,
1290 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1291 .policy = flow_policy,
1292 .doit = ovs_flow_cmd_del
1294 { .cmd = OVS_FLOW_CMD_GET,
1295 .flags = 0, /* OK for unprivileged users. */
1296 .policy = flow_policy,
1297 .doit = ovs_flow_cmd_get,
1298 .dumpit = ovs_flow_cmd_dump
1300 { .cmd = OVS_FLOW_CMD_SET,
1301 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1302 .policy = flow_policy,
1303 .doit = ovs_flow_cmd_set,
1307 static struct genl_family dp_flow_genl_family = {
1308 .id = GENL_ID_GENERATE,
1309 .hdrsize = sizeof(struct ovs_header),
1310 .name = OVS_FLOW_FAMILY,
1311 .version = OVS_FLOW_VERSION,
1312 .maxattr = OVS_FLOW_ATTR_MAX,
1314 .parallel_ops = true,
1315 .ops = dp_flow_genl_ops,
1316 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1317 .mcgrps = &ovs_dp_flow_multicast_group,
1321 static size_t ovs_dp_cmd_msg_size(void)
1323 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1325 msgsize += nla_total_size(IFNAMSIZ);
1326 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1327 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1328 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1333 /* Called with ovs_mutex or RCU read lock. */
1334 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1335 u32 portid, u32 seq, u32 flags, u8 cmd)
1337 struct ovs_header *ovs_header;
1338 struct ovs_dp_stats dp_stats;
1339 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1342 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1347 ovs_header->dp_ifindex = get_dpifindex(dp);
1349 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1351 goto nla_put_failure;
1353 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1354 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1356 goto nla_put_failure;
1358 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1359 sizeof(struct ovs_dp_megaflow_stats),
1360 &dp_megaflow_stats))
1361 goto nla_put_failure;
1363 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1364 goto nla_put_failure;
1366 return genlmsg_end(skb, ovs_header);
1369 genlmsg_cancel(skb, ovs_header);
1374 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1376 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1379 /* Called with rcu_read_lock or ovs_mutex. */
1380 static struct datapath *lookup_datapath(struct net *net,
1381 struct ovs_header *ovs_header,
1382 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1384 struct datapath *dp;
1386 if (!a[OVS_DP_ATTR_NAME])
1387 dp = get_dp(net, ovs_header->dp_ifindex);
1389 struct vport *vport;
1391 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1392 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1394 return dp ? dp : ERR_PTR(-ENODEV);
1397 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1399 struct datapath *dp;
1401 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1405 WARN(dp->user_features, "Dropping previously announced user features\n");
1406 dp->user_features = 0;
1409 static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1411 if (a[OVS_DP_ATTR_USER_FEATURES])
1412 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1415 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1417 struct nlattr **a = info->attrs;
1418 struct vport_parms parms;
1419 struct sk_buff *reply;
1420 struct datapath *dp;
1421 struct vport *vport;
1422 struct ovs_net *ovs_net;
1426 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1429 reply = ovs_dp_cmd_alloc_info(info);
1434 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1436 goto err_free_reply;
1438 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1440 /* Allocate table. */
1441 err = ovs_flow_tbl_init(&dp->table);
1445 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1446 if (!dp->stats_percpu) {
1448 goto err_destroy_table;
1451 for_each_possible_cpu(i) {
1452 struct dp_stats_percpu *dpath_stats;
1453 dpath_stats = per_cpu_ptr(dp->stats_percpu, i);
1454 u64_stats_init(&dpath_stats->sync);
1457 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1461 goto err_destroy_percpu;
1464 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1465 INIT_HLIST_HEAD(&dp->ports[i]);
1467 /* Set up our datapath device. */
1468 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1469 parms.type = OVS_VPORT_TYPE_INTERNAL;
1470 parms.options = NULL;
1472 parms.port_no = OVSP_LOCAL;
1473 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1475 ovs_dp_change(dp, a);
1477 /* So far only local changes have been made, now need the lock. */
1480 vport = new_vport(&parms);
1481 if (IS_ERR(vport)) {
1482 err = PTR_ERR(vport);
1486 if (err == -EEXIST) {
1487 /* An outdated user space instance that does not understand
1488 * the concept of user_features has attempted to create a new
1489 * datapath and is likely to reuse it. Drop all user features.
1491 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1492 ovs_dp_reset_user_features(skb, info);
1495 goto err_destroy_ports_array;
1498 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1499 info->snd_seq, 0, OVS_DP_CMD_NEW);
1502 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1503 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1507 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1510 err_destroy_ports_array:
1514 free_percpu(dp->stats_percpu);
1516 ovs_flow_tbl_destroy(&dp->table);
1518 release_net(ovs_dp_get_net(dp));
1526 /* Called with ovs_mutex. */
1527 static void __dp_destroy(struct datapath *dp)
1531 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1532 struct vport *vport;
1533 struct hlist_node *n;
1535 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1536 if (vport->port_no != OVSP_LOCAL)
1537 ovs_dp_detach_port(vport);
1540 list_del_rcu(&dp->list_node);
1542 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1543 * all ports in datapath are destroyed first before freeing datapath.
1545 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1547 /* RCU destroy the flow table */
1548 call_rcu(&dp->rcu, destroy_dp_rcu);
1551 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1553 struct sk_buff *reply;
1554 struct datapath *dp;
1557 reply = ovs_dp_cmd_alloc_info(info);
1562 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1565 goto err_unlock_free;
1567 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1568 info->snd_seq, 0, OVS_DP_CMD_DEL);
1574 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1583 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1585 struct sk_buff *reply;
1586 struct datapath *dp;
1589 reply = ovs_dp_cmd_alloc_info(info);
1594 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1597 goto err_unlock_free;
1599 ovs_dp_change(dp, info->attrs);
1601 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1602 info->snd_seq, 0, OVS_DP_CMD_NEW);
1606 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1615 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1617 struct sk_buff *reply;
1618 struct datapath *dp;
1621 reply = ovs_dp_cmd_alloc_info(info);
1626 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1629 goto err_unlock_free;
1631 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1632 info->snd_seq, 0, OVS_DP_CMD_NEW);
1636 return genlmsg_reply(reply, info);
1644 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1646 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1647 struct datapath *dp;
1648 int skip = cb->args[0];
1652 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1654 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1655 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1656 OVS_DP_CMD_NEW) < 0)
1667 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1668 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1669 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1670 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1673 static struct genl_ops dp_datapath_genl_ops[] = {
1674 { .cmd = OVS_DP_CMD_NEW,
1675 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1676 .policy = datapath_policy,
1677 .doit = ovs_dp_cmd_new
1679 { .cmd = OVS_DP_CMD_DEL,
1680 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1681 .policy = datapath_policy,
1682 .doit = ovs_dp_cmd_del
1684 { .cmd = OVS_DP_CMD_GET,
1685 .flags = 0, /* OK for unprivileged users. */
1686 .policy = datapath_policy,
1687 .doit = ovs_dp_cmd_get,
1688 .dumpit = ovs_dp_cmd_dump
1690 { .cmd = OVS_DP_CMD_SET,
1691 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1692 .policy = datapath_policy,
1693 .doit = ovs_dp_cmd_set,
1697 static struct genl_family dp_datapath_genl_family = {
1698 .id = GENL_ID_GENERATE,
1699 .hdrsize = sizeof(struct ovs_header),
1700 .name = OVS_DATAPATH_FAMILY,
1701 .version = OVS_DATAPATH_VERSION,
1702 .maxattr = OVS_DP_ATTR_MAX,
1704 .parallel_ops = true,
1705 .ops = dp_datapath_genl_ops,
1706 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1707 .mcgrps = &ovs_dp_datapath_multicast_group,
1711 /* Called with ovs_mutex or RCU read lock. */
1712 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1713 u32 portid, u32 seq, u32 flags, u8 cmd)
1715 struct ovs_header *ovs_header;
1716 struct ovs_vport_stats vport_stats;
1719 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1724 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1726 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1727 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1728 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)))
1729 goto nla_put_failure;
1731 ovs_vport_get_stats(vport, &vport_stats);
1732 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1734 goto nla_put_failure;
1736 if (ovs_vport_get_upcall_portids(vport, skb))
1737 goto nla_put_failure;
1739 err = ovs_vport_get_options(vport, skb);
1740 if (err == -EMSGSIZE)
1743 return genlmsg_end(skb, ovs_header);
1748 genlmsg_cancel(skb, ovs_header);
1752 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1754 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1757 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1758 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1761 struct sk_buff *skb;
1764 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1766 return ERR_PTR(-ENOMEM);
1768 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1774 /* Called with ovs_mutex or RCU read lock. */
1775 static struct vport *lookup_vport(struct net *net,
1776 struct ovs_header *ovs_header,
1777 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1779 struct datapath *dp;
1780 struct vport *vport;
1782 if (a[OVS_VPORT_ATTR_NAME]) {
1783 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1785 return ERR_PTR(-ENODEV);
1786 if (ovs_header->dp_ifindex &&
1787 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1788 return ERR_PTR(-ENODEV);
1790 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1791 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1793 if (port_no >= DP_MAX_PORTS)
1794 return ERR_PTR(-EFBIG);
1796 dp = get_dp(net, ovs_header->dp_ifindex);
1798 return ERR_PTR(-ENODEV);
1800 vport = ovs_vport_ovsl_rcu(dp, port_no);
1802 return ERR_PTR(-ENODEV);
1805 return ERR_PTR(-EINVAL);
1808 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1810 struct nlattr **a = info->attrs;
1811 struct ovs_header *ovs_header = info->userhdr;
1812 struct vport_parms parms;
1813 struct sk_buff *reply;
1814 struct vport *vport;
1815 struct datapath *dp;
1819 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1820 !a[OVS_VPORT_ATTR_UPCALL_PID])
1823 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1824 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1825 if (port_no >= DP_MAX_PORTS)
1828 reply = ovs_vport_cmd_alloc_info();
1833 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1836 goto exit_unlock_free;
1839 vport = ovs_vport_ovsl(dp, port_no);
1842 goto exit_unlock_free;
1844 for (port_no = 1; ; port_no++) {
1845 if (port_no >= DP_MAX_PORTS) {
1847 goto exit_unlock_free;
1849 vport = ovs_vport_ovsl(dp, port_no);
1855 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1856 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1857 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1859 parms.port_no = port_no;
1860 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1862 vport = new_vport(&parms);
1863 err = PTR_ERR(vport);
1865 goto exit_unlock_free;
1868 if (a[OVS_VPORT_ATTR_STATS])
1869 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1871 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1872 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1876 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1885 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1887 struct nlattr **a = info->attrs;
1888 struct sk_buff *reply;
1889 struct vport *vport;
1892 reply = ovs_vport_cmd_alloc_info();
1897 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1898 err = PTR_ERR(vport);
1900 goto exit_unlock_free;
1902 if (a[OVS_VPORT_ATTR_TYPE] &&
1903 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1905 goto exit_unlock_free;
1908 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1909 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1911 goto exit_unlock_free;
1914 if (a[OVS_VPORT_ATTR_STATS])
1915 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1918 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1919 err = ovs_vport_set_upcall_portids(vport,
1920 a[OVS_VPORT_ATTR_UPCALL_PID]);
1922 goto exit_unlock_free;
1925 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1926 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1930 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1939 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1941 struct nlattr **a = info->attrs;
1942 struct sk_buff *reply;
1943 struct vport *vport;
1946 reply = ovs_vport_cmd_alloc_info();
1951 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1952 err = PTR_ERR(vport);
1954 goto exit_unlock_free;
1956 if (vport->port_no == OVSP_LOCAL) {
1958 goto exit_unlock_free;
1961 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1962 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1964 ovs_dp_detach_port(vport);
1967 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1976 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1978 struct nlattr **a = info->attrs;
1979 struct ovs_header *ovs_header = info->userhdr;
1980 struct sk_buff *reply;
1981 struct vport *vport;
1984 reply = ovs_vport_cmd_alloc_info();
1989 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1990 err = PTR_ERR(vport);
1992 goto exit_unlock_free;
1993 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1994 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1998 return genlmsg_reply(reply, info);
2006 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2008 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2009 struct datapath *dp;
2010 int bucket = cb->args[0], skip = cb->args[1];
2014 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2019 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2020 struct vport *vport;
2023 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2025 ovs_vport_cmd_fill_info(vport, skb,
2026 NETLINK_CB(cb->skb).portid,
2029 OVS_VPORT_CMD_NEW) < 0)
2045 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2046 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2047 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2048 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2049 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2050 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2051 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2054 static struct genl_ops dp_vport_genl_ops[] = {
2055 { .cmd = OVS_VPORT_CMD_NEW,
2056 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2057 .policy = vport_policy,
2058 .doit = ovs_vport_cmd_new
2060 { .cmd = OVS_VPORT_CMD_DEL,
2061 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2062 .policy = vport_policy,
2063 .doit = ovs_vport_cmd_del
2065 { .cmd = OVS_VPORT_CMD_GET,
2066 .flags = 0, /* OK for unprivileged users. */
2067 .policy = vport_policy,
2068 .doit = ovs_vport_cmd_get,
2069 .dumpit = ovs_vport_cmd_dump
2071 { .cmd = OVS_VPORT_CMD_SET,
2072 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2073 .policy = vport_policy,
2074 .doit = ovs_vport_cmd_set,
2078 struct genl_family dp_vport_genl_family = {
2079 .id = GENL_ID_GENERATE,
2080 .hdrsize = sizeof(struct ovs_header),
2081 .name = OVS_VPORT_FAMILY,
2082 .version = OVS_VPORT_VERSION,
2083 .maxattr = OVS_VPORT_ATTR_MAX,
2085 .parallel_ops = true,
2086 .ops = dp_vport_genl_ops,
2087 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2088 .mcgrps = &ovs_dp_vport_multicast_group,
2092 static struct genl_family *dp_genl_families[] = {
2093 &dp_datapath_genl_family,
2094 &dp_vport_genl_family,
2095 &dp_flow_genl_family,
2096 &dp_packet_genl_family,
2099 static void dp_unregister_genl(int n_families)
2103 for (i = 0; i < n_families; i++)
2104 genl_unregister_family(dp_genl_families[i]);
2107 static int dp_register_genl(void)
2112 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2114 err = genl_register_family(dp_genl_families[i]);
2122 dp_unregister_genl(i);
2126 static int __net_init ovs_init_net(struct net *net)
2128 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2130 INIT_LIST_HEAD(&ovs_net->dps);
2131 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2135 static void __net_exit ovs_exit_net(struct net *net)
2137 struct datapath *dp, *dp_next;
2138 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2141 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2145 cancel_work_sync(&ovs_net->dp_notify_work);
2148 static struct pernet_operations ovs_net_ops = {
2149 .init = ovs_init_net,
2150 .exit = ovs_exit_net,
2152 .size = sizeof(struct ovs_net),
2155 DEFINE_COMPAT_PNET_REG_FUNC(device);
2157 static int __init dp_init(void)
2161 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2163 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2166 err = ovs_flow_init();
2170 err = ovs_vport_init();
2172 goto error_flow_exit;
2174 err = register_pernet_device(&ovs_net_ops);
2176 goto error_vport_exit;
2178 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2180 goto error_netns_exit;
2182 err = dp_register_genl();
2184 goto error_unreg_notifier;
2188 error_unreg_notifier:
2189 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2191 unregister_pernet_device(&ovs_net_ops);
2200 static void dp_cleanup(void)
2202 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2203 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2204 unregister_pernet_device(&ovs_net_ops);
2210 module_init(dp_init);
2211 module_exit(dp_cleanup);
2213 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2214 MODULE_LICENSE("GPL");
2215 MODULE_VERSION(VERSION);