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)
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 sw_flow_actions *sf_acts;
261 struct dp_stats_percpu *stats;
265 stats = this_cpu_ptr(dp->stats_percpu);
268 flow = ovs_flow_tbl_lookup_stats(&dp->table, pkt_key, skb_get_hash(skb),
270 if (unlikely(!flow)) {
271 struct dp_upcall_info upcall;
273 upcall.cmd = OVS_PACKET_CMD_MISS;
274 upcall.userdata = NULL;
275 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
276 upcall.egress_tun_info = NULL;
277 ovs_dp_upcall(dp, skb, &upcall);
279 stats_counter = &stats->n_missed;
283 ovs_flow_stats_update(flow, pkt_key->tp.flags, skb);
285 sf_acts = rcu_dereference(flow->sf_acts);
286 ovs_execute_actions(dp, skb, sf_acts);
287 stats_counter = &stats->n_hit;
290 /* Update datapath statistics. */
291 u64_stats_update_begin(&stats->sync);
293 stats->n_mask_hit += n_mask_hit;
294 u64_stats_update_end(&stats->sync);
297 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
298 const struct dp_upcall_info *upcall_info)
300 struct dp_stats_percpu *stats;
303 BUG_ON(!OVS_CB(skb)->pkt_key);
305 if (upcall_info->portid == 0) {
310 if (!skb_is_gso(skb))
311 err = queue_userspace_packet(dp, skb, upcall_info);
313 err = queue_gso_packets(dp, skb, upcall_info);
320 stats = this_cpu_ptr(dp->stats_percpu);
322 u64_stats_update_begin(&stats->sync);
324 u64_stats_update_end(&stats->sync);
329 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
330 const struct dp_upcall_info *upcall_info)
332 unsigned short gso_type = skb_shinfo(skb)->gso_type;
333 struct sw_flow_key later_key;
334 struct sk_buff *segs, *nskb;
337 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
339 return PTR_ERR(segs);
341 if (gso_type & SKB_GSO_UDP) {
342 /* The initial flow key extracted by ovs_flow_key_extract()
343 * in this case is for a first fragment, so we need to
344 * properly mark later fragments.
346 later_key = *OVS_CB(skb)->pkt_key;
347 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
350 /* Queue all of the segments. */
353 if (gso_type & SKB_GSO_UDP && skb != segs)
354 OVS_CB(skb)->pkt_key = &later_key;
356 err = queue_userspace_packet(dp, skb, upcall_info);
360 } while ((skb = skb->next));
362 /* Free all of the segments. */
370 } while ((skb = nskb));
374 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
377 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
378 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
379 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
381 /* OVS_PACKET_ATTR_USERDATA */
382 if (upcall_info->userdata)
383 size += NLA_ALIGN(upcall_info->userdata->nla_len);
385 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
386 if (upcall_info->egress_tun_info)
387 size += nla_total_size(ovs_tun_key_attr_size());
392 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
393 const struct dp_upcall_info *upcall_info)
395 struct ovs_header *upcall;
396 struct sk_buff *nskb = NULL;
397 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
398 struct sw_flow_key *pkt_key = OVS_CB(skb)->pkt_key;
400 struct genl_info info = {
401 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
402 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
404 .snd_portid = upcall_info->portid,
410 dp_ifindex = get_dpifindex(dp);
414 if (vlan_tx_tag_present(skb)) {
415 nskb = skb_clone(skb, GFP_ATOMIC);
419 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
423 vlan_set_tci(nskb, 0);
428 if (nla_attr_size(skb->len) > USHRT_MAX) {
433 /* Complete checksum if needed */
434 if (skb->ip_summed == CHECKSUM_PARTIAL &&
435 (err = skb_checksum_help(skb)))
438 /* Older versions of OVS user space enforce alignment of the last
439 * Netlink attribute to NLA_ALIGNTO which would require extensive
440 * padding logic. Only perform zerocopy if padding is not required.
442 if (dp->user_features & OVS_DP_F_UNALIGNED)
443 hlen = skb_zerocopy_headlen(skb);
447 len = upcall_msg_size(upcall_info, hlen);
448 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
454 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
455 0, upcall_info->cmd);
456 upcall->dp_ifindex = dp_ifindex;
458 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
459 err = ovs_nla_put_flow(dp, pkt_key, pkt_key, user_skb);
461 nla_nest_end(user_skb, nla);
463 if (upcall_info->userdata)
464 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
465 nla_len(upcall_info->userdata),
466 nla_data(upcall_info->userdata));
468 if (upcall_info->egress_tun_info) {
469 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
470 err = ovs_nla_put_egress_tunnel_key(user_skb,
471 upcall_info->egress_tun_info);
473 nla_nest_end(user_skb, nla);
476 /* Only reserve room for attribute header, packet data is added
477 * in skb_zerocopy() */
478 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
482 nla->nla_len = nla_attr_size(skb->len);
484 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
488 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
489 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
490 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
493 memset(skb_put(user_skb, plen), 0, plen);
496 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
498 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
509 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
511 struct ovs_header *ovs_header = info->userhdr;
512 struct nlattr **a = info->attrs;
513 struct sw_flow_actions *acts;
514 struct sk_buff *packet;
515 struct sw_flow *flow;
516 struct sw_flow_actions *sf_acts;
519 struct vport *input_vport;
524 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
525 !a[OVS_PACKET_ATTR_ACTIONS])
528 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
529 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
533 skb_reserve(packet, NET_IP_ALIGN);
535 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
537 skb_reset_mac_header(packet);
538 eth = eth_hdr(packet);
540 /* Normally, setting the skb 'protocol' field would be handled by a
541 * call to eth_type_trans(), but it assumes there's a sending
542 * device, which we may not have. */
543 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
544 packet->protocol = eth->h_proto;
546 packet->protocol = htons(ETH_P_802_2);
548 /* Build an sw_flow for sending this packet. */
549 flow = ovs_flow_alloc();
554 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
559 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
564 rcu_assign_pointer(flow->sf_acts, acts);
565 OVS_CB(packet)->pkt_key = &flow->key;
566 OVS_CB(skb)->egress_tun_info = NULL;
567 packet->priority = flow->key.phy.priority;
568 packet->mark = flow->key.phy.skb_mark;
571 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
576 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
578 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
583 OVS_CB(packet)->input_vport = input_vport;
584 sf_acts = rcu_dereference(flow->sf_acts);
587 err = ovs_execute_actions(dp, packet, sf_acts);
591 ovs_flow_free(flow, false);
597 ovs_flow_free(flow, false);
604 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
605 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
606 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
607 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
610 static struct genl_ops dp_packet_genl_ops[] = {
611 { .cmd = OVS_PACKET_CMD_EXECUTE,
612 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
613 .policy = packet_policy,
614 .doit = ovs_packet_cmd_execute
618 static struct genl_family dp_packet_genl_family = {
619 .id = GENL_ID_GENERATE,
620 .hdrsize = sizeof(struct ovs_header),
621 .name = OVS_PACKET_FAMILY,
622 .version = OVS_PACKET_VERSION,
623 .maxattr = OVS_PACKET_ATTR_MAX,
625 .parallel_ops = true,
626 .ops = dp_packet_genl_ops,
627 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
630 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
631 struct ovs_dp_megaflow_stats *mega_stats)
635 memset(mega_stats, 0, sizeof(*mega_stats));
637 stats->n_flows = ovs_flow_tbl_count(&dp->table);
638 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
640 stats->n_hit = stats->n_missed = stats->n_lost = 0;
642 for_each_possible_cpu(i) {
643 const struct dp_stats_percpu *percpu_stats;
644 struct dp_stats_percpu local_stats;
647 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
650 start = u64_stats_fetch_begin_irq(&percpu_stats->sync);
651 local_stats = *percpu_stats;
652 } while (u64_stats_fetch_retry_irq(&percpu_stats->sync, start));
654 stats->n_hit += local_stats.n_hit;
655 stats->n_missed += local_stats.n_missed;
656 stats->n_lost += local_stats.n_lost;
657 mega_stats->n_mask_hit += local_stats.n_mask_hit;
661 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
663 return NLMSG_ALIGN(sizeof(struct ovs_header))
664 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_KEY */
665 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_MASK */
666 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
667 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
668 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
669 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
672 /* Called with ovs_mutex or RCU read lock. */
673 static int ovs_flow_cmd_fill_match(struct datapath *dp,
674 const struct sw_flow *flow,
681 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
685 err = ovs_nla_put_flow(dp, &flow->unmasked_key,
686 &flow->unmasked_key, skb);
689 nla_nest_end(skb, nla);
691 /* Fill flow mask. */
692 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
696 err = ovs_nla_put_flow(dp, &flow->key, &flow->mask->key, skb);
699 nla_nest_end(skb, nla);
704 /* Called with ovs_mutex or RCU read lock. */
705 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
708 struct ovs_flow_stats stats;
712 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
715 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
718 if (stats.n_packets &&
719 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
722 if ((u8)ntohs(tcp_flags) &&
723 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
729 /* Called with ovs_mutex or RCU read lock. */
730 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
731 struct sk_buff *skb, int skb_orig_len)
733 struct nlattr *start;
736 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
737 * this is the first flow to be dumped into 'skb'. This is unusual for
738 * Netlink but individual action lists can be longer than
739 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
740 * The userspace caller can always fetch the actions separately if it
741 * really wants them. (Most userspace callers in fact don't care.)
743 * This can only fail for dump operations because the skb is always
744 * properly sized for single flows.
746 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
748 const struct sw_flow_actions *sf_acts;
750 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
751 err = ovs_nla_put_actions(sf_acts->actions,
752 sf_acts->actions_len, skb);
755 nla_nest_end(skb, start);
760 nla_nest_cancel(skb, start);
762 } else if (skb_orig_len) {
769 /* Called with ovs_mutex or RCU read lock. */
770 static int ovs_flow_cmd_fill_info(struct datapath *dp,
771 const struct sw_flow *flow, int dp_ifindex,
772 struct sk_buff *skb, u32 portid,
773 u32 seq, u32 flags, u8 cmd)
775 const int skb_orig_len = skb->len;
776 struct ovs_header *ovs_header;
779 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
782 ovs_header->dp_ifindex = dp_ifindex;
784 err = ovs_flow_cmd_fill_match(dp, flow, skb);
788 err = ovs_flow_cmd_fill_stats(flow, skb);
792 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
796 return genlmsg_end(skb, ovs_header);
799 genlmsg_cancel(skb, ovs_header);
803 /* May not be called with RCU read lock. */
804 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
805 struct genl_info *info,
810 if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
813 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
816 return ERR_PTR(-ENOMEM);
821 /* Called with ovs_mutex. */
822 static struct sk_buff *ovs_flow_cmd_build_info(struct datapath *dp,
823 const struct sw_flow *flow,
825 struct genl_info *info, u8 cmd,
831 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
833 if (!skb || IS_ERR(skb))
836 retval = ovs_flow_cmd_fill_info(dp, flow, dp_ifindex, skb,
837 info->snd_portid, info->snd_seq, 0,
843 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
845 struct nlattr **a = info->attrs;
846 struct ovs_header *ovs_header = info->userhdr;
847 struct sw_flow *flow, *new_flow;
848 struct sw_flow_mask mask;
849 struct sk_buff *reply;
851 struct sw_flow_actions *acts;
852 struct sw_flow_match match;
855 /* Must have key and actions. */
857 if (!a[OVS_FLOW_ATTR_KEY]) {
858 OVS_NLERR("Flow key attribute not present in new flow.\n");
861 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
862 OVS_NLERR("Flow actions attribute not present in new flow.\n");
866 /* Most of the time we need to allocate a new flow, do it before
868 new_flow = ovs_flow_alloc();
869 if (IS_ERR(new_flow)) {
870 error = PTR_ERR(new_flow);
875 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
876 error = ovs_nla_get_match(&match,
877 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
881 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
883 /* Validate actions. */
884 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
887 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
891 reply = ovs_flow_cmd_alloc_info(acts, info, false);
893 error = PTR_ERR(reply);
898 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
903 /* Check if this is a duplicate flow */
904 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
906 rcu_assign_pointer(new_flow->sf_acts, acts);
908 /* Put flow in bucket. */
909 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
910 if (unlikely(error)) {
915 if (unlikely(reply)) {
916 error = ovs_flow_cmd_fill_info(dp, new_flow,
917 ovs_header->dp_ifindex,
918 reply, info->snd_portid,
925 struct sw_flow_actions *old_acts;
927 /* Bail out if we're not allowed to modify an existing flow.
928 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
929 * because Generic Netlink treats the latter as a dump
930 * request. We also accept NLM_F_EXCL in case that bug ever
933 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
938 /* The unmasked key has to be the same for flow updates. */
939 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
940 /* Look for any overlapping flow. */
941 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
947 /* Update actions. */
948 old_acts = ovsl_dereference(flow->sf_acts);
949 rcu_assign_pointer(flow->sf_acts, acts);
951 if (unlikely(reply)) {
952 error = ovs_flow_cmd_fill_info(dp, flow,
953 ovs_header->dp_ifindex,
954 reply, info->snd_portid,
961 ovs_nla_free_flow_actions(old_acts);
962 ovs_flow_free(new_flow, false);
966 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
975 ovs_flow_free(new_flow, false);
980 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
981 static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
982 const struct sw_flow_key *key,
983 const struct sw_flow_mask *mask)
985 struct sw_flow_actions *acts;
986 struct sw_flow_key masked_key;
989 ovs_flow_mask_key(&masked_key, key, mask);
990 error = ovs_nla_copy_actions(a, &masked_key, &acts);
992 OVS_NLERR("Actions may not be safe on all matching packets.\n");
993 return ERR_PTR(error);
999 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1001 struct nlattr **a = info->attrs;
1002 struct ovs_header *ovs_header = info->userhdr;
1003 struct sw_flow_key key;
1004 struct sw_flow *flow;
1005 struct sw_flow_mask mask;
1006 struct sk_buff *reply = NULL;
1007 struct datapath *dp;
1008 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1009 struct sw_flow_match match;
1014 if (!a[OVS_FLOW_ATTR_KEY]) {
1015 OVS_NLERR("Flow key attribute not present in set flow.\n");
1019 ovs_match_init(&match, &key, &mask);
1020 error = ovs_nla_get_match(&match,
1021 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
1025 /* Validate actions. */
1026 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1027 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
1029 error = PTR_ERR(acts);
1033 /* Can allocate before locking if have acts. */
1034 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1035 if (IS_ERR(reply)) {
1036 error = PTR_ERR(reply);
1037 goto err_kfree_acts;
1042 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1043 if (unlikely(!dp)) {
1045 goto err_unlock_ovs;
1047 /* Check that the flow exists. */
1048 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1049 if (unlikely(!flow)) {
1051 goto err_unlock_ovs;
1054 /* Update actions, if present. */
1056 old_acts = ovsl_dereference(flow->sf_acts);
1057 rcu_assign_pointer(flow->sf_acts, acts);
1059 if (unlikely(reply)) {
1060 error = ovs_flow_cmd_fill_info(dp, flow,
1061 ovs_header->dp_ifindex,
1062 reply, info->snd_portid,
1068 /* Could not alloc without acts before locking. */
1069 reply = ovs_flow_cmd_build_info(dp, flow,
1070 ovs_header->dp_ifindex,
1071 info, OVS_FLOW_CMD_NEW, false);
1072 if (unlikely(IS_ERR(reply))) {
1073 error = PTR_ERR(reply);
1074 goto err_unlock_ovs;
1079 if (a[OVS_FLOW_ATTR_CLEAR])
1080 ovs_flow_stats_clear(flow);
1084 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1086 ovs_nla_free_flow_actions(old_acts);
1098 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1100 struct nlattr **a = info->attrs;
1101 struct ovs_header *ovs_header = info->userhdr;
1102 struct sw_flow_key key;
1103 struct sk_buff *reply;
1104 struct sw_flow *flow;
1105 struct datapath *dp;
1106 struct sw_flow_match match;
1109 if (!a[OVS_FLOW_ATTR_KEY]) {
1110 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1114 ovs_match_init(&match, &key, NULL);
1115 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1120 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1126 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1132 reply = ovs_flow_cmd_build_info(dp, flow, ovs_header->dp_ifindex, info,
1133 OVS_FLOW_CMD_NEW, true);
1134 if (IS_ERR(reply)) {
1135 err = PTR_ERR(reply);
1140 return genlmsg_reply(reply, info);
1146 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1148 struct nlattr **a = info->attrs;
1149 struct ovs_header *ovs_header = info->userhdr;
1150 struct sw_flow_key key;
1151 struct sk_buff *reply;
1152 struct sw_flow *flow;
1153 struct datapath *dp;
1154 struct sw_flow_match match;
1157 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1158 ovs_match_init(&match, &key, NULL);
1159 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1165 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1166 if (unlikely(!dp)) {
1170 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1171 err = ovs_flow_tbl_flush(&dp->table);
1174 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1175 if (unlikely(!flow)) {
1180 ovs_flow_tbl_remove(&dp->table, flow);
1183 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *)flow->sf_acts,
1186 if (likely(reply)) {
1187 if (likely(!IS_ERR(reply))) {
1188 rcu_read_lock(); /* Keep RCU checker happy. */
1189 err = ovs_flow_cmd_fill_info(dp, flow,
1190 ovs_header->dp_ifindex,
1191 reply, info->snd_portid,
1196 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1198 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1199 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1204 ovs_flow_free(flow, true);
1211 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1213 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1214 struct table_instance *ti;
1215 struct datapath *dp;
1218 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1224 ti = rcu_dereference(dp->table.ti);
1226 struct sw_flow *flow;
1229 bucket = cb->args[0];
1231 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1235 if (ovs_flow_cmd_fill_info(dp, flow, ovs_header->dp_ifindex, skb,
1236 NETLINK_CB(cb->skb).portid,
1237 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1238 OVS_FLOW_CMD_NEW) < 0)
1241 cb->args[0] = bucket;
1248 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1249 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1250 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1251 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1254 static struct genl_ops dp_flow_genl_ops[] = {
1255 { .cmd = OVS_FLOW_CMD_NEW,
1256 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1257 .policy = flow_policy,
1258 .doit = ovs_flow_cmd_new
1260 { .cmd = OVS_FLOW_CMD_DEL,
1261 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1262 .policy = flow_policy,
1263 .doit = ovs_flow_cmd_del
1265 { .cmd = OVS_FLOW_CMD_GET,
1266 .flags = 0, /* OK for unprivileged users. */
1267 .policy = flow_policy,
1268 .doit = ovs_flow_cmd_get,
1269 .dumpit = ovs_flow_cmd_dump
1271 { .cmd = OVS_FLOW_CMD_SET,
1272 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1273 .policy = flow_policy,
1274 .doit = ovs_flow_cmd_set,
1278 static struct genl_family dp_flow_genl_family = {
1279 .id = GENL_ID_GENERATE,
1280 .hdrsize = sizeof(struct ovs_header),
1281 .name = OVS_FLOW_FAMILY,
1282 .version = OVS_FLOW_VERSION,
1283 .maxattr = OVS_FLOW_ATTR_MAX,
1285 .parallel_ops = true,
1286 .ops = dp_flow_genl_ops,
1287 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1288 .mcgrps = &ovs_dp_flow_multicast_group,
1292 static size_t ovs_dp_cmd_msg_size(void)
1294 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1296 msgsize += nla_total_size(IFNAMSIZ);
1297 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1298 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1299 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1304 /* Called with ovs_mutex or RCU read lock. */
1305 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1306 u32 portid, u32 seq, u32 flags, u8 cmd)
1308 struct ovs_header *ovs_header;
1309 struct ovs_dp_stats dp_stats;
1310 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1313 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1318 ovs_header->dp_ifindex = get_dpifindex(dp);
1320 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1322 goto nla_put_failure;
1324 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1325 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1327 goto nla_put_failure;
1329 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1330 sizeof(struct ovs_dp_megaflow_stats),
1331 &dp_megaflow_stats))
1332 goto nla_put_failure;
1334 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1335 goto nla_put_failure;
1337 return genlmsg_end(skb, ovs_header);
1340 genlmsg_cancel(skb, ovs_header);
1345 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1347 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1350 /* Called with rcu_read_lock or ovs_mutex. */
1351 static struct datapath *lookup_datapath(struct net *net,
1352 struct ovs_header *ovs_header,
1353 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1355 struct datapath *dp;
1357 if (!a[OVS_DP_ATTR_NAME])
1358 dp = get_dp(net, ovs_header->dp_ifindex);
1360 struct vport *vport;
1362 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1363 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1365 return dp ? dp : ERR_PTR(-ENODEV);
1368 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1370 struct datapath *dp;
1372 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1376 WARN(dp->user_features, "Dropping previously announced user features\n");
1377 dp->user_features = 0;
1380 static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1382 if (a[OVS_DP_ATTR_USER_FEATURES])
1383 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1386 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1388 struct nlattr **a = info->attrs;
1389 struct vport_parms parms;
1390 struct sk_buff *reply;
1391 struct datapath *dp;
1392 struct vport *vport;
1393 struct ovs_net *ovs_net;
1397 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1400 reply = ovs_dp_cmd_alloc_info(info);
1405 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1407 goto err_free_reply;
1409 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1411 /* Allocate table. */
1412 err = ovs_flow_tbl_init(&dp->table);
1416 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1417 if (!dp->stats_percpu) {
1419 goto err_destroy_table;
1422 for_each_possible_cpu(i) {
1423 struct dp_stats_percpu *dpath_stats;
1424 dpath_stats = per_cpu_ptr(dp->stats_percpu, i);
1425 u64_stats_init(&dpath_stats->sync);
1428 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1432 goto err_destroy_percpu;
1435 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1436 INIT_HLIST_HEAD(&dp->ports[i]);
1438 /* Set up our datapath device. */
1439 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1440 parms.type = OVS_VPORT_TYPE_INTERNAL;
1441 parms.options = NULL;
1443 parms.port_no = OVSP_LOCAL;
1444 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1446 ovs_dp_change(dp, a);
1448 /* So far only local changes have been made, now need the lock. */
1451 vport = new_vport(&parms);
1452 if (IS_ERR(vport)) {
1453 err = PTR_ERR(vport);
1457 if (err == -EEXIST) {
1458 /* An outdated user space instance that does not understand
1459 * the concept of user_features has attempted to create a new
1460 * datapath and is likely to reuse it. Drop all user features.
1462 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1463 ovs_dp_reset_user_features(skb, info);
1466 goto err_destroy_ports_array;
1469 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1470 info->snd_seq, 0, OVS_DP_CMD_NEW);
1473 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1474 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1478 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1481 err_destroy_ports_array:
1485 free_percpu(dp->stats_percpu);
1487 ovs_flow_tbl_destroy(&dp->table);
1489 release_net(ovs_dp_get_net(dp));
1497 /* Called with ovs_mutex. */
1498 static void __dp_destroy(struct datapath *dp)
1502 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1503 struct vport *vport;
1504 struct hlist_node *n;
1506 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1507 if (vport->port_no != OVSP_LOCAL)
1508 ovs_dp_detach_port(vport);
1511 list_del_rcu(&dp->list_node);
1513 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1514 * all ports in datapath are destroyed first before freeing datapath.
1516 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1518 /* RCU destroy the flow table */
1519 call_rcu(&dp->rcu, destroy_dp_rcu);
1522 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1524 struct sk_buff *reply;
1525 struct datapath *dp;
1528 reply = ovs_dp_cmd_alloc_info(info);
1533 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1536 goto err_unlock_free;
1538 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1539 info->snd_seq, 0, OVS_DP_CMD_DEL);
1545 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1554 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1556 struct sk_buff *reply;
1557 struct datapath *dp;
1560 reply = ovs_dp_cmd_alloc_info(info);
1565 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1568 goto err_unlock_free;
1570 ovs_dp_change(dp, info->attrs);
1572 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1573 info->snd_seq, 0, OVS_DP_CMD_NEW);
1577 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1586 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1588 struct sk_buff *reply;
1589 struct datapath *dp;
1592 reply = ovs_dp_cmd_alloc_info(info);
1597 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1600 goto err_unlock_free;
1602 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1603 info->snd_seq, 0, OVS_DP_CMD_NEW);
1607 return genlmsg_reply(reply, info);
1615 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1617 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1618 struct datapath *dp;
1619 int skip = cb->args[0];
1623 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1625 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1626 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1627 OVS_DP_CMD_NEW) < 0)
1638 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1639 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1640 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1641 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1644 static struct genl_ops dp_datapath_genl_ops[] = {
1645 { .cmd = OVS_DP_CMD_NEW,
1646 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1647 .policy = datapath_policy,
1648 .doit = ovs_dp_cmd_new
1650 { .cmd = OVS_DP_CMD_DEL,
1651 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1652 .policy = datapath_policy,
1653 .doit = ovs_dp_cmd_del
1655 { .cmd = OVS_DP_CMD_GET,
1656 .flags = 0, /* OK for unprivileged users. */
1657 .policy = datapath_policy,
1658 .doit = ovs_dp_cmd_get,
1659 .dumpit = ovs_dp_cmd_dump
1661 { .cmd = OVS_DP_CMD_SET,
1662 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1663 .policy = datapath_policy,
1664 .doit = ovs_dp_cmd_set,
1668 static struct genl_family dp_datapath_genl_family = {
1669 .id = GENL_ID_GENERATE,
1670 .hdrsize = sizeof(struct ovs_header),
1671 .name = OVS_DATAPATH_FAMILY,
1672 .version = OVS_DATAPATH_VERSION,
1673 .maxattr = OVS_DP_ATTR_MAX,
1675 .parallel_ops = true,
1676 .ops = dp_datapath_genl_ops,
1677 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1678 .mcgrps = &ovs_dp_datapath_multicast_group,
1682 /* Called with ovs_mutex or RCU read lock. */
1683 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1684 u32 portid, u32 seq, u32 flags, u8 cmd)
1686 struct ovs_header *ovs_header;
1687 struct ovs_vport_stats vport_stats;
1690 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1695 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1697 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1698 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1699 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)))
1700 goto nla_put_failure;
1702 ovs_vport_get_stats(vport, &vport_stats);
1703 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1705 goto nla_put_failure;
1707 if (ovs_vport_get_upcall_portids(vport, skb))
1708 goto nla_put_failure;
1710 err = ovs_vport_get_options(vport, skb);
1711 if (err == -EMSGSIZE)
1714 return genlmsg_end(skb, ovs_header);
1719 genlmsg_cancel(skb, ovs_header);
1723 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1725 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1728 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1729 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1732 struct sk_buff *skb;
1735 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1737 return ERR_PTR(-ENOMEM);
1739 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1745 /* Called with ovs_mutex or RCU read lock. */
1746 static struct vport *lookup_vport(struct net *net,
1747 struct ovs_header *ovs_header,
1748 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1750 struct datapath *dp;
1751 struct vport *vport;
1753 if (a[OVS_VPORT_ATTR_NAME]) {
1754 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1756 return ERR_PTR(-ENODEV);
1757 if (ovs_header->dp_ifindex &&
1758 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1759 return ERR_PTR(-ENODEV);
1761 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1762 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1764 if (port_no >= DP_MAX_PORTS)
1765 return ERR_PTR(-EFBIG);
1767 dp = get_dp(net, ovs_header->dp_ifindex);
1769 return ERR_PTR(-ENODEV);
1771 vport = ovs_vport_ovsl_rcu(dp, port_no);
1773 return ERR_PTR(-ENODEV);
1776 return ERR_PTR(-EINVAL);
1779 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1781 struct nlattr **a = info->attrs;
1782 struct ovs_header *ovs_header = info->userhdr;
1783 struct vport_parms parms;
1784 struct sk_buff *reply;
1785 struct vport *vport;
1786 struct datapath *dp;
1790 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1791 !a[OVS_VPORT_ATTR_UPCALL_PID])
1794 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1795 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1796 if (port_no >= DP_MAX_PORTS)
1799 reply = ovs_vport_cmd_alloc_info();
1804 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1807 goto exit_unlock_free;
1810 vport = ovs_vport_ovsl(dp, port_no);
1813 goto exit_unlock_free;
1815 for (port_no = 1; ; port_no++) {
1816 if (port_no >= DP_MAX_PORTS) {
1818 goto exit_unlock_free;
1820 vport = ovs_vport_ovsl(dp, port_no);
1826 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1827 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1828 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1830 parms.port_no = port_no;
1831 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1833 vport = new_vport(&parms);
1834 err = PTR_ERR(vport);
1836 goto exit_unlock_free;
1839 if (a[OVS_VPORT_ATTR_STATS])
1840 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1842 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1843 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1847 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1856 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1858 struct nlattr **a = info->attrs;
1859 struct sk_buff *reply;
1860 struct vport *vport;
1863 reply = ovs_vport_cmd_alloc_info();
1868 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1869 err = PTR_ERR(vport);
1871 goto exit_unlock_free;
1873 if (a[OVS_VPORT_ATTR_TYPE] &&
1874 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1876 goto exit_unlock_free;
1879 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1880 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1882 goto exit_unlock_free;
1885 if (a[OVS_VPORT_ATTR_STATS])
1886 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1889 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1890 err = ovs_vport_set_upcall_portids(vport,
1891 a[OVS_VPORT_ATTR_UPCALL_PID]);
1893 goto exit_unlock_free;
1896 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1897 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1901 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1910 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1912 struct nlattr **a = info->attrs;
1913 struct sk_buff *reply;
1914 struct vport *vport;
1917 reply = ovs_vport_cmd_alloc_info();
1922 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1923 err = PTR_ERR(vport);
1925 goto exit_unlock_free;
1927 if (vport->port_no == OVSP_LOCAL) {
1929 goto exit_unlock_free;
1932 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1933 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1935 ovs_dp_detach_port(vport);
1938 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1947 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1949 struct nlattr **a = info->attrs;
1950 struct ovs_header *ovs_header = info->userhdr;
1951 struct sk_buff *reply;
1952 struct vport *vport;
1955 reply = ovs_vport_cmd_alloc_info();
1960 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1961 err = PTR_ERR(vport);
1963 goto exit_unlock_free;
1964 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1965 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1969 return genlmsg_reply(reply, info);
1977 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1979 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1980 struct datapath *dp;
1981 int bucket = cb->args[0], skip = cb->args[1];
1985 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1990 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1991 struct vport *vport;
1994 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1996 ovs_vport_cmd_fill_info(vport, skb,
1997 NETLINK_CB(cb->skb).portid,
2000 OVS_VPORT_CMD_NEW) < 0)
2016 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2017 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2018 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2019 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2020 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2021 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2022 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2025 static struct genl_ops dp_vport_genl_ops[] = {
2026 { .cmd = OVS_VPORT_CMD_NEW,
2027 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2028 .policy = vport_policy,
2029 .doit = ovs_vport_cmd_new
2031 { .cmd = OVS_VPORT_CMD_DEL,
2032 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2033 .policy = vport_policy,
2034 .doit = ovs_vport_cmd_del
2036 { .cmd = OVS_VPORT_CMD_GET,
2037 .flags = 0, /* OK for unprivileged users. */
2038 .policy = vport_policy,
2039 .doit = ovs_vport_cmd_get,
2040 .dumpit = ovs_vport_cmd_dump
2042 { .cmd = OVS_VPORT_CMD_SET,
2043 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2044 .policy = vport_policy,
2045 .doit = ovs_vport_cmd_set,
2049 struct genl_family dp_vport_genl_family = {
2050 .id = GENL_ID_GENERATE,
2051 .hdrsize = sizeof(struct ovs_header),
2052 .name = OVS_VPORT_FAMILY,
2053 .version = OVS_VPORT_VERSION,
2054 .maxattr = OVS_VPORT_ATTR_MAX,
2056 .parallel_ops = true,
2057 .ops = dp_vport_genl_ops,
2058 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2059 .mcgrps = &ovs_dp_vport_multicast_group,
2063 static struct genl_family *dp_genl_families[] = {
2064 &dp_datapath_genl_family,
2065 &dp_vport_genl_family,
2066 &dp_flow_genl_family,
2067 &dp_packet_genl_family,
2070 static void dp_unregister_genl(int n_families)
2074 for (i = 0; i < n_families; i++)
2075 genl_unregister_family(dp_genl_families[i]);
2078 static int dp_register_genl(void)
2083 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2085 err = genl_register_family(dp_genl_families[i]);
2093 dp_unregister_genl(i);
2097 static int __net_init ovs_init_net(struct net *net)
2099 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2101 INIT_LIST_HEAD(&ovs_net->dps);
2102 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2106 static void __net_exit ovs_exit_net(struct net *net)
2108 struct datapath *dp, *dp_next;
2109 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2112 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2116 cancel_work_sync(&ovs_net->dp_notify_work);
2119 static struct pernet_operations ovs_net_ops = {
2120 .init = ovs_init_net,
2121 .exit = ovs_exit_net,
2123 .size = sizeof(struct ovs_net),
2126 DEFINE_COMPAT_PNET_REG_FUNC(device);
2128 static int __init dp_init(void)
2132 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2134 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2137 err = action_fifos_init();
2141 err = ovs_flow_init();
2143 goto error_action_fifos_exit;
2145 err = ovs_vport_init();
2147 goto error_flow_exit;
2149 err = register_pernet_device(&ovs_net_ops);
2151 goto error_vport_exit;
2153 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2155 goto error_netns_exit;
2157 err = dp_register_genl();
2159 goto error_unreg_notifier;
2163 error_unreg_notifier:
2164 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2166 unregister_pernet_device(&ovs_net_ops);
2171 error_action_fifos_exit:
2172 action_fifos_exit();
2177 static void dp_cleanup(void)
2179 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2180 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2181 unregister_pernet_device(&ovs_net_ops);
2185 action_fifos_exit();
2188 module_init(dp_init);
2189 module_exit(dp_cleanup);
2191 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2192 MODULE_LICENSE("GPL");
2193 MODULE_VERSION(VERSION);