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 struct genl_family dp_packet_genl_family = {
339 .id = GENL_ID_GENERATE,
340 .hdrsize = sizeof(struct ovs_header),
341 .name = OVS_PACKET_FAMILY,
342 .version = OVS_PACKET_VERSION,
343 .maxattr = OVS_PACKET_ATTR_MAX
346 int 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(dp_ifindex, skb, upcall_info);
369 err = queue_gso_packets(dp_ifindex, skb, upcall_info);
376 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
378 write_seqcount_begin(&stats->seqlock);
380 write_seqcount_end(&stats->seqlock);
385 static int queue_gso_packets(int dp_ifindex, struct sk_buff *skb,
386 const struct dp_upcall_info *upcall_info)
388 struct dp_upcall_info later_info;
389 struct sw_flow_key later_key;
390 struct sk_buff *segs, *nskb;
393 segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
397 /* Queue all of the segments. */
400 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
404 if (skb == segs && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
405 /* The initial flow key extracted by flow_extract() in
406 * this case is for a first fragment, so we need to
407 * properly mark later fragments.
409 later_key = *upcall_info->key;
410 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
412 later_info = *upcall_info;
413 later_info.key = &later_key;
414 upcall_info = &later_info;
416 } while ((skb = skb->next));
418 /* Free all of the segments. */
426 } while ((skb = nskb));
430 static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
431 const struct dp_upcall_info *upcall_info)
433 struct ovs_header *upcall;
434 struct sk_buff *user_skb; /* to be queued to userspace */
439 err = vlan_deaccel_tag(skb);
443 if (nla_attr_size(skb->len) > USHRT_MAX)
446 len = sizeof(struct ovs_header);
447 len += nla_total_size(skb->len);
448 len += nla_total_size(FLOW_BUFSIZE);
449 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
450 len += nla_total_size(8);
452 user_skb = genlmsg_new(len, GFP_ATOMIC);
456 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
457 0, upcall_info->cmd);
458 upcall->dp_ifindex = dp_ifindex;
460 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
461 flow_to_nlattrs(upcall_info->key, user_skb);
462 nla_nest_end(user_skb, nla);
464 if (upcall_info->userdata)
465 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
466 nla_get_u64(upcall_info->userdata));
468 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
470 skb_copy_and_csum_dev(skb, nla_data(nla));
472 return genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
475 /* Called with genl_mutex. */
476 static int flush_flows(int dp_ifindex)
478 struct flow_table *old_table;
479 struct flow_table *new_table;
482 dp = get_dp(dp_ifindex);
486 old_table = get_table_protected(dp);
487 new_table = flow_tbl_alloc(TBL_MIN_BUCKETS);
491 rcu_assign_pointer(dp->table, new_table);
493 flow_tbl_deferred_destroy(old_table);
497 static int validate_actions(const struct nlattr *attr,
498 const struct sw_flow_key *key, int depth);
500 static int validate_sample(const struct nlattr *attr,
501 const struct sw_flow_key *key, int depth)
503 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
504 const struct nlattr *probability, *actions;
505 const struct nlattr *a;
508 memset(attrs, 0, sizeof(attrs));
509 nla_for_each_nested(a, attr, rem) {
510 int type = nla_type(a);
511 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
518 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
519 if (!probability || nla_len(probability) != sizeof(u32))
522 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
523 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
525 return validate_actions(actions, key, depth + 1);
528 static int validate_action_key(const struct nlattr *a,
529 const struct sw_flow_key *flow_key)
531 int act_type = nla_type(a);
532 const struct nlattr *ovs_key = nla_data(a);
533 int key_type = nla_type(ovs_key);
535 /* There can be only one key in a action */
536 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
539 if (key_type > OVS_KEY_ATTR_MAX ||
540 nla_len(ovs_key) != ovs_key_lens[key_type])
543 #define ACTION(act, key) (((act) << 8) | (key))
545 switch (ACTION(act_type, key_type)) {
546 const struct ovs_key_ipv4 *ipv4_key;
547 const struct ovs_key_8021q *q_key;
549 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_PRIORITY):
550 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_TUN_ID):
551 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_ETHERNET):
554 case ACTION(OVS_ACTION_ATTR_PUSH, OVS_KEY_ATTR_8021Q):
555 q_key = nla_data(ovs_key);
556 if (q_key->q_tpid != htons(ETH_P_8021Q))
559 if (q_key->q_tci & htons(VLAN_TAG_PRESENT))
563 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_IPV4):
564 if (flow_key->eth.type != htons(ETH_P_IP))
567 if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
570 ipv4_key = nla_data(ovs_key);
571 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
574 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
579 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_TCP):
580 if (flow_key->ip.proto != IPPROTO_TCP)
583 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
588 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_UDP):
589 if (flow_key->ip.proto != IPPROTO_UDP)
592 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
603 static int validate_userspace(const struct nlattr *attr)
605 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
606 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
607 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
609 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
612 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
613 attr, userspace_policy);
617 if (!a[OVS_USERSPACE_ATTR_PID] ||
618 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
624 static int validate_actions(const struct nlattr *attr,
625 const struct sw_flow_key *key, int depth)
627 const struct nlattr *a;
630 if (depth >= SAMPLE_ACTION_DEPTH)
633 nla_for_each_nested(a, attr, rem) {
634 /* Expected argument lengths, (u32)-1 for variable length. */
635 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
636 [OVS_ACTION_ATTR_OUTPUT] = 4,
637 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
638 [OVS_ACTION_ATTR_PUSH] = (u32)-1,
639 [OVS_ACTION_ATTR_POP] = 2,
640 [OVS_ACTION_ATTR_SET] = (u32)-1,
641 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
643 int type = nla_type(a);
645 if (type > OVS_ACTION_ATTR_MAX ||
646 (action_lens[type] != nla_len(a) &&
647 action_lens[type] != (u32)-1))
651 case OVS_ACTION_ATTR_UNSPEC:
654 case OVS_ACTION_ATTR_USERSPACE:
655 err = validate_userspace(a);
660 case OVS_ACTION_ATTR_OUTPUT:
661 if (nla_get_u32(a) >= DP_MAX_PORTS)
666 case OVS_ACTION_ATTR_POP:
667 if (nla_get_u16(a) != OVS_KEY_ATTR_8021Q)
671 case OVS_ACTION_ATTR_SET:
672 case OVS_ACTION_ATTR_PUSH:
673 err = validate_action_key(a, key);
678 case OVS_ACTION_ATTR_SAMPLE:
679 err = validate_sample(a, key, depth);
695 static void clear_stats(struct sw_flow *flow)
699 flow->packet_count = 0;
700 flow->byte_count = 0;
703 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
705 struct ovs_header *ovs_header = info->userhdr;
706 struct nlattr **a = info->attrs;
707 struct sw_flow_actions *acts;
708 struct sk_buff *packet;
709 struct sw_flow *flow;
717 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
718 !a[OVS_PACKET_ATTR_ACTIONS] ||
719 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
722 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
723 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
727 skb_reserve(packet, NET_IP_ALIGN);
729 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
731 skb_reset_mac_header(packet);
732 eth = eth_hdr(packet);
734 /* Normally, setting the skb 'protocol' field would be handled by a
735 * call to eth_type_trans(), but it assumes there's a sending
736 * device, which we may not have. */
737 if (ntohs(eth->h_proto) >= 1536)
738 packet->protocol = eth->h_proto;
740 packet->protocol = htons(ETH_P_802_2);
742 /* Build an sw_flow for sending this packet. */
748 err = flow_extract(packet, -1, &flow->key, &key_len);
752 err = flow_metadata_from_nlattrs(&flow->key.phy.priority,
753 &flow->key.phy.in_port,
754 &flow->key.phy.tun_id,
755 a[OVS_PACKET_ATTR_KEY]);
759 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
763 flow->hash = flow_hash(&flow->key, key_len);
765 acts = flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
769 rcu_assign_pointer(flow->sf_acts, acts);
771 OVS_CB(packet)->flow = flow;
772 packet->priority = flow->key.phy.priority;
775 dp = get_dp(ovs_header->dp_ifindex);
781 err = execute_actions(dp, packet);
798 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
799 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
800 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
801 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
804 static struct genl_ops dp_packet_genl_ops[] = {
805 { .cmd = OVS_PACKET_CMD_EXECUTE,
806 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
807 .policy = packet_policy,
808 .doit = ovs_packet_cmd_execute
812 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
815 struct flow_table *table = get_table_protected(dp);
817 stats->n_flows = flow_tbl_count(table);
819 stats->n_hit = stats->n_missed = stats->n_lost = 0;
820 for_each_possible_cpu(i) {
821 const struct dp_stats_percpu *percpu_stats;
822 struct dp_stats_percpu local_stats;
825 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
828 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
829 local_stats = *percpu_stats;
830 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
832 stats->n_hit += local_stats.n_hit;
833 stats->n_missed += local_stats.n_missed;
834 stats->n_lost += local_stats.n_lost;
838 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
839 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
840 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
841 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
844 static struct genl_family dp_flow_genl_family = {
845 .id = GENL_ID_GENERATE,
846 .hdrsize = sizeof(struct ovs_header),
847 .name = OVS_FLOW_FAMILY,
848 .version = OVS_FLOW_VERSION,
849 .maxattr = OVS_FLOW_ATTR_MAX
852 static struct genl_multicast_group dp_flow_multicast_group = {
853 .name = OVS_FLOW_MCGROUP
856 /* Called with genl_lock. */
857 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
858 struct sk_buff *skb, u32 pid,
859 u32 seq, u32 flags, u8 cmd)
861 const int skb_orig_len = skb->len;
862 const struct sw_flow_actions *sf_acts;
863 struct ovs_flow_stats stats;
864 struct ovs_header *ovs_header;
870 sf_acts = rcu_dereference_protected(flow->sf_acts,
871 lockdep_genl_is_held());
873 ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
877 ovs_header->dp_ifindex = get_dpifindex(dp);
879 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
881 goto nla_put_failure;
882 err = flow_to_nlattrs(&flow->key, skb);
885 nla_nest_end(skb, nla);
887 spin_lock_bh(&flow->lock);
889 stats.n_packets = flow->packet_count;
890 stats.n_bytes = flow->byte_count;
891 tcp_flags = flow->tcp_flags;
892 spin_unlock_bh(&flow->lock);
895 NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, flow_used_time(used));
898 NLA_PUT(skb, OVS_FLOW_ATTR_STATS,
899 sizeof(struct ovs_flow_stats), &stats);
902 NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
904 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
905 * this is the first flow to be dumped into 'skb'. This is unusual for
906 * Netlink but individual action lists can be longer than
907 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
908 * The userspace caller can always fetch the actions separately if it
909 * really wants them. (Most userspace callers in fact don't care.)
911 * This can only fail for dump operations because the skb is always
912 * properly sized for single flows.
914 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
916 if (err < 0 && skb_orig_len)
919 return genlmsg_end(skb, ovs_header);
924 genlmsg_cancel(skb, ovs_header);
928 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
930 const struct sw_flow_actions *sf_acts;
933 sf_acts = rcu_dereference_protected(flow->sf_acts,
934 lockdep_genl_is_held());
936 /* OVS_FLOW_ATTR_KEY */
937 len = nla_total_size(FLOW_BUFSIZE);
938 /* OVS_FLOW_ATTR_ACTIONS */
939 len += nla_total_size(sf_acts->actions_len);
940 /* OVS_FLOW_ATTR_STATS */
941 len += nla_total_size(sizeof(struct ovs_flow_stats));
942 /* OVS_FLOW_ATTR_TCP_FLAGS */
943 len += nla_total_size(1);
944 /* OVS_FLOW_ATTR_USED */
945 len += nla_total_size(8);
947 len += NLMSG_ALIGN(sizeof(struct ovs_header));
949 return genlmsg_new(len, GFP_KERNEL);
952 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
954 u32 pid, u32 seq, u8 cmd)
959 skb = ovs_flow_cmd_alloc_info(flow);
961 return ERR_PTR(-ENOMEM);
963 retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
968 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
970 struct nlattr **a = info->attrs;
971 struct ovs_header *ovs_header = info->userhdr;
972 struct sw_flow_key key;
973 struct sw_flow *flow;
974 struct sk_buff *reply;
976 struct flow_table *table;
982 if (!a[OVS_FLOW_ATTR_KEY])
984 error = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
988 /* Validate actions. */
989 if (a[OVS_FLOW_ATTR_ACTIONS]) {
990 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
993 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
998 dp = get_dp(ovs_header->dp_ifindex);
1003 table = get_table_protected(dp);
1004 flow = flow_tbl_lookup(table, &key, key_len);
1006 struct sw_flow_actions *acts;
1008 /* Bail out if we're not allowed to create a new flow. */
1010 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1013 /* Expand table, if necessary, to make room. */
1014 if (flow_tbl_need_to_expand(table)) {
1015 struct flow_table *new_table;
1017 new_table = flow_tbl_expand(table);
1018 if (!IS_ERR(new_table)) {
1019 rcu_assign_pointer(dp->table, new_table);
1020 flow_tbl_deferred_destroy(table);
1021 table = get_table_protected(dp);
1025 /* Allocate flow. */
1026 flow = flow_alloc();
1028 error = PTR_ERR(flow);
1034 /* Obtain actions. */
1035 acts = flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1036 error = PTR_ERR(acts);
1038 goto error_free_flow;
1039 rcu_assign_pointer(flow->sf_acts, acts);
1041 /* Put flow in bucket. */
1042 flow->hash = flow_hash(&key, key_len);
1043 flow_tbl_insert(table, flow);
1045 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1049 /* We found a matching flow. */
1050 struct sw_flow_actions *old_acts;
1051 struct nlattr *acts_attrs;
1053 /* Bail out if we're not allowed to modify an existing flow.
1054 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1055 * because Generic Netlink treats the latter as a dump
1056 * request. We also accept NLM_F_EXCL in case that bug ever
1060 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1061 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1064 /* Update actions. */
1065 old_acts = rcu_dereference_protected(flow->sf_acts,
1066 lockdep_genl_is_held());
1067 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1069 (old_acts->actions_len != nla_len(acts_attrs) ||
1070 memcmp(old_acts->actions, nla_data(acts_attrs),
1071 old_acts->actions_len))) {
1072 struct sw_flow_actions *new_acts;
1074 new_acts = flow_actions_alloc(acts_attrs);
1075 error = PTR_ERR(new_acts);
1076 if (IS_ERR(new_acts))
1079 rcu_assign_pointer(flow->sf_acts, new_acts);
1080 flow_deferred_free_acts(old_acts);
1083 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1084 info->snd_seq, OVS_FLOW_CMD_NEW);
1087 if (a[OVS_FLOW_ATTR_CLEAR]) {
1088 spin_lock_bh(&flow->lock);
1090 spin_unlock_bh(&flow->lock);
1095 genl_notify(reply, genl_info_net(info), info->snd_pid,
1096 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1098 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1099 dp_flow_multicast_group.id, PTR_ERR(reply));
1108 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1110 struct nlattr **a = info->attrs;
1111 struct ovs_header *ovs_header = info->userhdr;
1112 struct sw_flow_key key;
1113 struct sk_buff *reply;
1114 struct sw_flow *flow;
1115 struct datapath *dp;
1116 struct flow_table *table;
1120 if (!a[OVS_FLOW_ATTR_KEY])
1122 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1126 dp = get_dp(ovs_header->dp_ifindex);
1130 table = get_table_protected(dp);
1131 flow = flow_tbl_lookup(table, &key, key_len);
1135 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1136 info->snd_seq, OVS_FLOW_CMD_NEW);
1138 return PTR_ERR(reply);
1140 return genlmsg_reply(reply, info);
1143 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1145 struct nlattr **a = info->attrs;
1146 struct ovs_header *ovs_header = info->userhdr;
1147 struct sw_flow_key key;
1148 struct sk_buff *reply;
1149 struct sw_flow *flow;
1150 struct datapath *dp;
1151 struct flow_table *table;
1155 if (!a[OVS_FLOW_ATTR_KEY])
1156 return flush_flows(ovs_header->dp_ifindex);
1157 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1161 dp = get_dp(ovs_header->dp_ifindex);
1165 table = get_table_protected(dp);
1166 flow = flow_tbl_lookup(table, &key, key_len);
1170 reply = ovs_flow_cmd_alloc_info(flow);
1174 flow_tbl_remove(table, flow);
1176 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1177 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1180 flow_deferred_free(flow);
1182 genl_notify(reply, genl_info_net(info), info->snd_pid,
1183 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1187 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1189 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1190 struct datapath *dp;
1192 dp = get_dp(ovs_header->dp_ifindex);
1197 struct sw_flow *flow;
1200 bucket = cb->args[0];
1202 flow = flow_tbl_next(get_table_protected(dp), &bucket, &obj);
1206 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1207 NETLINK_CB(cb->skb).pid,
1208 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1209 OVS_FLOW_CMD_NEW) < 0)
1212 cb->args[0] = bucket;
1218 static struct genl_ops dp_flow_genl_ops[] = {
1219 { .cmd = OVS_FLOW_CMD_NEW,
1220 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1221 .policy = flow_policy,
1222 .doit = ovs_flow_cmd_new_or_set
1224 { .cmd = OVS_FLOW_CMD_DEL,
1225 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1226 .policy = flow_policy,
1227 .doit = ovs_flow_cmd_del
1229 { .cmd = OVS_FLOW_CMD_GET,
1230 .flags = 0, /* OK for unprivileged users. */
1231 .policy = flow_policy,
1232 .doit = ovs_flow_cmd_get,
1233 .dumpit = ovs_flow_cmd_dump
1235 { .cmd = OVS_FLOW_CMD_SET,
1236 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1237 .policy = flow_policy,
1238 .doit = ovs_flow_cmd_new_or_set,
1242 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1243 #ifdef HAVE_NLA_NUL_STRING
1244 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1246 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1249 static struct genl_family dp_datapath_genl_family = {
1250 .id = GENL_ID_GENERATE,
1251 .hdrsize = sizeof(struct ovs_header),
1252 .name = OVS_DATAPATH_FAMILY,
1253 .version = OVS_DATAPATH_VERSION,
1254 .maxattr = OVS_DP_ATTR_MAX
1257 static struct genl_multicast_group dp_datapath_multicast_group = {
1258 .name = OVS_DATAPATH_MCGROUP
1261 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1262 u32 pid, u32 seq, u32 flags, u8 cmd)
1264 struct ovs_header *ovs_header;
1268 ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1273 ovs_header->dp_ifindex = get_dpifindex(dp);
1276 err = nla_put_string(skb, OVS_DP_ATTR_NAME, dp_name(dp));
1279 goto nla_put_failure;
1281 nla = nla_reserve(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats));
1283 goto nla_put_failure;
1284 get_dp_stats(dp, nla_data(nla));
1286 return genlmsg_end(skb, ovs_header);
1289 genlmsg_cancel(skb, ovs_header);
1294 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1297 struct sk_buff *skb;
1300 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1302 return ERR_PTR(-ENOMEM);
1304 retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1307 return ERR_PTR(retval);
1312 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1314 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1317 /* Called with genl_mutex and optionally with RTNL lock also. */
1318 static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
1319 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1321 struct datapath *dp;
1323 if (!a[OVS_DP_ATTR_NAME])
1324 dp = get_dp(ovs_header->dp_ifindex);
1326 struct vport *vport;
1329 vport = vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
1330 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1333 return dp ? dp : ERR_PTR(-ENODEV);
1336 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1338 struct nlattr **a = info->attrs;
1339 struct vport_parms parms;
1340 struct sk_buff *reply;
1341 struct datapath *dp;
1342 struct vport *vport;
1346 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1349 err = ovs_dp_cmd_validate(a);
1355 if (!try_module_get(THIS_MODULE))
1356 goto err_unlock_rtnl;
1359 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1361 goto err_put_module;
1362 INIT_LIST_HEAD(&dp->port_list);
1364 /* Initialize kobject for bridge. This will be added as
1365 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1366 dp->ifobj.kset = NULL;
1367 kobject_init(&dp->ifobj, &dp_ktype);
1369 /* Allocate table. */
1371 rcu_assign_pointer(dp->table, flow_tbl_alloc(TBL_MIN_BUCKETS));
1375 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1376 if (!dp->stats_percpu) {
1378 goto err_destroy_table;
1381 /* Set up our datapath device. */
1382 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1383 parms.type = OVS_VPORT_TYPE_INTERNAL;
1384 parms.options = NULL;
1386 parms.port_no = OVSP_LOCAL;
1387 parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1389 vport = new_vport(&parms);
1390 if (IS_ERR(vport)) {
1391 err = PTR_ERR(vport);
1395 goto err_destroy_percpu;
1398 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1399 info->snd_seq, OVS_DP_CMD_NEW);
1400 err = PTR_ERR(reply);
1402 goto err_destroy_local_port;
1404 list_add_tail(&dp->list_node, &dps);
1405 dp_sysfs_add_dp(dp);
1409 genl_notify(reply, genl_info_net(info), info->snd_pid,
1410 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1413 err_destroy_local_port:
1414 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1416 free_percpu(dp->stats_percpu);
1418 flow_tbl_destroy(get_table_protected(dp));
1422 module_put(THIS_MODULE);
1429 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1431 struct vport *vport, *next_vport;
1432 struct sk_buff *reply;
1433 struct datapath *dp;
1436 err = ovs_dp_cmd_validate(info->attrs);
1441 dp = lookup_datapath(info->userhdr, info->attrs);
1446 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1447 info->snd_seq, OVS_DP_CMD_DEL);
1448 err = PTR_ERR(reply);
1452 list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
1453 if (vport->port_no != OVSP_LOCAL)
1454 dp_detach_port(vport);
1456 dp_sysfs_del_dp(dp);
1457 list_del(&dp->list_node);
1458 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1460 /* rtnl_unlock() will wait until all the references to devices that
1461 * are pending unregistration have been dropped. We do it here to
1462 * ensure that any internal devices (which contain DP pointers) are
1463 * fully destroyed before freeing the datapath.
1467 call_rcu(&dp->rcu, destroy_dp_rcu);
1468 module_put(THIS_MODULE);
1470 genl_notify(reply, genl_info_net(info), info->snd_pid,
1471 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1481 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1483 struct sk_buff *reply;
1484 struct datapath *dp;
1487 err = ovs_dp_cmd_validate(info->attrs);
1491 dp = lookup_datapath(info->userhdr, info->attrs);
1495 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1496 info->snd_seq, OVS_DP_CMD_NEW);
1497 if (IS_ERR(reply)) {
1498 err = PTR_ERR(reply);
1499 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1500 dp_datapath_multicast_group.id, err);
1504 genl_notify(reply, genl_info_net(info), info->snd_pid,
1505 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1509 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1511 struct sk_buff *reply;
1512 struct datapath *dp;
1515 err = ovs_dp_cmd_validate(info->attrs);
1519 dp = lookup_datapath(info->userhdr, info->attrs);
1523 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1524 info->snd_seq, OVS_DP_CMD_NEW);
1526 return PTR_ERR(reply);
1528 return genlmsg_reply(reply, info);
1531 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1533 struct datapath *dp;
1534 int skip = cb->args[0];
1537 list_for_each_entry(dp, &dps, list_node) {
1540 if (ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1541 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1542 OVS_DP_CMD_NEW) < 0)
1552 static struct genl_ops dp_datapath_genl_ops[] = {
1553 { .cmd = OVS_DP_CMD_NEW,
1554 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1555 .policy = datapath_policy,
1556 .doit = ovs_dp_cmd_new
1558 { .cmd = OVS_DP_CMD_DEL,
1559 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1560 .policy = datapath_policy,
1561 .doit = ovs_dp_cmd_del
1563 { .cmd = OVS_DP_CMD_GET,
1564 .flags = 0, /* OK for unprivileged users. */
1565 .policy = datapath_policy,
1566 .doit = ovs_dp_cmd_get,
1567 .dumpit = ovs_dp_cmd_dump
1569 { .cmd = OVS_DP_CMD_SET,
1570 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1571 .policy = datapath_policy,
1572 .doit = ovs_dp_cmd_set,
1576 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1577 #ifdef HAVE_NLA_NUL_STRING
1578 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1579 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1580 [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1582 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1583 [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1585 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1586 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1587 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1588 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1591 static struct genl_family dp_vport_genl_family = {
1592 .id = GENL_ID_GENERATE,
1593 .hdrsize = sizeof(struct ovs_header),
1594 .name = OVS_VPORT_FAMILY,
1595 .version = OVS_VPORT_VERSION,
1596 .maxattr = OVS_VPORT_ATTR_MAX
1599 struct genl_multicast_group dp_vport_multicast_group = {
1600 .name = OVS_VPORT_MCGROUP
1603 /* Called with RTNL lock or RCU read lock. */
1604 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1605 u32 pid, u32 seq, u32 flags, u8 cmd)
1607 struct ovs_header *ovs_header;
1611 ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1616 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1618 NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1619 NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type);
1620 NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport));
1621 NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1623 nla = nla_reserve(skb, OVS_VPORT_ATTR_STATS,
1624 sizeof(struct ovs_vport_stats));
1626 goto nla_put_failure;
1628 vport_get_stats(vport, nla_data(nla));
1630 NLA_PUT(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1631 vport->ops->get_addr(vport));
1633 err = vport_get_options(vport, skb);
1634 if (err == -EMSGSIZE)
1637 return genlmsg_end(skb, ovs_header);
1642 genlmsg_cancel(skb, ovs_header);
1646 /* Called with RTNL lock or RCU read lock. */
1647 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1650 struct sk_buff *skb;
1653 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1655 return ERR_PTR(-ENOMEM);
1657 retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1660 return ERR_PTR(retval);
1665 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1667 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1670 /* Called with RTNL lock or RCU read lock. */
1671 static struct vport *lookup_vport(struct ovs_header *ovs_header,
1672 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1674 struct datapath *dp;
1675 struct vport *vport;
1677 if (a[OVS_VPORT_ATTR_NAME]) {
1678 vport = vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
1680 return ERR_PTR(-ENODEV);
1682 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1683 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1685 if (port_no >= DP_MAX_PORTS)
1686 return ERR_PTR(-EFBIG);
1688 dp = get_dp(ovs_header->dp_ifindex);
1690 return ERR_PTR(-ENODEV);
1692 vport = get_vport_protected(dp, port_no);
1694 return ERR_PTR(-ENOENT);
1697 return ERR_PTR(-EINVAL);
1700 /* Called with RTNL lock. */
1701 static int change_vport(struct vport *vport,
1702 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1706 if (a[OVS_VPORT_ATTR_STATS])
1707 vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1709 if (a[OVS_VPORT_ATTR_ADDRESS])
1710 err = vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1715 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1717 struct nlattr **a = info->attrs;
1718 struct ovs_header *ovs_header = info->userhdr;
1719 struct vport_parms parms;
1720 struct sk_buff *reply;
1721 struct vport *vport;
1722 struct datapath *dp;
1727 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1728 !a[OVS_VPORT_ATTR_UPCALL_PID])
1731 err = ovs_vport_cmd_validate(a);
1736 dp = get_dp(ovs_header->dp_ifindex);
1741 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1742 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1745 if (port_no >= DP_MAX_PORTS)
1748 vport = get_vport_protected(dp, port_no);
1753 for (port_no = 1; ; port_no++) {
1754 if (port_no >= DP_MAX_PORTS) {
1758 vport = get_vport_protected(dp, port_no);
1764 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1765 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1766 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1768 parms.port_no = port_no;
1769 parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1771 vport = new_vport(&parms);
1772 err = PTR_ERR(vport);
1776 dp_sysfs_add_if(vport);
1778 err = change_vport(vport, a);
1780 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1784 err = PTR_ERR(reply);
1787 dp_detach_port(vport);
1790 genl_notify(reply, genl_info_net(info), info->snd_pid,
1791 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1800 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1802 struct nlattr **a = info->attrs;
1803 struct sk_buff *reply;
1804 struct vport *vport;
1807 err = ovs_vport_cmd_validate(a);
1812 vport = lookup_vport(info->userhdr, a);
1813 err = PTR_ERR(vport);
1818 if (a[OVS_VPORT_ATTR_TYPE] &&
1819 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1822 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1823 err = vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1825 err = change_vport(vport, a);
1826 if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1827 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1829 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1831 if (IS_ERR(reply)) {
1832 err = PTR_ERR(reply);
1833 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1834 dp_vport_multicast_group.id, err);
1838 genl_notify(reply, genl_info_net(info), info->snd_pid,
1839 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1847 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1849 struct nlattr **a = info->attrs;
1850 struct sk_buff *reply;
1851 struct vport *vport;
1854 err = ovs_vport_cmd_validate(a);
1859 vport = lookup_vport(info->userhdr, a);
1860 err = PTR_ERR(vport);
1864 if (vport->port_no == OVSP_LOCAL) {
1869 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1871 err = PTR_ERR(reply);
1875 dp_detach_port(vport);
1877 genl_notify(reply, genl_info_net(info), info->snd_pid,
1878 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1886 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1888 struct nlattr **a = info->attrs;
1889 struct ovs_header *ovs_header = info->userhdr;
1890 struct sk_buff *reply;
1891 struct vport *vport;
1894 err = ovs_vport_cmd_validate(a);
1899 vport = lookup_vport(ovs_header, a);
1900 err = PTR_ERR(vport);
1904 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1906 err = PTR_ERR(reply);
1912 return genlmsg_reply(reply, info);
1920 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1922 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1923 struct datapath *dp;
1927 dp = get_dp(ovs_header->dp_ifindex);
1932 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1933 struct vport *vport;
1935 vport = get_vport_protected(dp, port_no);
1939 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1940 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1941 OVS_VPORT_CMD_NEW) < 0)
1946 cb->args[0] = port_no;
1952 static struct genl_ops dp_vport_genl_ops[] = {
1953 { .cmd = OVS_VPORT_CMD_NEW,
1954 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1955 .policy = vport_policy,
1956 .doit = ovs_vport_cmd_new
1958 { .cmd = OVS_VPORT_CMD_DEL,
1959 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1960 .policy = vport_policy,
1961 .doit = ovs_vport_cmd_del
1963 { .cmd = OVS_VPORT_CMD_GET,
1964 .flags = 0, /* OK for unprivileged users. */
1965 .policy = vport_policy,
1966 .doit = ovs_vport_cmd_get,
1967 .dumpit = ovs_vport_cmd_dump
1969 { .cmd = OVS_VPORT_CMD_SET,
1970 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1971 .policy = vport_policy,
1972 .doit = ovs_vport_cmd_set,
1976 struct genl_family_and_ops {
1977 struct genl_family *family;
1978 struct genl_ops *ops;
1980 struct genl_multicast_group *group;
1983 static const struct genl_family_and_ops dp_genl_families[] = {
1984 { &dp_datapath_genl_family,
1985 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1986 &dp_datapath_multicast_group },
1987 { &dp_vport_genl_family,
1988 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1989 &dp_vport_multicast_group },
1990 { &dp_flow_genl_family,
1991 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1992 &dp_flow_multicast_group },
1993 { &dp_packet_genl_family,
1994 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1998 static void dp_unregister_genl(int n_families)
2002 for (i = 0; i < n_families; i++)
2003 genl_unregister_family(dp_genl_families[i].family);
2006 static int dp_register_genl(void)
2013 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2014 const struct genl_family_and_ops *f = &dp_genl_families[i];
2016 err = genl_register_family_with_ops(f->family, f->ops,
2023 err = genl_register_mc_group(f->family, f->group);
2032 dp_unregister_genl(n_registered);
2036 static int __init dp_init(void)
2038 struct sk_buff *dummy_skb;
2041 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2043 pr_info("Open vSwitch %s, built "__DATE__" "__TIME__"\n",
2052 goto error_tnl_exit;
2056 goto error_flow_exit;
2058 err = register_netdevice_notifier(&dp_device_notifier);
2060 goto error_vport_exit;
2062 err = dp_register_genl();
2064 goto error_unreg_notifier;
2068 error_unreg_notifier:
2069 unregister_netdevice_notifier(&dp_device_notifier);
2080 static void dp_cleanup(void)
2083 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2084 unregister_netdevice_notifier(&dp_device_notifier);
2090 module_init(dp_init);
2091 module_exit(dp_cleanup);
2093 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2094 MODULE_LICENSE("GPL");