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/genetlink.h>
52 #include "vport-internal_dev.h"
54 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
55 LINUX_VERSION_CODE > KERNEL_VERSION(3,2,0)
56 #error Kernels before 2.6.18 or after 3.2 are not supported by this version of Open vSwitch.
59 int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
60 EXPORT_SYMBOL(dp_ioctl_hook);
65 * Writes to device state (add/remove datapath, port, set operations on vports,
66 * etc.) are protected by RTNL.
68 * Writes to other state (flow table modifications, set miscellaneous datapath
69 * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
72 * Reads are protected by RCU.
74 * There are a few special cases (mostly stats) that have their own
75 * synchronization but they nest under all of above and don't interact with
79 /* Global list of datapaths to enable dumping them all out.
80 * Protected by genl_mutex.
82 static LIST_HEAD(dps);
84 static struct vport *new_vport(const struct vport_parms *);
85 static int queue_gso_packets(int dp_ifindex, struct sk_buff *,
86 const struct dp_upcall_info *);
87 static int queue_userspace_packet(int dp_ifindex, struct sk_buff *,
88 const struct dp_upcall_info *);
90 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
91 struct datapath *get_dp(int dp_ifindex)
93 struct datapath *dp = NULL;
94 struct net_device *dev;
97 dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
99 struct vport *vport = internal_dev_get_vport(dev);
107 EXPORT_SYMBOL_GPL(get_dp);
109 /* Must be called with genl_mutex. */
110 static struct flow_table *get_table_protected(struct datapath *dp)
112 return rcu_dereference_protected(dp->table, lockdep_genl_is_held());
115 /* Must be called with rcu_read_lock or RTNL lock. */
116 static struct vport *get_vport_protected(struct datapath *dp, u16 port_no)
118 return rcu_dereference_rtnl(dp->ports[port_no]);
121 /* Must be called with rcu_read_lock or RTNL lock. */
122 const char *dp_name(const struct datapath *dp)
124 struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
125 return vport->ops->get_name(vport);
128 static int get_dpifindex(struct datapath *dp)
135 local = get_vport_protected(dp, OVSP_LOCAL);
137 ifindex = local->ops->get_ifindex(local);
146 static size_t br_nlmsg_size(void)
148 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
149 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
150 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
151 + nla_total_size(4) /* IFLA_MASTER */
152 + nla_total_size(4) /* IFLA_MTU */
153 + nla_total_size(1); /* IFLA_OPERSTATE */
156 /* Caller must hold RTNL lock. */
157 static int dp_fill_ifinfo(struct sk_buff *skb,
158 const struct vport *port,
159 int event, unsigned int flags)
161 struct datapath *dp = port->dp;
162 struct ifinfomsg *hdr;
163 struct nlmsghdr *nlh;
165 if (!port->ops->get_ifindex)
168 nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
172 hdr = nlmsg_data(nlh);
173 hdr->ifi_family = AF_BRIDGE;
175 hdr->ifi_type = ARPHRD_ETHER;
176 hdr->ifi_index = port->ops->get_ifindex(port);
177 hdr->ifi_flags = port->ops->get_dev_flags(port);
180 NLA_PUT_STRING(skb, IFLA_IFNAME, port->ops->get_name(port));
181 NLA_PUT_U32(skb, IFLA_MASTER, get_dpifindex(dp));
182 NLA_PUT_U32(skb, IFLA_MTU, port->ops->get_mtu(port));
183 #ifdef IFLA_OPERSTATE
184 NLA_PUT_U8(skb, IFLA_OPERSTATE,
185 port->ops->is_running(port)
186 ? port->ops->get_operstate(port)
190 NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, port->ops->get_addr(port));
192 return nlmsg_end(skb, nlh);
195 nlmsg_cancel(skb, nlh);
199 /* Caller must hold RTNL lock. */
200 static void dp_ifinfo_notify(int event, struct vport *port)
205 skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
211 err = dp_fill_ifinfo(skb, port, event, 0);
213 if (err == -ENODEV) {
216 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
217 WARN_ON(err == -EMSGSIZE);
222 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
226 rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
231 static void release_dp(struct kobject *kobj)
233 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
237 static struct kobj_type dp_ktype = {
238 .release = release_dp
241 static void destroy_dp_rcu(struct rcu_head *rcu)
243 struct datapath *dp = container_of(rcu, struct datapath, rcu);
245 flow_tbl_destroy(dp->table);
246 free_percpu(dp->stats_percpu);
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 = 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 dp_detach_port(struct vport *p)
273 if (p->port_no != OVSP_LOCAL)
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 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 = flow_extract(skb, p->port_no, &key, &key_len);
302 if (unlikely(error)) {
308 flow = 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 dp_upcall(dp, skb, &upcall);
319 stats_counter = &stats->n_missed;
323 OVS_CB(skb)->flow = flow;
326 stats_counter = &stats->n_hit;
327 flow_used(OVS_CB(skb)->flow, skb);
328 execute_actions(dp, skb);
331 /* Update datapath statistics. */
333 write_seqcount_begin(&stats->seqlock);
335 write_seqcount_end(&stats->seqlock);
338 static void copy_and_csum_skb(struct sk_buff *skb, void *to)
340 u16 csum_start, csum_offset;
343 get_skb_csum_pointers(skb, &csum_start, &csum_offset);
344 csum_start -= skb_headroom(skb);
346 skb_copy_bits(skb, 0, to, csum_start);
348 csum = skb_copy_and_csum_bits(skb, csum_start, to + csum_start,
349 skb->len - csum_start, 0);
350 *(__sum16 *)(to + csum_start + csum_offset) = csum_fold(csum);
353 static struct genl_family dp_packet_genl_family = {
354 .id = GENL_ID_GENERATE,
355 .hdrsize = sizeof(struct ovs_header),
356 .name = OVS_PACKET_FAMILY,
357 .version = OVS_PACKET_VERSION,
358 .maxattr = OVS_PACKET_ATTR_MAX
361 int dp_upcall(struct datapath *dp, struct sk_buff *skb,
362 const struct dp_upcall_info *upcall_info)
364 struct dp_stats_percpu *stats;
368 if (upcall_info->pid == 0) {
373 dp_ifindex = get_dpifindex(dp);
379 forward_ip_summed(skb, true);
381 if (!skb_is_gso(skb))
382 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
384 err = queue_gso_packets(dp_ifindex, skb, upcall_info);
391 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
393 write_seqcount_begin(&stats->seqlock);
395 write_seqcount_end(&stats->seqlock);
400 static int queue_gso_packets(int dp_ifindex, struct sk_buff *skb,
401 const struct dp_upcall_info *upcall_info)
403 struct dp_upcall_info later_info;
404 struct sw_flow_key later_key;
405 struct sk_buff *segs, *nskb;
408 segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
412 /* Queue all of the segments. */
415 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
419 if (skb == segs && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
420 /* The initial flow key extracted by flow_extract() in
421 * this case is for a first fragment, so we need to
422 * properly mark later fragments.
424 later_key = *upcall_info->key;
425 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
427 later_info = *upcall_info;
428 later_info.key = &later_key;
429 upcall_info = &later_info;
431 } while ((skb = skb->next));
433 /* Free all of the segments. */
441 } while ((skb = nskb));
445 static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
446 const struct dp_upcall_info *upcall_info)
448 struct ovs_header *upcall;
449 struct sk_buff *user_skb; /* to be queued to userspace */
454 err = vlan_deaccel_tag(skb);
458 if (nla_attr_size(skb->len) > USHRT_MAX)
461 len = sizeof(struct ovs_header);
462 len += nla_total_size(skb->len);
463 len += nla_total_size(FLOW_BUFSIZE);
464 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
465 len += nla_total_size(8);
467 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 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);
484 if (skb->ip_summed == CHECKSUM_PARTIAL)
485 copy_and_csum_skb(skb, nla_data(nla));
487 skb_copy_bits(skb, 0, nla_data(nla), skb->len);
489 return genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
492 /* Called with genl_mutex. */
493 static int flush_flows(int dp_ifindex)
495 struct flow_table *old_table;
496 struct flow_table *new_table;
499 dp = get_dp(dp_ifindex);
503 old_table = get_table_protected(dp);
504 new_table = flow_tbl_alloc(TBL_MIN_BUCKETS);
508 rcu_assign_pointer(dp->table, new_table);
510 flow_tbl_deferred_destroy(old_table);
514 static int validate_actions(const struct nlattr *attr,
515 const struct sw_flow_key *key, int depth);
517 static int validate_sample(const struct nlattr *attr,
518 const struct sw_flow_key *key, int depth)
520 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
521 const struct nlattr *probability, *actions;
522 const struct nlattr *a;
525 memset(attrs, 0, sizeof(attrs));
526 nla_for_each_nested(a, attr, rem) {
527 int type = nla_type(a);
528 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
535 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
536 if (!probability || nla_len(probability) != sizeof(u32))
539 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
540 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
542 return validate_actions(actions, key, depth + 1);
545 static int validate_action_key(const struct nlattr *a,
546 const struct sw_flow_key *flow_key)
548 int act_type = nla_type(a);
549 const struct nlattr *ovs_key = nla_data(a);
550 int key_type = nla_type(ovs_key);
552 /* There can be only one key in a action */
553 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
556 if (key_type > OVS_KEY_ATTR_MAX ||
557 nla_len(ovs_key) != ovs_key_lens[key_type])
560 #define ACTION(act, key) (((act) << 8) | (key))
562 switch (ACTION(act_type, key_type)) {
563 const struct ovs_key_ipv4 *ipv4_key;
564 const struct ovs_key_8021q *q_key;
566 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_PRIORITY):
567 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_TUN_ID):
568 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_ETHERNET):
571 case ACTION(OVS_ACTION_ATTR_PUSH, OVS_KEY_ATTR_8021Q):
572 q_key = nla_data(ovs_key);
573 if (q_key->q_tpid != htons(ETH_P_8021Q))
576 if (q_key->q_tci & htons(VLAN_TAG_PRESENT))
580 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_IPV4):
581 if (flow_key->eth.type != htons(ETH_P_IP))
584 if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
587 ipv4_key = nla_data(ovs_key);
588 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
591 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
596 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_TCP):
597 if (flow_key->ip.proto != IPPROTO_TCP)
600 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
605 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_UDP):
606 if (flow_key->ip.proto != IPPROTO_UDP)
609 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
620 static int validate_userspace(const struct nlattr *attr)
622 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
623 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
624 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
626 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
629 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
630 attr, userspace_policy);
634 if (!a[OVS_USERSPACE_ATTR_PID] ||
635 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
641 static int validate_actions(const struct nlattr *attr,
642 const struct sw_flow_key *key, int depth)
644 const struct nlattr *a;
647 if (depth >= SAMPLE_ACTION_DEPTH)
650 nla_for_each_nested(a, attr, rem) {
651 /* Expected argument lengths, (u32)-1 for variable length. */
652 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
653 [OVS_ACTION_ATTR_OUTPUT] = 4,
654 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
655 [OVS_ACTION_ATTR_PUSH] = (u32)-1,
656 [OVS_ACTION_ATTR_POP] = 2,
657 [OVS_ACTION_ATTR_SET] = (u32)-1,
658 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
660 int type = nla_type(a);
662 if (type > OVS_ACTION_ATTR_MAX ||
663 (action_lens[type] != nla_len(a) &&
664 action_lens[type] != (u32)-1))
668 case OVS_ACTION_ATTR_UNSPEC:
671 case OVS_ACTION_ATTR_USERSPACE:
672 err = validate_userspace(a);
677 case OVS_ACTION_ATTR_OUTPUT:
678 if (nla_get_u32(a) >= DP_MAX_PORTS)
683 case OVS_ACTION_ATTR_POP:
684 if (nla_get_u16(a) != OVS_KEY_ATTR_8021Q)
688 case OVS_ACTION_ATTR_SET:
689 case OVS_ACTION_ATTR_PUSH:
690 err = validate_action_key(a, key);
695 case OVS_ACTION_ATTR_SAMPLE:
696 err = validate_sample(a, key, depth);
712 static void clear_stats(struct sw_flow *flow)
716 flow->packet_count = 0;
717 flow->byte_count = 0;
720 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
722 struct ovs_header *ovs_header = info->userhdr;
723 struct nlattr **a = info->attrs;
724 struct sw_flow_actions *acts;
725 struct sk_buff *packet;
726 struct sw_flow *flow;
734 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
735 !a[OVS_PACKET_ATTR_ACTIONS] ||
736 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
739 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
740 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
744 skb_reserve(packet, NET_IP_ALIGN);
746 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
748 skb_reset_mac_header(packet);
749 eth = eth_hdr(packet);
751 /* Normally, setting the skb 'protocol' field would be handled by a
752 * call to eth_type_trans(), but it assumes there's a sending
753 * device, which we may not have. */
754 if (ntohs(eth->h_proto) >= 1536)
755 packet->protocol = eth->h_proto;
757 packet->protocol = htons(ETH_P_802_2);
759 /* Build an sw_flow for sending this packet. */
765 err = flow_extract(packet, -1, &flow->key, &key_len);
769 err = flow_metadata_from_nlattrs(&flow->key.phy.priority,
770 &flow->key.phy.in_port,
771 &flow->key.phy.tun_id,
772 a[OVS_PACKET_ATTR_KEY]);
776 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
780 flow->hash = flow_hash(&flow->key, key_len);
782 acts = flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
786 rcu_assign_pointer(flow->sf_acts, acts);
788 OVS_CB(packet)->flow = flow;
789 packet->priority = flow->key.phy.priority;
792 dp = get_dp(ovs_header->dp_ifindex);
798 err = execute_actions(dp, packet);
815 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
816 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
817 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
818 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
821 static struct genl_ops dp_packet_genl_ops[] = {
822 { .cmd = OVS_PACKET_CMD_EXECUTE,
823 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
824 .policy = packet_policy,
825 .doit = ovs_packet_cmd_execute
829 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
832 struct flow_table *table = get_table_protected(dp);
834 stats->n_flows = flow_tbl_count(table);
836 stats->n_hit = stats->n_missed = stats->n_lost = 0;
837 for_each_possible_cpu(i) {
838 const struct dp_stats_percpu *percpu_stats;
839 struct dp_stats_percpu local_stats;
842 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
845 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
846 local_stats = *percpu_stats;
847 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
849 stats->n_hit += local_stats.n_hit;
850 stats->n_missed += local_stats.n_missed;
851 stats->n_lost += local_stats.n_lost;
855 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
856 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
857 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
858 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
861 static struct genl_family dp_flow_genl_family = {
862 .id = GENL_ID_GENERATE,
863 .hdrsize = sizeof(struct ovs_header),
864 .name = OVS_FLOW_FAMILY,
865 .version = OVS_FLOW_VERSION,
866 .maxattr = OVS_FLOW_ATTR_MAX
869 static struct genl_multicast_group dp_flow_multicast_group = {
870 .name = OVS_FLOW_MCGROUP
873 /* Called with genl_lock. */
874 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
875 struct sk_buff *skb, u32 pid,
876 u32 seq, u32 flags, u8 cmd)
878 const int skb_orig_len = skb->len;
879 const struct sw_flow_actions *sf_acts;
880 struct ovs_flow_stats stats;
881 struct ovs_header *ovs_header;
887 sf_acts = rcu_dereference_protected(flow->sf_acts,
888 lockdep_genl_is_held());
890 ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
894 ovs_header->dp_ifindex = get_dpifindex(dp);
896 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
898 goto nla_put_failure;
899 err = flow_to_nlattrs(&flow->key, skb);
902 nla_nest_end(skb, nla);
904 spin_lock_bh(&flow->lock);
906 stats.n_packets = flow->packet_count;
907 stats.n_bytes = flow->byte_count;
908 tcp_flags = flow->tcp_flags;
909 spin_unlock_bh(&flow->lock);
912 NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, flow_used_time(used));
915 NLA_PUT(skb, OVS_FLOW_ATTR_STATS,
916 sizeof(struct ovs_flow_stats), &stats);
919 NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
921 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
922 * this is the first flow to be dumped into 'skb'. This is unusual for
923 * Netlink but individual action lists can be longer than
924 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
925 * The userspace caller can always fetch the actions separately if it
926 * really wants them. (Most userspace callers in fact don't care.)
928 * This can only fail for dump operations because the skb is always
929 * properly sized for single flows.
931 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
933 if (err < 0 && skb_orig_len)
936 return genlmsg_end(skb, ovs_header);
941 genlmsg_cancel(skb, ovs_header);
945 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
947 const struct sw_flow_actions *sf_acts;
950 sf_acts = rcu_dereference_protected(flow->sf_acts,
951 lockdep_genl_is_held());
953 /* OVS_FLOW_ATTR_KEY */
954 len = nla_total_size(FLOW_BUFSIZE);
955 /* OVS_FLOW_ATTR_ACTIONS */
956 len += nla_total_size(sf_acts->actions_len);
957 /* OVS_FLOW_ATTR_STATS */
958 len += nla_total_size(sizeof(struct ovs_flow_stats));
959 /* OVS_FLOW_ATTR_TCP_FLAGS */
960 len += nla_total_size(1);
961 /* OVS_FLOW_ATTR_USED */
962 len += nla_total_size(8);
964 len += NLMSG_ALIGN(sizeof(struct ovs_header));
966 return genlmsg_new(len, GFP_KERNEL);
969 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
971 u32 pid, u32 seq, u8 cmd)
976 skb = ovs_flow_cmd_alloc_info(flow);
978 return ERR_PTR(-ENOMEM);
980 retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
985 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
987 struct nlattr **a = info->attrs;
988 struct ovs_header *ovs_header = info->userhdr;
989 struct sw_flow_key key;
990 struct sw_flow *flow;
991 struct sk_buff *reply;
993 struct flow_table *table;
999 if (!a[OVS_FLOW_ATTR_KEY])
1001 error = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1005 /* Validate actions. */
1006 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1007 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
1010 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1015 dp = get_dp(ovs_header->dp_ifindex);
1020 table = get_table_protected(dp);
1021 flow = flow_tbl_lookup(table, &key, key_len);
1023 struct sw_flow_actions *acts;
1025 /* Bail out if we're not allowed to create a new flow. */
1027 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1030 /* Expand table, if necessary, to make room. */
1031 if (flow_tbl_need_to_expand(table)) {
1032 struct flow_table *new_table;
1034 new_table = flow_tbl_expand(table);
1035 if (!IS_ERR(new_table)) {
1036 rcu_assign_pointer(dp->table, new_table);
1037 flow_tbl_deferred_destroy(table);
1038 table = get_table_protected(dp);
1042 /* Allocate flow. */
1043 flow = flow_alloc();
1045 error = PTR_ERR(flow);
1051 /* Obtain actions. */
1052 acts = flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1053 error = PTR_ERR(acts);
1055 goto error_free_flow;
1056 rcu_assign_pointer(flow->sf_acts, acts);
1058 /* Put flow in bucket. */
1059 flow->hash = flow_hash(&key, key_len);
1060 flow_tbl_insert(table, flow);
1062 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1066 /* We found a matching flow. */
1067 struct sw_flow_actions *old_acts;
1068 struct nlattr *acts_attrs;
1070 /* Bail out if we're not allowed to modify an existing flow.
1071 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1072 * because Generic Netlink treats the latter as a dump
1073 * request. We also accept NLM_F_EXCL in case that bug ever
1077 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1078 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1081 /* Update actions. */
1082 old_acts = rcu_dereference_protected(flow->sf_acts,
1083 lockdep_genl_is_held());
1084 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1086 (old_acts->actions_len != nla_len(acts_attrs) ||
1087 memcmp(old_acts->actions, nla_data(acts_attrs),
1088 old_acts->actions_len))) {
1089 struct sw_flow_actions *new_acts;
1091 new_acts = flow_actions_alloc(acts_attrs);
1092 error = PTR_ERR(new_acts);
1093 if (IS_ERR(new_acts))
1096 rcu_assign_pointer(flow->sf_acts, new_acts);
1097 flow_deferred_free_acts(old_acts);
1100 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1101 info->snd_seq, OVS_FLOW_CMD_NEW);
1104 if (a[OVS_FLOW_ATTR_CLEAR]) {
1105 spin_lock_bh(&flow->lock);
1107 spin_unlock_bh(&flow->lock);
1112 genl_notify(reply, genl_info_net(info), info->snd_pid,
1113 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1115 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1116 dp_flow_multicast_group.id, PTR_ERR(reply));
1125 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1127 struct nlattr **a = info->attrs;
1128 struct ovs_header *ovs_header = info->userhdr;
1129 struct sw_flow_key key;
1130 struct sk_buff *reply;
1131 struct sw_flow *flow;
1132 struct datapath *dp;
1133 struct flow_table *table;
1137 if (!a[OVS_FLOW_ATTR_KEY])
1139 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1143 dp = get_dp(ovs_header->dp_ifindex);
1147 table = get_table_protected(dp);
1148 flow = flow_tbl_lookup(table, &key, key_len);
1152 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1153 info->snd_seq, OVS_FLOW_CMD_NEW);
1155 return PTR_ERR(reply);
1157 return genlmsg_reply(reply, info);
1160 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1162 struct nlattr **a = info->attrs;
1163 struct ovs_header *ovs_header = info->userhdr;
1164 struct sw_flow_key key;
1165 struct sk_buff *reply;
1166 struct sw_flow *flow;
1167 struct datapath *dp;
1168 struct flow_table *table;
1172 if (!a[OVS_FLOW_ATTR_KEY])
1173 return flush_flows(ovs_header->dp_ifindex);
1174 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1178 dp = get_dp(ovs_header->dp_ifindex);
1182 table = get_table_protected(dp);
1183 flow = flow_tbl_lookup(table, &key, key_len);
1187 reply = ovs_flow_cmd_alloc_info(flow);
1191 flow_tbl_remove(table, flow);
1193 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1194 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1197 flow_deferred_free(flow);
1199 genl_notify(reply, genl_info_net(info), info->snd_pid,
1200 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1204 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1206 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1207 struct datapath *dp;
1209 dp = get_dp(ovs_header->dp_ifindex);
1214 struct sw_flow *flow;
1217 bucket = cb->args[0];
1219 flow = flow_tbl_next(get_table_protected(dp), &bucket, &obj);
1223 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1224 NETLINK_CB(cb->skb).pid,
1225 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1226 OVS_FLOW_CMD_NEW) < 0)
1229 cb->args[0] = bucket;
1235 static struct genl_ops dp_flow_genl_ops[] = {
1236 { .cmd = OVS_FLOW_CMD_NEW,
1237 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1238 .policy = flow_policy,
1239 .doit = ovs_flow_cmd_new_or_set
1241 { .cmd = OVS_FLOW_CMD_DEL,
1242 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1243 .policy = flow_policy,
1244 .doit = ovs_flow_cmd_del
1246 { .cmd = OVS_FLOW_CMD_GET,
1247 .flags = 0, /* OK for unprivileged users. */
1248 .policy = flow_policy,
1249 .doit = ovs_flow_cmd_get,
1250 .dumpit = ovs_flow_cmd_dump
1252 { .cmd = OVS_FLOW_CMD_SET,
1253 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1254 .policy = flow_policy,
1255 .doit = ovs_flow_cmd_new_or_set,
1259 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1260 #ifdef HAVE_NLA_NUL_STRING
1261 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1263 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1266 static struct genl_family dp_datapath_genl_family = {
1267 .id = GENL_ID_GENERATE,
1268 .hdrsize = sizeof(struct ovs_header),
1269 .name = OVS_DATAPATH_FAMILY,
1270 .version = OVS_DATAPATH_VERSION,
1271 .maxattr = OVS_DP_ATTR_MAX
1274 static struct genl_multicast_group dp_datapath_multicast_group = {
1275 .name = OVS_DATAPATH_MCGROUP
1278 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1279 u32 pid, u32 seq, u32 flags, u8 cmd)
1281 struct ovs_header *ovs_header;
1285 ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1290 ovs_header->dp_ifindex = get_dpifindex(dp);
1293 err = nla_put_string(skb, OVS_DP_ATTR_NAME, dp_name(dp));
1296 goto nla_put_failure;
1298 nla = nla_reserve(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats));
1300 goto nla_put_failure;
1301 get_dp_stats(dp, nla_data(nla));
1303 return genlmsg_end(skb, ovs_header);
1306 genlmsg_cancel(skb, ovs_header);
1311 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1314 struct sk_buff *skb;
1317 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1319 return ERR_PTR(-ENOMEM);
1321 retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1324 return ERR_PTR(retval);
1329 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1331 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1334 /* Called with genl_mutex and optionally with RTNL lock also. */
1335 static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
1336 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1338 struct datapath *dp;
1340 if (!a[OVS_DP_ATTR_NAME])
1341 dp = get_dp(ovs_header->dp_ifindex);
1343 struct vport *vport;
1346 vport = vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
1347 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1350 return dp ? dp : ERR_PTR(-ENODEV);
1353 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1355 struct nlattr **a = info->attrs;
1356 struct vport_parms parms;
1357 struct sk_buff *reply;
1358 struct datapath *dp;
1359 struct vport *vport;
1363 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1366 err = ovs_dp_cmd_validate(a);
1372 if (!try_module_get(THIS_MODULE))
1373 goto err_unlock_rtnl;
1376 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1378 goto err_put_module;
1379 INIT_LIST_HEAD(&dp->port_list);
1381 /* Initialize kobject for bridge. This will be added as
1382 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1383 dp->ifobj.kset = NULL;
1384 kobject_init(&dp->ifobj, &dp_ktype);
1386 /* Allocate table. */
1388 rcu_assign_pointer(dp->table, flow_tbl_alloc(TBL_MIN_BUCKETS));
1392 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1393 if (!dp->stats_percpu) {
1395 goto err_destroy_table;
1398 /* Set up our datapath device. */
1399 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1400 parms.type = OVS_VPORT_TYPE_INTERNAL;
1401 parms.options = NULL;
1403 parms.port_no = OVSP_LOCAL;
1404 parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1406 vport = new_vport(&parms);
1407 if (IS_ERR(vport)) {
1408 err = PTR_ERR(vport);
1412 goto err_destroy_percpu;
1415 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1416 info->snd_seq, OVS_DP_CMD_NEW);
1417 err = PTR_ERR(reply);
1419 goto err_destroy_local_port;
1421 list_add_tail(&dp->list_node, &dps);
1422 dp_sysfs_add_dp(dp);
1426 genl_notify(reply, genl_info_net(info), info->snd_pid,
1427 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1430 err_destroy_local_port:
1431 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1433 free_percpu(dp->stats_percpu);
1435 flow_tbl_destroy(get_table_protected(dp));
1439 module_put(THIS_MODULE);
1446 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1448 struct vport *vport, *next_vport;
1449 struct sk_buff *reply;
1450 struct datapath *dp;
1453 err = ovs_dp_cmd_validate(info->attrs);
1458 dp = lookup_datapath(info->userhdr, info->attrs);
1463 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1464 info->snd_seq, OVS_DP_CMD_DEL);
1465 err = PTR_ERR(reply);
1469 list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
1470 if (vport->port_no != OVSP_LOCAL)
1471 dp_detach_port(vport);
1473 dp_sysfs_del_dp(dp);
1474 list_del(&dp->list_node);
1475 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1477 /* rtnl_unlock() will wait until all the references to devices that
1478 * are pending unregistration have been dropped. We do it here to
1479 * ensure that any internal devices (which contain DP pointers) are
1480 * fully destroyed before freeing the datapath.
1484 call_rcu(&dp->rcu, destroy_dp_rcu);
1485 module_put(THIS_MODULE);
1487 genl_notify(reply, genl_info_net(info), info->snd_pid,
1488 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1498 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1500 struct sk_buff *reply;
1501 struct datapath *dp;
1504 err = ovs_dp_cmd_validate(info->attrs);
1508 dp = lookup_datapath(info->userhdr, info->attrs);
1512 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1513 info->snd_seq, OVS_DP_CMD_NEW);
1514 if (IS_ERR(reply)) {
1515 err = PTR_ERR(reply);
1516 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1517 dp_datapath_multicast_group.id, err);
1521 genl_notify(reply, genl_info_net(info), info->snd_pid,
1522 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
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(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 datapath *dp;
1551 int skip = cb->args[0];
1554 list_for_each_entry(dp, &dps, list_node) {
1557 if (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
1616 struct genl_multicast_group dp_vport_multicast_group = {
1617 .name = OVS_VPORT_MCGROUP
1620 /* Called with RTNL lock or RCU read lock. */
1621 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1622 u32 pid, u32 seq, u32 flags, u8 cmd)
1624 struct ovs_header *ovs_header;
1628 ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1633 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1635 NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1636 NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type);
1637 NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport));
1638 NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1640 nla = nla_reserve(skb, OVS_VPORT_ATTR_STATS,
1641 sizeof(struct ovs_vport_stats));
1643 goto nla_put_failure;
1645 vport_get_stats(vport, nla_data(nla));
1647 NLA_PUT(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1648 vport->ops->get_addr(vport));
1650 err = vport_get_options(vport, skb);
1651 if (err == -EMSGSIZE)
1654 return genlmsg_end(skb, ovs_header);
1659 genlmsg_cancel(skb, ovs_header);
1663 /* Called with RTNL lock or RCU read lock. */
1664 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1667 struct sk_buff *skb;
1670 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1672 return ERR_PTR(-ENOMEM);
1674 retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1677 return ERR_PTR(retval);
1682 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1684 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1687 /* Called with RTNL lock or RCU read lock. */
1688 static struct vport *lookup_vport(struct ovs_header *ovs_header,
1689 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1691 struct datapath *dp;
1692 struct vport *vport;
1694 if (a[OVS_VPORT_ATTR_NAME]) {
1695 vport = vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
1697 return ERR_PTR(-ENODEV);
1699 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1700 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1702 if (port_no >= DP_MAX_PORTS)
1703 return ERR_PTR(-EFBIG);
1705 dp = get_dp(ovs_header->dp_ifindex);
1707 return ERR_PTR(-ENODEV);
1709 vport = get_vport_protected(dp, port_no);
1711 return ERR_PTR(-ENOENT);
1714 return ERR_PTR(-EINVAL);
1717 /* Called with RTNL lock. */
1718 static int change_vport(struct vport *vport,
1719 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1723 if (a[OVS_VPORT_ATTR_STATS])
1724 vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1726 if (a[OVS_VPORT_ATTR_ADDRESS])
1727 err = vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1732 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1734 struct nlattr **a = info->attrs;
1735 struct ovs_header *ovs_header = info->userhdr;
1736 struct vport_parms parms;
1737 struct sk_buff *reply;
1738 struct vport *vport;
1739 struct datapath *dp;
1744 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1745 !a[OVS_VPORT_ATTR_UPCALL_PID])
1748 err = ovs_vport_cmd_validate(a);
1753 dp = get_dp(ovs_header->dp_ifindex);
1758 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1759 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1762 if (port_no >= DP_MAX_PORTS)
1765 vport = get_vport_protected(dp, port_no);
1770 for (port_no = 1; ; port_no++) {
1771 if (port_no >= DP_MAX_PORTS) {
1775 vport = get_vport_protected(dp, port_no);
1781 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1782 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1783 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1785 parms.port_no = port_no;
1786 parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1788 vport = new_vport(&parms);
1789 err = PTR_ERR(vport);
1793 dp_sysfs_add_if(vport);
1795 err = change_vport(vport, a);
1797 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1801 err = PTR_ERR(reply);
1804 dp_detach_port(vport);
1807 genl_notify(reply, genl_info_net(info), info->snd_pid,
1808 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1817 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1819 struct nlattr **a = info->attrs;
1820 struct sk_buff *reply;
1821 struct vport *vport;
1824 err = ovs_vport_cmd_validate(a);
1829 vport = lookup_vport(info->userhdr, a);
1830 err = PTR_ERR(vport);
1835 if (a[OVS_VPORT_ATTR_TYPE] &&
1836 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1839 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1840 err = vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1842 err = change_vport(vport, a);
1843 if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1844 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1846 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1848 if (IS_ERR(reply)) {
1849 err = PTR_ERR(reply);
1850 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1851 dp_vport_multicast_group.id, err);
1855 genl_notify(reply, genl_info_net(info), info->snd_pid,
1856 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1864 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1866 struct nlattr **a = info->attrs;
1867 struct sk_buff *reply;
1868 struct vport *vport;
1871 err = ovs_vport_cmd_validate(a);
1876 vport = lookup_vport(info->userhdr, a);
1877 err = PTR_ERR(vport);
1881 if (vport->port_no == OVSP_LOCAL) {
1886 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1888 err = PTR_ERR(reply);
1892 dp_detach_port(vport);
1894 genl_notify(reply, genl_info_net(info), info->snd_pid,
1895 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1903 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1905 struct nlattr **a = info->attrs;
1906 struct ovs_header *ovs_header = info->userhdr;
1907 struct sk_buff *reply;
1908 struct vport *vport;
1911 err = ovs_vport_cmd_validate(a);
1916 vport = lookup_vport(ovs_header, a);
1917 err = PTR_ERR(vport);
1921 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1923 err = PTR_ERR(reply);
1929 return genlmsg_reply(reply, info);
1937 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1939 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1940 struct datapath *dp;
1944 dp = get_dp(ovs_header->dp_ifindex);
1949 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1950 struct vport *vport;
1952 vport = get_vport_protected(dp, port_no);
1956 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1957 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1958 OVS_VPORT_CMD_NEW) < 0)
1963 cb->args[0] = port_no;
1969 static struct genl_ops dp_vport_genl_ops[] = {
1970 { .cmd = OVS_VPORT_CMD_NEW,
1971 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1972 .policy = vport_policy,
1973 .doit = ovs_vport_cmd_new
1975 { .cmd = OVS_VPORT_CMD_DEL,
1976 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1977 .policy = vport_policy,
1978 .doit = ovs_vport_cmd_del
1980 { .cmd = OVS_VPORT_CMD_GET,
1981 .flags = 0, /* OK for unprivileged users. */
1982 .policy = vport_policy,
1983 .doit = ovs_vport_cmd_get,
1984 .dumpit = ovs_vport_cmd_dump
1986 { .cmd = OVS_VPORT_CMD_SET,
1987 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1988 .policy = vport_policy,
1989 .doit = ovs_vport_cmd_set,
1993 struct genl_family_and_ops {
1994 struct genl_family *family;
1995 struct genl_ops *ops;
1997 struct genl_multicast_group *group;
2000 static const struct genl_family_and_ops dp_genl_families[] = {
2001 { &dp_datapath_genl_family,
2002 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2003 &dp_datapath_multicast_group },
2004 { &dp_vport_genl_family,
2005 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2006 &dp_vport_multicast_group },
2007 { &dp_flow_genl_family,
2008 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2009 &dp_flow_multicast_group },
2010 { &dp_packet_genl_family,
2011 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2015 static void dp_unregister_genl(int n_families)
2019 for (i = 0; i < n_families; i++)
2020 genl_unregister_family(dp_genl_families[i].family);
2023 static int dp_register_genl(void)
2030 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2031 const struct genl_family_and_ops *f = &dp_genl_families[i];
2033 err = genl_register_family_with_ops(f->family, f->ops,
2040 err = genl_register_mc_group(f->family, f->group);
2049 dp_unregister_genl(n_registered);
2053 static int __init dp_init(void)
2055 struct sk_buff *dummy_skb;
2058 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2060 pr_info("Open vSwitch %s, built "__DATE__" "__TIME__"\n",
2069 goto error_tnl_exit;
2073 goto error_flow_exit;
2075 err = register_netdevice_notifier(&dp_device_notifier);
2077 goto error_vport_exit;
2079 err = dp_register_genl();
2081 goto error_unreg_notifier;
2085 error_unreg_notifier:
2086 unregister_netdevice_notifier(&dp_device_notifier);
2097 static void dp_cleanup(void)
2100 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2101 unregister_netdevice_notifier(&dp_device_notifier);
2107 module_init(dp_init);
2108 module_exit(dp_cleanup);
2110 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2111 MODULE_LICENSE("GPL");