2 * Copyright (c) 2007, 2008, 2009, 2010, 2011 Nicira Networks.
3 * Distributed under the terms of the GNU GPL version 2.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
9 /* Functions for managing the dp interface/device. */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/if_arp.h>
16 #include <linux/if_vlan.h>
19 #include <linux/jhash.h>
20 #include <linux/delay.h>
21 #include <linux/time.h>
22 #include <linux/etherdevice.h>
23 #include <linux/genetlink.h>
24 #include <linux/kernel.h>
25 #include <linux/kthread.h>
26 #include <linux/mutex.h>
27 #include <linux/percpu.h>
28 #include <linux/rcupdate.h>
29 #include <linux/tcp.h>
30 #include <linux/udp.h>
31 #include <linux/version.h>
32 #include <linux/ethtool.h>
33 #include <linux/wait.h>
34 #include <asm/system.h>
35 #include <asm/div64.h>
36 #include <linux/highmem.h>
37 #include <linux/netfilter_bridge.h>
38 #include <linux/netfilter_ipv4.h>
39 #include <linux/inetdevice.h>
40 #include <linux/list.h>
41 #include <linux/openvswitch.h>
42 #include <linux/rculist.h>
43 #include <linux/dmi.h>
44 #include <net/inet_ecn.h>
45 #include <net/genetlink.h>
53 #include "vport-internal_dev.h"
55 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
56 LINUX_VERSION_CODE > KERNEL_VERSION(3,2,0)
57 #error Kernels before 2.6.18 or after 3.2 are not supported by this version of Open vSwitch.
60 int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
61 EXPORT_SYMBOL(dp_ioctl_hook);
66 * Writes to device state (add/remove datapath, port, set operations on vports,
67 * etc.) are protected by RTNL.
69 * Writes to other state (flow table modifications, set miscellaneous datapath
70 * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
73 * Reads are protected by RCU.
75 * There are a few special cases (mostly stats) that have their own
76 * synchronization but they nest under all of above and don't interact with
80 /* Global list of datapaths to enable dumping them all out.
81 * Protected by genl_mutex.
83 static LIST_HEAD(dps);
85 static struct vport *new_vport(const struct vport_parms *);
86 static int queue_gso_packets(int dp_ifindex, struct sk_buff *,
87 const struct dp_upcall_info *);
88 static int queue_userspace_packet(int dp_ifindex, struct sk_buff *,
89 const struct dp_upcall_info *);
91 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
92 struct datapath *get_dp(int dp_ifindex)
94 struct datapath *dp = NULL;
95 struct net_device *dev;
98 dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
100 struct vport *vport = internal_dev_get_vport(dev);
108 EXPORT_SYMBOL_GPL(get_dp);
110 /* Must be called with genl_mutex. */
111 static struct flow_table *get_table_protected(struct datapath *dp)
113 return rcu_dereference_protected(dp->table, lockdep_genl_is_held());
116 /* Must be called with rcu_read_lock or RTNL lock. */
117 static struct vport *get_vport_protected(struct datapath *dp, u16 port_no)
119 return rcu_dereference_rtnl(dp->ports[port_no]);
122 /* Must be called with rcu_read_lock or RTNL lock. */
123 const char *dp_name(const struct datapath *dp)
125 struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
126 return vport->ops->get_name(vport);
129 static int get_dpifindex(struct datapath *dp)
136 local = get_vport_protected(dp, OVSP_LOCAL);
138 ifindex = local->ops->get_ifindex(local);
147 static size_t br_nlmsg_size(void)
149 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
150 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
151 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
152 + nla_total_size(4) /* IFLA_MASTER */
153 + nla_total_size(4) /* IFLA_MTU */
154 + nla_total_size(1); /* IFLA_OPERSTATE */
157 /* Caller must hold RTNL lock. */
158 static int dp_fill_ifinfo(struct sk_buff *skb,
159 const struct vport *port,
160 int event, unsigned int flags)
162 struct datapath *dp = port->dp;
163 struct ifinfomsg *hdr;
164 struct nlmsghdr *nlh;
166 if (!port->ops->get_ifindex)
169 nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
173 hdr = nlmsg_data(nlh);
174 hdr->ifi_family = AF_BRIDGE;
176 hdr->ifi_type = ARPHRD_ETHER;
177 hdr->ifi_index = port->ops->get_ifindex(port);
178 hdr->ifi_flags = port->ops->get_dev_flags(port);
181 NLA_PUT_STRING(skb, IFLA_IFNAME, port->ops->get_name(port));
182 NLA_PUT_U32(skb, IFLA_MASTER, get_dpifindex(dp));
183 NLA_PUT_U32(skb, IFLA_MTU, port->ops->get_mtu(port));
184 #ifdef IFLA_OPERSTATE
185 NLA_PUT_U8(skb, IFLA_OPERSTATE,
186 port->ops->is_running(port)
187 ? port->ops->get_operstate(port)
191 NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, port->ops->get_addr(port));
193 return nlmsg_end(skb, nlh);
196 nlmsg_cancel(skb, nlh);
200 /* Caller must hold RTNL lock. */
201 static void dp_ifinfo_notify(int event, struct vport *port)
206 skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
212 err = dp_fill_ifinfo(skb, port, event, 0);
214 if (err == -ENODEV) {
217 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
218 WARN_ON(err == -EMSGSIZE);
223 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
227 rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
232 static void release_dp(struct kobject *kobj)
234 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
238 static struct kobj_type dp_ktype = {
239 .release = release_dp
242 static void destroy_dp_rcu(struct rcu_head *rcu)
244 struct datapath *dp = container_of(rcu, struct datapath, rcu);
246 flow_tbl_destroy(dp->table);
247 free_percpu(dp->stats_percpu);
248 kobject_put(&dp->ifobj);
251 /* Called with RTNL lock and genl_lock. */
252 static struct vport *new_vport(const struct vport_parms *parms)
256 vport = vport_add(parms);
257 if (!IS_ERR(vport)) {
258 struct datapath *dp = parms->dp;
260 rcu_assign_pointer(dp->ports[parms->port_no], vport);
261 list_add(&vport->node, &dp->port_list);
263 dp_ifinfo_notify(RTM_NEWLINK, vport);
269 /* Called with RTNL lock. */
270 void dp_detach_port(struct vport *p)
274 if (p->port_no != OVSP_LOCAL)
276 dp_ifinfo_notify(RTM_DELLINK, p);
278 /* First drop references to device. */
280 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
282 /* Then destroy it. */
286 /* Must be called with rcu_read_lock. */
287 void dp_process_received_packet(struct vport *p, struct sk_buff *skb)
289 struct datapath *dp = p->dp;
290 struct sw_flow *flow;
291 struct dp_stats_percpu *stats;
295 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
297 if (!OVS_CB(skb)->flow) {
298 struct sw_flow_key key;
301 /* Extract flow from 'skb' into 'key'. */
302 error = flow_extract(skb, p->port_no, &key, &key_len);
303 if (unlikely(error)) {
309 flow = flow_tbl_lookup(rcu_dereference(dp->table),
311 if (unlikely(!flow)) {
312 struct dp_upcall_info upcall;
314 upcall.cmd = OVS_PACKET_CMD_MISS;
316 upcall.userdata = NULL;
317 upcall.pid = p->upcall_pid;
318 dp_upcall(dp, skb, &upcall);
320 stats_counter = &stats->n_missed;
324 OVS_CB(skb)->flow = flow;
327 stats_counter = &stats->n_hit;
328 flow_used(OVS_CB(skb)->flow, skb);
329 execute_actions(dp, skb);
332 /* Update datapath statistics. */
334 write_seqcount_begin(&stats->seqlock);
336 write_seqcount_end(&stats->seqlock);
339 static void copy_and_csum_skb(struct sk_buff *skb, void *to)
341 u16 csum_start, csum_offset;
344 get_skb_csum_pointers(skb, &csum_start, &csum_offset);
345 csum_start -= skb_headroom(skb);
347 skb_copy_bits(skb, 0, to, csum_start);
349 csum = skb_copy_and_csum_bits(skb, csum_start, to + csum_start,
350 skb->len - csum_start, 0);
351 *(__sum16 *)(to + csum_start + csum_offset) = csum_fold(csum);
354 static struct genl_family dp_packet_genl_family = {
355 .id = GENL_ID_GENERATE,
356 .hdrsize = sizeof(struct ovs_header),
357 .name = OVS_PACKET_FAMILY,
358 .version = OVS_PACKET_VERSION,
359 .maxattr = OVS_PACKET_ATTR_MAX
362 int dp_upcall(struct datapath *dp, struct sk_buff *skb,
363 const struct dp_upcall_info *upcall_info)
365 struct dp_stats_percpu *stats;
369 if (upcall_info->pid == 0) {
374 dp_ifindex = get_dpifindex(dp);
380 forward_ip_summed(skb, true);
382 if (!skb_is_gso(skb))
383 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
385 err = queue_gso_packets(dp_ifindex, skb, upcall_info);
392 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
394 write_seqcount_begin(&stats->seqlock);
396 write_seqcount_end(&stats->seqlock);
401 static int queue_gso_packets(int dp_ifindex, struct sk_buff *skb,
402 const struct dp_upcall_info *upcall_info)
404 struct dp_upcall_info later_info;
405 struct sw_flow_key later_key;
406 struct sk_buff *segs, *nskb;
409 segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
413 /* Queue all of the segments. */
416 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
420 if (skb == segs && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
421 /* The initial flow key extracted by flow_extract() in
422 * this case is for a first fragment, so we need to
423 * properly mark later fragments.
425 later_key = *upcall_info->key;
426 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
428 later_info = *upcall_info;
429 later_info.key = &later_key;
430 upcall_info = &later_info;
432 } while ((skb = skb->next));
434 /* Free all of the segments. */
442 } while ((skb = nskb));
446 static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
447 const struct dp_upcall_info *upcall_info)
449 struct ovs_header *upcall;
450 struct sk_buff *user_skb; /* to be queued to userspace */
455 err = vlan_deaccel_tag(skb);
459 if (nla_attr_size(skb->len) > USHRT_MAX)
462 len = sizeof(struct ovs_header);
463 len += nla_total_size(skb->len);
464 len += nla_total_size(FLOW_BUFSIZE);
465 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
466 len += nla_total_size(8);
468 user_skb = genlmsg_new(len, GFP_ATOMIC);
472 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
473 0, upcall_info->cmd);
474 upcall->dp_ifindex = dp_ifindex;
476 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
477 flow_to_nlattrs(upcall_info->key, user_skb);
478 nla_nest_end(user_skb, nla);
480 if (upcall_info->userdata)
481 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
482 nla_get_u64(upcall_info->userdata));
484 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
485 if (skb->ip_summed == CHECKSUM_PARTIAL)
486 copy_and_csum_skb(skb, nla_data(nla));
488 skb_copy_bits(skb, 0, nla_data(nla), skb->len);
490 return genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
493 /* Called with genl_mutex. */
494 static int flush_flows(int dp_ifindex)
496 struct flow_table *old_table;
497 struct flow_table *new_table;
500 dp = get_dp(dp_ifindex);
504 old_table = get_table_protected(dp);
505 new_table = flow_tbl_alloc(TBL_MIN_BUCKETS);
509 rcu_assign_pointer(dp->table, new_table);
511 flow_tbl_deferred_destroy(old_table);
515 static int validate_actions(const struct nlattr *attr,
516 const struct sw_flow_key *key, int depth);
518 static int validate_sample(const struct nlattr *attr,
519 const struct sw_flow_key *key, int depth)
521 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
522 const struct nlattr *probability, *actions;
523 const struct nlattr *a;
526 memset(attrs, 0, sizeof(attrs));
527 nla_for_each_nested(a, attr, rem) {
528 int type = nla_type(a);
529 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
536 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
537 if (!probability || nla_len(probability) != sizeof(u32))
540 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
541 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
543 return validate_actions(actions, key, depth + 1);
546 static int validate_action_key(const struct nlattr *a,
547 const struct sw_flow_key *flow_key)
549 int act_type = nla_type(a);
550 const struct nlattr *ovs_key = nla_data(a);
551 int key_type = nla_type(ovs_key);
553 /* There can be only one key in a action */
554 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
557 if (key_type > OVS_KEY_ATTR_MAX ||
558 nla_len(ovs_key) != ovs_key_lens[key_type])
561 #define ACTION(act, key) (((act) << 8) | (key))
563 switch (ACTION(act_type, key_type)) {
564 const struct ovs_key_ipv4 *ipv4_key;
565 const struct ovs_key_8021q *q_key;
567 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_PRIORITY):
568 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_TUN_ID):
569 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_ETHERNET):
572 case ACTION(OVS_ACTION_ATTR_PUSH, OVS_KEY_ATTR_8021Q):
573 q_key = nla_data(ovs_key);
574 if (q_key->q_tpid != htons(ETH_P_8021Q))
577 if (q_key->q_tci & htons(VLAN_TAG_PRESENT))
581 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_IPV4):
582 if (flow_key->eth.type != htons(ETH_P_IP))
585 if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
588 ipv4_key = nla_data(ovs_key);
589 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
592 if (ipv4_key->ipv4_tos & INET_ECN_MASK)
595 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
600 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_TCP):
601 if (flow_key->ip.proto != IPPROTO_TCP)
604 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
609 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_UDP):
610 if (flow_key->ip.proto != IPPROTO_UDP)
613 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
624 static int validate_userspace(const struct nlattr *attr)
626 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
627 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
628 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
630 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
633 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
634 attr, userspace_policy);
638 if (!a[OVS_USERSPACE_ATTR_PID] ||
639 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
645 static int validate_actions(const struct nlattr *attr,
646 const struct sw_flow_key *key, int depth)
648 const struct nlattr *a;
651 if (depth >= SAMPLE_ACTION_DEPTH)
654 nla_for_each_nested(a, attr, rem) {
655 /* Expected argument lengths, (u32)-1 for variable length. */
656 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
657 [OVS_ACTION_ATTR_OUTPUT] = 4,
658 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
659 [OVS_ACTION_ATTR_PUSH] = (u32)-1,
660 [OVS_ACTION_ATTR_POP] = 2,
661 [OVS_ACTION_ATTR_SET] = (u32)-1,
662 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
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:
688 if (nla_get_u16(a) != OVS_KEY_ATTR_8021Q)
692 case OVS_ACTION_ATTR_SET:
693 case OVS_ACTION_ATTR_PUSH:
694 err = validate_action_key(a, key);
699 case OVS_ACTION_ATTR_SAMPLE:
700 err = validate_sample(a, key, depth);
716 static void clear_stats(struct sw_flow *flow)
720 flow->packet_count = 0;
721 flow->byte_count = 0;
724 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
726 struct ovs_header *ovs_header = info->userhdr;
727 struct nlattr **a = info->attrs;
728 struct sw_flow_actions *acts;
729 struct sk_buff *packet;
730 struct sw_flow *flow;
738 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
739 !a[OVS_PACKET_ATTR_ACTIONS] ||
740 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
743 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
744 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
748 skb_reserve(packet, NET_IP_ALIGN);
750 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
752 skb_reset_mac_header(packet);
753 eth = eth_hdr(packet);
755 /* Normally, setting the skb 'protocol' field would be handled by a
756 * call to eth_type_trans(), but it assumes there's a sending
757 * device, which we may not have. */
758 if (ntohs(eth->h_proto) >= 1536)
759 packet->protocol = eth->h_proto;
761 packet->protocol = htons(ETH_P_802_2);
763 /* Build an sw_flow for sending this packet. */
769 err = flow_extract(packet, -1, &flow->key, &key_len);
773 err = flow_metadata_from_nlattrs(&flow->key.phy.priority,
774 &flow->key.phy.in_port,
775 &flow->key.phy.tun_id,
776 a[OVS_PACKET_ATTR_KEY]);
780 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
784 flow->hash = flow_hash(&flow->key, key_len);
786 acts = flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
790 rcu_assign_pointer(flow->sf_acts, acts);
792 OVS_CB(packet)->flow = flow;
793 packet->priority = flow->key.phy.priority;
796 dp = get_dp(ovs_header->dp_ifindex);
802 err = execute_actions(dp, packet);
819 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
820 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
821 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
822 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
825 static struct genl_ops dp_packet_genl_ops[] = {
826 { .cmd = OVS_PACKET_CMD_EXECUTE,
827 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
828 .policy = packet_policy,
829 .doit = ovs_packet_cmd_execute
833 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
836 struct flow_table *table = get_table_protected(dp);
838 stats->n_flows = flow_tbl_count(table);
840 stats->n_hit = stats->n_missed = stats->n_lost = 0;
841 for_each_possible_cpu(i) {
842 const struct dp_stats_percpu *percpu_stats;
843 struct dp_stats_percpu local_stats;
846 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
849 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
850 local_stats = *percpu_stats;
851 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
853 stats->n_hit += local_stats.n_hit;
854 stats->n_missed += local_stats.n_missed;
855 stats->n_lost += local_stats.n_lost;
859 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
860 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
861 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
862 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
865 static struct genl_family dp_flow_genl_family = {
866 .id = GENL_ID_GENERATE,
867 .hdrsize = sizeof(struct ovs_header),
868 .name = OVS_FLOW_FAMILY,
869 .version = OVS_FLOW_VERSION,
870 .maxattr = OVS_FLOW_ATTR_MAX
873 static struct genl_multicast_group dp_flow_multicast_group = {
874 .name = OVS_FLOW_MCGROUP
877 /* Called with genl_lock. */
878 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
879 struct sk_buff *skb, u32 pid,
880 u32 seq, u32 flags, u8 cmd)
882 const int skb_orig_len = skb->len;
883 const struct sw_flow_actions *sf_acts;
884 struct ovs_flow_stats stats;
885 struct ovs_header *ovs_header;
891 sf_acts = rcu_dereference_protected(flow->sf_acts,
892 lockdep_genl_is_held());
894 ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
898 ovs_header->dp_ifindex = get_dpifindex(dp);
900 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
902 goto nla_put_failure;
903 err = flow_to_nlattrs(&flow->key, skb);
906 nla_nest_end(skb, nla);
908 spin_lock_bh(&flow->lock);
910 stats.n_packets = flow->packet_count;
911 stats.n_bytes = flow->byte_count;
912 tcp_flags = flow->tcp_flags;
913 spin_unlock_bh(&flow->lock);
916 NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, flow_used_time(used));
919 NLA_PUT(skb, OVS_FLOW_ATTR_STATS,
920 sizeof(struct ovs_flow_stats), &stats);
923 NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
925 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
926 * this is the first flow to be dumped into 'skb'. This is unusual for
927 * Netlink but individual action lists can be longer than
928 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
929 * The userspace caller can always fetch the actions separately if it
930 * really wants them. (Most userspace callers in fact don't care.)
932 * This can only fail for dump operations because the skb is always
933 * properly sized for single flows.
935 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
937 if (err < 0 && skb_orig_len)
940 return genlmsg_end(skb, ovs_header);
945 genlmsg_cancel(skb, ovs_header);
949 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
951 const struct sw_flow_actions *sf_acts;
954 sf_acts = rcu_dereference_protected(flow->sf_acts,
955 lockdep_genl_is_held());
957 /* OVS_FLOW_ATTR_KEY */
958 len = nla_total_size(FLOW_BUFSIZE);
959 /* OVS_FLOW_ATTR_ACTIONS */
960 len += nla_total_size(sf_acts->actions_len);
961 /* OVS_FLOW_ATTR_STATS */
962 len += nla_total_size(sizeof(struct ovs_flow_stats));
963 /* OVS_FLOW_ATTR_TCP_FLAGS */
964 len += nla_total_size(1);
965 /* OVS_FLOW_ATTR_USED */
966 len += nla_total_size(8);
968 len += NLMSG_ALIGN(sizeof(struct ovs_header));
970 return genlmsg_new(len, GFP_KERNEL);
973 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
975 u32 pid, u32 seq, u8 cmd)
980 skb = ovs_flow_cmd_alloc_info(flow);
982 return ERR_PTR(-ENOMEM);
984 retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
989 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
991 struct nlattr **a = info->attrs;
992 struct ovs_header *ovs_header = info->userhdr;
993 struct sw_flow_key key;
994 struct sw_flow *flow;
995 struct sk_buff *reply;
997 struct flow_table *table;
1003 if (!a[OVS_FLOW_ATTR_KEY])
1005 error = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1009 /* Validate actions. */
1010 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1011 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
1014 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1019 dp = get_dp(ovs_header->dp_ifindex);
1024 table = get_table_protected(dp);
1025 flow = flow_tbl_lookup(table, &key, key_len);
1027 struct sw_flow_actions *acts;
1029 /* Bail out if we're not allowed to create a new flow. */
1031 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1034 /* Expand table, if necessary, to make room. */
1035 if (flow_tbl_need_to_expand(table)) {
1036 struct flow_table *new_table;
1038 new_table = flow_tbl_expand(table);
1039 if (!IS_ERR(new_table)) {
1040 rcu_assign_pointer(dp->table, new_table);
1041 flow_tbl_deferred_destroy(table);
1042 table = get_table_protected(dp);
1046 /* Allocate flow. */
1047 flow = flow_alloc();
1049 error = PTR_ERR(flow);
1055 /* Obtain actions. */
1056 acts = flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1057 error = PTR_ERR(acts);
1059 goto error_free_flow;
1060 rcu_assign_pointer(flow->sf_acts, acts);
1062 /* Put flow in bucket. */
1063 flow->hash = flow_hash(&key, key_len);
1064 flow_tbl_insert(table, flow);
1066 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1070 /* We found a matching flow. */
1071 struct sw_flow_actions *old_acts;
1072 struct nlattr *acts_attrs;
1074 /* Bail out if we're not allowed to modify an existing flow.
1075 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1076 * because Generic Netlink treats the latter as a dump
1077 * request. We also accept NLM_F_EXCL in case that bug ever
1081 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1082 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1085 /* Update actions. */
1086 old_acts = rcu_dereference_protected(flow->sf_acts,
1087 lockdep_genl_is_held());
1088 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1090 (old_acts->actions_len != nla_len(acts_attrs) ||
1091 memcmp(old_acts->actions, nla_data(acts_attrs),
1092 old_acts->actions_len))) {
1093 struct sw_flow_actions *new_acts;
1095 new_acts = flow_actions_alloc(acts_attrs);
1096 error = PTR_ERR(new_acts);
1097 if (IS_ERR(new_acts))
1100 rcu_assign_pointer(flow->sf_acts, new_acts);
1101 flow_deferred_free_acts(old_acts);
1104 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1105 info->snd_seq, OVS_FLOW_CMD_NEW);
1108 if (a[OVS_FLOW_ATTR_CLEAR]) {
1109 spin_lock_bh(&flow->lock);
1111 spin_unlock_bh(&flow->lock);
1116 genl_notify(reply, genl_info_net(info), info->snd_pid,
1117 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1119 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1120 dp_flow_multicast_group.id, PTR_ERR(reply));
1129 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1131 struct nlattr **a = info->attrs;
1132 struct ovs_header *ovs_header = info->userhdr;
1133 struct sw_flow_key key;
1134 struct sk_buff *reply;
1135 struct sw_flow *flow;
1136 struct datapath *dp;
1137 struct flow_table *table;
1141 if (!a[OVS_FLOW_ATTR_KEY])
1143 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1147 dp = get_dp(ovs_header->dp_ifindex);
1151 table = get_table_protected(dp);
1152 flow = flow_tbl_lookup(table, &key, key_len);
1156 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1157 info->snd_seq, OVS_FLOW_CMD_NEW);
1159 return PTR_ERR(reply);
1161 return genlmsg_reply(reply, info);
1164 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1166 struct nlattr **a = info->attrs;
1167 struct ovs_header *ovs_header = info->userhdr;
1168 struct sw_flow_key key;
1169 struct sk_buff *reply;
1170 struct sw_flow *flow;
1171 struct datapath *dp;
1172 struct flow_table *table;
1176 if (!a[OVS_FLOW_ATTR_KEY])
1177 return flush_flows(ovs_header->dp_ifindex);
1178 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1182 dp = get_dp(ovs_header->dp_ifindex);
1186 table = get_table_protected(dp);
1187 flow = flow_tbl_lookup(table, &key, key_len);
1191 reply = ovs_flow_cmd_alloc_info(flow);
1195 flow_tbl_remove(table, flow);
1197 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1198 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1201 flow_deferred_free(flow);
1203 genl_notify(reply, genl_info_net(info), info->snd_pid,
1204 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1208 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1210 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1211 struct datapath *dp;
1213 dp = get_dp(ovs_header->dp_ifindex);
1218 struct sw_flow *flow;
1221 bucket = cb->args[0];
1223 flow = flow_tbl_next(get_table_protected(dp), &bucket, &obj);
1227 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1228 NETLINK_CB(cb->skb).pid,
1229 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1230 OVS_FLOW_CMD_NEW) < 0)
1233 cb->args[0] = bucket;
1239 static struct genl_ops dp_flow_genl_ops[] = {
1240 { .cmd = OVS_FLOW_CMD_NEW,
1241 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1242 .policy = flow_policy,
1243 .doit = ovs_flow_cmd_new_or_set
1245 { .cmd = OVS_FLOW_CMD_DEL,
1246 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1247 .policy = flow_policy,
1248 .doit = ovs_flow_cmd_del
1250 { .cmd = OVS_FLOW_CMD_GET,
1251 .flags = 0, /* OK for unprivileged users. */
1252 .policy = flow_policy,
1253 .doit = ovs_flow_cmd_get,
1254 .dumpit = ovs_flow_cmd_dump
1256 { .cmd = OVS_FLOW_CMD_SET,
1257 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1258 .policy = flow_policy,
1259 .doit = ovs_flow_cmd_new_or_set,
1263 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1264 #ifdef HAVE_NLA_NUL_STRING
1265 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1267 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1270 static struct genl_family dp_datapath_genl_family = {
1271 .id = GENL_ID_GENERATE,
1272 .hdrsize = sizeof(struct ovs_header),
1273 .name = OVS_DATAPATH_FAMILY,
1274 .version = OVS_DATAPATH_VERSION,
1275 .maxattr = OVS_DP_ATTR_MAX
1278 static struct genl_multicast_group dp_datapath_multicast_group = {
1279 .name = OVS_DATAPATH_MCGROUP
1282 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1283 u32 pid, u32 seq, u32 flags, u8 cmd)
1285 struct ovs_header *ovs_header;
1289 ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1294 ovs_header->dp_ifindex = get_dpifindex(dp);
1297 err = nla_put_string(skb, OVS_DP_ATTR_NAME, dp_name(dp));
1300 goto nla_put_failure;
1302 nla = nla_reserve(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats));
1304 goto nla_put_failure;
1305 get_dp_stats(dp, nla_data(nla));
1307 return genlmsg_end(skb, ovs_header);
1310 genlmsg_cancel(skb, ovs_header);
1315 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1318 struct sk_buff *skb;
1321 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1323 return ERR_PTR(-ENOMEM);
1325 retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1328 return ERR_PTR(retval);
1333 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1335 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1338 /* Called with genl_mutex and optionally with RTNL lock also. */
1339 static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
1340 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1342 struct datapath *dp;
1344 if (!a[OVS_DP_ATTR_NAME])
1345 dp = get_dp(ovs_header->dp_ifindex);
1347 struct vport *vport;
1350 vport = vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
1351 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1354 return dp ? dp : ERR_PTR(-ENODEV);
1357 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1359 struct nlattr **a = info->attrs;
1360 struct vport_parms parms;
1361 struct sk_buff *reply;
1362 struct datapath *dp;
1363 struct vport *vport;
1367 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1370 err = ovs_dp_cmd_validate(a);
1376 if (!try_module_get(THIS_MODULE))
1377 goto err_unlock_rtnl;
1380 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1382 goto err_put_module;
1383 INIT_LIST_HEAD(&dp->port_list);
1385 /* Initialize kobject for bridge. This will be added as
1386 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1387 dp->ifobj.kset = NULL;
1388 kobject_init(&dp->ifobj, &dp_ktype);
1390 /* Allocate table. */
1392 rcu_assign_pointer(dp->table, flow_tbl_alloc(TBL_MIN_BUCKETS));
1396 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1397 if (!dp->stats_percpu) {
1399 goto err_destroy_table;
1402 /* Set up our datapath device. */
1403 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1404 parms.type = OVS_VPORT_TYPE_INTERNAL;
1405 parms.options = NULL;
1407 parms.port_no = OVSP_LOCAL;
1408 parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1410 vport = new_vport(&parms);
1411 if (IS_ERR(vport)) {
1412 err = PTR_ERR(vport);
1416 goto err_destroy_percpu;
1419 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1420 info->snd_seq, OVS_DP_CMD_NEW);
1421 err = PTR_ERR(reply);
1423 goto err_destroy_local_port;
1425 list_add_tail(&dp->list_node, &dps);
1426 dp_sysfs_add_dp(dp);
1430 genl_notify(reply, genl_info_net(info), info->snd_pid,
1431 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1434 err_destroy_local_port:
1435 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1437 free_percpu(dp->stats_percpu);
1439 flow_tbl_destroy(get_table_protected(dp));
1443 module_put(THIS_MODULE);
1450 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1452 struct vport *vport, *next_vport;
1453 struct sk_buff *reply;
1454 struct datapath *dp;
1457 err = ovs_dp_cmd_validate(info->attrs);
1462 dp = lookup_datapath(info->userhdr, info->attrs);
1467 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1468 info->snd_seq, OVS_DP_CMD_DEL);
1469 err = PTR_ERR(reply);
1473 list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
1474 if (vport->port_no != OVSP_LOCAL)
1475 dp_detach_port(vport);
1477 dp_sysfs_del_dp(dp);
1478 list_del(&dp->list_node);
1479 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1481 /* rtnl_unlock() will wait until all the references to devices that
1482 * are pending unregistration have been dropped. We do it here to
1483 * ensure that any internal devices (which contain DP pointers) are
1484 * fully destroyed before freeing the datapath.
1488 call_rcu(&dp->rcu, destroy_dp_rcu);
1489 module_put(THIS_MODULE);
1491 genl_notify(reply, genl_info_net(info), info->snd_pid,
1492 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1502 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1504 struct sk_buff *reply;
1505 struct datapath *dp;
1508 err = ovs_dp_cmd_validate(info->attrs);
1512 dp = lookup_datapath(info->userhdr, info->attrs);
1516 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1517 info->snd_seq, OVS_DP_CMD_NEW);
1518 if (IS_ERR(reply)) {
1519 err = PTR_ERR(reply);
1520 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1521 dp_datapath_multicast_group.id, err);
1525 genl_notify(reply, genl_info_net(info), info->snd_pid,
1526 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1530 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1532 struct sk_buff *reply;
1533 struct datapath *dp;
1536 err = ovs_dp_cmd_validate(info->attrs);
1540 dp = lookup_datapath(info->userhdr, info->attrs);
1544 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1545 info->snd_seq, OVS_DP_CMD_NEW);
1547 return PTR_ERR(reply);
1549 return genlmsg_reply(reply, info);
1552 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1554 struct datapath *dp;
1555 int skip = cb->args[0];
1558 list_for_each_entry(dp, &dps, list_node) {
1561 if (ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1562 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1563 OVS_DP_CMD_NEW) < 0)
1573 static struct genl_ops dp_datapath_genl_ops[] = {
1574 { .cmd = OVS_DP_CMD_NEW,
1575 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1576 .policy = datapath_policy,
1577 .doit = ovs_dp_cmd_new
1579 { .cmd = OVS_DP_CMD_DEL,
1580 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1581 .policy = datapath_policy,
1582 .doit = ovs_dp_cmd_del
1584 { .cmd = OVS_DP_CMD_GET,
1585 .flags = 0, /* OK for unprivileged users. */
1586 .policy = datapath_policy,
1587 .doit = ovs_dp_cmd_get,
1588 .dumpit = ovs_dp_cmd_dump
1590 { .cmd = OVS_DP_CMD_SET,
1591 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1592 .policy = datapath_policy,
1593 .doit = ovs_dp_cmd_set,
1597 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1598 #ifdef HAVE_NLA_NUL_STRING
1599 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1600 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1601 [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1603 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1604 [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1606 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1607 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1608 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1609 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1612 static struct genl_family dp_vport_genl_family = {
1613 .id = GENL_ID_GENERATE,
1614 .hdrsize = sizeof(struct ovs_header),
1615 .name = OVS_VPORT_FAMILY,
1616 .version = OVS_VPORT_VERSION,
1617 .maxattr = OVS_VPORT_ATTR_MAX
1620 struct genl_multicast_group dp_vport_multicast_group = {
1621 .name = OVS_VPORT_MCGROUP
1624 /* Called with RTNL lock or RCU read lock. */
1625 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1626 u32 pid, u32 seq, u32 flags, u8 cmd)
1628 struct ovs_header *ovs_header;
1632 ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1637 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1639 NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1640 NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type);
1641 NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport));
1642 NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1644 nla = nla_reserve(skb, OVS_VPORT_ATTR_STATS,
1645 sizeof(struct ovs_vport_stats));
1647 goto nla_put_failure;
1649 vport_get_stats(vport, nla_data(nla));
1651 NLA_PUT(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1652 vport->ops->get_addr(vport));
1654 err = vport_get_options(vport, skb);
1655 if (err == -EMSGSIZE)
1658 return genlmsg_end(skb, ovs_header);
1663 genlmsg_cancel(skb, ovs_header);
1667 /* Called with RTNL lock or RCU read lock. */
1668 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1671 struct sk_buff *skb;
1674 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1676 return ERR_PTR(-ENOMEM);
1678 retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1681 return ERR_PTR(retval);
1686 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1688 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1691 /* Called with RTNL lock or RCU read lock. */
1692 static struct vport *lookup_vport(struct ovs_header *ovs_header,
1693 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1695 struct datapath *dp;
1696 struct vport *vport;
1698 if (a[OVS_VPORT_ATTR_NAME]) {
1699 vport = vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
1701 return ERR_PTR(-ENODEV);
1703 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1704 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1706 if (port_no >= DP_MAX_PORTS)
1707 return ERR_PTR(-EFBIG);
1709 dp = get_dp(ovs_header->dp_ifindex);
1711 return ERR_PTR(-ENODEV);
1713 vport = get_vport_protected(dp, port_no);
1715 return ERR_PTR(-ENOENT);
1718 return ERR_PTR(-EINVAL);
1721 /* Called with RTNL lock. */
1722 static int change_vport(struct vport *vport,
1723 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1727 if (a[OVS_VPORT_ATTR_STATS])
1728 vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1730 if (a[OVS_VPORT_ATTR_ADDRESS])
1731 err = vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1736 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1738 struct nlattr **a = info->attrs;
1739 struct ovs_header *ovs_header = info->userhdr;
1740 struct vport_parms parms;
1741 struct sk_buff *reply;
1742 struct vport *vport;
1743 struct datapath *dp;
1748 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1749 !a[OVS_VPORT_ATTR_UPCALL_PID])
1752 err = ovs_vport_cmd_validate(a);
1757 dp = get_dp(ovs_header->dp_ifindex);
1762 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1763 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1766 if (port_no >= DP_MAX_PORTS)
1769 vport = get_vport_protected(dp, port_no);
1774 for (port_no = 1; ; port_no++) {
1775 if (port_no >= DP_MAX_PORTS) {
1779 vport = get_vport_protected(dp, port_no);
1785 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1786 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1787 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1789 parms.port_no = port_no;
1790 parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1792 vport = new_vport(&parms);
1793 err = PTR_ERR(vport);
1797 dp_sysfs_add_if(vport);
1799 err = change_vport(vport, a);
1801 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1805 err = PTR_ERR(reply);
1808 dp_detach_port(vport);
1811 genl_notify(reply, genl_info_net(info), info->snd_pid,
1812 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1821 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1823 struct nlattr **a = info->attrs;
1824 struct sk_buff *reply;
1825 struct vport *vport;
1828 err = ovs_vport_cmd_validate(a);
1833 vport = lookup_vport(info->userhdr, a);
1834 err = PTR_ERR(vport);
1839 if (a[OVS_VPORT_ATTR_TYPE] &&
1840 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1843 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1844 err = vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1846 err = change_vport(vport, a);
1847 if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1848 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1850 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1852 if (IS_ERR(reply)) {
1853 err = PTR_ERR(reply);
1854 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1855 dp_vport_multicast_group.id, err);
1859 genl_notify(reply, genl_info_net(info), info->snd_pid,
1860 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1868 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1870 struct nlattr **a = info->attrs;
1871 struct sk_buff *reply;
1872 struct vport *vport;
1875 err = ovs_vport_cmd_validate(a);
1880 vport = lookup_vport(info->userhdr, a);
1881 err = PTR_ERR(vport);
1885 if (vport->port_no == OVSP_LOCAL) {
1890 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1892 err = PTR_ERR(reply);
1896 dp_detach_port(vport);
1898 genl_notify(reply, genl_info_net(info), info->snd_pid,
1899 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1907 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1909 struct nlattr **a = info->attrs;
1910 struct ovs_header *ovs_header = info->userhdr;
1911 struct sk_buff *reply;
1912 struct vport *vport;
1915 err = ovs_vport_cmd_validate(a);
1920 vport = lookup_vport(ovs_header, a);
1921 err = PTR_ERR(vport);
1925 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1927 err = PTR_ERR(reply);
1933 return genlmsg_reply(reply, info);
1941 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1943 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1944 struct datapath *dp;
1948 dp = get_dp(ovs_header->dp_ifindex);
1953 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1954 struct vport *vport;
1956 vport = get_vport_protected(dp, port_no);
1960 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1961 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1962 OVS_VPORT_CMD_NEW) < 0)
1967 cb->args[0] = port_no;
1973 static struct genl_ops dp_vport_genl_ops[] = {
1974 { .cmd = OVS_VPORT_CMD_NEW,
1975 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1976 .policy = vport_policy,
1977 .doit = ovs_vport_cmd_new
1979 { .cmd = OVS_VPORT_CMD_DEL,
1980 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1981 .policy = vport_policy,
1982 .doit = ovs_vport_cmd_del
1984 { .cmd = OVS_VPORT_CMD_GET,
1985 .flags = 0, /* OK for unprivileged users. */
1986 .policy = vport_policy,
1987 .doit = ovs_vport_cmd_get,
1988 .dumpit = ovs_vport_cmd_dump
1990 { .cmd = OVS_VPORT_CMD_SET,
1991 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1992 .policy = vport_policy,
1993 .doit = ovs_vport_cmd_set,
1997 struct genl_family_and_ops {
1998 struct genl_family *family;
1999 struct genl_ops *ops;
2001 struct genl_multicast_group *group;
2004 static const struct genl_family_and_ops dp_genl_families[] = {
2005 { &dp_datapath_genl_family,
2006 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2007 &dp_datapath_multicast_group },
2008 { &dp_vport_genl_family,
2009 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2010 &dp_vport_multicast_group },
2011 { &dp_flow_genl_family,
2012 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2013 &dp_flow_multicast_group },
2014 { &dp_packet_genl_family,
2015 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2019 static void dp_unregister_genl(int n_families)
2023 for (i = 0; i < n_families; i++)
2024 genl_unregister_family(dp_genl_families[i].family);
2027 static int dp_register_genl(void)
2034 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2035 const struct genl_family_and_ops *f = &dp_genl_families[i];
2037 err = genl_register_family_with_ops(f->family, f->ops,
2044 err = genl_register_mc_group(f->family, f->group);
2053 dp_unregister_genl(n_registered);
2057 static int __init dp_init(void)
2059 struct sk_buff *dummy_skb;
2062 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2064 pr_info("Open vSwitch %s, built "__DATE__" "__TIME__"\n",
2073 goto error_tnl_exit;
2077 goto error_flow_exit;
2079 err = register_netdevice_notifier(&dp_device_notifier);
2081 goto error_vport_exit;
2083 err = dp_register_genl();
2085 goto error_unreg_notifier;
2089 error_unreg_notifier:
2090 unregister_netdevice_notifier(&dp_device_notifier);
2101 static void dp_cleanup(void)
2104 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2105 unregister_netdevice_notifier(&dp_device_notifier);
2111 module_init(dp_init);
2112 module_exit(dp_cleanup);
2114 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2115 MODULE_LICENSE("GPL");