2 * Copyright (c) 2007-2012 Nicira Networks.
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,3,0)
66 #error Kernels before 2.6.18 or after 3.2 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 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)
189 NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, port->ops->get_addr(port));
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));
925 NLA_PUT(skb, OVS_FLOW_ATTR_STATS,
926 sizeof(struct ovs_flow_stats), &stats);
929 NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
931 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
932 * this is the first flow to be dumped into 'skb'. This is unusual for
933 * Netlink but individual action lists can be longer than
934 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
935 * The userspace caller can always fetch the actions separately if it
936 * really wants them. (Most userspace callers in fact don't care.)
938 * This can only fail for dump operations because the skb is always
939 * properly sized for single flows.
941 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
943 if (err < 0 && skb_orig_len)
946 return genlmsg_end(skb, ovs_header);
951 genlmsg_cancel(skb, ovs_header);
955 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
957 const struct sw_flow_actions *sf_acts;
960 sf_acts = rcu_dereference_protected(flow->sf_acts,
961 lockdep_genl_is_held());
963 /* OVS_FLOW_ATTR_KEY */
964 len = nla_total_size(FLOW_BUFSIZE);
965 /* OVS_FLOW_ATTR_ACTIONS */
966 len += nla_total_size(sf_acts->actions_len);
967 /* OVS_FLOW_ATTR_STATS */
968 len += nla_total_size(sizeof(struct ovs_flow_stats));
969 /* OVS_FLOW_ATTR_TCP_FLAGS */
970 len += nla_total_size(1);
971 /* OVS_FLOW_ATTR_USED */
972 len += nla_total_size(8);
974 len += NLMSG_ALIGN(sizeof(struct ovs_header));
976 return genlmsg_new(len, GFP_KERNEL);
979 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
981 u32 pid, u32 seq, u8 cmd)
986 skb = ovs_flow_cmd_alloc_info(flow);
988 return ERR_PTR(-ENOMEM);
990 retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
995 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
997 struct nlattr **a = info->attrs;
998 struct ovs_header *ovs_header = info->userhdr;
999 struct sw_flow_key key;
1000 struct sw_flow *flow;
1001 struct sk_buff *reply;
1002 struct datapath *dp;
1003 struct flow_table *table;
1009 if (!a[OVS_FLOW_ATTR_KEY])
1011 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1015 /* Validate actions. */
1016 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1017 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
1020 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1025 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1030 table = genl_dereference(dp->table);
1031 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1033 struct sw_flow_actions *acts;
1035 /* Bail out if we're not allowed to create a new flow. */
1037 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1040 /* Expand table, if necessary, to make room. */
1041 if (ovs_flow_tbl_need_to_expand(table)) {
1042 struct flow_table *new_table;
1044 new_table = ovs_flow_tbl_expand(table);
1045 if (!IS_ERR(new_table)) {
1046 rcu_assign_pointer(dp->table, new_table);
1047 ovs_flow_tbl_deferred_destroy(table);
1048 table = genl_dereference(dp->table);
1052 /* Allocate flow. */
1053 flow = ovs_flow_alloc();
1055 error = PTR_ERR(flow);
1061 /* Obtain actions. */
1062 acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1063 error = PTR_ERR(acts);
1065 goto error_free_flow;
1066 rcu_assign_pointer(flow->sf_acts, acts);
1068 /* Put flow in bucket. */
1069 flow->hash = ovs_flow_hash(&key, key_len);
1070 ovs_flow_tbl_insert(table, flow);
1072 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1076 /* We found a matching flow. */
1077 struct sw_flow_actions *old_acts;
1078 struct nlattr *acts_attrs;
1080 /* Bail out if we're not allowed to modify an existing flow.
1081 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1082 * because Generic Netlink treats the latter as a dump
1083 * request. We also accept NLM_F_EXCL in case that bug ever
1087 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1088 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1091 /* Update actions. */
1092 old_acts = rcu_dereference_protected(flow->sf_acts,
1093 lockdep_genl_is_held());
1094 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1096 (old_acts->actions_len != nla_len(acts_attrs) ||
1097 memcmp(old_acts->actions, nla_data(acts_attrs),
1098 old_acts->actions_len))) {
1099 struct sw_flow_actions *new_acts;
1101 new_acts = ovs_flow_actions_alloc(acts_attrs);
1102 error = PTR_ERR(new_acts);
1103 if (IS_ERR(new_acts))
1106 rcu_assign_pointer(flow->sf_acts, new_acts);
1107 ovs_flow_deferred_free_acts(old_acts);
1110 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1111 info->snd_seq, OVS_FLOW_CMD_NEW);
1114 if (a[OVS_FLOW_ATTR_CLEAR]) {
1115 spin_lock_bh(&flow->lock);
1117 spin_unlock_bh(&flow->lock);
1122 genl_notify(reply, genl_info_net(info), info->snd_pid,
1123 ovs_dp_flow_multicast_group.id, info->nlhdr,
1126 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1127 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1136 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1138 struct nlattr **a = info->attrs;
1139 struct ovs_header *ovs_header = info->userhdr;
1140 struct sw_flow_key key;
1141 struct sk_buff *reply;
1142 struct sw_flow *flow;
1143 struct datapath *dp;
1144 struct flow_table *table;
1148 if (!a[OVS_FLOW_ATTR_KEY])
1150 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1154 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1158 table = genl_dereference(dp->table);
1159 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1163 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1164 info->snd_seq, OVS_FLOW_CMD_NEW);
1166 return PTR_ERR(reply);
1168 return genlmsg_reply(reply, info);
1171 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1173 struct nlattr **a = info->attrs;
1174 struct ovs_header *ovs_header = info->userhdr;
1175 struct sw_flow_key key;
1176 struct sk_buff *reply;
1177 struct sw_flow *flow;
1178 struct datapath *dp;
1179 struct flow_table *table;
1183 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1187 if (!a[OVS_FLOW_ATTR_KEY])
1188 return flush_flows(dp);
1190 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1194 table = genl_dereference(dp->table);
1195 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1199 reply = ovs_flow_cmd_alloc_info(flow);
1203 ovs_flow_tbl_remove(table, flow);
1205 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1206 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1209 ovs_flow_deferred_free(flow);
1211 genl_notify(reply, genl_info_net(info), info->snd_pid,
1212 ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1216 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1218 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1219 struct datapath *dp;
1220 struct flow_table *table;
1222 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1226 table = genl_dereference(dp->table);
1229 struct sw_flow *flow;
1232 bucket = cb->args[0];
1234 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1238 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1239 NETLINK_CB(cb->skb).pid,
1240 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1241 OVS_FLOW_CMD_NEW) < 0)
1244 cb->args[0] = bucket;
1250 static struct genl_ops dp_flow_genl_ops[] = {
1251 { .cmd = OVS_FLOW_CMD_NEW,
1252 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1253 .policy = flow_policy,
1254 .doit = ovs_flow_cmd_new_or_set
1256 { .cmd = OVS_FLOW_CMD_DEL,
1257 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1258 .policy = flow_policy,
1259 .doit = ovs_flow_cmd_del
1261 { .cmd = OVS_FLOW_CMD_GET,
1262 .flags = 0, /* OK for unprivileged users. */
1263 .policy = flow_policy,
1264 .doit = ovs_flow_cmd_get,
1265 .dumpit = ovs_flow_cmd_dump
1267 { .cmd = OVS_FLOW_CMD_SET,
1268 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1269 .policy = flow_policy,
1270 .doit = ovs_flow_cmd_new_or_set,
1274 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1275 #ifdef HAVE_NLA_NUL_STRING
1276 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1278 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1281 static struct genl_family dp_datapath_genl_family = {
1282 .id = GENL_ID_GENERATE,
1283 .hdrsize = sizeof(struct ovs_header),
1284 .name = OVS_DATAPATH_FAMILY,
1285 .version = OVS_DATAPATH_VERSION,
1286 .maxattr = OVS_DP_ATTR_MAX,
1290 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1291 .name = OVS_DATAPATH_MCGROUP
1294 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1295 u32 pid, u32 seq, u32 flags, u8 cmd)
1297 struct ovs_header *ovs_header;
1298 struct ovs_dp_stats dp_stats;
1301 ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1306 ovs_header->dp_ifindex = get_dpifindex(dp);
1309 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1312 goto nla_put_failure;
1314 get_dp_stats(dp, &dp_stats);
1315 NLA_PUT(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats);
1317 return genlmsg_end(skb, ovs_header);
1320 genlmsg_cancel(skb, ovs_header);
1325 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1328 struct sk_buff *skb;
1331 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1333 return ERR_PTR(-ENOMEM);
1335 retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1338 return ERR_PTR(retval);
1343 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1345 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1348 /* Called with genl_mutex and optionally with RTNL lock also. */
1349 static struct datapath *lookup_datapath(struct net *net,
1350 struct ovs_header *ovs_header,
1351 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1353 struct datapath *dp;
1355 if (!a[OVS_DP_ATTR_NAME])
1356 dp = get_dp(net, ovs_header->dp_ifindex);
1358 struct vport *vport;
1361 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1362 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1365 return dp ? dp : ERR_PTR(-ENODEV);
1368 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1370 struct nlattr **a = info->attrs;
1371 struct vport_parms parms;
1372 struct sk_buff *reply;
1373 struct datapath *dp;
1374 struct vport *vport;
1375 struct ovs_net *ovs_net;
1379 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1382 err = ovs_dp_cmd_validate(a);
1389 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1391 goto err_unlock_rtnl;
1393 /* Initialize kobject for bridge. This will be added as
1394 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1395 dp->ifobj.kset = NULL;
1396 kobject_init(&dp->ifobj, &dp_ktype);
1398 /* Allocate table. */
1400 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1404 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1405 if (!dp->stats_percpu) {
1407 goto err_destroy_table;
1409 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1411 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1415 goto err_destroy_percpu;
1418 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1419 INIT_HLIST_HEAD(&dp->ports[i]);
1421 /* Set up our datapath device. */
1422 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1423 parms.type = OVS_VPORT_TYPE_INTERNAL;
1424 parms.options = NULL;
1426 parms.port_no = OVSP_LOCAL;
1427 parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1429 vport = new_vport(&parms);
1430 if (IS_ERR(vport)) {
1431 err = PTR_ERR(vport);
1435 goto err_destroy_ports_array;
1438 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1439 info->snd_seq, OVS_DP_CMD_NEW);
1440 err = PTR_ERR(reply);
1442 goto err_destroy_local_port;
1444 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1445 list_add_tail(&dp->list_node, &ovs_net->dps);
1446 ovs_dp_sysfs_add_dp(dp);
1450 genl_notify(reply, genl_info_net(info), info->snd_pid,
1451 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1455 err_destroy_local_port:
1456 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1457 err_destroy_ports_array:
1460 free_percpu(dp->stats_percpu);
1462 ovs_flow_tbl_destroy(genl_dereference(dp->table));
1471 /* Called with genl_mutex. */
1472 static void __dp_destroy(struct datapath *dp)
1478 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1479 struct vport *vport;
1480 struct hlist_node *node, *n;
1482 hlist_for_each_entry_safe(vport, node, n, &dp->ports[i], dp_hash_node)
1483 if (vport->port_no != OVSP_LOCAL)
1484 ovs_dp_detach_port(vport);
1487 ovs_dp_sysfs_del_dp(dp);
1488 list_del(&dp->list_node);
1489 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1491 /* rtnl_unlock() will wait until all the references to devices that
1492 * are pending unregistration have been dropped. We do it here to
1493 * ensure that any internal devices (which contain DP pointers) are
1494 * fully destroyed before freeing the datapath.
1498 call_rcu(&dp->rcu, destroy_dp_rcu);
1501 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1503 struct sk_buff *reply;
1504 struct datapath *dp;
1507 err = ovs_dp_cmd_validate(info->attrs);
1511 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1516 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1517 info->snd_seq, OVS_DP_CMD_DEL);
1518 err = PTR_ERR(reply);
1524 genl_notify(reply, genl_info_net(info), info->snd_pid,
1525 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1531 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1533 struct sk_buff *reply;
1534 struct datapath *dp;
1537 err = ovs_dp_cmd_validate(info->attrs);
1541 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1545 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1546 info->snd_seq, OVS_DP_CMD_NEW);
1547 if (IS_ERR(reply)) {
1548 err = PTR_ERR(reply);
1549 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1550 ovs_dp_datapath_multicast_group.id, err);
1554 genl_notify(reply, genl_info_net(info), info->snd_pid,
1555 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1561 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1563 struct sk_buff *reply;
1564 struct datapath *dp;
1567 err = ovs_dp_cmd_validate(info->attrs);
1571 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1575 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1576 info->snd_seq, OVS_DP_CMD_NEW);
1578 return PTR_ERR(reply);
1580 return genlmsg_reply(reply, info);
1583 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1585 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1586 struct datapath *dp;
1587 int skip = cb->args[0];
1590 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1592 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1593 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1594 OVS_DP_CMD_NEW) < 0)
1604 static struct genl_ops dp_datapath_genl_ops[] = {
1605 { .cmd = OVS_DP_CMD_NEW,
1606 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1607 .policy = datapath_policy,
1608 .doit = ovs_dp_cmd_new
1610 { .cmd = OVS_DP_CMD_DEL,
1611 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1612 .policy = datapath_policy,
1613 .doit = ovs_dp_cmd_del
1615 { .cmd = OVS_DP_CMD_GET,
1616 .flags = 0, /* OK for unprivileged users. */
1617 .policy = datapath_policy,
1618 .doit = ovs_dp_cmd_get,
1619 .dumpit = ovs_dp_cmd_dump
1621 { .cmd = OVS_DP_CMD_SET,
1622 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1623 .policy = datapath_policy,
1624 .doit = ovs_dp_cmd_set,
1628 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1629 #ifdef HAVE_NLA_NUL_STRING
1630 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1631 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1632 [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1634 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1635 [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1637 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1638 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1639 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1640 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1643 static struct genl_family dp_vport_genl_family = {
1644 .id = GENL_ID_GENERATE,
1645 .hdrsize = sizeof(struct ovs_header),
1646 .name = OVS_VPORT_FAMILY,
1647 .version = OVS_VPORT_VERSION,
1648 .maxattr = OVS_VPORT_ATTR_MAX,
1652 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1653 .name = OVS_VPORT_MCGROUP
1656 /* Called with RTNL lock or RCU read lock. */
1657 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1658 u32 pid, u32 seq, u32 flags, u8 cmd)
1660 struct ovs_header *ovs_header;
1661 struct ovs_vport_stats vport_stats;
1664 ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1669 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1671 NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1672 NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type);
1673 NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport));
1674 NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1676 ovs_vport_get_stats(vport, &vport_stats);
1677 NLA_PUT(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1680 NLA_PUT(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1681 vport->ops->get_addr(vport));
1683 err = ovs_vport_get_options(vport, skb);
1684 if (err == -EMSGSIZE)
1687 return genlmsg_end(skb, ovs_header);
1692 genlmsg_cancel(skb, ovs_header);
1696 /* Called with RTNL lock or RCU read lock. */
1697 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1700 struct sk_buff *skb;
1703 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1705 return ERR_PTR(-ENOMEM);
1707 retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1710 return ERR_PTR(retval);
1715 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1717 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1720 /* Called with RTNL lock or RCU read lock. */
1721 static struct vport *lookup_vport(struct net *net,
1722 struct ovs_header *ovs_header,
1723 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1725 struct datapath *dp;
1726 struct vport *vport;
1728 if (a[OVS_VPORT_ATTR_NAME]) {
1729 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1731 return ERR_PTR(-ENODEV);
1732 if (ovs_header->dp_ifindex &&
1733 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1734 return ERR_PTR(-ENODEV);
1736 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1737 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1739 if (port_no >= DP_MAX_PORTS)
1740 return ERR_PTR(-EFBIG);
1742 dp = get_dp(net, ovs_header->dp_ifindex);
1744 return ERR_PTR(-ENODEV);
1746 vport = ovs_vport_rtnl_rcu(dp, port_no);
1748 return ERR_PTR(-ENOENT);
1751 return ERR_PTR(-EINVAL);
1754 /* Called with RTNL lock. */
1755 static int change_vport(struct vport *vport,
1756 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1760 if (a[OVS_VPORT_ATTR_STATS])
1761 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1763 if (a[OVS_VPORT_ATTR_ADDRESS])
1764 err = ovs_vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1769 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1771 struct nlattr **a = info->attrs;
1772 struct ovs_header *ovs_header = info->userhdr;
1773 struct vport_parms parms;
1774 struct sk_buff *reply;
1775 struct vport *vport;
1776 struct datapath *dp;
1781 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1782 !a[OVS_VPORT_ATTR_UPCALL_PID])
1785 err = ovs_vport_cmd_validate(a);
1790 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1795 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1796 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1799 if (port_no >= DP_MAX_PORTS)
1802 vport = ovs_vport_rtnl(dp, port_no);
1807 for (port_no = 1; ; port_no++) {
1808 if (port_no >= DP_MAX_PORTS) {
1812 vport = ovs_vport_rtnl(dp, port_no);
1818 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1819 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1820 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1822 parms.port_no = port_no;
1823 parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1825 vport = new_vport(&parms);
1826 err = PTR_ERR(vport);
1830 ovs_dp_sysfs_add_if(vport);
1832 err = change_vport(vport, a);
1834 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1838 err = PTR_ERR(reply);
1841 ovs_dp_detach_port(vport);
1844 genl_notify(reply, genl_info_net(info), info->snd_pid,
1845 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1853 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1855 struct nlattr **a = info->attrs;
1856 struct sk_buff *reply;
1857 struct vport *vport;
1860 err = ovs_vport_cmd_validate(a);
1865 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1866 err = PTR_ERR(vport);
1871 if (a[OVS_VPORT_ATTR_TYPE] &&
1872 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1875 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1876 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1878 err = change_vport(vport, a);
1879 if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1880 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1882 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1884 if (IS_ERR(reply)) {
1885 err = PTR_ERR(reply);
1886 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1887 ovs_dp_vport_multicast_group.id, err);
1891 genl_notify(reply, genl_info_net(info), info->snd_pid,
1892 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1900 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1902 struct nlattr **a = info->attrs;
1903 struct sk_buff *reply;
1904 struct vport *vport;
1907 err = ovs_vport_cmd_validate(a);
1912 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1913 err = PTR_ERR(vport);
1917 if (vport->port_no == OVSP_LOCAL) {
1922 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1924 err = PTR_ERR(reply);
1928 ovs_dp_detach_port(vport);
1930 genl_notify(reply, genl_info_net(info), info->snd_pid,
1931 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1939 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1941 struct nlattr **a = info->attrs;
1942 struct ovs_header *ovs_header = info->userhdr;
1943 struct sk_buff *reply;
1944 struct vport *vport;
1947 err = ovs_vport_cmd_validate(a);
1952 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1953 err = PTR_ERR(vport);
1957 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1959 err = PTR_ERR(reply);
1965 return genlmsg_reply(reply, info);
1973 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1975 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1976 struct datapath *dp;
1977 int bucket = cb->args[0], skip = cb->args[1];
1980 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1985 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1986 struct vport *vport;
1987 struct hlist_node *n;
1990 hlist_for_each_entry_rcu(vport, n, &dp->ports[i], dp_hash_node) {
1992 ovs_vport_cmd_fill_info(vport, skb,
1993 NETLINK_CB(cb->skb).pid,
1996 OVS_VPORT_CMD_NEW) < 0)
2012 static struct genl_ops dp_vport_genl_ops[] = {
2013 { .cmd = OVS_VPORT_CMD_NEW,
2014 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2015 .policy = vport_policy,
2016 .doit = ovs_vport_cmd_new
2018 { .cmd = OVS_VPORT_CMD_DEL,
2019 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2020 .policy = vport_policy,
2021 .doit = ovs_vport_cmd_del
2023 { .cmd = OVS_VPORT_CMD_GET,
2024 .flags = 0, /* OK for unprivileged users. */
2025 .policy = vport_policy,
2026 .doit = ovs_vport_cmd_get,
2027 .dumpit = ovs_vport_cmd_dump
2029 { .cmd = OVS_VPORT_CMD_SET,
2030 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2031 .policy = vport_policy,
2032 .doit = ovs_vport_cmd_set,
2036 struct genl_family_and_ops {
2037 struct genl_family *family;
2038 struct genl_ops *ops;
2040 struct genl_multicast_group *group;
2043 static const struct genl_family_and_ops dp_genl_families[] = {
2044 { &dp_datapath_genl_family,
2045 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2046 &ovs_dp_datapath_multicast_group },
2047 { &dp_vport_genl_family,
2048 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2049 &ovs_dp_vport_multicast_group },
2050 { &dp_flow_genl_family,
2051 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2052 &ovs_dp_flow_multicast_group },
2053 { &dp_packet_genl_family,
2054 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2058 static void dp_unregister_genl(int n_families)
2062 for (i = 0; i < n_families; i++)
2063 genl_unregister_family(dp_genl_families[i].family);
2066 static int dp_register_genl(void)
2073 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2074 const struct genl_family_and_ops *f = &dp_genl_families[i];
2076 err = genl_register_family_with_ops(f->family, f->ops,
2083 err = genl_register_mc_group(f->family, f->group);
2092 dp_unregister_genl(n_registered);
2096 static int __rehash_flow_table(void *dummy)
2098 struct datapath *dp;
2103 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2105 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2106 struct flow_table *old_table = genl_dereference(dp->table);
2107 struct flow_table *new_table;
2109 new_table = ovs_flow_tbl_rehash(old_table);
2110 if (!IS_ERR(new_table)) {
2111 rcu_assign_pointer(dp->table, new_table);
2112 ovs_flow_tbl_deferred_destroy(old_table);
2120 static void rehash_flow_table(struct work_struct *work)
2122 genl_exec(__rehash_flow_table, NULL);
2123 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2126 static int dp_destroy_all(void *data)
2128 struct datapath *dp, *dp_next;
2129 struct ovs_net *ovs_net = data;
2131 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2137 static int __net_init ovs_init_net(struct net *net)
2139 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2141 INIT_LIST_HEAD(&ovs_net->dps);
2145 static void __net_exit ovs_exit_net(struct net *net)
2147 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2149 genl_exec(dp_destroy_all, ovs_net);
2152 static struct pernet_operations ovs_net_ops = {
2153 .init = ovs_init_net,
2154 .exit = ovs_exit_net,
2156 .size = sizeof(struct ovs_net),
2159 static int __init dp_init(void)
2161 struct sk_buff *dummy_skb;
2164 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2166 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2169 err = genl_exec_init();
2173 err = ovs_workqueues_init();
2175 goto error_genl_exec;
2177 err = ovs_tnl_init();
2181 err = ovs_flow_init();
2183 goto error_tnl_exit;
2185 err = ovs_vport_init();
2187 goto error_flow_exit;
2189 err = register_pernet_device(&ovs_net_ops);
2191 goto error_vport_exit;
2193 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2195 goto error_netns_exit;
2197 err = dp_register_genl();
2199 goto error_unreg_notifier;
2201 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2205 error_unreg_notifier:
2206 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2208 unregister_pernet_device(&ovs_net_ops);
2216 ovs_workqueues_exit();
2223 static void dp_cleanup(void)
2225 cancel_delayed_work_sync(&rehash_flow_wq);
2226 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2227 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2228 unregister_pernet_device(&ovs_net_ops);
2233 ovs_workqueues_exit();
2237 module_init(dp_init);
2238 module_exit(dp_cleanup);
2240 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2241 MODULE_LICENSE("GPL");