2 * Copyright (c) 2007-2012 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.h>
27 #include <linux/jhash.h>
28 #include <linux/delay.h>
29 #include <linux/time.h>
30 #include <linux/etherdevice.h>
31 #include <linux/genetlink.h>
32 #include <linux/kernel.h>
33 #include <linux/kthread.h>
34 #include <linux/mutex.h>
35 #include <linux/percpu.h>
36 #include <linux/rcupdate.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/version.h>
40 #include <linux/ethtool.h>
41 #include <linux/wait.h>
42 #include <asm/div64.h>
43 #include <linux/highmem.h>
44 #include <linux/netfilter_bridge.h>
45 #include <linux/netfilter_ipv4.h>
46 #include <linux/inetdevice.h>
47 #include <linux/list.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <net/genetlink.h>
52 #include <net/net_namespace.h>
53 #include <net/netns/generic.h>
58 #include "genl_exec.h"
61 #include "vport-internal_dev.h"
63 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
64 LINUX_VERSION_CODE >= KERNEL_VERSION(3,6,0)
65 #error Kernels before 2.6.18 or after 3.5 are not supported by this version of Open vSwitch.
68 #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
69 static void rehash_flow_table(struct work_struct *work);
70 static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
72 int ovs_net_id __read_mostly;
74 int (*ovs_dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
75 EXPORT_SYMBOL(ovs_dp_ioctl_hook);
80 * Writes to device state (add/remove datapath, port, set operations on vports,
81 * etc.) are protected by RTNL.
83 * Writes to other state (flow table modifications, set miscellaneous datapath
84 * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
87 * Reads are protected by RCU.
89 * There are a few special cases (mostly stats) that have their own
90 * synchronization but they nest under all of above and don't interact with
94 static struct vport *new_vport(const struct vport_parms *);
95 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
96 const struct dp_upcall_info *);
97 static int queue_userspace_packet(struct net *, int dp_ifindex,
99 const struct dp_upcall_info *);
101 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
102 static struct datapath *get_dp(struct net *net, int dp_ifindex)
104 struct datapath *dp = NULL;
105 struct net_device *dev;
108 dev = dev_get_by_index_rcu(net, dp_ifindex);
110 struct vport *vport = ovs_internal_dev_get_vport(dev);
119 /* Must be called with rcu_read_lock or RTNL lock. */
120 const char *ovs_dp_name(const struct datapath *dp)
122 struct vport *vport = ovs_vport_rtnl_rcu(dp, OVSP_LOCAL);
123 return vport->ops->get_name(vport);
126 static int get_dpifindex(struct datapath *dp)
133 local = ovs_vport_rcu(dp, OVSP_LOCAL);
135 ifindex = local->ops->get_ifindex(local);
144 static size_t br_nlmsg_size(void)
146 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
147 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
148 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
149 + nla_total_size(4) /* IFLA_MASTER */
150 + nla_total_size(4) /* IFLA_MTU */
151 + nla_total_size(1); /* IFLA_OPERSTATE */
154 /* Caller must hold RTNL lock. */
155 static int dp_fill_ifinfo(struct sk_buff *skb,
156 const struct vport *port,
157 int event, unsigned int flags)
159 struct datapath *dp = port->dp;
160 struct ifinfomsg *hdr;
161 struct nlmsghdr *nlh;
163 if (!port->ops->get_ifindex)
166 nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
170 hdr = nlmsg_data(nlh);
171 hdr->ifi_family = AF_BRIDGE;
173 hdr->ifi_type = ARPHRD_ETHER;
174 hdr->ifi_index = port->ops->get_ifindex(port);
175 hdr->ifi_flags = port->ops->get_dev_flags(port);
178 if (nla_put_string(skb, IFLA_IFNAME, port->ops->get_name(port)) ||
179 nla_put_u32(skb, IFLA_MASTER, get_dpifindex(dp)) ||
180 nla_put_u32(skb, IFLA_MTU, port->ops->get_mtu(port)) ||
181 #ifdef IFLA_OPERSTATE
182 nla_put_u8(skb, IFLA_OPERSTATE,
183 port->ops->is_running(port) ?
184 port->ops->get_operstate(port) :
187 nla_put(skb, IFLA_ADDRESS, ETH_ALEN, port->ops->get_addr(port)))
188 goto nla_put_failure;
190 return nlmsg_end(skb, nlh);
193 nlmsg_cancel(skb, nlh);
197 /* Caller must hold RTNL lock. */
198 static void dp_ifinfo_notify(int event, struct vport *port)
203 skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
209 err = dp_fill_ifinfo(skb, port, event, 0);
211 if (err == -ENODEV) {
214 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
215 WARN_ON(err == -EMSGSIZE);
220 rtnl_notify(skb, ovs_dp_get_net(port->dp), 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
224 rtnl_set_sk_err(ovs_dp_get_net(port->dp), RTNLGRP_LINK, err);
229 static void release_dp(struct kobject *kobj)
231 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
235 static struct kobj_type dp_ktype = {
236 .release = release_dp
239 static void destroy_dp_rcu(struct rcu_head *rcu)
241 struct datapath *dp = container_of(rcu, struct datapath, rcu);
243 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
244 free_percpu(dp->stats_percpu);
245 release_net(ovs_dp_get_net(dp));
247 kobject_put(&dp->ifobj);
250 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
253 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
256 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
259 struct hlist_node *n;
260 struct hlist_head *head;
262 head = vport_hash_bucket(dp, port_no);
263 hlist_for_each_entry_rcu(vport, n, head, dp_hash_node) {
264 if (vport->port_no == port_no)
270 /* Called with RTNL lock and genl_lock. */
271 static struct vport *new_vport(const struct vport_parms *parms)
275 vport = ovs_vport_add(parms);
276 if (!IS_ERR(vport)) {
277 struct datapath *dp = parms->dp;
278 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
280 hlist_add_head_rcu(&vport->dp_hash_node, head);
281 dp_ifinfo_notify(RTM_NEWLINK, vport);
286 /* Called with RTNL lock. */
287 void ovs_dp_detach_port(struct vport *p)
291 if (p->port_no != OVSP_LOCAL)
292 ovs_dp_sysfs_del_if(p);
294 dp_ifinfo_notify(RTM_DELLINK, p);
296 /* First drop references to device. */
297 hlist_del_rcu(&p->dp_hash_node);
299 /* Then destroy it. */
303 /* Must be called with rcu_read_lock. */
304 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
306 struct datapath *dp = p->dp;
307 struct sw_flow *flow;
308 struct dp_stats_percpu *stats;
312 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
314 if (!OVS_CB(skb)->flow) {
315 struct sw_flow_key key;
318 /* Extract flow from 'skb' into 'key'. */
319 error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
320 if (unlikely(error)) {
326 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table),
328 if (unlikely(!flow)) {
329 struct dp_upcall_info upcall;
331 upcall.cmd = OVS_PACKET_CMD_MISS;
333 upcall.userdata = NULL;
334 upcall.pid = p->upcall_pid;
335 ovs_dp_upcall(dp, skb, &upcall);
337 stats_counter = &stats->n_missed;
341 OVS_CB(skb)->flow = flow;
344 stats_counter = &stats->n_hit;
345 ovs_flow_used(OVS_CB(skb)->flow, skb);
346 ovs_execute_actions(dp, skb);
349 /* Update datapath statistics. */
350 u64_stats_update_begin(&stats->sync);
352 u64_stats_update_end(&stats->sync);
355 static struct genl_family dp_packet_genl_family = {
356 .id = GENL_ID_GENERATE,
357 .hdrsize = sizeof(struct ovs_header),
358 .name = OVS_PACKET_FAMILY,
359 .version = OVS_PACKET_VERSION,
360 .maxattr = OVS_PACKET_ATTR_MAX,
364 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
365 const struct dp_upcall_info *upcall_info)
367 struct dp_stats_percpu *stats;
371 if (upcall_info->pid == 0) {
376 dp_ifindex = get_dpifindex(dp);
382 forward_ip_summed(skb, true);
384 if (!skb_is_gso(skb))
385 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
387 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
394 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
396 u64_stats_update_begin(&stats->sync);
398 u64_stats_update_end(&stats->sync);
403 static int queue_gso_packets(struct net *net, int dp_ifindex,
405 const struct dp_upcall_info *upcall_info)
407 unsigned short gso_type = skb_shinfo(skb)->gso_type;
408 struct dp_upcall_info later_info;
409 struct sw_flow_key later_key;
410 struct sk_buff *segs, *nskb;
413 segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
415 return PTR_ERR(segs);
417 /* Queue all of the segments. */
420 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
424 if (skb == segs && gso_type & SKB_GSO_UDP) {
425 /* The initial flow key extracted by ovs_flow_extract()
426 * in this case is for a first fragment, so we need to
427 * properly mark later fragments.
429 later_key = *upcall_info->key;
430 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
432 later_info = *upcall_info;
433 later_info.key = &later_key;
434 upcall_info = &later_info;
436 } while ((skb = skb->next));
438 /* Free all of the segments. */
446 } while ((skb = nskb));
450 static int queue_userspace_packet(struct net *net, int dp_ifindex,
452 const struct dp_upcall_info *upcall_info)
454 struct ovs_header *upcall;
455 struct sk_buff *nskb = NULL;
456 struct sk_buff *user_skb; /* to be queued to userspace */
461 if (vlan_tx_tag_present(skb)) {
462 nskb = skb_clone(skb, GFP_ATOMIC);
466 err = vlan_deaccel_tag(nskb);
473 if (nla_attr_size(skb->len) > USHRT_MAX) {
478 len = sizeof(struct ovs_header);
479 len += nla_total_size(skb->len);
480 len += nla_total_size(FLOW_BUFSIZE);
481 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
482 len += nla_total_size(8);
484 user_skb = genlmsg_new(len, GFP_ATOMIC);
490 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
491 0, upcall_info->cmd);
492 upcall->dp_ifindex = dp_ifindex;
494 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
495 ovs_flow_to_nlattrs(upcall_info->key, user_skb);
496 nla_nest_end(user_skb, nla);
498 if (upcall_info->userdata)
499 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
500 nla_get_u64(upcall_info->userdata));
502 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
504 skb_copy_and_csum_dev(skb, nla_data(nla));
506 err = genlmsg_unicast(net, user_skb, upcall_info->pid);
513 /* Called with genl_mutex. */
514 static int flush_flows(struct datapath *dp)
516 struct flow_table *old_table;
517 struct flow_table *new_table;
519 old_table = genl_dereference(dp->table);
520 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
524 rcu_assign_pointer(dp->table, new_table);
526 ovs_flow_tbl_deferred_destroy(old_table);
530 static int validate_actions(const struct nlattr *attr,
531 const struct sw_flow_key *key, int depth);
533 static int validate_sample(const struct nlattr *attr,
534 const struct sw_flow_key *key, int depth)
536 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
537 const struct nlattr *probability, *actions;
538 const struct nlattr *a;
541 memset(attrs, 0, sizeof(attrs));
542 nla_for_each_nested(a, attr, rem) {
543 int type = nla_type(a);
544 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
551 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
552 if (!probability || nla_len(probability) != sizeof(u32))
555 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
556 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
558 return validate_actions(actions, key, depth + 1);
561 static int validate_tp_port(const struct sw_flow_key *flow_key)
563 if (flow_key->eth.type == htons(ETH_P_IP)) {
564 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
566 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
567 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
574 static int validate_set(const struct nlattr *a,
575 const struct sw_flow_key *flow_key)
577 const struct nlattr *ovs_key = nla_data(a);
578 int key_type = nla_type(ovs_key);
580 /* There can be only one key in a action */
581 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
584 if (key_type > OVS_KEY_ATTR_MAX ||
585 nla_len(ovs_key) != ovs_key_lens[key_type])
589 const struct ovs_key_ipv4 *ipv4_key;
591 case OVS_KEY_ATTR_PRIORITY:
592 case OVS_KEY_ATTR_TUN_ID:
593 case OVS_KEY_ATTR_ETHERNET:
596 case OVS_KEY_ATTR_IPV4:
597 if (flow_key->eth.type != htons(ETH_P_IP))
600 if (!flow_key->ip.proto)
603 ipv4_key = nla_data(ovs_key);
604 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
607 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
612 case OVS_KEY_ATTR_TCP:
613 if (flow_key->ip.proto != IPPROTO_TCP)
616 return validate_tp_port(flow_key);
618 case OVS_KEY_ATTR_UDP:
619 if (flow_key->ip.proto != IPPROTO_UDP)
622 return validate_tp_port(flow_key);
631 static int validate_userspace(const struct nlattr *attr)
633 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
634 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
635 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
637 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
640 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
641 attr, userspace_policy);
645 if (!a[OVS_USERSPACE_ATTR_PID] ||
646 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
652 static int validate_actions(const struct nlattr *attr,
653 const struct sw_flow_key *key, int depth)
655 const struct nlattr *a;
658 if (depth >= SAMPLE_ACTION_DEPTH)
661 nla_for_each_nested(a, attr, rem) {
662 /* Expected argument lengths, (u32)-1 for variable length. */
663 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
664 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
665 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
666 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
667 [OVS_ACTION_ATTR_POP_VLAN] = 0,
668 [OVS_ACTION_ATTR_SET] = (u32)-1,
669 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
671 const struct ovs_action_push_vlan *vlan;
672 int type = nla_type(a);
674 if (type > OVS_ACTION_ATTR_MAX ||
675 (action_lens[type] != nla_len(a) &&
676 action_lens[type] != (u32)-1))
680 case OVS_ACTION_ATTR_UNSPEC:
683 case OVS_ACTION_ATTR_USERSPACE:
684 err = validate_userspace(a);
689 case OVS_ACTION_ATTR_OUTPUT:
690 if (nla_get_u32(a) >= DP_MAX_PORTS)
695 case OVS_ACTION_ATTR_POP_VLAN:
698 case OVS_ACTION_ATTR_PUSH_VLAN:
700 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
702 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
706 case OVS_ACTION_ATTR_SET:
707 err = validate_set(a, key);
712 case OVS_ACTION_ATTR_SAMPLE:
713 err = validate_sample(a, key, depth);
729 static void clear_stats(struct sw_flow *flow)
733 flow->packet_count = 0;
734 flow->byte_count = 0;
737 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
739 struct ovs_header *ovs_header = info->userhdr;
740 struct nlattr **a = info->attrs;
741 struct sw_flow_actions *acts;
742 struct sk_buff *packet;
743 struct sw_flow *flow;
751 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
752 !a[OVS_PACKET_ATTR_ACTIONS] ||
753 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
756 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
757 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
761 skb_reserve(packet, NET_IP_ALIGN);
763 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
765 skb_reset_mac_header(packet);
766 eth = eth_hdr(packet);
768 /* Normally, setting the skb 'protocol' field would be handled by a
769 * call to eth_type_trans(), but it assumes there's a sending
770 * device, which we may not have. */
771 if (ntohs(eth->h_proto) >= 1536)
772 packet->protocol = eth->h_proto;
774 packet->protocol = htons(ETH_P_802_2);
776 /* Build an sw_flow for sending this packet. */
777 flow = ovs_flow_alloc();
782 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
786 err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
787 &flow->key.phy.in_port,
788 &flow->key.phy.tun_id,
789 a[OVS_PACKET_ATTR_KEY]);
793 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
797 flow->hash = ovs_flow_hash(&flow->key, key_len);
799 acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
803 rcu_assign_pointer(flow->sf_acts, acts);
805 OVS_CB(packet)->flow = flow;
806 packet->priority = flow->key.phy.priority;
809 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
815 err = ovs_execute_actions(dp, packet);
832 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
833 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
834 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
835 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
838 static struct genl_ops dp_packet_genl_ops[] = {
839 { .cmd = OVS_PACKET_CMD_EXECUTE,
840 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
841 .policy = packet_policy,
842 .doit = ovs_packet_cmd_execute
846 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
849 struct flow_table *table = genl_dereference(dp->table);
851 stats->n_flows = ovs_flow_tbl_count(table);
853 stats->n_hit = stats->n_missed = stats->n_lost = 0;
854 for_each_possible_cpu(i) {
855 const struct dp_stats_percpu *percpu_stats;
856 struct dp_stats_percpu local_stats;
859 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
862 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
863 local_stats = *percpu_stats;
864 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
866 stats->n_hit += local_stats.n_hit;
867 stats->n_missed += local_stats.n_missed;
868 stats->n_lost += local_stats.n_lost;
872 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
873 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
874 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
875 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
878 static struct genl_family dp_flow_genl_family = {
879 .id = GENL_ID_GENERATE,
880 .hdrsize = sizeof(struct ovs_header),
881 .name = OVS_FLOW_FAMILY,
882 .version = OVS_FLOW_VERSION,
883 .maxattr = OVS_FLOW_ATTR_MAX,
887 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
888 .name = OVS_FLOW_MCGROUP
891 /* Called with genl_lock. */
892 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
893 struct sk_buff *skb, u32 pid,
894 u32 seq, u32 flags, u8 cmd)
896 const int skb_orig_len = skb->len;
897 const struct sw_flow_actions *sf_acts;
898 struct ovs_flow_stats stats;
899 struct ovs_header *ovs_header;
905 sf_acts = rcu_dereference_protected(flow->sf_acts,
906 lockdep_genl_is_held());
908 ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
912 ovs_header->dp_ifindex = get_dpifindex(dp);
914 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
916 goto nla_put_failure;
917 err = ovs_flow_to_nlattrs(&flow->key, skb);
920 nla_nest_end(skb, nla);
922 spin_lock_bh(&flow->lock);
924 stats.n_packets = flow->packet_count;
925 stats.n_bytes = flow->byte_count;
926 tcp_flags = flow->tcp_flags;
927 spin_unlock_bh(&flow->lock);
930 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
931 goto nla_put_failure;
933 if (stats.n_packets &&
934 nla_put(skb, OVS_FLOW_ATTR_STATS,
935 sizeof(struct ovs_flow_stats), &stats))
936 goto nla_put_failure;
939 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
940 goto nla_put_failure;
942 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
943 * this is the first flow to be dumped into 'skb'. This is unusual for
944 * Netlink but individual action lists can be longer than
945 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
946 * The userspace caller can always fetch the actions separately if it
947 * really wants them. (Most userspace callers in fact don't care.)
949 * This can only fail for dump operations because the skb is always
950 * properly sized for single flows.
952 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
954 if (err < 0 && skb_orig_len)
957 return genlmsg_end(skb, ovs_header);
962 genlmsg_cancel(skb, ovs_header);
966 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
968 const struct sw_flow_actions *sf_acts;
971 sf_acts = rcu_dereference_protected(flow->sf_acts,
972 lockdep_genl_is_held());
974 /* OVS_FLOW_ATTR_KEY */
975 len = nla_total_size(FLOW_BUFSIZE);
976 /* OVS_FLOW_ATTR_ACTIONS */
977 len += nla_total_size(sf_acts->actions_len);
978 /* OVS_FLOW_ATTR_STATS */
979 len += nla_total_size(sizeof(struct ovs_flow_stats));
980 /* OVS_FLOW_ATTR_TCP_FLAGS */
981 len += nla_total_size(1);
982 /* OVS_FLOW_ATTR_USED */
983 len += nla_total_size(8);
985 len += NLMSG_ALIGN(sizeof(struct ovs_header));
987 return genlmsg_new(len, GFP_KERNEL);
990 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
992 u32 pid, u32 seq, u8 cmd)
997 skb = ovs_flow_cmd_alloc_info(flow);
999 return ERR_PTR(-ENOMEM);
1001 retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
1006 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1008 struct nlattr **a = info->attrs;
1009 struct ovs_header *ovs_header = info->userhdr;
1010 struct sw_flow_key key;
1011 struct sw_flow *flow;
1012 struct sk_buff *reply;
1013 struct datapath *dp;
1014 struct flow_table *table;
1020 if (!a[OVS_FLOW_ATTR_KEY])
1022 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1026 /* Validate actions. */
1027 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1028 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
1031 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1036 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1041 table = genl_dereference(dp->table);
1042 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1044 struct sw_flow_actions *acts;
1046 /* Bail out if we're not allowed to create a new flow. */
1048 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1051 /* Expand table, if necessary, to make room. */
1052 if (ovs_flow_tbl_need_to_expand(table)) {
1053 struct flow_table *new_table;
1055 new_table = ovs_flow_tbl_expand(table);
1056 if (!IS_ERR(new_table)) {
1057 rcu_assign_pointer(dp->table, new_table);
1058 ovs_flow_tbl_deferred_destroy(table);
1059 table = genl_dereference(dp->table);
1063 /* Allocate flow. */
1064 flow = ovs_flow_alloc();
1066 error = PTR_ERR(flow);
1072 /* Obtain actions. */
1073 acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1074 error = PTR_ERR(acts);
1076 goto error_free_flow;
1077 rcu_assign_pointer(flow->sf_acts, acts);
1079 /* Put flow in bucket. */
1080 flow->hash = ovs_flow_hash(&key, key_len);
1081 ovs_flow_tbl_insert(table, flow);
1083 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1087 /* We found a matching flow. */
1088 struct sw_flow_actions *old_acts;
1089 struct nlattr *acts_attrs;
1091 /* Bail out if we're not allowed to modify an existing flow.
1092 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1093 * because Generic Netlink treats the latter as a dump
1094 * request. We also accept NLM_F_EXCL in case that bug ever
1098 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1099 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1102 /* Update actions. */
1103 old_acts = rcu_dereference_protected(flow->sf_acts,
1104 lockdep_genl_is_held());
1105 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1107 (old_acts->actions_len != nla_len(acts_attrs) ||
1108 memcmp(old_acts->actions, nla_data(acts_attrs),
1109 old_acts->actions_len))) {
1110 struct sw_flow_actions *new_acts;
1112 new_acts = ovs_flow_actions_alloc(acts_attrs);
1113 error = PTR_ERR(new_acts);
1114 if (IS_ERR(new_acts))
1117 rcu_assign_pointer(flow->sf_acts, new_acts);
1118 ovs_flow_deferred_free_acts(old_acts);
1121 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1122 info->snd_seq, OVS_FLOW_CMD_NEW);
1125 if (a[OVS_FLOW_ATTR_CLEAR]) {
1126 spin_lock_bh(&flow->lock);
1128 spin_unlock_bh(&flow->lock);
1133 genl_notify(reply, genl_info_net(info), info->snd_pid,
1134 ovs_dp_flow_multicast_group.id, info->nlhdr,
1137 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1138 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1147 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1149 struct nlattr **a = info->attrs;
1150 struct ovs_header *ovs_header = info->userhdr;
1151 struct sw_flow_key key;
1152 struct sk_buff *reply;
1153 struct sw_flow *flow;
1154 struct datapath *dp;
1155 struct flow_table *table;
1159 if (!a[OVS_FLOW_ATTR_KEY])
1161 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1165 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1169 table = genl_dereference(dp->table);
1170 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1174 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1175 info->snd_seq, OVS_FLOW_CMD_NEW);
1177 return PTR_ERR(reply);
1179 return genlmsg_reply(reply, info);
1182 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1184 struct nlattr **a = info->attrs;
1185 struct ovs_header *ovs_header = info->userhdr;
1186 struct sw_flow_key key;
1187 struct sk_buff *reply;
1188 struct sw_flow *flow;
1189 struct datapath *dp;
1190 struct flow_table *table;
1194 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1198 if (!a[OVS_FLOW_ATTR_KEY])
1199 return flush_flows(dp);
1201 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1205 table = genl_dereference(dp->table);
1206 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1210 reply = ovs_flow_cmd_alloc_info(flow);
1214 ovs_flow_tbl_remove(table, flow);
1216 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1217 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1220 ovs_flow_deferred_free(flow);
1222 genl_notify(reply, genl_info_net(info), info->snd_pid,
1223 ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1227 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1229 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1230 struct datapath *dp;
1231 struct flow_table *table;
1233 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1237 table = genl_dereference(dp->table);
1240 struct sw_flow *flow;
1243 bucket = cb->args[0];
1245 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1249 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1250 NETLINK_CB(cb->skb).pid,
1251 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1252 OVS_FLOW_CMD_NEW) < 0)
1255 cb->args[0] = bucket;
1261 static struct genl_ops dp_flow_genl_ops[] = {
1262 { .cmd = OVS_FLOW_CMD_NEW,
1263 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1264 .policy = flow_policy,
1265 .doit = ovs_flow_cmd_new_or_set
1267 { .cmd = OVS_FLOW_CMD_DEL,
1268 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1269 .policy = flow_policy,
1270 .doit = ovs_flow_cmd_del
1272 { .cmd = OVS_FLOW_CMD_GET,
1273 .flags = 0, /* OK for unprivileged users. */
1274 .policy = flow_policy,
1275 .doit = ovs_flow_cmd_get,
1276 .dumpit = ovs_flow_cmd_dump
1278 { .cmd = OVS_FLOW_CMD_SET,
1279 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1280 .policy = flow_policy,
1281 .doit = ovs_flow_cmd_new_or_set,
1285 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1286 #ifdef HAVE_NLA_NUL_STRING
1287 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1289 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1292 static struct genl_family dp_datapath_genl_family = {
1293 .id = GENL_ID_GENERATE,
1294 .hdrsize = sizeof(struct ovs_header),
1295 .name = OVS_DATAPATH_FAMILY,
1296 .version = OVS_DATAPATH_VERSION,
1297 .maxattr = OVS_DP_ATTR_MAX,
1301 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1302 .name = OVS_DATAPATH_MCGROUP
1305 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1306 u32 pid, u32 seq, u32 flags, u8 cmd)
1308 struct ovs_header *ovs_header;
1309 struct ovs_dp_stats dp_stats;
1312 ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1317 ovs_header->dp_ifindex = get_dpifindex(dp);
1320 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1323 goto nla_put_failure;
1325 get_dp_stats(dp, &dp_stats);
1326 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1327 goto nla_put_failure;
1329 return genlmsg_end(skb, ovs_header);
1332 genlmsg_cancel(skb, ovs_header);
1337 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1340 struct sk_buff *skb;
1343 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1345 return ERR_PTR(-ENOMEM);
1347 retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1350 return ERR_PTR(retval);
1355 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1357 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1360 /* Called with genl_mutex and optionally with RTNL lock also. */
1361 static struct datapath *lookup_datapath(struct net *net,
1362 struct ovs_header *ovs_header,
1363 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1365 struct datapath *dp;
1367 if (!a[OVS_DP_ATTR_NAME])
1368 dp = get_dp(net, ovs_header->dp_ifindex);
1370 struct vport *vport;
1373 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1374 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1377 return dp ? dp : ERR_PTR(-ENODEV);
1380 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1382 struct nlattr **a = info->attrs;
1383 struct vport_parms parms;
1384 struct sk_buff *reply;
1385 struct datapath *dp;
1386 struct vport *vport;
1387 struct ovs_net *ovs_net;
1391 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1394 err = ovs_dp_cmd_validate(a);
1401 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1403 goto err_unlock_rtnl;
1405 /* Initialize kobject for bridge. This will be added as
1406 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1407 dp->ifobj.kset = NULL;
1408 kobject_init(&dp->ifobj, &dp_ktype);
1410 /* Allocate table. */
1412 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1416 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1417 if (!dp->stats_percpu) {
1419 goto err_destroy_table;
1421 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1423 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1427 goto err_destroy_percpu;
1430 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1431 INIT_HLIST_HEAD(&dp->ports[i]);
1433 /* Set up our datapath device. */
1434 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1435 parms.type = OVS_VPORT_TYPE_INTERNAL;
1436 parms.options = NULL;
1438 parms.port_no = OVSP_LOCAL;
1439 parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1441 vport = new_vport(&parms);
1442 if (IS_ERR(vport)) {
1443 err = PTR_ERR(vport);
1447 goto err_destroy_ports_array;
1450 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1451 info->snd_seq, OVS_DP_CMD_NEW);
1452 err = PTR_ERR(reply);
1454 goto err_destroy_local_port;
1456 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1457 list_add_tail(&dp->list_node, &ovs_net->dps);
1458 ovs_dp_sysfs_add_dp(dp);
1462 genl_notify(reply, genl_info_net(info), info->snd_pid,
1463 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1467 err_destroy_local_port:
1468 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1469 err_destroy_ports_array:
1472 free_percpu(dp->stats_percpu);
1474 ovs_flow_tbl_destroy(genl_dereference(dp->table));
1483 /* Called with genl_mutex. */
1484 static void __dp_destroy(struct datapath *dp)
1490 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1491 struct vport *vport;
1492 struct hlist_node *node, *n;
1494 hlist_for_each_entry_safe(vport, node, n, &dp->ports[i], dp_hash_node)
1495 if (vport->port_no != OVSP_LOCAL)
1496 ovs_dp_detach_port(vport);
1499 ovs_dp_sysfs_del_dp(dp);
1500 list_del(&dp->list_node);
1501 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1503 /* rtnl_unlock() will wait until all the references to devices that
1504 * are pending unregistration have been dropped. We do it here to
1505 * ensure that any internal devices (which contain DP pointers) are
1506 * fully destroyed before freeing the datapath.
1510 call_rcu(&dp->rcu, destroy_dp_rcu);
1513 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1515 struct sk_buff *reply;
1516 struct datapath *dp;
1519 err = ovs_dp_cmd_validate(info->attrs);
1523 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1528 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1529 info->snd_seq, OVS_DP_CMD_DEL);
1530 err = PTR_ERR(reply);
1536 genl_notify(reply, genl_info_net(info), info->snd_pid,
1537 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1543 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1545 struct sk_buff *reply;
1546 struct datapath *dp;
1549 err = ovs_dp_cmd_validate(info->attrs);
1553 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1557 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1558 info->snd_seq, OVS_DP_CMD_NEW);
1559 if (IS_ERR(reply)) {
1560 err = PTR_ERR(reply);
1561 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1562 ovs_dp_datapath_multicast_group.id, err);
1566 genl_notify(reply, genl_info_net(info), info->snd_pid,
1567 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1573 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1575 struct sk_buff *reply;
1576 struct datapath *dp;
1579 err = ovs_dp_cmd_validate(info->attrs);
1583 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1587 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1588 info->snd_seq, OVS_DP_CMD_NEW);
1590 return PTR_ERR(reply);
1592 return genlmsg_reply(reply, info);
1595 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1597 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1598 struct datapath *dp;
1599 int skip = cb->args[0];
1602 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1604 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1605 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1606 OVS_DP_CMD_NEW) < 0)
1616 static struct genl_ops dp_datapath_genl_ops[] = {
1617 { .cmd = OVS_DP_CMD_NEW,
1618 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1619 .policy = datapath_policy,
1620 .doit = ovs_dp_cmd_new
1622 { .cmd = OVS_DP_CMD_DEL,
1623 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1624 .policy = datapath_policy,
1625 .doit = ovs_dp_cmd_del
1627 { .cmd = OVS_DP_CMD_GET,
1628 .flags = 0, /* OK for unprivileged users. */
1629 .policy = datapath_policy,
1630 .doit = ovs_dp_cmd_get,
1631 .dumpit = ovs_dp_cmd_dump
1633 { .cmd = OVS_DP_CMD_SET,
1634 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1635 .policy = datapath_policy,
1636 .doit = ovs_dp_cmd_set,
1640 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1641 #ifdef HAVE_NLA_NUL_STRING
1642 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1643 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1644 [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1646 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1647 [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1649 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1650 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1651 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1652 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1655 static struct genl_family dp_vport_genl_family = {
1656 .id = GENL_ID_GENERATE,
1657 .hdrsize = sizeof(struct ovs_header),
1658 .name = OVS_VPORT_FAMILY,
1659 .version = OVS_VPORT_VERSION,
1660 .maxattr = OVS_VPORT_ATTR_MAX,
1664 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1665 .name = OVS_VPORT_MCGROUP
1668 /* Called with RTNL lock or RCU read lock. */
1669 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1670 u32 pid, u32 seq, u32 flags, u8 cmd)
1672 struct ovs_header *ovs_header;
1673 struct ovs_vport_stats vport_stats;
1676 ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1681 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1683 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1684 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1685 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1686 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid))
1687 goto nla_put_failure;
1689 ovs_vport_get_stats(vport, &vport_stats);
1690 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1692 goto nla_put_failure;
1694 if (nla_put(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1695 vport->ops->get_addr(vport)))
1696 goto nla_put_failure;
1698 err = ovs_vport_get_options(vport, skb);
1699 if (err == -EMSGSIZE)
1702 return genlmsg_end(skb, ovs_header);
1707 genlmsg_cancel(skb, ovs_header);
1711 /* Called with RTNL lock or RCU read lock. */
1712 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1715 struct sk_buff *skb;
1718 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1720 return ERR_PTR(-ENOMEM);
1722 retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1725 return ERR_PTR(retval);
1730 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1732 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1735 /* Called with RTNL lock or RCU read lock. */
1736 static struct vport *lookup_vport(struct net *net,
1737 struct ovs_header *ovs_header,
1738 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1740 struct datapath *dp;
1741 struct vport *vport;
1743 if (a[OVS_VPORT_ATTR_NAME]) {
1744 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1746 return ERR_PTR(-ENODEV);
1747 if (ovs_header->dp_ifindex &&
1748 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1749 return ERR_PTR(-ENODEV);
1751 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1752 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1754 if (port_no >= DP_MAX_PORTS)
1755 return ERR_PTR(-EFBIG);
1757 dp = get_dp(net, ovs_header->dp_ifindex);
1759 return ERR_PTR(-ENODEV);
1761 vport = ovs_vport_rtnl_rcu(dp, port_no);
1763 return ERR_PTR(-ENOENT);
1766 return ERR_PTR(-EINVAL);
1769 /* Called with RTNL lock. */
1770 static int change_vport(struct vport *vport,
1771 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1775 if (a[OVS_VPORT_ATTR_STATS])
1776 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1778 if (a[OVS_VPORT_ATTR_ADDRESS])
1779 err = ovs_vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1784 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1786 struct nlattr **a = info->attrs;
1787 struct ovs_header *ovs_header = info->userhdr;
1788 struct vport_parms parms;
1789 struct sk_buff *reply;
1790 struct vport *vport;
1791 struct datapath *dp;
1796 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1797 !a[OVS_VPORT_ATTR_UPCALL_PID])
1800 err = ovs_vport_cmd_validate(a);
1805 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1810 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1811 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1814 if (port_no >= DP_MAX_PORTS)
1817 vport = ovs_vport_rtnl(dp, port_no);
1822 for (port_no = 1; ; port_no++) {
1823 if (port_no >= DP_MAX_PORTS) {
1827 vport = ovs_vport_rtnl(dp, port_no);
1833 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1834 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1835 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1837 parms.port_no = port_no;
1838 parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1840 vport = new_vport(&parms);
1841 err = PTR_ERR(vport);
1845 ovs_dp_sysfs_add_if(vport);
1847 err = change_vport(vport, a);
1849 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1853 err = PTR_ERR(reply);
1856 ovs_dp_detach_port(vport);
1859 genl_notify(reply, genl_info_net(info), info->snd_pid,
1860 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1868 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1870 struct nlattr **a = info->attrs;
1871 struct sk_buff *reply;
1872 struct vport *vport;
1875 err = ovs_vport_cmd_validate(a);
1880 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1881 err = PTR_ERR(vport);
1886 if (a[OVS_VPORT_ATTR_TYPE] &&
1887 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1890 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1891 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1893 err = change_vport(vport, a);
1896 if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1897 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1899 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1901 if (IS_ERR(reply)) {
1902 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1903 ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
1907 genl_notify(reply, genl_info_net(info), info->snd_pid,
1908 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1916 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1918 struct nlattr **a = info->attrs;
1919 struct sk_buff *reply;
1920 struct vport *vport;
1923 err = ovs_vport_cmd_validate(a);
1928 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1929 err = PTR_ERR(vport);
1933 if (vport->port_no == OVSP_LOCAL) {
1938 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1940 err = PTR_ERR(reply);
1944 ovs_dp_detach_port(vport);
1946 genl_notify(reply, genl_info_net(info), info->snd_pid,
1947 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1955 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1957 struct nlattr **a = info->attrs;
1958 struct ovs_header *ovs_header = info->userhdr;
1959 struct sk_buff *reply;
1960 struct vport *vport;
1963 err = ovs_vport_cmd_validate(a);
1968 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1969 err = PTR_ERR(vport);
1973 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1975 err = PTR_ERR(reply);
1981 return genlmsg_reply(reply, info);
1989 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1991 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1992 struct datapath *dp;
1993 int bucket = cb->args[0], skip = cb->args[1];
1996 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2001 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2002 struct vport *vport;
2003 struct hlist_node *n;
2006 hlist_for_each_entry_rcu(vport, n, &dp->ports[i], dp_hash_node) {
2008 ovs_vport_cmd_fill_info(vport, skb,
2009 NETLINK_CB(cb->skb).pid,
2012 OVS_VPORT_CMD_NEW) < 0)
2028 static struct genl_ops dp_vport_genl_ops[] = {
2029 { .cmd = OVS_VPORT_CMD_NEW,
2030 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2031 .policy = vport_policy,
2032 .doit = ovs_vport_cmd_new
2034 { .cmd = OVS_VPORT_CMD_DEL,
2035 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2036 .policy = vport_policy,
2037 .doit = ovs_vport_cmd_del
2039 { .cmd = OVS_VPORT_CMD_GET,
2040 .flags = 0, /* OK for unprivileged users. */
2041 .policy = vport_policy,
2042 .doit = ovs_vport_cmd_get,
2043 .dumpit = ovs_vport_cmd_dump
2045 { .cmd = OVS_VPORT_CMD_SET,
2046 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2047 .policy = vport_policy,
2048 .doit = ovs_vport_cmd_set,
2052 struct genl_family_and_ops {
2053 struct genl_family *family;
2054 struct genl_ops *ops;
2056 struct genl_multicast_group *group;
2059 static const struct genl_family_and_ops dp_genl_families[] = {
2060 { &dp_datapath_genl_family,
2061 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2062 &ovs_dp_datapath_multicast_group },
2063 { &dp_vport_genl_family,
2064 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2065 &ovs_dp_vport_multicast_group },
2066 { &dp_flow_genl_family,
2067 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2068 &ovs_dp_flow_multicast_group },
2069 { &dp_packet_genl_family,
2070 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2074 static void dp_unregister_genl(int n_families)
2078 for (i = 0; i < n_families; i++)
2079 genl_unregister_family(dp_genl_families[i].family);
2082 static int dp_register_genl(void)
2089 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2090 const struct genl_family_and_ops *f = &dp_genl_families[i];
2092 err = genl_register_family_with_ops(f->family, f->ops,
2099 err = genl_register_mc_group(f->family, f->group);
2108 dp_unregister_genl(n_registered);
2112 static int __rehash_flow_table(void *dummy)
2114 struct datapath *dp;
2119 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2121 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2122 struct flow_table *old_table = genl_dereference(dp->table);
2123 struct flow_table *new_table;
2125 new_table = ovs_flow_tbl_rehash(old_table);
2126 if (!IS_ERR(new_table)) {
2127 rcu_assign_pointer(dp->table, new_table);
2128 ovs_flow_tbl_deferred_destroy(old_table);
2136 static void rehash_flow_table(struct work_struct *work)
2138 genl_exec(__rehash_flow_table, NULL);
2139 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2142 static int dp_destroy_all(void *data)
2144 struct datapath *dp, *dp_next;
2145 struct ovs_net *ovs_net = data;
2147 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2153 static int __net_init ovs_init_net(struct net *net)
2155 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2157 INIT_LIST_HEAD(&ovs_net->dps);
2161 static void __net_exit ovs_exit_net(struct net *net)
2163 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2165 genl_exec(dp_destroy_all, ovs_net);
2168 static struct pernet_operations ovs_net_ops = {
2169 .init = ovs_init_net,
2170 .exit = ovs_exit_net,
2172 .size = sizeof(struct ovs_net),
2175 static int __init dp_init(void)
2177 struct sk_buff *dummy_skb;
2180 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2182 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2185 err = genl_exec_init();
2189 err = ovs_workqueues_init();
2191 goto error_genl_exec;
2193 err = ovs_tnl_init();
2197 err = ovs_flow_init();
2199 goto error_tnl_exit;
2201 err = ovs_vport_init();
2203 goto error_flow_exit;
2205 err = register_pernet_device(&ovs_net_ops);
2207 goto error_vport_exit;
2209 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2211 goto error_netns_exit;
2213 err = dp_register_genl();
2215 goto error_unreg_notifier;
2217 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2221 error_unreg_notifier:
2222 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2224 unregister_pernet_device(&ovs_net_ops);
2232 ovs_workqueues_exit();
2239 static void dp_cleanup(void)
2241 cancel_delayed_work_sync(&rehash_flow_wq);
2242 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2243 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2244 unregister_pernet_device(&ovs_net_ops);
2249 ovs_workqueues_exit();
2253 module_init(dp_init);
2254 module_exit(dp_cleanup);
2256 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2257 MODULE_LICENSE("GPL");