2 * Copyright (c) 2007-2013 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 struct vport *new_vport(const struct vport_parms *);
139 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
140 const struct dp_upcall_info *);
141 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
142 const struct dp_upcall_info *);
144 /* Must be called with rcu_read_lock or ovs_mutex. */
145 static struct datapath *get_dp(struct net *net, int dp_ifindex)
147 struct datapath *dp = NULL;
148 struct net_device *dev;
151 dev = dev_get_by_index_rcu(net, dp_ifindex);
153 struct vport *vport = ovs_internal_dev_get_vport(dev);
162 /* Must be called with rcu_read_lock or ovs_mutex. */
163 const char *ovs_dp_name(const struct datapath *dp)
165 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
166 return vport->ops->get_name(vport);
169 static int get_dpifindex(struct datapath *dp)
176 local = ovs_vport_rcu(dp, OVSP_LOCAL);
178 ifindex = netdev_vport_priv(local)->dev->ifindex;
187 static void destroy_dp_rcu(struct rcu_head *rcu)
189 struct datapath *dp = container_of(rcu, struct datapath, rcu);
191 free_percpu(dp->stats_percpu);
192 release_net(ovs_dp_get_net(dp));
197 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
200 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
203 /* Called with ovs_mutex or RCU read lock. */
204 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
207 struct hlist_head *head;
209 head = vport_hash_bucket(dp, port_no);
210 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
211 if (vport->port_no == port_no)
217 /* Called with ovs_mutex. */
218 static struct vport *new_vport(const struct vport_parms *parms)
222 vport = ovs_vport_add(parms);
223 if (!IS_ERR(vport)) {
224 struct datapath *dp = parms->dp;
225 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
227 hlist_add_head_rcu(&vport->dp_hash_node, head);
232 void ovs_dp_detach_port(struct vport *p)
236 /* First drop references to device. */
237 hlist_del_rcu(&p->dp_hash_node);
239 /* Then destroy it. */
243 /* Must be called with rcu_read_lock. */
244 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
246 struct datapath *dp = p->dp;
247 struct sw_flow *flow;
248 struct dp_stats_percpu *stats;
249 struct sw_flow_key key;
254 stats = this_cpu_ptr(dp->stats_percpu);
256 /* Extract flow from 'skb' into 'key'. */
257 error = ovs_flow_extract(skb, p->port_no, &key);
258 if (unlikely(error)) {
264 flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit);
265 if (unlikely(!flow)) {
266 struct dp_upcall_info upcall;
268 upcall.cmd = OVS_PACKET_CMD_MISS;
270 upcall.userdata = NULL;
271 upcall.portid = p->upcall_portid;
272 ovs_dp_upcall(dp, skb, &upcall);
274 stats_counter = &stats->n_missed;
278 OVS_CB(skb)->flow = flow;
279 OVS_CB(skb)->pkt_key = &key;
281 ovs_flow_stats_update(OVS_CB(skb)->flow, skb);
282 ovs_execute_actions(dp, skb);
283 stats_counter = &stats->n_hit;
286 /* Update datapath statistics. */
287 u64_stats_update_begin(&stats->sync);
289 stats->n_mask_hit += n_mask_hit;
290 u64_stats_update_end(&stats->sync);
293 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
294 const struct dp_upcall_info *upcall_info)
296 struct dp_stats_percpu *stats;
299 if (upcall_info->portid == 0) {
304 if (!skb_is_gso(skb))
305 err = queue_userspace_packet(dp, skb, upcall_info);
307 err = queue_gso_packets(dp, skb, upcall_info);
314 stats = this_cpu_ptr(dp->stats_percpu);
316 u64_stats_update_begin(&stats->sync);
318 u64_stats_update_end(&stats->sync);
323 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
324 const struct dp_upcall_info *upcall_info)
326 unsigned short gso_type = skb_shinfo(skb)->gso_type;
327 struct dp_upcall_info later_info;
328 struct sw_flow_key later_key;
329 struct sk_buff *segs, *nskb;
332 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
334 return PTR_ERR(segs);
336 /* Queue all of the segments. */
339 err = queue_userspace_packet(dp, skb, upcall_info);
343 if (skb == segs && gso_type & SKB_GSO_UDP) {
344 /* The initial flow key extracted by ovs_flow_extract()
345 * in this case is for a first fragment, so we need to
346 * properly mark later fragments.
348 later_key = *upcall_info->key;
349 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
351 later_info = *upcall_info;
352 later_info.key = &later_key;
353 upcall_info = &later_info;
355 } while ((skb = skb->next));
357 /* Free all of the segments. */
365 } while ((skb = nskb));
369 static size_t key_attr_size(void)
371 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
372 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
373 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
374 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
375 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
376 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
377 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
378 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
379 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
380 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
381 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
382 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
383 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
384 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
385 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
386 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
387 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
388 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
389 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
392 static size_t upcall_msg_size(const struct nlattr *userdata,
395 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
396 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
397 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
399 /* OVS_PACKET_ATTR_USERDATA */
401 size += NLA_ALIGN(userdata->nla_len);
406 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
407 const struct dp_upcall_info *upcall_info)
409 struct ovs_header *upcall;
410 struct sk_buff *nskb = NULL;
411 struct sk_buff *user_skb; /* to be queued to userspace */
413 struct genl_info info = {
414 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
415 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
417 .snd_portid = upcall_info->portid,
423 dp_ifindex = get_dpifindex(dp);
427 if (vlan_tx_tag_present(skb)) {
428 nskb = skb_clone(skb, GFP_ATOMIC);
432 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
436 vlan_set_tci(nskb, 0);
441 if (nla_attr_size(skb->len) > USHRT_MAX) {
446 /* Complete checksum if needed */
447 if (skb->ip_summed == CHECKSUM_PARTIAL &&
448 (err = skb_checksum_help(skb)))
451 /* Older versions of OVS user space enforce alignment of the last
452 * Netlink attribute to NLA_ALIGNTO which would require extensive
453 * padding logic. Only perform zerocopy if padding is not required.
455 if (dp->user_features & OVS_DP_F_UNALIGNED)
456 hlen = skb_zerocopy_headlen(skb);
460 len = upcall_msg_size(upcall_info->userdata, hlen);
461 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
467 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
468 0, upcall_info->cmd);
469 upcall->dp_ifindex = dp_ifindex;
471 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
472 ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
473 nla_nest_end(user_skb, nla);
475 if (upcall_info->userdata)
476 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
477 nla_len(upcall_info->userdata),
478 nla_data(upcall_info->userdata));
480 /* Only reserve room for attribute header, packet data is added
481 * in skb_zerocopy() */
482 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
486 nla->nla_len = nla_attr_size(skb->len);
488 skb_zerocopy(user_skb, skb, skb->len, hlen);
490 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
491 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
492 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
495 memset(skb_put(user_skb, plen), 0, plen);
498 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
500 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
506 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
508 struct ovs_header *ovs_header = info->userhdr;
509 struct nlattr **a = info->attrs;
510 struct sw_flow_actions *acts;
511 struct sk_buff *packet;
512 struct sw_flow *flow;
519 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
520 !a[OVS_PACKET_ATTR_ACTIONS])
523 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
524 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
528 skb_reserve(packet, NET_IP_ALIGN);
530 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
532 skb_reset_mac_header(packet);
533 eth = eth_hdr(packet);
535 /* Normally, setting the skb 'protocol' field would be handled by a
536 * call to eth_type_trans(), but it assumes there's a sending
537 * device, which we may not have. */
538 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
539 packet->protocol = eth->h_proto;
541 packet->protocol = htons(ETH_P_802_2);
543 /* Build an sw_flow for sending this packet. */
544 flow = ovs_flow_alloc();
549 err = ovs_flow_extract(packet, -1, &flow->key);
553 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
556 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
561 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
562 &flow->key, 0, &acts);
563 rcu_assign_pointer(flow->sf_acts, acts);
567 OVS_CB(packet)->flow = flow;
568 OVS_CB(packet)->pkt_key = &flow->key;
569 packet->priority = flow->key.phy.priority;
570 packet->mark = flow->key.phy.skb_mark;
573 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
579 err = ovs_execute_actions(dp, packet);
583 ovs_flow_free(flow, false);
589 ovs_flow_free(flow, false);
596 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
597 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
598 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
599 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
602 static struct genl_ops dp_packet_genl_ops[] = {
603 { .cmd = OVS_PACKET_CMD_EXECUTE,
604 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
605 .policy = packet_policy,
606 .doit = ovs_packet_cmd_execute
610 static struct genl_family dp_packet_genl_family = {
611 .id = GENL_ID_GENERATE,
612 .hdrsize = sizeof(struct ovs_header),
613 .name = OVS_PACKET_FAMILY,
614 .version = OVS_PACKET_VERSION,
615 .maxattr = OVS_PACKET_ATTR_MAX,
617 .parallel_ops = true,
618 .ops = dp_packet_genl_ops,
619 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
622 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
623 struct ovs_dp_megaflow_stats *mega_stats)
627 memset(mega_stats, 0, sizeof(*mega_stats));
629 stats->n_flows = ovs_flow_tbl_count(&dp->table);
630 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
632 stats->n_hit = stats->n_missed = stats->n_lost = 0;
634 for_each_possible_cpu(i) {
635 const struct dp_stats_percpu *percpu_stats;
636 struct dp_stats_percpu local_stats;
639 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
642 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
643 local_stats = *percpu_stats;
644 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
646 stats->n_hit += local_stats.n_hit;
647 stats->n_missed += local_stats.n_missed;
648 stats->n_lost += local_stats.n_lost;
649 mega_stats->n_mask_hit += local_stats.n_mask_hit;
653 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
655 return NLMSG_ALIGN(sizeof(struct ovs_header))
656 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
657 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
658 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
659 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
660 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
661 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
664 /* Called with ovs_mutex or RCU read lock. */
665 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
666 struct sk_buff *skb, u32 portid,
667 u32 seq, u32 flags, u8 cmd)
669 const int skb_orig_len = skb->len;
670 struct nlattr *start;
671 struct ovs_flow_stats stats;
674 struct ovs_header *ovs_header;
678 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
682 ovs_header->dp_ifindex = dp_ifindex;
685 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
687 goto nla_put_failure;
689 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
692 nla_nest_end(skb, nla);
694 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
696 goto nla_put_failure;
698 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
702 nla_nest_end(skb, nla);
704 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
707 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
708 goto nla_put_failure;
710 if (stats.n_packets &&
711 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
712 goto nla_put_failure;
714 if ((u8)ntohs(tcp_flags) &&
715 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
716 goto nla_put_failure;
718 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
719 * this is the first flow to be dumped into 'skb'. This is unusual for
720 * Netlink but individual action lists can be longer than
721 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
722 * The userspace caller can always fetch the actions separately if it
723 * really wants them. (Most userspace callers in fact don't care.)
725 * This can only fail for dump operations because the skb is always
726 * properly sized for single flows.
728 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
730 const struct sw_flow_actions *sf_acts;
732 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
733 err = ovs_nla_put_actions(sf_acts->actions,
734 sf_acts->actions_len, skb);
737 nla_nest_end(skb, start);
742 nla_nest_cancel(skb, start);
744 } else if (skb_orig_len)
745 goto nla_put_failure;
747 return genlmsg_end(skb, ovs_header);
752 genlmsg_cancel(skb, ovs_header);
756 /* May not be called with RCU read lock. */
757 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
758 struct genl_info *info,
763 if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
766 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
769 return ERR_PTR(-ENOMEM);
774 /* Called with ovs_mutex. */
775 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
777 struct genl_info *info, u8 cmd,
783 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
785 if (!skb || IS_ERR(skb))
788 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
789 info->snd_portid, info->snd_seq, 0,
795 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
797 struct nlattr **a = info->attrs;
798 struct ovs_header *ovs_header = info->userhdr;
799 struct sw_flow *flow, *new_flow;
800 struct sw_flow_mask mask;
801 struct sk_buff *reply;
803 struct sw_flow_actions *acts;
804 struct sw_flow_match match;
807 /* Must have key and actions. */
809 if (!a[OVS_FLOW_ATTR_KEY])
811 if (!a[OVS_FLOW_ATTR_ACTIONS])
814 /* Most of the time we need to allocate a new flow, do it before
816 new_flow = ovs_flow_alloc();
817 if (IS_ERR(new_flow)) {
818 error = PTR_ERR(new_flow);
823 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
824 error = ovs_nla_get_match(&match,
825 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
829 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
831 /* Validate actions. */
832 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
833 error = PTR_ERR(acts);
837 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
840 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
844 reply = ovs_flow_cmd_alloc_info(acts, info, false);
846 error = PTR_ERR(reply);
851 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
856 /* Check if this is a duplicate flow */
857 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
859 rcu_assign_pointer(new_flow->sf_acts, acts);
861 /* Put flow in bucket. */
862 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
863 if (unlikely(error)) {
868 if (unlikely(reply)) {
869 error = ovs_flow_cmd_fill_info(new_flow,
870 ovs_header->dp_ifindex,
871 reply, info->snd_portid,
878 struct sw_flow_actions *old_acts;
880 /* Bail out if we're not allowed to modify an existing flow.
881 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
882 * because Generic Netlink treats the latter as a dump
883 * request. We also accept NLM_F_EXCL in case that bug ever
886 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
891 /* The unmasked key has to be the same for flow updates. */
892 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
896 /* Update actions. */
897 old_acts = ovsl_dereference(flow->sf_acts);
898 rcu_assign_pointer(flow->sf_acts, acts);
900 if (unlikely(reply)) {
901 error = ovs_flow_cmd_fill_info(flow,
902 ovs_header->dp_ifindex,
903 reply, info->snd_portid,
910 ovs_nla_free_flow_actions(old_acts);
911 ovs_flow_free(new_flow, false);
915 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
924 ovs_flow_free(new_flow, false);
929 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
931 struct nlattr **a = info->attrs;
932 struct ovs_header *ovs_header = info->userhdr;
933 struct sw_flow_key key, masked_key;
934 struct sw_flow *flow;
935 struct sw_flow_mask mask;
936 struct sk_buff *reply = NULL;
938 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
939 struct sw_flow_match match;
944 if (!a[OVS_FLOW_ATTR_KEY])
947 ovs_match_init(&match, &key, &mask);
948 error = ovs_nla_get_match(&match,
949 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
953 /* Validate actions. */
954 if (a[OVS_FLOW_ATTR_ACTIONS]) {
955 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
956 error = PTR_ERR(acts);
960 ovs_flow_mask_key(&masked_key, &key, &mask);
961 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
962 &masked_key, 0, &acts);
964 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
969 /* Can allocate before locking if have acts. */
971 reply = ovs_flow_cmd_alloc_info(acts, info, false);
973 error = PTR_ERR(reply);
979 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
984 /* Check that the flow exists. */
985 flow = ovs_flow_tbl_lookup(&dp->table, &key);
986 if (unlikely(!flow)) {
990 /* The unmasked key has to be the same for flow updates. */
991 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
995 /* Update actions, if present. */
997 old_acts = ovsl_dereference(flow->sf_acts);
998 rcu_assign_pointer(flow->sf_acts, acts);
1000 if (unlikely(reply)) {
1001 error = ovs_flow_cmd_fill_info(flow,
1002 ovs_header->dp_ifindex,
1003 reply, info->snd_portid,
1009 /* Could not alloc without acts before locking. */
1010 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1011 info, OVS_FLOW_CMD_NEW, false);
1012 if (unlikely(IS_ERR(reply))) {
1013 error = PTR_ERR(reply);
1014 goto err_unlock_ovs;
1019 if (a[OVS_FLOW_ATTR_CLEAR])
1020 ovs_flow_stats_clear(flow);
1024 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1026 ovs_nla_free_flow_actions(old_acts);
1038 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1040 struct nlattr **a = info->attrs;
1041 struct ovs_header *ovs_header = info->userhdr;
1042 struct sw_flow_key key;
1043 struct sk_buff *reply;
1044 struct sw_flow *flow;
1045 struct datapath *dp;
1046 struct sw_flow_match match;
1049 if (!a[OVS_FLOW_ATTR_KEY]) {
1050 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1054 ovs_match_init(&match, &key, NULL);
1055 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1060 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1066 flow = ovs_flow_tbl_lookup(&dp->table, &key);
1067 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
1072 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1073 OVS_FLOW_CMD_NEW, true);
1074 if (IS_ERR(reply)) {
1075 err = PTR_ERR(reply);
1080 return genlmsg_reply(reply, info);
1086 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1088 struct nlattr **a = info->attrs;
1089 struct ovs_header *ovs_header = info->userhdr;
1090 struct sw_flow_key key;
1091 struct sk_buff *reply;
1092 struct sw_flow *flow;
1093 struct datapath *dp;
1094 struct sw_flow_match match;
1097 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1098 ovs_match_init(&match, &key, NULL);
1099 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1105 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1106 if (unlikely(!dp)) {
1110 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1111 err = ovs_flow_tbl_flush(&dp->table);
1114 flow = ovs_flow_tbl_lookup(&dp->table, &key);
1115 if (unlikely(!flow || !ovs_flow_cmp_unmasked_key(flow, &match))) {
1120 ovs_flow_tbl_remove(&dp->table, flow);
1123 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *)flow->sf_acts,
1126 if (likely(reply)) {
1127 if (likely(!IS_ERR(reply))) {
1128 rcu_read_lock(); /* Keep RCU checker happy. */
1129 err = ovs_flow_cmd_fill_info(flow,
1130 ovs_header->dp_ifindex,
1131 reply, info->snd_portid,
1136 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1138 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1139 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1144 ovs_flow_free(flow, true);
1151 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1153 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1154 struct table_instance *ti;
1155 struct datapath *dp;
1158 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1164 ti = rcu_dereference(dp->table.ti);
1166 struct sw_flow *flow;
1169 bucket = cb->args[0];
1171 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1175 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1176 NETLINK_CB(cb->skb).portid,
1177 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1178 OVS_FLOW_CMD_NEW) < 0)
1181 cb->args[0] = bucket;
1188 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1189 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1190 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1191 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1194 static struct genl_ops dp_flow_genl_ops[] = {
1195 { .cmd = OVS_FLOW_CMD_NEW,
1196 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1197 .policy = flow_policy,
1198 .doit = ovs_flow_cmd_new
1200 { .cmd = OVS_FLOW_CMD_DEL,
1201 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1202 .policy = flow_policy,
1203 .doit = ovs_flow_cmd_del
1205 { .cmd = OVS_FLOW_CMD_GET,
1206 .flags = 0, /* OK for unprivileged users. */
1207 .policy = flow_policy,
1208 .doit = ovs_flow_cmd_get,
1209 .dumpit = ovs_flow_cmd_dump
1211 { .cmd = OVS_FLOW_CMD_SET,
1212 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1213 .policy = flow_policy,
1214 .doit = ovs_flow_cmd_set,
1218 static struct genl_family dp_flow_genl_family = {
1219 .id = GENL_ID_GENERATE,
1220 .hdrsize = sizeof(struct ovs_header),
1221 .name = OVS_FLOW_FAMILY,
1222 .version = OVS_FLOW_VERSION,
1223 .maxattr = OVS_FLOW_ATTR_MAX,
1225 .parallel_ops = true,
1226 .ops = dp_flow_genl_ops,
1227 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1228 .mcgrps = &ovs_dp_flow_multicast_group,
1232 static size_t ovs_dp_cmd_msg_size(void)
1234 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1236 msgsize += nla_total_size(IFNAMSIZ);
1237 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1238 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1239 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1244 /* Called with ovs_mutex or RCU read lock. */
1245 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1246 u32 portid, u32 seq, u32 flags, u8 cmd)
1248 struct ovs_header *ovs_header;
1249 struct ovs_dp_stats dp_stats;
1250 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1253 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1258 ovs_header->dp_ifindex = get_dpifindex(dp);
1260 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1262 goto nla_put_failure;
1264 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1265 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1267 goto nla_put_failure;
1269 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1270 sizeof(struct ovs_dp_megaflow_stats),
1271 &dp_megaflow_stats))
1272 goto nla_put_failure;
1274 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1275 goto nla_put_failure;
1277 return genlmsg_end(skb, ovs_header);
1280 genlmsg_cancel(skb, ovs_header);
1285 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1287 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1290 /* Called with rcu_read_lock or ovs_mutex. */
1291 static struct datapath *lookup_datapath(struct net *net,
1292 struct ovs_header *ovs_header,
1293 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1295 struct datapath *dp;
1297 if (!a[OVS_DP_ATTR_NAME])
1298 dp = get_dp(net, ovs_header->dp_ifindex);
1300 struct vport *vport;
1302 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1303 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1305 return dp ? dp : ERR_PTR(-ENODEV);
1308 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1310 struct datapath *dp;
1312 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1316 WARN(dp->user_features, "Dropping previously announced user features\n");
1317 dp->user_features = 0;
1320 static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1322 if (a[OVS_DP_ATTR_USER_FEATURES])
1323 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1326 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1328 struct nlattr **a = info->attrs;
1329 struct vport_parms parms;
1330 struct sk_buff *reply;
1331 struct datapath *dp;
1332 struct vport *vport;
1333 struct ovs_net *ovs_net;
1337 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1340 reply = ovs_dp_cmd_alloc_info(info);
1345 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1347 goto err_free_reply;
1349 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1351 /* Allocate table. */
1352 err = ovs_flow_tbl_init(&dp->table);
1356 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1357 if (!dp->stats_percpu) {
1359 goto err_destroy_table;
1362 for_each_possible_cpu(i) {
1363 struct dp_stats_percpu *dpath_stats;
1364 dpath_stats = per_cpu_ptr(dp->stats_percpu, i);
1365 u64_stats_init(&dpath_stats->sync);
1368 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1372 goto err_destroy_percpu;
1375 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1376 INIT_HLIST_HEAD(&dp->ports[i]);
1378 /* Set up our datapath device. */
1379 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1380 parms.type = OVS_VPORT_TYPE_INTERNAL;
1381 parms.options = NULL;
1383 parms.port_no = OVSP_LOCAL;
1384 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1386 ovs_dp_change(dp, a);
1388 /* So far only local changes have been made, now need the lock. */
1391 vport = new_vport(&parms);
1392 if (IS_ERR(vport)) {
1393 err = PTR_ERR(vport);
1397 if (err == -EEXIST) {
1398 /* An outdated user space instance that does not understand
1399 * the concept of user_features has attempted to create a new
1400 * datapath and is likely to reuse it. Drop all user features.
1402 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1403 ovs_dp_reset_user_features(skb, info);
1406 goto err_destroy_ports_array;
1409 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1410 info->snd_seq, 0, OVS_DP_CMD_NEW);
1413 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1414 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1418 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1421 err_destroy_ports_array:
1425 free_percpu(dp->stats_percpu);
1427 ovs_flow_tbl_destroy(&dp->table, false);
1429 release_net(ovs_dp_get_net(dp));
1437 /* Called with ovs_mutex. */
1438 static void __dp_destroy(struct datapath *dp)
1442 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1443 struct vport *vport;
1444 struct hlist_node *n;
1446 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1447 if (vport->port_no != OVSP_LOCAL)
1448 ovs_dp_detach_port(vport);
1451 list_del_rcu(&dp->list_node);
1453 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1454 * all ports in datapath are destroyed first before freeing datapath.
1456 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1458 /* RCU destroy the flow table */
1459 ovs_flow_tbl_destroy(&dp->table, true);
1461 call_rcu(&dp->rcu, destroy_dp_rcu);
1464 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1466 struct sk_buff *reply;
1467 struct datapath *dp;
1470 reply = ovs_dp_cmd_alloc_info(info);
1475 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1478 goto err_unlock_free;
1480 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1481 info->snd_seq, 0, OVS_DP_CMD_DEL);
1487 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1496 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1498 struct sk_buff *reply;
1499 struct datapath *dp;
1502 reply = ovs_dp_cmd_alloc_info(info);
1507 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1510 goto err_unlock_free;
1512 ovs_dp_change(dp, info->attrs);
1514 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1515 info->snd_seq, 0, OVS_DP_CMD_NEW);
1519 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1528 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1530 struct sk_buff *reply;
1531 struct datapath *dp;
1534 reply = ovs_dp_cmd_alloc_info(info);
1539 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1542 goto err_unlock_free;
1544 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1545 info->snd_seq, 0, OVS_DP_CMD_NEW);
1549 return genlmsg_reply(reply, info);
1557 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1559 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1560 struct datapath *dp;
1561 int skip = cb->args[0];
1565 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1567 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1568 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1569 OVS_DP_CMD_NEW) < 0)
1580 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1581 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1582 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1583 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1586 static struct genl_ops dp_datapath_genl_ops[] = {
1587 { .cmd = OVS_DP_CMD_NEW,
1588 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1589 .policy = datapath_policy,
1590 .doit = ovs_dp_cmd_new
1592 { .cmd = OVS_DP_CMD_DEL,
1593 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1594 .policy = datapath_policy,
1595 .doit = ovs_dp_cmd_del
1597 { .cmd = OVS_DP_CMD_GET,
1598 .flags = 0, /* OK for unprivileged users. */
1599 .policy = datapath_policy,
1600 .doit = ovs_dp_cmd_get,
1601 .dumpit = ovs_dp_cmd_dump
1603 { .cmd = OVS_DP_CMD_SET,
1604 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1605 .policy = datapath_policy,
1606 .doit = ovs_dp_cmd_set,
1610 static struct genl_family dp_datapath_genl_family = {
1611 .id = GENL_ID_GENERATE,
1612 .hdrsize = sizeof(struct ovs_header),
1613 .name = OVS_DATAPATH_FAMILY,
1614 .version = OVS_DATAPATH_VERSION,
1615 .maxattr = OVS_DP_ATTR_MAX,
1617 .parallel_ops = true,
1618 .ops = dp_datapath_genl_ops,
1619 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1620 .mcgrps = &ovs_dp_datapath_multicast_group,
1624 /* Called with ovs_mutex or RCU read lock. */
1625 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1626 u32 portid, u32 seq, u32 flags, u8 cmd)
1628 struct ovs_header *ovs_header;
1629 struct ovs_vport_stats vport_stats;
1632 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1637 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1639 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1640 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1641 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1642 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1643 goto nla_put_failure;
1645 ovs_vport_get_stats(vport, &vport_stats);
1646 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1648 goto nla_put_failure;
1650 err = ovs_vport_get_options(vport, skb);
1651 if (err == -EMSGSIZE)
1654 return genlmsg_end(skb, ovs_header);
1659 genlmsg_cancel(skb, ovs_header);
1663 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1665 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1668 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1669 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1672 struct sk_buff *skb;
1675 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1677 return ERR_PTR(-ENOMEM);
1679 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1685 /* Called with ovs_mutex or RCU read lock. */
1686 static struct vport *lookup_vport(struct net *net,
1687 struct ovs_header *ovs_header,
1688 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1690 struct datapath *dp;
1691 struct vport *vport;
1693 if (a[OVS_VPORT_ATTR_NAME]) {
1694 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1696 return ERR_PTR(-ENODEV);
1697 if (ovs_header->dp_ifindex &&
1698 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1699 return ERR_PTR(-ENODEV);
1701 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1702 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1704 if (port_no >= DP_MAX_PORTS)
1705 return ERR_PTR(-EFBIG);
1707 dp = get_dp(net, ovs_header->dp_ifindex);
1709 return ERR_PTR(-ENODEV);
1711 vport = ovs_vport_ovsl_rcu(dp, port_no);
1713 return ERR_PTR(-ENODEV);
1716 return ERR_PTR(-EINVAL);
1719 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1721 struct nlattr **a = info->attrs;
1722 struct ovs_header *ovs_header = info->userhdr;
1723 struct vport_parms parms;
1724 struct sk_buff *reply;
1725 struct vport *vport;
1726 struct datapath *dp;
1730 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1731 !a[OVS_VPORT_ATTR_UPCALL_PID])
1734 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1735 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1736 if (port_no >= DP_MAX_PORTS)
1739 reply = ovs_vport_cmd_alloc_info();
1744 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1747 goto exit_unlock_free;
1750 vport = ovs_vport_ovsl(dp, port_no);
1753 goto exit_unlock_free;
1755 for (port_no = 1; ; port_no++) {
1756 if (port_no >= DP_MAX_PORTS) {
1758 goto exit_unlock_free;
1760 vport = ovs_vport_ovsl(dp, port_no);
1766 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1767 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1768 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1770 parms.port_no = port_no;
1771 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1773 vport = new_vport(&parms);
1774 err = PTR_ERR(vport);
1776 goto exit_unlock_free;
1779 if (a[OVS_VPORT_ATTR_STATS])
1780 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1782 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1783 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1787 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1796 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1798 struct nlattr **a = info->attrs;
1799 struct sk_buff *reply;
1800 struct vport *vport;
1803 reply = ovs_vport_cmd_alloc_info();
1808 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1809 err = PTR_ERR(vport);
1811 goto exit_unlock_free;
1813 if (a[OVS_VPORT_ATTR_TYPE] &&
1814 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1816 goto exit_unlock_free;
1819 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1820 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1822 goto exit_unlock_free;
1825 if (a[OVS_VPORT_ATTR_STATS])
1826 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1828 if (a[OVS_VPORT_ATTR_UPCALL_PID])
1829 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1831 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1832 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1836 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1845 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1847 struct nlattr **a = info->attrs;
1848 struct sk_buff *reply;
1849 struct vport *vport;
1852 reply = ovs_vport_cmd_alloc_info();
1857 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1858 err = PTR_ERR(vport);
1860 goto exit_unlock_free;
1862 if (vport->port_no == OVSP_LOCAL) {
1864 goto exit_unlock_free;
1867 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1868 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1870 ovs_dp_detach_port(vport);
1873 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1882 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1884 struct nlattr **a = info->attrs;
1885 struct ovs_header *ovs_header = info->userhdr;
1886 struct sk_buff *reply;
1887 struct vport *vport;
1890 reply = ovs_vport_cmd_alloc_info();
1895 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1896 err = PTR_ERR(vport);
1898 goto exit_unlock_free;
1899 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1900 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1904 return genlmsg_reply(reply, info);
1912 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1914 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1915 struct datapath *dp;
1916 int bucket = cb->args[0], skip = cb->args[1];
1920 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1925 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1926 struct vport *vport;
1929 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1931 ovs_vport_cmd_fill_info(vport, skb,
1932 NETLINK_CB(cb->skb).portid,
1935 OVS_VPORT_CMD_NEW) < 0)
1951 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1952 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1953 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1954 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1955 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1956 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1957 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1960 static struct genl_ops dp_vport_genl_ops[] = {
1961 { .cmd = OVS_VPORT_CMD_NEW,
1962 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1963 .policy = vport_policy,
1964 .doit = ovs_vport_cmd_new
1966 { .cmd = OVS_VPORT_CMD_DEL,
1967 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1968 .policy = vport_policy,
1969 .doit = ovs_vport_cmd_del
1971 { .cmd = OVS_VPORT_CMD_GET,
1972 .flags = 0, /* OK for unprivileged users. */
1973 .policy = vport_policy,
1974 .doit = ovs_vport_cmd_get,
1975 .dumpit = ovs_vport_cmd_dump
1977 { .cmd = OVS_VPORT_CMD_SET,
1978 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1979 .policy = vport_policy,
1980 .doit = ovs_vport_cmd_set,
1984 struct genl_family dp_vport_genl_family = {
1985 .id = GENL_ID_GENERATE,
1986 .hdrsize = sizeof(struct ovs_header),
1987 .name = OVS_VPORT_FAMILY,
1988 .version = OVS_VPORT_VERSION,
1989 .maxattr = OVS_VPORT_ATTR_MAX,
1991 .parallel_ops = true,
1992 .ops = dp_vport_genl_ops,
1993 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
1994 .mcgrps = &ovs_dp_vport_multicast_group,
1998 static struct genl_family *dp_genl_families[] = {
1999 &dp_datapath_genl_family,
2000 &dp_vport_genl_family,
2001 &dp_flow_genl_family,
2002 &dp_packet_genl_family,
2005 static void dp_unregister_genl(int n_families)
2009 for (i = 0; i < n_families; i++)
2010 genl_unregister_family(dp_genl_families[i]);
2013 static int dp_register_genl(void)
2018 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2020 err = genl_register_family(dp_genl_families[i]);
2028 dp_unregister_genl(i);
2032 static int __net_init ovs_init_net(struct net *net)
2034 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2036 INIT_LIST_HEAD(&ovs_net->dps);
2037 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2041 static void __net_exit ovs_exit_net(struct net *net)
2043 struct datapath *dp, *dp_next;
2044 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2047 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2051 cancel_work_sync(&ovs_net->dp_notify_work);
2054 static struct pernet_operations ovs_net_ops = {
2055 .init = ovs_init_net,
2056 .exit = ovs_exit_net,
2058 .size = sizeof(struct ovs_net),
2061 DEFINE_COMPAT_PNET_REG_FUNC(device);
2063 static int __init dp_init(void)
2067 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2069 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2072 err = ovs_flow_init();
2076 err = ovs_vport_init();
2078 goto error_flow_exit;
2080 err = register_pernet_device(&ovs_net_ops);
2082 goto error_vport_exit;
2084 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2086 goto error_netns_exit;
2088 err = dp_register_genl();
2090 goto error_unreg_notifier;
2094 error_unreg_notifier:
2095 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2097 unregister_pernet_device(&ovs_net_ops);
2106 static void dp_cleanup(void)
2108 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2109 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2110 unregister_pernet_device(&ovs_net_ops);
2116 module_init(dp_init);
2117 module_exit(dp_cleanup);
2119 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2120 MODULE_LICENSE("GPL");
2121 MODULE_VERSION(VERSION);