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/system.h>
43 #include <asm/div64.h>
44 #include <linux/highmem.h>
45 #include <linux/netfilter_bridge.h>
46 #include <linux/netfilter_ipv4.h>
47 #include <linux/inetdevice.h>
48 #include <linux/list.h>
49 #include <linux/openvswitch.h>
50 #include <linux/rculist.h>
51 #include <linux/dmi.h>
52 #include <net/genetlink.h>
53 #include <net/net_namespace.h>
54 #include <net/netns/generic.h>
59 #include "genl_exec.h"
62 #include "vport-internal_dev.h"
64 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
65 LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
66 #error Kernels before 2.6.18 or after 3.3 are not supported by this version of Open vSwitch.
69 #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
70 static void rehash_flow_table(struct work_struct *work);
71 static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
73 int ovs_net_id __read_mostly;
75 int (*ovs_dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
76 EXPORT_SYMBOL(ovs_dp_ioctl_hook);
81 * Writes to device state (add/remove datapath, port, set operations on vports,
82 * etc.) are protected by RTNL.
84 * Writes to other state (flow table modifications, set miscellaneous datapath
85 * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
88 * Reads are protected by RCU.
90 * There are a few special cases (mostly stats) that have their own
91 * synchronization but they nest under all of above and don't interact with
95 static struct vport *new_vport(const struct vport_parms *);
96 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
97 const struct dp_upcall_info *);
98 static int queue_userspace_packet(struct net *, int dp_ifindex,
100 const struct dp_upcall_info *);
102 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
103 static struct datapath *get_dp(struct net *net, int dp_ifindex)
105 struct datapath *dp = NULL;
106 struct net_device *dev;
109 dev = dev_get_by_index_rcu(net, dp_ifindex);
111 struct vport *vport = ovs_internal_dev_get_vport(dev);
120 /* Must be called with rcu_read_lock or RTNL lock. */
121 const char *ovs_dp_name(const struct datapath *dp)
123 struct vport *vport = ovs_vport_rtnl_rcu(dp, OVSP_LOCAL);
124 return vport->ops->get_name(vport);
127 static int get_dpifindex(struct datapath *dp)
134 local = ovs_vport_rcu(dp, OVSP_LOCAL);
136 ifindex = local->ops->get_ifindex(local);
145 static size_t br_nlmsg_size(void)
147 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
148 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
149 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
150 + nla_total_size(4) /* IFLA_MASTER */
151 + nla_total_size(4) /* IFLA_MTU */
152 + nla_total_size(1); /* IFLA_OPERSTATE */
155 /* Caller must hold RTNL lock. */
156 static int dp_fill_ifinfo(struct sk_buff *skb,
157 const struct vport *port,
158 int event, unsigned int flags)
160 struct datapath *dp = port->dp;
161 struct ifinfomsg *hdr;
162 struct nlmsghdr *nlh;
164 if (!port->ops->get_ifindex)
167 nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
171 hdr = nlmsg_data(nlh);
172 hdr->ifi_family = AF_BRIDGE;
174 hdr->ifi_type = ARPHRD_ETHER;
175 hdr->ifi_index = port->ops->get_ifindex(port);
176 hdr->ifi_flags = port->ops->get_dev_flags(port);
179 if (nla_put_string(skb, IFLA_IFNAME, port->ops->get_name(port)) ||
180 nla_put_u32(skb, IFLA_MASTER, get_dpifindex(dp)) ||
181 nla_put_u32(skb, IFLA_MTU, port->ops->get_mtu(port)) ||
182 #ifdef IFLA_OPERSTATE
183 nla_put_u8(skb, IFLA_OPERSTATE,
184 port->ops->is_running(port) ?
185 port->ops->get_operstate(port) :
188 nla_put(skb, IFLA_ADDRESS, ETH_ALEN, port->ops->get_addr(port)))
189 goto nla_put_failure;
191 return nlmsg_end(skb, nlh);
194 nlmsg_cancel(skb, nlh);
198 /* Caller must hold RTNL lock. */
199 static void dp_ifinfo_notify(int event, struct vport *port)
204 skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
210 err = dp_fill_ifinfo(skb, port, event, 0);
212 if (err == -ENODEV) {
215 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
216 WARN_ON(err == -EMSGSIZE);
221 rtnl_notify(skb, ovs_dp_get_net(port->dp), 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
225 rtnl_set_sk_err(ovs_dp_get_net(port->dp), RTNLGRP_LINK, err);
230 static void release_dp(struct kobject *kobj)
232 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
236 static struct kobj_type dp_ktype = {
237 .release = release_dp
240 static void destroy_dp_rcu(struct rcu_head *rcu)
242 struct datapath *dp = container_of(rcu, struct datapath, rcu);
244 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
245 free_percpu(dp->stats_percpu);
246 release_net(ovs_dp_get_net(dp));
248 kobject_put(&dp->ifobj);
251 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
254 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
257 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
260 struct hlist_node *n;
261 struct hlist_head *head;
263 head = vport_hash_bucket(dp, port_no);
264 hlist_for_each_entry_rcu(vport, n, head, dp_hash_node) {
265 if (vport->port_no == port_no)
271 /* Called with RTNL lock and genl_lock. */
272 static struct vport *new_vport(const struct vport_parms *parms)
276 vport = ovs_vport_add(parms);
277 if (!IS_ERR(vport)) {
278 struct datapath *dp = parms->dp;
279 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
281 hlist_add_head_rcu(&vport->dp_hash_node, head);
282 dp_ifinfo_notify(RTM_NEWLINK, vport);
287 /* Called with RTNL lock. */
288 void ovs_dp_detach_port(struct vport *p)
292 if (p->port_no != OVSP_LOCAL)
293 ovs_dp_sysfs_del_if(p);
295 dp_ifinfo_notify(RTM_DELLINK, p);
297 /* First drop references to device. */
298 hlist_del_rcu(&p->dp_hash_node);
300 /* Then destroy it. */
304 /* Must be called with rcu_read_lock. */
305 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
307 struct datapath *dp = p->dp;
308 struct sw_flow *flow;
309 struct dp_stats_percpu *stats;
313 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
315 if (!OVS_CB(skb)->flow) {
316 struct sw_flow_key key;
319 /* Extract flow from 'skb' into 'key'. */
320 error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
321 if (unlikely(error)) {
327 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table),
329 if (unlikely(!flow)) {
330 struct dp_upcall_info upcall;
332 upcall.cmd = OVS_PACKET_CMD_MISS;
334 upcall.userdata = NULL;
335 upcall.pid = p->upcall_pid;
336 ovs_dp_upcall(dp, skb, &upcall);
338 stats_counter = &stats->n_missed;
342 OVS_CB(skb)->flow = flow;
345 stats_counter = &stats->n_hit;
346 ovs_flow_used(OVS_CB(skb)->flow, skb);
347 ovs_execute_actions(dp, skb);
350 /* Update datapath statistics. */
351 u64_stats_update_begin(&stats->sync);
353 u64_stats_update_end(&stats->sync);
356 static struct genl_family dp_packet_genl_family = {
357 .id = GENL_ID_GENERATE,
358 .hdrsize = sizeof(struct ovs_header),
359 .name = OVS_PACKET_FAMILY,
360 .version = OVS_PACKET_VERSION,
361 .maxattr = OVS_PACKET_ATTR_MAX,
365 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
366 const struct dp_upcall_info *upcall_info)
368 struct dp_stats_percpu *stats;
372 if (upcall_info->pid == 0) {
377 dp_ifindex = get_dpifindex(dp);
383 forward_ip_summed(skb, true);
385 if (!skb_is_gso(skb))
386 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
388 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
395 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
397 u64_stats_update_begin(&stats->sync);
399 u64_stats_update_end(&stats->sync);
404 static int queue_gso_packets(struct net *net, int dp_ifindex,
406 const struct dp_upcall_info *upcall_info)
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);
417 /* Queue all of the segments. */
420 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
424 if (skb == segs && skb_shinfo(skb)->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_set(const struct nlattr *a,
562 const struct sw_flow_key *flow_key)
564 const struct nlattr *ovs_key = nla_data(a);
565 int key_type = nla_type(ovs_key);
567 /* There can be only one key in a action */
568 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
571 if (key_type > OVS_KEY_ATTR_MAX ||
572 nla_len(ovs_key) != ovs_key_lens[key_type])
576 const struct ovs_key_ipv4 *ipv4_key;
578 case OVS_KEY_ATTR_PRIORITY:
579 case OVS_KEY_ATTR_TUN_ID:
580 case OVS_KEY_ATTR_ETHERNET:
583 case OVS_KEY_ATTR_IPV4:
584 if (flow_key->eth.type != htons(ETH_P_IP))
587 if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
590 ipv4_key = nla_data(ovs_key);
591 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
594 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
599 case OVS_KEY_ATTR_TCP:
600 if (flow_key->ip.proto != IPPROTO_TCP)
603 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
608 case OVS_KEY_ATTR_UDP:
609 if (flow_key->ip.proto != IPPROTO_UDP)
612 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
623 static int validate_userspace(const struct nlattr *attr)
625 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
626 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
627 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
629 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
632 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
633 attr, userspace_policy);
637 if (!a[OVS_USERSPACE_ATTR_PID] ||
638 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
644 static int validate_actions(const struct nlattr *attr,
645 const struct sw_flow_key *key, int depth)
647 const struct nlattr *a;
650 if (depth >= SAMPLE_ACTION_DEPTH)
653 nla_for_each_nested(a, attr, rem) {
654 /* Expected argument lengths, (u32)-1 for variable length. */
655 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
656 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
657 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
658 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
659 [OVS_ACTION_ATTR_POP_VLAN] = 0,
660 [OVS_ACTION_ATTR_SET] = (u32)-1,
661 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
663 const struct ovs_action_push_vlan *vlan;
664 int type = nla_type(a);
666 if (type > OVS_ACTION_ATTR_MAX ||
667 (action_lens[type] != nla_len(a) &&
668 action_lens[type] != (u32)-1))
672 case OVS_ACTION_ATTR_UNSPEC:
675 case OVS_ACTION_ATTR_USERSPACE:
676 err = validate_userspace(a);
681 case OVS_ACTION_ATTR_OUTPUT:
682 if (nla_get_u32(a) >= DP_MAX_PORTS)
687 case OVS_ACTION_ATTR_POP_VLAN:
690 case OVS_ACTION_ATTR_PUSH_VLAN:
692 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
694 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
698 case OVS_ACTION_ATTR_SET:
699 err = validate_set(a, key);
704 case OVS_ACTION_ATTR_SAMPLE:
705 err = validate_sample(a, key, depth);
721 static void clear_stats(struct sw_flow *flow)
725 flow->packet_count = 0;
726 flow->byte_count = 0;
729 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
731 struct ovs_header *ovs_header = info->userhdr;
732 struct nlattr **a = info->attrs;
733 struct sw_flow_actions *acts;
734 struct sk_buff *packet;
735 struct sw_flow *flow;
743 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
744 !a[OVS_PACKET_ATTR_ACTIONS] ||
745 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
748 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
749 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
753 skb_reserve(packet, NET_IP_ALIGN);
755 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
757 skb_reset_mac_header(packet);
758 eth = eth_hdr(packet);
760 /* Normally, setting the skb 'protocol' field would be handled by a
761 * call to eth_type_trans(), but it assumes there's a sending
762 * device, which we may not have. */
763 if (ntohs(eth->h_proto) >= 1536)
764 packet->protocol = eth->h_proto;
766 packet->protocol = htons(ETH_P_802_2);
768 /* Build an sw_flow for sending this packet. */
769 flow = ovs_flow_alloc();
774 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
778 err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
779 &flow->key.phy.in_port,
780 &flow->key.phy.tun_id,
781 a[OVS_PACKET_ATTR_KEY]);
785 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
789 flow->hash = ovs_flow_hash(&flow->key, key_len);
791 acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
795 rcu_assign_pointer(flow->sf_acts, acts);
797 OVS_CB(packet)->flow = flow;
798 packet->priority = flow->key.phy.priority;
801 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
807 err = ovs_execute_actions(dp, packet);
824 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
825 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
826 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
827 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
830 static struct genl_ops dp_packet_genl_ops[] = {
831 { .cmd = OVS_PACKET_CMD_EXECUTE,
832 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
833 .policy = packet_policy,
834 .doit = ovs_packet_cmd_execute
838 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
841 struct flow_table *table = genl_dereference(dp->table);
843 stats->n_flows = ovs_flow_tbl_count(table);
845 stats->n_hit = stats->n_missed = stats->n_lost = 0;
846 for_each_possible_cpu(i) {
847 const struct dp_stats_percpu *percpu_stats;
848 struct dp_stats_percpu local_stats;
851 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
854 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
855 local_stats = *percpu_stats;
856 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
858 stats->n_hit += local_stats.n_hit;
859 stats->n_missed += local_stats.n_missed;
860 stats->n_lost += local_stats.n_lost;
864 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
865 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
866 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
867 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
870 static struct genl_family dp_flow_genl_family = {
871 .id = GENL_ID_GENERATE,
872 .hdrsize = sizeof(struct ovs_header),
873 .name = OVS_FLOW_FAMILY,
874 .version = OVS_FLOW_VERSION,
875 .maxattr = OVS_FLOW_ATTR_MAX,
879 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
880 .name = OVS_FLOW_MCGROUP
883 /* Called with genl_lock. */
884 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
885 struct sk_buff *skb, u32 pid,
886 u32 seq, u32 flags, u8 cmd)
888 const int skb_orig_len = skb->len;
889 const struct sw_flow_actions *sf_acts;
890 struct ovs_flow_stats stats;
891 struct ovs_header *ovs_header;
897 sf_acts = rcu_dereference_protected(flow->sf_acts,
898 lockdep_genl_is_held());
900 ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
904 ovs_header->dp_ifindex = get_dpifindex(dp);
906 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
908 goto nla_put_failure;
909 err = ovs_flow_to_nlattrs(&flow->key, skb);
912 nla_nest_end(skb, nla);
914 spin_lock_bh(&flow->lock);
916 stats.n_packets = flow->packet_count;
917 stats.n_bytes = flow->byte_count;
918 tcp_flags = flow->tcp_flags;
919 spin_unlock_bh(&flow->lock);
922 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
923 goto nla_put_failure;
925 if (stats.n_packets &&
926 nla_put(skb, OVS_FLOW_ATTR_STATS,
927 sizeof(struct ovs_flow_stats), &stats))
928 goto nla_put_failure;
931 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
932 goto nla_put_failure;
934 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
935 * this is the first flow to be dumped into 'skb'. This is unusual for
936 * Netlink but individual action lists can be longer than
937 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
938 * The userspace caller can always fetch the actions separately if it
939 * really wants them. (Most userspace callers in fact don't care.)
941 * This can only fail for dump operations because the skb is always
942 * properly sized for single flows.
944 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
946 if (err < 0 && skb_orig_len)
949 return genlmsg_end(skb, ovs_header);
954 genlmsg_cancel(skb, ovs_header);
958 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
960 const struct sw_flow_actions *sf_acts;
963 sf_acts = rcu_dereference_protected(flow->sf_acts,
964 lockdep_genl_is_held());
966 /* OVS_FLOW_ATTR_KEY */
967 len = nla_total_size(FLOW_BUFSIZE);
968 /* OVS_FLOW_ATTR_ACTIONS */
969 len += nla_total_size(sf_acts->actions_len);
970 /* OVS_FLOW_ATTR_STATS */
971 len += nla_total_size(sizeof(struct ovs_flow_stats));
972 /* OVS_FLOW_ATTR_TCP_FLAGS */
973 len += nla_total_size(1);
974 /* OVS_FLOW_ATTR_USED */
975 len += nla_total_size(8);
977 len += NLMSG_ALIGN(sizeof(struct ovs_header));
979 return genlmsg_new(len, GFP_KERNEL);
982 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
984 u32 pid, u32 seq, u8 cmd)
989 skb = ovs_flow_cmd_alloc_info(flow);
991 return ERR_PTR(-ENOMEM);
993 retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
998 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1000 struct nlattr **a = info->attrs;
1001 struct ovs_header *ovs_header = info->userhdr;
1002 struct sw_flow_key key;
1003 struct sw_flow *flow;
1004 struct sk_buff *reply;
1005 struct datapath *dp;
1006 struct flow_table *table;
1012 if (!a[OVS_FLOW_ATTR_KEY])
1014 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1018 /* Validate actions. */
1019 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1020 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
1023 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1028 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1033 table = genl_dereference(dp->table);
1034 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1036 struct sw_flow_actions *acts;
1038 /* Bail out if we're not allowed to create a new flow. */
1040 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1043 /* Expand table, if necessary, to make room. */
1044 if (ovs_flow_tbl_need_to_expand(table)) {
1045 struct flow_table *new_table;
1047 new_table = ovs_flow_tbl_expand(table);
1048 if (!IS_ERR(new_table)) {
1049 rcu_assign_pointer(dp->table, new_table);
1050 ovs_flow_tbl_deferred_destroy(table);
1051 table = genl_dereference(dp->table);
1055 /* Allocate flow. */
1056 flow = ovs_flow_alloc();
1058 error = PTR_ERR(flow);
1064 /* Obtain actions. */
1065 acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1066 error = PTR_ERR(acts);
1068 goto error_free_flow;
1069 rcu_assign_pointer(flow->sf_acts, acts);
1071 /* Put flow in bucket. */
1072 flow->hash = ovs_flow_hash(&key, key_len);
1073 ovs_flow_tbl_insert(table, flow);
1075 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1079 /* We found a matching flow. */
1080 struct sw_flow_actions *old_acts;
1081 struct nlattr *acts_attrs;
1083 /* Bail out if we're not allowed to modify an existing flow.
1084 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1085 * because Generic Netlink treats the latter as a dump
1086 * request. We also accept NLM_F_EXCL in case that bug ever
1090 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1091 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1094 /* Update actions. */
1095 old_acts = rcu_dereference_protected(flow->sf_acts,
1096 lockdep_genl_is_held());
1097 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1099 (old_acts->actions_len != nla_len(acts_attrs) ||
1100 memcmp(old_acts->actions, nla_data(acts_attrs),
1101 old_acts->actions_len))) {
1102 struct sw_flow_actions *new_acts;
1104 new_acts = ovs_flow_actions_alloc(acts_attrs);
1105 error = PTR_ERR(new_acts);
1106 if (IS_ERR(new_acts))
1109 rcu_assign_pointer(flow->sf_acts, new_acts);
1110 ovs_flow_deferred_free_acts(old_acts);
1113 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1114 info->snd_seq, OVS_FLOW_CMD_NEW);
1117 if (a[OVS_FLOW_ATTR_CLEAR]) {
1118 spin_lock_bh(&flow->lock);
1120 spin_unlock_bh(&flow->lock);
1125 genl_notify(reply, genl_info_net(info), info->snd_pid,
1126 ovs_dp_flow_multicast_group.id, info->nlhdr,
1129 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1130 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1139 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1141 struct nlattr **a = info->attrs;
1142 struct ovs_header *ovs_header = info->userhdr;
1143 struct sw_flow_key key;
1144 struct sk_buff *reply;
1145 struct sw_flow *flow;
1146 struct datapath *dp;
1147 struct flow_table *table;
1151 if (!a[OVS_FLOW_ATTR_KEY])
1153 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1157 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1161 table = genl_dereference(dp->table);
1162 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1166 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1167 info->snd_seq, OVS_FLOW_CMD_NEW);
1169 return PTR_ERR(reply);
1171 return genlmsg_reply(reply, info);
1174 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1176 struct nlattr **a = info->attrs;
1177 struct ovs_header *ovs_header = info->userhdr;
1178 struct sw_flow_key key;
1179 struct sk_buff *reply;
1180 struct sw_flow *flow;
1181 struct datapath *dp;
1182 struct flow_table *table;
1186 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1190 if (!a[OVS_FLOW_ATTR_KEY])
1191 return flush_flows(dp);
1193 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1197 table = genl_dereference(dp->table);
1198 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1202 reply = ovs_flow_cmd_alloc_info(flow);
1206 ovs_flow_tbl_remove(table, flow);
1208 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1209 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1212 ovs_flow_deferred_free(flow);
1214 genl_notify(reply, genl_info_net(info), info->snd_pid,
1215 ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1219 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1221 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1222 struct datapath *dp;
1223 struct flow_table *table;
1225 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1229 table = genl_dereference(dp->table);
1232 struct sw_flow *flow;
1235 bucket = cb->args[0];
1237 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1241 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1242 NETLINK_CB(cb->skb).pid,
1243 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1244 OVS_FLOW_CMD_NEW) < 0)
1247 cb->args[0] = bucket;
1253 static struct genl_ops dp_flow_genl_ops[] = {
1254 { .cmd = OVS_FLOW_CMD_NEW,
1255 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1256 .policy = flow_policy,
1257 .doit = ovs_flow_cmd_new_or_set
1259 { .cmd = OVS_FLOW_CMD_DEL,
1260 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1261 .policy = flow_policy,
1262 .doit = ovs_flow_cmd_del
1264 { .cmd = OVS_FLOW_CMD_GET,
1265 .flags = 0, /* OK for unprivileged users. */
1266 .policy = flow_policy,
1267 .doit = ovs_flow_cmd_get,
1268 .dumpit = ovs_flow_cmd_dump
1270 { .cmd = OVS_FLOW_CMD_SET,
1271 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1272 .policy = flow_policy,
1273 .doit = ovs_flow_cmd_new_or_set,
1277 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1278 #ifdef HAVE_NLA_NUL_STRING
1279 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1281 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1284 static struct genl_family dp_datapath_genl_family = {
1285 .id = GENL_ID_GENERATE,
1286 .hdrsize = sizeof(struct ovs_header),
1287 .name = OVS_DATAPATH_FAMILY,
1288 .version = OVS_DATAPATH_VERSION,
1289 .maxattr = OVS_DP_ATTR_MAX,
1293 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1294 .name = OVS_DATAPATH_MCGROUP
1297 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1298 u32 pid, u32 seq, u32 flags, u8 cmd)
1300 struct ovs_header *ovs_header;
1301 struct ovs_dp_stats dp_stats;
1304 ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1309 ovs_header->dp_ifindex = get_dpifindex(dp);
1312 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1315 goto nla_put_failure;
1317 get_dp_stats(dp, &dp_stats);
1318 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1319 goto nla_put_failure;
1321 return genlmsg_end(skb, ovs_header);
1324 genlmsg_cancel(skb, ovs_header);
1329 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1332 struct sk_buff *skb;
1335 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1337 return ERR_PTR(-ENOMEM);
1339 retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1342 return ERR_PTR(retval);
1347 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1349 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1352 /* Called with genl_mutex and optionally with RTNL lock also. */
1353 static struct datapath *lookup_datapath(struct net *net,
1354 struct ovs_header *ovs_header,
1355 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1357 struct datapath *dp;
1359 if (!a[OVS_DP_ATTR_NAME])
1360 dp = get_dp(net, ovs_header->dp_ifindex);
1362 struct vport *vport;
1365 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1366 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1369 return dp ? dp : ERR_PTR(-ENODEV);
1372 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1374 struct nlattr **a = info->attrs;
1375 struct vport_parms parms;
1376 struct sk_buff *reply;
1377 struct datapath *dp;
1378 struct vport *vport;
1379 struct ovs_net *ovs_net;
1383 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1386 err = ovs_dp_cmd_validate(a);
1393 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1395 goto err_unlock_rtnl;
1397 /* Initialize kobject for bridge. This will be added as
1398 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1399 dp->ifobj.kset = NULL;
1400 kobject_init(&dp->ifobj, &dp_ktype);
1402 /* Allocate table. */
1404 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1408 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1409 if (!dp->stats_percpu) {
1411 goto err_destroy_table;
1413 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1415 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1419 goto err_destroy_percpu;
1422 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1423 INIT_HLIST_HEAD(&dp->ports[i]);
1425 /* Set up our datapath device. */
1426 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1427 parms.type = OVS_VPORT_TYPE_INTERNAL;
1428 parms.options = NULL;
1430 parms.port_no = OVSP_LOCAL;
1431 parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1433 vport = new_vport(&parms);
1434 if (IS_ERR(vport)) {
1435 err = PTR_ERR(vport);
1439 goto err_destroy_ports_array;
1442 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1443 info->snd_seq, OVS_DP_CMD_NEW);
1444 err = PTR_ERR(reply);
1446 goto err_destroy_local_port;
1448 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1449 list_add_tail(&dp->list_node, &ovs_net->dps);
1450 ovs_dp_sysfs_add_dp(dp);
1454 genl_notify(reply, genl_info_net(info), info->snd_pid,
1455 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1459 err_destroy_local_port:
1460 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1461 err_destroy_ports_array:
1464 free_percpu(dp->stats_percpu);
1466 ovs_flow_tbl_destroy(genl_dereference(dp->table));
1475 /* Called with genl_mutex. */
1476 static void __dp_destroy(struct datapath *dp)
1482 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1483 struct vport *vport;
1484 struct hlist_node *node, *n;
1486 hlist_for_each_entry_safe(vport, node, n, &dp->ports[i], dp_hash_node)
1487 if (vport->port_no != OVSP_LOCAL)
1488 ovs_dp_detach_port(vport);
1491 ovs_dp_sysfs_del_dp(dp);
1492 list_del(&dp->list_node);
1493 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1495 /* rtnl_unlock() will wait until all the references to devices that
1496 * are pending unregistration have been dropped. We do it here to
1497 * ensure that any internal devices (which contain DP pointers) are
1498 * fully destroyed before freeing the datapath.
1502 call_rcu(&dp->rcu, destroy_dp_rcu);
1505 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1507 struct sk_buff *reply;
1508 struct datapath *dp;
1511 err = ovs_dp_cmd_validate(info->attrs);
1515 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1520 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1521 info->snd_seq, OVS_DP_CMD_DEL);
1522 err = PTR_ERR(reply);
1528 genl_notify(reply, genl_info_net(info), info->snd_pid,
1529 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1535 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1537 struct sk_buff *reply;
1538 struct datapath *dp;
1541 err = ovs_dp_cmd_validate(info->attrs);
1545 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1549 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1550 info->snd_seq, OVS_DP_CMD_NEW);
1551 if (IS_ERR(reply)) {
1552 err = PTR_ERR(reply);
1553 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1554 ovs_dp_datapath_multicast_group.id, err);
1558 genl_notify(reply, genl_info_net(info), info->snd_pid,
1559 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1565 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1567 struct sk_buff *reply;
1568 struct datapath *dp;
1571 err = ovs_dp_cmd_validate(info->attrs);
1575 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1579 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1580 info->snd_seq, OVS_DP_CMD_NEW);
1582 return PTR_ERR(reply);
1584 return genlmsg_reply(reply, info);
1587 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1589 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1590 struct datapath *dp;
1591 int skip = cb->args[0];
1594 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1596 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1597 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1598 OVS_DP_CMD_NEW) < 0)
1608 static struct genl_ops dp_datapath_genl_ops[] = {
1609 { .cmd = OVS_DP_CMD_NEW,
1610 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1611 .policy = datapath_policy,
1612 .doit = ovs_dp_cmd_new
1614 { .cmd = OVS_DP_CMD_DEL,
1615 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1616 .policy = datapath_policy,
1617 .doit = ovs_dp_cmd_del
1619 { .cmd = OVS_DP_CMD_GET,
1620 .flags = 0, /* OK for unprivileged users. */
1621 .policy = datapath_policy,
1622 .doit = ovs_dp_cmd_get,
1623 .dumpit = ovs_dp_cmd_dump
1625 { .cmd = OVS_DP_CMD_SET,
1626 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1627 .policy = datapath_policy,
1628 .doit = ovs_dp_cmd_set,
1632 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1633 #ifdef HAVE_NLA_NUL_STRING
1634 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1635 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1636 [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1638 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1639 [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1641 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1642 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1643 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1644 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1647 static struct genl_family dp_vport_genl_family = {
1648 .id = GENL_ID_GENERATE,
1649 .hdrsize = sizeof(struct ovs_header),
1650 .name = OVS_VPORT_FAMILY,
1651 .version = OVS_VPORT_VERSION,
1652 .maxattr = OVS_VPORT_ATTR_MAX,
1656 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1657 .name = OVS_VPORT_MCGROUP
1660 /* Called with RTNL lock or RCU read lock. */
1661 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1662 u32 pid, u32 seq, u32 flags, u8 cmd)
1664 struct ovs_header *ovs_header;
1665 struct ovs_vport_stats vport_stats;
1668 ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1673 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1675 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1676 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1677 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1678 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid))
1679 goto nla_put_failure;
1681 ovs_vport_get_stats(vport, &vport_stats);
1682 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1684 goto nla_put_failure;
1686 if (nla_put(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1687 vport->ops->get_addr(vport)))
1688 goto nla_put_failure;
1690 err = ovs_vport_get_options(vport, skb);
1691 if (err == -EMSGSIZE)
1694 return genlmsg_end(skb, ovs_header);
1699 genlmsg_cancel(skb, ovs_header);
1703 /* Called with RTNL lock or RCU read lock. */
1704 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1707 struct sk_buff *skb;
1710 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1712 return ERR_PTR(-ENOMEM);
1714 retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1717 return ERR_PTR(retval);
1722 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1724 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1727 /* Called with RTNL lock or RCU read lock. */
1728 static struct vport *lookup_vport(struct net *net,
1729 struct ovs_header *ovs_header,
1730 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1732 struct datapath *dp;
1733 struct vport *vport;
1735 if (a[OVS_VPORT_ATTR_NAME]) {
1736 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1738 return ERR_PTR(-ENODEV);
1739 if (ovs_header->dp_ifindex &&
1740 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1741 return ERR_PTR(-ENODEV);
1743 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1744 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1746 if (port_no >= DP_MAX_PORTS)
1747 return ERR_PTR(-EFBIG);
1749 dp = get_dp(net, ovs_header->dp_ifindex);
1751 return ERR_PTR(-ENODEV);
1753 vport = ovs_vport_rtnl_rcu(dp, port_no);
1755 return ERR_PTR(-ENOENT);
1758 return ERR_PTR(-EINVAL);
1761 /* Called with RTNL lock. */
1762 static int change_vport(struct vport *vport,
1763 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1767 if (a[OVS_VPORT_ATTR_STATS])
1768 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1770 if (a[OVS_VPORT_ATTR_ADDRESS])
1771 err = ovs_vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1776 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1778 struct nlattr **a = info->attrs;
1779 struct ovs_header *ovs_header = info->userhdr;
1780 struct vport_parms parms;
1781 struct sk_buff *reply;
1782 struct vport *vport;
1783 struct datapath *dp;
1788 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1789 !a[OVS_VPORT_ATTR_UPCALL_PID])
1792 err = ovs_vport_cmd_validate(a);
1797 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1802 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1803 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1806 if (port_no >= DP_MAX_PORTS)
1809 vport = ovs_vport_rtnl(dp, port_no);
1814 for (port_no = 1; ; port_no++) {
1815 if (port_no >= DP_MAX_PORTS) {
1819 vport = ovs_vport_rtnl(dp, port_no);
1825 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1826 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1827 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1829 parms.port_no = port_no;
1830 parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1832 vport = new_vport(&parms);
1833 err = PTR_ERR(vport);
1837 ovs_dp_sysfs_add_if(vport);
1839 err = change_vport(vport, a);
1841 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1845 err = PTR_ERR(reply);
1848 ovs_dp_detach_port(vport);
1851 genl_notify(reply, genl_info_net(info), info->snd_pid,
1852 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1860 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1862 struct nlattr **a = info->attrs;
1863 struct sk_buff *reply;
1864 struct vport *vport;
1867 err = ovs_vport_cmd_validate(a);
1872 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1873 err = PTR_ERR(vport);
1878 if (a[OVS_VPORT_ATTR_TYPE] &&
1879 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1882 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1883 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1885 err = change_vport(vport, a);
1888 if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1889 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1891 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1893 if (IS_ERR(reply)) {
1894 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1895 ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
1899 genl_notify(reply, genl_info_net(info), info->snd_pid,
1900 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1908 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1910 struct nlattr **a = info->attrs;
1911 struct sk_buff *reply;
1912 struct vport *vport;
1915 err = ovs_vport_cmd_validate(a);
1920 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1921 err = PTR_ERR(vport);
1925 if (vport->port_no == OVSP_LOCAL) {
1930 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1932 err = PTR_ERR(reply);
1936 ovs_dp_detach_port(vport);
1938 genl_notify(reply, genl_info_net(info), info->snd_pid,
1939 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
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 err = ovs_vport_cmd_validate(a);
1960 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1961 err = PTR_ERR(vport);
1965 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1967 err = PTR_ERR(reply);
1973 return genlmsg_reply(reply, info);
1981 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1983 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1984 struct datapath *dp;
1985 int bucket = cb->args[0], skip = cb->args[1];
1988 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1993 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1994 struct vport *vport;
1995 struct hlist_node *n;
1998 hlist_for_each_entry_rcu(vport, n, &dp->ports[i], dp_hash_node) {
2000 ovs_vport_cmd_fill_info(vport, skb,
2001 NETLINK_CB(cb->skb).pid,
2004 OVS_VPORT_CMD_NEW) < 0)
2020 static struct genl_ops dp_vport_genl_ops[] = {
2021 { .cmd = OVS_VPORT_CMD_NEW,
2022 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2023 .policy = vport_policy,
2024 .doit = ovs_vport_cmd_new
2026 { .cmd = OVS_VPORT_CMD_DEL,
2027 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2028 .policy = vport_policy,
2029 .doit = ovs_vport_cmd_del
2031 { .cmd = OVS_VPORT_CMD_GET,
2032 .flags = 0, /* OK for unprivileged users. */
2033 .policy = vport_policy,
2034 .doit = ovs_vport_cmd_get,
2035 .dumpit = ovs_vport_cmd_dump
2037 { .cmd = OVS_VPORT_CMD_SET,
2038 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2039 .policy = vport_policy,
2040 .doit = ovs_vport_cmd_set,
2044 struct genl_family_and_ops {
2045 struct genl_family *family;
2046 struct genl_ops *ops;
2048 struct genl_multicast_group *group;
2051 static const struct genl_family_and_ops dp_genl_families[] = {
2052 { &dp_datapath_genl_family,
2053 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2054 &ovs_dp_datapath_multicast_group },
2055 { &dp_vport_genl_family,
2056 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2057 &ovs_dp_vport_multicast_group },
2058 { &dp_flow_genl_family,
2059 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2060 &ovs_dp_flow_multicast_group },
2061 { &dp_packet_genl_family,
2062 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2066 static void dp_unregister_genl(int n_families)
2070 for (i = 0; i < n_families; i++)
2071 genl_unregister_family(dp_genl_families[i].family);
2074 static int dp_register_genl(void)
2081 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2082 const struct genl_family_and_ops *f = &dp_genl_families[i];
2084 err = genl_register_family_with_ops(f->family, f->ops,
2091 err = genl_register_mc_group(f->family, f->group);
2100 dp_unregister_genl(n_registered);
2104 static int __rehash_flow_table(void *dummy)
2106 struct datapath *dp;
2111 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2113 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2114 struct flow_table *old_table = genl_dereference(dp->table);
2115 struct flow_table *new_table;
2117 new_table = ovs_flow_tbl_rehash(old_table);
2118 if (!IS_ERR(new_table)) {
2119 rcu_assign_pointer(dp->table, new_table);
2120 ovs_flow_tbl_deferred_destroy(old_table);
2128 static void rehash_flow_table(struct work_struct *work)
2130 genl_exec(__rehash_flow_table, NULL);
2131 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2134 static int dp_destroy_all(void *data)
2136 struct datapath *dp, *dp_next;
2137 struct ovs_net *ovs_net = data;
2139 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2145 static int __net_init ovs_init_net(struct net *net)
2147 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2149 INIT_LIST_HEAD(&ovs_net->dps);
2153 static void __net_exit ovs_exit_net(struct net *net)
2155 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2157 genl_exec(dp_destroy_all, ovs_net);
2160 static struct pernet_operations ovs_net_ops = {
2161 .init = ovs_init_net,
2162 .exit = ovs_exit_net,
2164 .size = sizeof(struct ovs_net),
2167 static int __init dp_init(void)
2169 struct sk_buff *dummy_skb;
2172 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2174 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2177 err = genl_exec_init();
2181 err = ovs_workqueues_init();
2183 goto error_genl_exec;
2185 err = ovs_tnl_init();
2189 err = ovs_flow_init();
2191 goto error_tnl_exit;
2193 err = ovs_vport_init();
2195 goto error_flow_exit;
2197 err = register_pernet_device(&ovs_net_ops);
2199 goto error_vport_exit;
2201 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2203 goto error_netns_exit;
2205 err = dp_register_genl();
2207 goto error_unreg_notifier;
2209 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2213 error_unreg_notifier:
2214 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2216 unregister_pernet_device(&ovs_net_ops);
2224 ovs_workqueues_exit();
2231 static void dp_cleanup(void)
2233 cancel_delayed_work_sync(&rehash_flow_wq);
2234 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2235 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2236 unregister_pernet_device(&ovs_net_ops);
2241 ovs_workqueues_exit();
2245 module_init(dp_init);
2246 module_exit(dp_cleanup);
2248 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2249 MODULE_LICENSE("GPL");