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 = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
124 return vport->ops->get_name(vport);
127 static int get_dpifindex(struct datapath *dp)
134 local = rcu_dereference(dp->ports[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));
247 kobject_put(&dp->ifobj);
250 /* Called with RTNL lock and genl_lock. */
251 static struct vport *new_vport(const struct vport_parms *parms)
255 vport = ovs_vport_add(parms);
256 if (!IS_ERR(vport)) {
257 struct datapath *dp = parms->dp;
259 rcu_assign_pointer(dp->ports[parms->port_no], vport);
260 list_add(&vport->node, &dp->port_list);
262 dp_ifinfo_notify(RTM_NEWLINK, vport);
268 /* Called with RTNL lock. */
269 void ovs_dp_detach_port(struct vport *p)
273 if (p->port_no != OVSP_LOCAL)
274 ovs_dp_sysfs_del_if(p);
275 dp_ifinfo_notify(RTM_DELLINK, p);
277 /* First drop references to device. */
279 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
281 /* Then destroy it. */
285 /* Must be called with rcu_read_lock. */
286 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
288 struct datapath *dp = p->dp;
289 struct sw_flow *flow;
290 struct dp_stats_percpu *stats;
294 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
296 if (!OVS_CB(skb)->flow) {
297 struct sw_flow_key key;
300 /* Extract flow from 'skb' into 'key'. */
301 error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
302 if (unlikely(error)) {
308 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table),
310 if (unlikely(!flow)) {
311 struct dp_upcall_info upcall;
313 upcall.cmd = OVS_PACKET_CMD_MISS;
315 upcall.userdata = NULL;
316 upcall.pid = p->upcall_pid;
317 ovs_dp_upcall(dp, skb, &upcall);
319 stats_counter = &stats->n_missed;
323 OVS_CB(skb)->flow = flow;
326 stats_counter = &stats->n_hit;
327 ovs_flow_used(OVS_CB(skb)->flow, skb);
328 ovs_execute_actions(dp, skb);
331 /* Update datapath statistics. */
332 u64_stats_update_begin(&stats->sync);
334 u64_stats_update_end(&stats->sync);
337 static struct genl_family dp_packet_genl_family = {
338 .id = GENL_ID_GENERATE,
339 .hdrsize = sizeof(struct ovs_header),
340 .name = OVS_PACKET_FAMILY,
341 .version = OVS_PACKET_VERSION,
342 .maxattr = OVS_PACKET_ATTR_MAX,
346 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
347 const struct dp_upcall_info *upcall_info)
349 struct dp_stats_percpu *stats;
353 if (upcall_info->pid == 0) {
358 dp_ifindex = get_dpifindex(dp);
364 forward_ip_summed(skb, true);
366 if (!skb_is_gso(skb))
367 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
369 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
376 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
378 u64_stats_update_begin(&stats->sync);
380 u64_stats_update_end(&stats->sync);
385 static int queue_gso_packets(struct net *net, int dp_ifindex,
387 const struct dp_upcall_info *upcall_info)
389 struct dp_upcall_info later_info;
390 struct sw_flow_key later_key;
391 struct sk_buff *segs, *nskb;
394 segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
398 /* Queue all of the segments. */
401 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
405 if (skb == segs && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
406 /* The initial flow key extracted by ovs_flow_extract()
407 * in this case is for a first fragment, so we need to
408 * properly mark later fragments.
410 later_key = *upcall_info->key;
411 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
413 later_info = *upcall_info;
414 later_info.key = &later_key;
415 upcall_info = &later_info;
417 } while ((skb = skb->next));
419 /* Free all of the segments. */
427 } while ((skb = nskb));
431 static int queue_userspace_packet(struct net *net, int dp_ifindex,
433 const struct dp_upcall_info *upcall_info)
435 struct ovs_header *upcall;
436 struct sk_buff *nskb = NULL;
437 struct sk_buff *user_skb; /* to be queued to userspace */
442 if (vlan_tx_tag_present(skb)) {
443 nskb = skb_clone(skb, GFP_ATOMIC);
447 err = vlan_deaccel_tag(nskb);
454 if (nla_attr_size(skb->len) > USHRT_MAX) {
459 len = sizeof(struct ovs_header);
460 len += nla_total_size(skb->len);
461 len += nla_total_size(FLOW_BUFSIZE);
462 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
463 len += nla_total_size(8);
465 user_skb = genlmsg_new(len, GFP_ATOMIC);
471 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
472 0, upcall_info->cmd);
473 upcall->dp_ifindex = dp_ifindex;
475 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
476 ovs_flow_to_nlattrs(upcall_info->key, user_skb);
477 nla_nest_end(user_skb, nla);
479 if (upcall_info->userdata)
480 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
481 nla_get_u64(upcall_info->userdata));
483 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
485 skb_copy_and_csum_dev(skb, nla_data(nla));
487 err = genlmsg_unicast(net, user_skb, upcall_info->pid);
494 /* Called with genl_mutex. */
495 static int flush_flows(struct datapath *dp)
497 struct flow_table *old_table;
498 struct flow_table *new_table;
500 old_table = genl_dereference(dp->table);
501 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
505 rcu_assign_pointer(dp->table, new_table);
507 ovs_flow_tbl_deferred_destroy(old_table);
511 static int validate_actions(const struct nlattr *attr,
512 const struct sw_flow_key *key, int depth);
514 static int validate_sample(const struct nlattr *attr,
515 const struct sw_flow_key *key, int depth)
517 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
518 const struct nlattr *probability, *actions;
519 const struct nlattr *a;
522 memset(attrs, 0, sizeof(attrs));
523 nla_for_each_nested(a, attr, rem) {
524 int type = nla_type(a);
525 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
532 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
533 if (!probability || nla_len(probability) != sizeof(u32))
536 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
537 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
539 return validate_actions(actions, key, depth + 1);
542 static int validate_set(const struct nlattr *a,
543 const struct sw_flow_key *flow_key)
545 const struct nlattr *ovs_key = nla_data(a);
546 int key_type = nla_type(ovs_key);
548 /* There can be only one key in a action */
549 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
552 if (key_type > OVS_KEY_ATTR_MAX ||
553 nla_len(ovs_key) != ovs_key_lens[key_type])
557 const struct ovs_key_ipv4 *ipv4_key;
559 case OVS_KEY_ATTR_PRIORITY:
560 case OVS_KEY_ATTR_TUN_ID:
561 case OVS_KEY_ATTR_ETHERNET:
564 case OVS_KEY_ATTR_IPV4:
565 if (flow_key->eth.type != htons(ETH_P_IP))
568 if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
571 ipv4_key = nla_data(ovs_key);
572 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
575 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
580 case OVS_KEY_ATTR_TCP:
581 if (flow_key->ip.proto != IPPROTO_TCP)
584 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
589 case OVS_KEY_ATTR_UDP:
590 if (flow_key->ip.proto != IPPROTO_UDP)
593 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
604 static int validate_userspace(const struct nlattr *attr)
606 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
607 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
608 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
610 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
613 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
614 attr, userspace_policy);
618 if (!a[OVS_USERSPACE_ATTR_PID] ||
619 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
625 static int validate_actions(const struct nlattr *attr,
626 const struct sw_flow_key *key, int depth)
628 const struct nlattr *a;
631 if (depth >= SAMPLE_ACTION_DEPTH)
634 nla_for_each_nested(a, attr, rem) {
635 /* Expected argument lengths, (u32)-1 for variable length. */
636 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
637 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
638 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
639 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
640 [OVS_ACTION_ATTR_POP_VLAN] = 0,
641 [OVS_ACTION_ATTR_SET] = (u32)-1,
642 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
644 const struct ovs_action_push_vlan *vlan;
645 int type = nla_type(a);
647 if (type > OVS_ACTION_ATTR_MAX ||
648 (action_lens[type] != nla_len(a) &&
649 action_lens[type] != (u32)-1))
653 case OVS_ACTION_ATTR_UNSPEC:
656 case OVS_ACTION_ATTR_USERSPACE:
657 err = validate_userspace(a);
662 case OVS_ACTION_ATTR_OUTPUT:
663 if (nla_get_u32(a) >= DP_MAX_PORTS)
668 case OVS_ACTION_ATTR_POP_VLAN:
671 case OVS_ACTION_ATTR_PUSH_VLAN:
673 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
675 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
679 case OVS_ACTION_ATTR_SET:
680 err = validate_set(a, key);
685 case OVS_ACTION_ATTR_SAMPLE:
686 err = validate_sample(a, key, depth);
702 static void clear_stats(struct sw_flow *flow)
706 flow->packet_count = 0;
707 flow->byte_count = 0;
710 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
712 struct ovs_header *ovs_header = info->userhdr;
713 struct nlattr **a = info->attrs;
714 struct sw_flow_actions *acts;
715 struct sk_buff *packet;
716 struct sw_flow *flow;
724 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
725 !a[OVS_PACKET_ATTR_ACTIONS] ||
726 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
729 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
730 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
734 skb_reserve(packet, NET_IP_ALIGN);
736 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
738 skb_reset_mac_header(packet);
739 eth = eth_hdr(packet);
741 /* Normally, setting the skb 'protocol' field would be handled by a
742 * call to eth_type_trans(), but it assumes there's a sending
743 * device, which we may not have. */
744 if (ntohs(eth->h_proto) >= 1536)
745 packet->protocol = eth->h_proto;
747 packet->protocol = htons(ETH_P_802_2);
749 /* Build an sw_flow for sending this packet. */
750 flow = ovs_flow_alloc();
755 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
759 err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
760 &flow->key.phy.in_port,
761 &flow->key.phy.tun_id,
762 a[OVS_PACKET_ATTR_KEY]);
766 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
770 flow->hash = ovs_flow_hash(&flow->key, key_len);
772 acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
776 rcu_assign_pointer(flow->sf_acts, acts);
778 OVS_CB(packet)->flow = flow;
779 packet->priority = flow->key.phy.priority;
782 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
788 err = ovs_execute_actions(dp, packet);
805 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
806 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
807 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
808 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
811 static struct genl_ops dp_packet_genl_ops[] = {
812 { .cmd = OVS_PACKET_CMD_EXECUTE,
813 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
814 .policy = packet_policy,
815 .doit = ovs_packet_cmd_execute
819 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
822 struct flow_table *table = genl_dereference(dp->table);
824 stats->n_flows = ovs_flow_tbl_count(table);
826 stats->n_hit = stats->n_missed = stats->n_lost = 0;
827 for_each_possible_cpu(i) {
828 const struct dp_stats_percpu *percpu_stats;
829 struct dp_stats_percpu local_stats;
832 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
835 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
836 local_stats = *percpu_stats;
837 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
839 stats->n_hit += local_stats.n_hit;
840 stats->n_missed += local_stats.n_missed;
841 stats->n_lost += local_stats.n_lost;
845 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
846 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
847 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
848 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
851 static struct genl_family dp_flow_genl_family = {
852 .id = GENL_ID_GENERATE,
853 .hdrsize = sizeof(struct ovs_header),
854 .name = OVS_FLOW_FAMILY,
855 .version = OVS_FLOW_VERSION,
856 .maxattr = OVS_FLOW_ATTR_MAX,
860 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
861 .name = OVS_FLOW_MCGROUP
864 /* Called with genl_lock. */
865 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
866 struct sk_buff *skb, u32 pid,
867 u32 seq, u32 flags, u8 cmd)
869 const int skb_orig_len = skb->len;
870 const struct sw_flow_actions *sf_acts;
871 struct ovs_flow_stats stats;
872 struct ovs_header *ovs_header;
878 sf_acts = rcu_dereference_protected(flow->sf_acts,
879 lockdep_genl_is_held());
881 ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
885 ovs_header->dp_ifindex = get_dpifindex(dp);
887 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
889 goto nla_put_failure;
890 err = ovs_flow_to_nlattrs(&flow->key, skb);
893 nla_nest_end(skb, nla);
895 spin_lock_bh(&flow->lock);
897 stats.n_packets = flow->packet_count;
898 stats.n_bytes = flow->byte_count;
899 tcp_flags = flow->tcp_flags;
900 spin_unlock_bh(&flow->lock);
903 NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used));
906 NLA_PUT(skb, OVS_FLOW_ATTR_STATS,
907 sizeof(struct ovs_flow_stats), &stats);
910 NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
912 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
913 * this is the first flow to be dumped into 'skb'. This is unusual for
914 * Netlink but individual action lists can be longer than
915 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
916 * The userspace caller can always fetch the actions separately if it
917 * really wants them. (Most userspace callers in fact don't care.)
919 * This can only fail for dump operations because the skb is always
920 * properly sized for single flows.
922 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
924 if (err < 0 && skb_orig_len)
927 return genlmsg_end(skb, ovs_header);
932 genlmsg_cancel(skb, ovs_header);
936 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
938 const struct sw_flow_actions *sf_acts;
941 sf_acts = rcu_dereference_protected(flow->sf_acts,
942 lockdep_genl_is_held());
944 /* OVS_FLOW_ATTR_KEY */
945 len = nla_total_size(FLOW_BUFSIZE);
946 /* OVS_FLOW_ATTR_ACTIONS */
947 len += nla_total_size(sf_acts->actions_len);
948 /* OVS_FLOW_ATTR_STATS */
949 len += nla_total_size(sizeof(struct ovs_flow_stats));
950 /* OVS_FLOW_ATTR_TCP_FLAGS */
951 len += nla_total_size(1);
952 /* OVS_FLOW_ATTR_USED */
953 len += nla_total_size(8);
955 len += NLMSG_ALIGN(sizeof(struct ovs_header));
957 return genlmsg_new(len, GFP_KERNEL);
960 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
962 u32 pid, u32 seq, u8 cmd)
967 skb = ovs_flow_cmd_alloc_info(flow);
969 return ERR_PTR(-ENOMEM);
971 retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
976 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
978 struct nlattr **a = info->attrs;
979 struct ovs_header *ovs_header = info->userhdr;
980 struct sw_flow_key key;
981 struct sw_flow *flow;
982 struct sk_buff *reply;
984 struct flow_table *table;
990 if (!a[OVS_FLOW_ATTR_KEY])
992 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
996 /* Validate actions. */
997 if (a[OVS_FLOW_ATTR_ACTIONS]) {
998 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
1001 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1006 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1011 table = genl_dereference(dp->table);
1012 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1014 struct sw_flow_actions *acts;
1016 /* Bail out if we're not allowed to create a new flow. */
1018 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1021 /* Expand table, if necessary, to make room. */
1022 if (ovs_flow_tbl_need_to_expand(table)) {
1023 struct flow_table *new_table;
1025 new_table = ovs_flow_tbl_expand(table);
1026 if (!IS_ERR(new_table)) {
1027 rcu_assign_pointer(dp->table, new_table);
1028 ovs_flow_tbl_deferred_destroy(table);
1029 table = genl_dereference(dp->table);
1033 /* Allocate flow. */
1034 flow = ovs_flow_alloc();
1036 error = PTR_ERR(flow);
1042 /* Obtain actions. */
1043 acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1044 error = PTR_ERR(acts);
1046 goto error_free_flow;
1047 rcu_assign_pointer(flow->sf_acts, acts);
1049 /* Put flow in bucket. */
1050 flow->hash = ovs_flow_hash(&key, key_len);
1051 ovs_flow_tbl_insert(table, flow);
1053 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1057 /* We found a matching flow. */
1058 struct sw_flow_actions *old_acts;
1059 struct nlattr *acts_attrs;
1061 /* Bail out if we're not allowed to modify an existing flow.
1062 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1063 * because Generic Netlink treats the latter as a dump
1064 * request. We also accept NLM_F_EXCL in case that bug ever
1068 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1069 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1072 /* Update actions. */
1073 old_acts = rcu_dereference_protected(flow->sf_acts,
1074 lockdep_genl_is_held());
1075 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1077 (old_acts->actions_len != nla_len(acts_attrs) ||
1078 memcmp(old_acts->actions, nla_data(acts_attrs),
1079 old_acts->actions_len))) {
1080 struct sw_flow_actions *new_acts;
1082 new_acts = ovs_flow_actions_alloc(acts_attrs);
1083 error = PTR_ERR(new_acts);
1084 if (IS_ERR(new_acts))
1087 rcu_assign_pointer(flow->sf_acts, new_acts);
1088 ovs_flow_deferred_free_acts(old_acts);
1091 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1092 info->snd_seq, OVS_FLOW_CMD_NEW);
1095 if (a[OVS_FLOW_ATTR_CLEAR]) {
1096 spin_lock_bh(&flow->lock);
1098 spin_unlock_bh(&flow->lock);
1103 genl_notify(reply, genl_info_net(info), info->snd_pid,
1104 ovs_dp_flow_multicast_group.id, info->nlhdr,
1107 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1108 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1117 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1119 struct nlattr **a = info->attrs;
1120 struct ovs_header *ovs_header = info->userhdr;
1121 struct sw_flow_key key;
1122 struct sk_buff *reply;
1123 struct sw_flow *flow;
1124 struct datapath *dp;
1125 struct flow_table *table;
1129 if (!a[OVS_FLOW_ATTR_KEY])
1131 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1135 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1139 table = genl_dereference(dp->table);
1140 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1144 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1145 info->snd_seq, OVS_FLOW_CMD_NEW);
1147 return PTR_ERR(reply);
1149 return genlmsg_reply(reply, info);
1152 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1154 struct nlattr **a = info->attrs;
1155 struct ovs_header *ovs_header = info->userhdr;
1156 struct sw_flow_key key;
1157 struct sk_buff *reply;
1158 struct sw_flow *flow;
1159 struct datapath *dp;
1160 struct flow_table *table;
1164 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1168 if (!a[OVS_FLOW_ATTR_KEY])
1169 return flush_flows(dp);
1171 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1175 table = genl_dereference(dp->table);
1176 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1180 reply = ovs_flow_cmd_alloc_info(flow);
1184 ovs_flow_tbl_remove(table, flow);
1186 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1187 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1190 ovs_flow_deferred_free(flow);
1192 genl_notify(reply, genl_info_net(info), info->snd_pid,
1193 ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1197 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1199 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1200 struct datapath *dp;
1201 struct flow_table *table;
1203 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1207 table = genl_dereference(dp->table);
1210 struct sw_flow *flow;
1213 bucket = cb->args[0];
1215 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1219 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1220 NETLINK_CB(cb->skb).pid,
1221 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1222 OVS_FLOW_CMD_NEW) < 0)
1225 cb->args[0] = bucket;
1231 static struct genl_ops dp_flow_genl_ops[] = {
1232 { .cmd = OVS_FLOW_CMD_NEW,
1233 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1234 .policy = flow_policy,
1235 .doit = ovs_flow_cmd_new_or_set
1237 { .cmd = OVS_FLOW_CMD_DEL,
1238 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1239 .policy = flow_policy,
1240 .doit = ovs_flow_cmd_del
1242 { .cmd = OVS_FLOW_CMD_GET,
1243 .flags = 0, /* OK for unprivileged users. */
1244 .policy = flow_policy,
1245 .doit = ovs_flow_cmd_get,
1246 .dumpit = ovs_flow_cmd_dump
1248 { .cmd = OVS_FLOW_CMD_SET,
1249 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1250 .policy = flow_policy,
1251 .doit = ovs_flow_cmd_new_or_set,
1255 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1256 #ifdef HAVE_NLA_NUL_STRING
1257 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1259 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1262 static struct genl_family dp_datapath_genl_family = {
1263 .id = GENL_ID_GENERATE,
1264 .hdrsize = sizeof(struct ovs_header),
1265 .name = OVS_DATAPATH_FAMILY,
1266 .version = OVS_DATAPATH_VERSION,
1267 .maxattr = OVS_DP_ATTR_MAX,
1271 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1272 .name = OVS_DATAPATH_MCGROUP
1275 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1276 u32 pid, u32 seq, u32 flags, u8 cmd)
1278 struct ovs_header *ovs_header;
1279 struct ovs_dp_stats dp_stats;
1282 ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1287 ovs_header->dp_ifindex = get_dpifindex(dp);
1290 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1293 goto nla_put_failure;
1295 get_dp_stats(dp, &dp_stats);
1296 NLA_PUT(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats);
1298 return genlmsg_end(skb, ovs_header);
1301 genlmsg_cancel(skb, ovs_header);
1306 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1309 struct sk_buff *skb;
1312 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1314 return ERR_PTR(-ENOMEM);
1316 retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1319 return ERR_PTR(retval);
1324 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1326 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1329 /* Called with genl_mutex and optionally with RTNL lock also. */
1330 static struct datapath *lookup_datapath(struct net *net,
1331 struct ovs_header *ovs_header,
1332 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1334 struct datapath *dp;
1336 if (!a[OVS_DP_ATTR_NAME])
1337 dp = get_dp(net, ovs_header->dp_ifindex);
1339 struct vport *vport;
1342 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1343 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1346 return dp ? dp : ERR_PTR(-ENODEV);
1349 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1351 struct nlattr **a = info->attrs;
1352 struct vport_parms parms;
1353 struct sk_buff *reply;
1354 struct datapath *dp;
1355 struct vport *vport;
1356 struct ovs_net *ovs_net;
1360 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1363 err = ovs_dp_cmd_validate(a);
1370 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1372 goto err_unlock_rtnl;
1374 INIT_LIST_HEAD(&dp->port_list);
1376 /* Initialize kobject for bridge. This will be added as
1377 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1378 dp->ifobj.kset = NULL;
1379 kobject_init(&dp->ifobj, &dp_ktype);
1381 /* Allocate table. */
1383 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1387 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1388 if (!dp->stats_percpu) {
1390 goto err_destroy_table;
1392 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1394 /* Set up our datapath device. */
1395 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1396 parms.type = OVS_VPORT_TYPE_INTERNAL;
1397 parms.options = NULL;
1399 parms.port_no = OVSP_LOCAL;
1400 parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1402 vport = new_vport(&parms);
1403 if (IS_ERR(vport)) {
1404 err = PTR_ERR(vport);
1408 goto err_destroy_percpu;
1411 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1412 info->snd_seq, OVS_DP_CMD_NEW);
1413 err = PTR_ERR(reply);
1415 goto err_destroy_local_port;
1417 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1418 list_add_tail(&dp->list_node, &ovs_net->dps);
1419 ovs_dp_sysfs_add_dp(dp);
1423 genl_notify(reply, genl_info_net(info), info->snd_pid,
1424 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1428 err_destroy_local_port:
1429 ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
1431 free_percpu(dp->stats_percpu);
1433 ovs_flow_tbl_destroy(genl_dereference(dp->table));
1442 /* Called with genl_mutex. */
1443 static void __dp_destroy(struct datapath *dp)
1445 struct vport *vport, *next_vport;
1448 list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
1449 if (vport->port_no != OVSP_LOCAL)
1450 ovs_dp_detach_port(vport);
1452 ovs_dp_sysfs_del_dp(dp);
1453 list_del(&dp->list_node);
1454 ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
1456 /* rtnl_unlock() will wait until all the references to devices that
1457 * are pending unregistration have been dropped. We do it here to
1458 * ensure that any internal devices (which contain DP pointers) are
1459 * fully destroyed before freeing the datapath.
1463 call_rcu(&dp->rcu, destroy_dp_rcu);
1466 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1468 struct sk_buff *reply;
1469 struct datapath *dp;
1472 err = ovs_dp_cmd_validate(info->attrs);
1476 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1481 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1482 info->snd_seq, OVS_DP_CMD_DEL);
1483 err = PTR_ERR(reply);
1489 genl_notify(reply, genl_info_net(info), info->snd_pid,
1490 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1496 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1498 struct sk_buff *reply;
1499 struct datapath *dp;
1502 err = ovs_dp_cmd_validate(info->attrs);
1506 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1510 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1511 info->snd_seq, OVS_DP_CMD_NEW);
1512 if (IS_ERR(reply)) {
1513 err = PTR_ERR(reply);
1514 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1515 ovs_dp_datapath_multicast_group.id, err);
1519 genl_notify(reply, genl_info_net(info), info->snd_pid,
1520 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1526 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1528 struct sk_buff *reply;
1529 struct datapath *dp;
1532 err = ovs_dp_cmd_validate(info->attrs);
1536 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1540 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1541 info->snd_seq, OVS_DP_CMD_NEW);
1543 return PTR_ERR(reply);
1545 return genlmsg_reply(reply, info);
1548 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1550 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1551 struct datapath *dp;
1552 int skip = cb->args[0];
1555 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1557 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1558 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1559 OVS_DP_CMD_NEW) < 0)
1569 static struct genl_ops dp_datapath_genl_ops[] = {
1570 { .cmd = OVS_DP_CMD_NEW,
1571 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1572 .policy = datapath_policy,
1573 .doit = ovs_dp_cmd_new
1575 { .cmd = OVS_DP_CMD_DEL,
1576 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1577 .policy = datapath_policy,
1578 .doit = ovs_dp_cmd_del
1580 { .cmd = OVS_DP_CMD_GET,
1581 .flags = 0, /* OK for unprivileged users. */
1582 .policy = datapath_policy,
1583 .doit = ovs_dp_cmd_get,
1584 .dumpit = ovs_dp_cmd_dump
1586 { .cmd = OVS_DP_CMD_SET,
1587 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1588 .policy = datapath_policy,
1589 .doit = ovs_dp_cmd_set,
1593 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1594 #ifdef HAVE_NLA_NUL_STRING
1595 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1596 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1597 [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1599 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1600 [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1602 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1603 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1604 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1605 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1608 static struct genl_family dp_vport_genl_family = {
1609 .id = GENL_ID_GENERATE,
1610 .hdrsize = sizeof(struct ovs_header),
1611 .name = OVS_VPORT_FAMILY,
1612 .version = OVS_VPORT_VERSION,
1613 .maxattr = OVS_VPORT_ATTR_MAX,
1617 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1618 .name = OVS_VPORT_MCGROUP
1621 /* Called with RTNL lock or RCU read lock. */
1622 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1623 u32 pid, u32 seq, u32 flags, u8 cmd)
1625 struct ovs_header *ovs_header;
1626 struct ovs_vport_stats vport_stats;
1629 ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1634 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1636 NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1637 NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type);
1638 NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport));
1639 NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1641 ovs_vport_get_stats(vport, &vport_stats);
1642 NLA_PUT(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1645 NLA_PUT(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1646 vport->ops->get_addr(vport));
1648 err = ovs_vport_get_options(vport, skb);
1649 if (err == -EMSGSIZE)
1652 return genlmsg_end(skb, ovs_header);
1657 genlmsg_cancel(skb, ovs_header);
1661 /* Called with RTNL lock or RCU read lock. */
1662 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1665 struct sk_buff *skb;
1668 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1670 return ERR_PTR(-ENOMEM);
1672 retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1675 return ERR_PTR(retval);
1680 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1682 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1685 /* Called with RTNL lock or RCU read lock. */
1686 static struct vport *lookup_vport(struct net *net,
1687 struct ovs_header *ovs_header,
1688 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1690 struct datapath *dp;
1691 struct vport *vport;
1693 if (a[OVS_VPORT_ATTR_NAME]) {
1694 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1696 return ERR_PTR(-ENODEV);
1698 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1699 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1701 if (port_no >= DP_MAX_PORTS)
1702 return ERR_PTR(-EFBIG);
1704 dp = get_dp(net, ovs_header->dp_ifindex);
1706 return ERR_PTR(-ENODEV);
1708 vport = rcu_dereference_rtnl(dp->ports[port_no]);
1710 return ERR_PTR(-ENOENT);
1713 return ERR_PTR(-EINVAL);
1716 /* Called with RTNL lock. */
1717 static int change_vport(struct vport *vport,
1718 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1722 if (a[OVS_VPORT_ATTR_STATS])
1723 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1725 if (a[OVS_VPORT_ATTR_ADDRESS])
1726 err = ovs_vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1731 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1733 struct nlattr **a = info->attrs;
1734 struct ovs_header *ovs_header = info->userhdr;
1735 struct vport_parms parms;
1736 struct sk_buff *reply;
1737 struct vport *vport;
1738 struct datapath *dp;
1743 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1744 !a[OVS_VPORT_ATTR_UPCALL_PID])
1747 err = ovs_vport_cmd_validate(a);
1752 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1757 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1758 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1761 if (port_no >= DP_MAX_PORTS)
1764 vport = rtnl_dereference(dp->ports[port_no]);
1769 for (port_no = 1; ; port_no++) {
1770 if (port_no >= DP_MAX_PORTS) {
1774 vport = rtnl_dereference(dp->ports[port_no]);
1780 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1781 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1782 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1784 parms.port_no = port_no;
1785 parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1787 vport = new_vport(&parms);
1788 err = PTR_ERR(vport);
1792 ovs_dp_sysfs_add_if(vport);
1794 err = change_vport(vport, a);
1796 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1800 err = PTR_ERR(reply);
1803 ovs_dp_detach_port(vport);
1806 genl_notify(reply, genl_info_net(info), info->snd_pid,
1807 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1815 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1817 struct nlattr **a = info->attrs;
1818 struct sk_buff *reply;
1819 struct vport *vport;
1822 err = ovs_vport_cmd_validate(a);
1827 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1828 err = PTR_ERR(vport);
1833 if (a[OVS_VPORT_ATTR_TYPE] &&
1834 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1837 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1838 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1840 err = change_vport(vport, a);
1841 if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1842 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1844 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1846 if (IS_ERR(reply)) {
1847 err = PTR_ERR(reply);
1848 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1849 ovs_dp_vport_multicast_group.id, err);
1853 genl_notify(reply, genl_info_net(info), info->snd_pid,
1854 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1862 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1864 struct nlattr **a = info->attrs;
1865 struct sk_buff *reply;
1866 struct vport *vport;
1869 err = ovs_vport_cmd_validate(a);
1874 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1875 err = PTR_ERR(vport);
1879 if (vport->port_no == OVSP_LOCAL) {
1884 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1886 err = PTR_ERR(reply);
1890 ovs_dp_detach_port(vport);
1892 genl_notify(reply, genl_info_net(info), info->snd_pid,
1893 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1901 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1903 struct nlattr **a = info->attrs;
1904 struct ovs_header *ovs_header = info->userhdr;
1905 struct sk_buff *reply;
1906 struct vport *vport;
1909 err = ovs_vport_cmd_validate(a);
1914 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1915 err = PTR_ERR(vport);
1919 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1921 err = PTR_ERR(reply);
1927 return genlmsg_reply(reply, info);
1935 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1937 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1938 struct datapath *dp;
1942 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1947 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1948 struct vport *vport;
1950 vport = rcu_dereference(dp->ports[port_no]);
1954 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1955 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1956 OVS_VPORT_CMD_NEW) < 0)
1961 cb->args[0] = port_no;
1967 static struct genl_ops dp_vport_genl_ops[] = {
1968 { .cmd = OVS_VPORT_CMD_NEW,
1969 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1970 .policy = vport_policy,
1971 .doit = ovs_vport_cmd_new
1973 { .cmd = OVS_VPORT_CMD_DEL,
1974 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1975 .policy = vport_policy,
1976 .doit = ovs_vport_cmd_del
1978 { .cmd = OVS_VPORT_CMD_GET,
1979 .flags = 0, /* OK for unprivileged users. */
1980 .policy = vport_policy,
1981 .doit = ovs_vport_cmd_get,
1982 .dumpit = ovs_vport_cmd_dump
1984 { .cmd = OVS_VPORT_CMD_SET,
1985 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1986 .policy = vport_policy,
1987 .doit = ovs_vport_cmd_set,
1991 struct genl_family_and_ops {
1992 struct genl_family *family;
1993 struct genl_ops *ops;
1995 struct genl_multicast_group *group;
1998 static const struct genl_family_and_ops dp_genl_families[] = {
1999 { &dp_datapath_genl_family,
2000 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2001 &ovs_dp_datapath_multicast_group },
2002 { &dp_vport_genl_family,
2003 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2004 &ovs_dp_vport_multicast_group },
2005 { &dp_flow_genl_family,
2006 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2007 &ovs_dp_flow_multicast_group },
2008 { &dp_packet_genl_family,
2009 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2013 static void dp_unregister_genl(int n_families)
2017 for (i = 0; i < n_families; i++)
2018 genl_unregister_family(dp_genl_families[i].family);
2021 static int dp_register_genl(void)
2028 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2029 const struct genl_family_and_ops *f = &dp_genl_families[i];
2031 err = genl_register_family_with_ops(f->family, f->ops,
2038 err = genl_register_mc_group(f->family, f->group);
2047 dp_unregister_genl(n_registered);
2051 static int __rehash_flow_table(void *dummy)
2053 struct datapath *dp;
2058 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2060 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2061 struct flow_table *old_table = genl_dereference(dp->table);
2062 struct flow_table *new_table;
2064 new_table = ovs_flow_tbl_rehash(old_table);
2065 if (!IS_ERR(new_table)) {
2066 rcu_assign_pointer(dp->table, new_table);
2067 ovs_flow_tbl_deferred_destroy(old_table);
2075 static void rehash_flow_table(struct work_struct *work)
2077 genl_exec(__rehash_flow_table, NULL);
2078 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2081 static int dp_destroy_all(void *data)
2083 struct datapath *dp, *dp_next;
2084 struct ovs_net *ovs_net = data;
2086 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2092 static int __net_init ovs_init_net(struct net *net)
2094 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2096 INIT_LIST_HEAD(&ovs_net->dps);
2100 static void __net_exit ovs_exit_net(struct net *net)
2102 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2104 genl_exec(dp_destroy_all, ovs_net);
2107 static struct pernet_operations ovs_net_ops = {
2108 .init = ovs_init_net,
2109 .exit = ovs_exit_net,
2111 .size = sizeof(struct ovs_net),
2114 static int __init dp_init(void)
2116 struct sk_buff *dummy_skb;
2119 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2121 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2124 err = genl_exec_init();
2128 err = ovs_workqueues_init();
2130 goto error_genl_exec;
2132 err = ovs_tnl_init();
2136 err = ovs_flow_init();
2138 goto error_tnl_exit;
2140 err = ovs_vport_init();
2142 goto error_flow_exit;
2144 err = register_pernet_device(&ovs_net_ops);
2146 goto error_vport_exit;
2148 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2150 goto error_netns_exit;
2152 err = dp_register_genl();
2154 goto error_unreg_notifier;
2156 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2160 error_unreg_notifier:
2161 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2163 unregister_pernet_device(&ovs_net_ops);
2171 ovs_workqueues_exit();
2178 static void dp_cleanup(void)
2180 cancel_delayed_work_sync(&rehash_flow_wq);
2181 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2182 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2183 unregister_pernet_device(&ovs_net_ops);
2188 ovs_workqueues_exit();
2192 module_init(dp_init);
2193 module_exit(dp_cleanup);
2195 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2196 MODULE_LICENSE("GPL");