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.tos_frag &= ~OVS_FRAG_TYPE_MASK;
427 later_key.ip.tos_frag |= OVS_FRAG_TYPE_LATER;
429 later_info = *upcall_info;
430 later_info.key = &later_key;
431 upcall_info = &later_info;
433 } while ((skb = skb->next));
435 /* Free all of the segments. */
443 } while ((skb = nskb));
447 static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
448 const struct dp_upcall_info *upcall_info)
450 struct ovs_header *upcall;
451 struct sk_buff *user_skb; /* to be queued to userspace */
456 err = vlan_deaccel_tag(skb);
460 if (nla_attr_size(skb->len) > USHRT_MAX)
463 len = sizeof(struct ovs_header);
464 len += nla_total_size(skb->len);
465 len += nla_total_size(FLOW_BUFSIZE);
466 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
467 len += nla_total_size(8);
469 user_skb = genlmsg_new(len, GFP_ATOMIC);
473 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
474 0, upcall_info->cmd);
475 upcall->dp_ifindex = dp_ifindex;
477 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
478 flow_to_nlattrs(upcall_info->key, user_skb);
479 nla_nest_end(user_skb, nla);
481 if (upcall_info->userdata)
482 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
483 nla_get_u64(upcall_info->userdata));
485 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
486 if (skb->ip_summed == CHECKSUM_PARTIAL)
487 copy_and_csum_skb(skb, nla_data(nla));
489 skb_copy_bits(skb, 0, nla_data(nla), skb->len);
491 return genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
494 /* Called with genl_mutex. */
495 static int flush_flows(int dp_ifindex)
497 struct flow_table *old_table;
498 struct flow_table *new_table;
501 dp = get_dp(dp_ifindex);
505 old_table = get_table_protected(dp);
506 new_table = flow_tbl_alloc(TBL_MIN_BUCKETS);
510 rcu_assign_pointer(dp->table, new_table);
512 flow_tbl_deferred_destroy(old_table);
516 static int validate_actions(const struct nlattr *attr,
517 const struct sw_flow_key *key, int depth);
519 static int validate_sample(const struct nlattr *attr,
520 const struct sw_flow_key *key, int depth)
522 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
523 const struct nlattr *probability, *actions;
524 const struct nlattr *a;
527 memset(attrs, 0, sizeof(attrs));
528 nla_for_each_nested(a, attr, rem) {
529 int type = nla_type(a);
530 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
537 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
538 if (!probability || nla_len(probability) != sizeof(u32))
541 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
542 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
544 return validate_actions(actions, key, depth + 1);
547 static int validate_action_key(const struct nlattr *a,
548 const struct sw_flow_key *flow_key)
550 int act_type = nla_type(a);
551 const struct nlattr *ovs_key = nla_data(a);
552 int key_type = nla_type(ovs_key);
554 /* There can be only one key in a action */
555 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
558 if (key_type > OVS_KEY_ATTR_MAX ||
559 nla_len(ovs_key) != ovs_key_lens[key_type])
562 #define ACTION(act, key) (((act) << 8) | (key))
564 switch (ACTION(act_type, key_type)) {
565 const struct ovs_key_ipv4 *ipv4_key;
566 const struct ovs_key_8021q *q_key;
568 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_PRIORITY):
569 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_TUN_ID):
570 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_ETHERNET):
573 case ACTION(OVS_ACTION_ATTR_PUSH, OVS_KEY_ATTR_8021Q):
574 q_key = nla_data(ovs_key);
575 if (q_key->q_tpid != htons(ETH_P_8021Q))
578 if (q_key->q_tci & htons(VLAN_TAG_PRESENT))
582 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_IPV4):
583 if (flow_key->eth.type != htons(ETH_P_IP))
586 if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
589 ipv4_key = nla_data(ovs_key);
590 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
593 if (ipv4_key->ipv4_tos & INET_ECN_MASK)
596 if (ipv4_key->ipv4_frag !=
597 (flow_key->ip.tos_frag & OVS_FRAG_TYPE_MASK))
602 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_TCP):
603 if (flow_key->ip.proto != IPPROTO_TCP)
606 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
611 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_UDP):
612 if (flow_key->ip.proto != IPPROTO_UDP)
615 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
626 static int validate_userspace(const struct nlattr *attr)
628 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
629 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
630 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
632 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
635 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
636 attr, userspace_policy);
640 if (!a[OVS_USERSPACE_ATTR_PID] ||
641 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
647 static int validate_actions(const struct nlattr *attr,
648 const struct sw_flow_key *key, int depth)
650 const struct nlattr *a;
653 if (depth >= SAMPLE_ACTION_DEPTH)
656 nla_for_each_nested(a, attr, rem) {
657 /* Expected argument lengths, (u32)-1 for variable length. */
658 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
659 [OVS_ACTION_ATTR_OUTPUT] = 4,
660 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
661 [OVS_ACTION_ATTR_PUSH] = (u32)-1,
662 [OVS_ACTION_ATTR_POP] = 2,
663 [OVS_ACTION_ATTR_SET] = (u32)-1,
664 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
666 int type = nla_type(a);
668 if (type > OVS_ACTION_ATTR_MAX ||
669 (action_lens[type] != nla_len(a) &&
670 action_lens[type] != (u32)-1))
674 case OVS_ACTION_ATTR_UNSPEC:
677 case OVS_ACTION_ATTR_USERSPACE:
678 err = validate_userspace(a);
683 case OVS_ACTION_ATTR_OUTPUT:
684 if (nla_get_u32(a) >= DP_MAX_PORTS)
689 case OVS_ACTION_ATTR_POP:
690 if (nla_get_u16(a) != OVS_KEY_ATTR_8021Q)
694 case OVS_ACTION_ATTR_SET:
695 case OVS_ACTION_ATTR_PUSH:
696 err = validate_action_key(a, key);
701 case OVS_ACTION_ATTR_SAMPLE:
702 err = validate_sample(a, key, depth);
718 static void clear_stats(struct sw_flow *flow)
722 flow->packet_count = 0;
723 flow->byte_count = 0;
726 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
728 struct ovs_header *ovs_header = info->userhdr;
729 struct nlattr **a = info->attrs;
730 struct sw_flow_actions *acts;
731 struct sk_buff *packet;
732 struct sw_flow *flow;
740 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
741 !a[OVS_PACKET_ATTR_ACTIONS] ||
742 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
745 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
746 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
750 skb_reserve(packet, NET_IP_ALIGN);
752 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
754 skb_reset_mac_header(packet);
755 eth = eth_hdr(packet);
757 /* Normally, setting the skb 'protocol' field would be handled by a
758 * call to eth_type_trans(), but it assumes there's a sending
759 * device, which we may not have. */
760 if (ntohs(eth->h_proto) >= 1536)
761 packet->protocol = eth->h_proto;
763 packet->protocol = htons(ETH_P_802_2);
765 /* Build an sw_flow for sending this packet. */
771 err = flow_extract(packet, -1, &flow->key, &key_len);
775 err = flow_metadata_from_nlattrs(&flow->key.phy.priority,
776 &flow->key.phy.in_port,
777 &flow->key.phy.tun_id,
778 a[OVS_PACKET_ATTR_KEY]);
782 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
786 flow->hash = flow_hash(&flow->key, key_len);
788 acts = flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
792 rcu_assign_pointer(flow->sf_acts, acts);
794 OVS_CB(packet)->flow = flow;
795 packet->priority = flow->key.phy.priority;
798 dp = get_dp(ovs_header->dp_ifindex);
804 err = execute_actions(dp, packet);
821 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
822 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
823 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
824 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
827 static struct genl_ops dp_packet_genl_ops[] = {
828 { .cmd = OVS_PACKET_CMD_EXECUTE,
829 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
830 .policy = packet_policy,
831 .doit = ovs_packet_cmd_execute
835 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
838 struct flow_table *table = get_table_protected(dp);
840 stats->n_flows = flow_tbl_count(table);
842 stats->n_hit = stats->n_missed = stats->n_lost = 0;
843 for_each_possible_cpu(i) {
844 const struct dp_stats_percpu *percpu_stats;
845 struct dp_stats_percpu local_stats;
848 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
851 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
852 local_stats = *percpu_stats;
853 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
855 stats->n_hit += local_stats.n_hit;
856 stats->n_missed += local_stats.n_missed;
857 stats->n_lost += local_stats.n_lost;
861 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
862 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
863 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
864 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
867 static struct genl_family dp_flow_genl_family = {
868 .id = GENL_ID_GENERATE,
869 .hdrsize = sizeof(struct ovs_header),
870 .name = OVS_FLOW_FAMILY,
871 .version = OVS_FLOW_VERSION,
872 .maxattr = OVS_FLOW_ATTR_MAX
875 static struct genl_multicast_group dp_flow_multicast_group = {
876 .name = OVS_FLOW_MCGROUP
879 /* Called with genl_lock. */
880 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
881 struct sk_buff *skb, u32 pid,
882 u32 seq, u32 flags, u8 cmd)
884 const int skb_orig_len = skb->len;
885 const struct sw_flow_actions *sf_acts;
886 struct ovs_flow_stats stats;
887 struct ovs_header *ovs_header;
893 sf_acts = rcu_dereference_protected(flow->sf_acts,
894 lockdep_genl_is_held());
896 ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
900 ovs_header->dp_ifindex = get_dpifindex(dp);
902 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
904 goto nla_put_failure;
905 err = flow_to_nlattrs(&flow->key, skb);
908 nla_nest_end(skb, nla);
910 spin_lock_bh(&flow->lock);
912 stats.n_packets = flow->packet_count;
913 stats.n_bytes = flow->byte_count;
914 tcp_flags = flow->tcp_flags;
915 spin_unlock_bh(&flow->lock);
918 NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, flow_used_time(used));
921 NLA_PUT(skb, OVS_FLOW_ATTR_STATS,
922 sizeof(struct ovs_flow_stats), &stats);
925 NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
927 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
928 * this is the first flow to be dumped into 'skb'. This is unusual for
929 * Netlink but individual action lists can be longer than
930 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
931 * The userspace caller can always fetch the actions separately if it
932 * really wants them. (Most userspace callers in fact don't care.)
934 * This can only fail for dump operations because the skb is always
935 * properly sized for single flows.
937 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
939 if (err < 0 && skb_orig_len)
942 return genlmsg_end(skb, ovs_header);
947 genlmsg_cancel(skb, ovs_header);
951 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
953 const struct sw_flow_actions *sf_acts;
956 sf_acts = rcu_dereference_protected(flow->sf_acts,
957 lockdep_genl_is_held());
959 /* OVS_FLOW_ATTR_KEY */
960 len = nla_total_size(FLOW_BUFSIZE);
961 /* OVS_FLOW_ATTR_ACTIONS */
962 len += nla_total_size(sf_acts->actions_len);
963 /* OVS_FLOW_ATTR_STATS */
964 len += nla_total_size(sizeof(struct ovs_flow_stats));
965 /* OVS_FLOW_ATTR_TCP_FLAGS */
966 len += nla_total_size(1);
967 /* OVS_FLOW_ATTR_USED */
968 len += nla_total_size(8);
970 len += NLMSG_ALIGN(sizeof(struct ovs_header));
972 return genlmsg_new(len, GFP_KERNEL);
975 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
977 u32 pid, u32 seq, u8 cmd)
982 skb = ovs_flow_cmd_alloc_info(flow);
984 return ERR_PTR(-ENOMEM);
986 retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
991 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
993 struct nlattr **a = info->attrs;
994 struct ovs_header *ovs_header = info->userhdr;
995 struct sw_flow_key key;
996 struct sw_flow *flow;
997 struct sk_buff *reply;
999 struct flow_table *table;
1005 if (!a[OVS_FLOW_ATTR_KEY])
1007 error = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1011 /* Validate actions. */
1012 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1013 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
1016 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1021 dp = get_dp(ovs_header->dp_ifindex);
1026 table = get_table_protected(dp);
1027 flow = flow_tbl_lookup(table, &key, key_len);
1029 struct sw_flow_actions *acts;
1031 /* Bail out if we're not allowed to create a new flow. */
1033 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1036 /* Expand table, if necessary, to make room. */
1037 if (flow_tbl_need_to_expand(table)) {
1038 struct flow_table *new_table;
1040 new_table = flow_tbl_expand(table);
1041 if (!IS_ERR(new_table)) {
1042 rcu_assign_pointer(dp->table, new_table);
1043 flow_tbl_deferred_destroy(table);
1044 table = get_table_protected(dp);
1048 /* Allocate flow. */
1049 flow = flow_alloc();
1051 error = PTR_ERR(flow);
1057 /* Obtain actions. */
1058 acts = flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1059 error = PTR_ERR(acts);
1061 goto error_free_flow;
1062 rcu_assign_pointer(flow->sf_acts, acts);
1064 /* Put flow in bucket. */
1065 flow->hash = flow_hash(&key, key_len);
1066 flow_tbl_insert(table, flow);
1068 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1072 /* We found a matching flow. */
1073 struct sw_flow_actions *old_acts;
1074 struct nlattr *acts_attrs;
1076 /* Bail out if we're not allowed to modify an existing flow.
1077 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1078 * because Generic Netlink treats the latter as a dump
1079 * request. We also accept NLM_F_EXCL in case that bug ever
1083 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1084 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1087 /* Update actions. */
1088 old_acts = rcu_dereference_protected(flow->sf_acts,
1089 lockdep_genl_is_held());
1090 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1092 (old_acts->actions_len != nla_len(acts_attrs) ||
1093 memcmp(old_acts->actions, nla_data(acts_attrs),
1094 old_acts->actions_len))) {
1095 struct sw_flow_actions *new_acts;
1097 new_acts = flow_actions_alloc(acts_attrs);
1098 error = PTR_ERR(new_acts);
1099 if (IS_ERR(new_acts))
1102 rcu_assign_pointer(flow->sf_acts, new_acts);
1103 flow_deferred_free_acts(old_acts);
1106 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1107 info->snd_seq, OVS_FLOW_CMD_NEW);
1110 if (a[OVS_FLOW_ATTR_CLEAR]) {
1111 spin_lock_bh(&flow->lock);
1113 spin_unlock_bh(&flow->lock);
1118 genl_notify(reply, genl_info_net(info), info->snd_pid,
1119 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1121 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1122 dp_flow_multicast_group.id, PTR_ERR(reply));
1131 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1133 struct nlattr **a = info->attrs;
1134 struct ovs_header *ovs_header = info->userhdr;
1135 struct sw_flow_key key;
1136 struct sk_buff *reply;
1137 struct sw_flow *flow;
1138 struct datapath *dp;
1139 struct flow_table *table;
1143 if (!a[OVS_FLOW_ATTR_KEY])
1145 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1149 dp = get_dp(ovs_header->dp_ifindex);
1153 table = get_table_protected(dp);
1154 flow = flow_tbl_lookup(table, &key, key_len);
1158 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1159 info->snd_seq, OVS_FLOW_CMD_NEW);
1161 return PTR_ERR(reply);
1163 return genlmsg_reply(reply, info);
1166 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1168 struct nlattr **a = info->attrs;
1169 struct ovs_header *ovs_header = info->userhdr;
1170 struct sw_flow_key key;
1171 struct sk_buff *reply;
1172 struct sw_flow *flow;
1173 struct datapath *dp;
1174 struct flow_table *table;
1178 if (!a[OVS_FLOW_ATTR_KEY])
1179 return flush_flows(ovs_header->dp_ifindex);
1180 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1184 dp = get_dp(ovs_header->dp_ifindex);
1188 table = get_table_protected(dp);
1189 flow = flow_tbl_lookup(table, &key, key_len);
1193 reply = ovs_flow_cmd_alloc_info(flow);
1197 flow_tbl_remove(table, flow);
1199 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1200 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1203 flow_deferred_free(flow);
1205 genl_notify(reply, genl_info_net(info), info->snd_pid,
1206 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1210 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1212 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1213 struct datapath *dp;
1215 dp = get_dp(ovs_header->dp_ifindex);
1220 struct sw_flow *flow;
1223 bucket = cb->args[0];
1225 flow = flow_tbl_next(get_table_protected(dp), &bucket, &obj);
1229 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1230 NETLINK_CB(cb->skb).pid,
1231 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1232 OVS_FLOW_CMD_NEW) < 0)
1235 cb->args[0] = bucket;
1241 static struct genl_ops dp_flow_genl_ops[] = {
1242 { .cmd = OVS_FLOW_CMD_NEW,
1243 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1244 .policy = flow_policy,
1245 .doit = ovs_flow_cmd_new_or_set
1247 { .cmd = OVS_FLOW_CMD_DEL,
1248 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1249 .policy = flow_policy,
1250 .doit = ovs_flow_cmd_del
1252 { .cmd = OVS_FLOW_CMD_GET,
1253 .flags = 0, /* OK for unprivileged users. */
1254 .policy = flow_policy,
1255 .doit = ovs_flow_cmd_get,
1256 .dumpit = ovs_flow_cmd_dump
1258 { .cmd = OVS_FLOW_CMD_SET,
1259 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1260 .policy = flow_policy,
1261 .doit = ovs_flow_cmd_new_or_set,
1265 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1266 #ifdef HAVE_NLA_NUL_STRING
1267 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1269 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1272 static struct genl_family dp_datapath_genl_family = {
1273 .id = GENL_ID_GENERATE,
1274 .hdrsize = sizeof(struct ovs_header),
1275 .name = OVS_DATAPATH_FAMILY,
1276 .version = OVS_DATAPATH_VERSION,
1277 .maxattr = OVS_DP_ATTR_MAX
1280 static struct genl_multicast_group dp_datapath_multicast_group = {
1281 .name = OVS_DATAPATH_MCGROUP
1284 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1285 u32 pid, u32 seq, u32 flags, u8 cmd)
1287 struct ovs_header *ovs_header;
1291 ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1296 ovs_header->dp_ifindex = get_dpifindex(dp);
1299 err = nla_put_string(skb, OVS_DP_ATTR_NAME, dp_name(dp));
1302 goto nla_put_failure;
1304 nla = nla_reserve(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats));
1306 goto nla_put_failure;
1307 get_dp_stats(dp, nla_data(nla));
1309 return genlmsg_end(skb, ovs_header);
1312 genlmsg_cancel(skb, ovs_header);
1317 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1320 struct sk_buff *skb;
1323 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1325 return ERR_PTR(-ENOMEM);
1327 retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1330 return ERR_PTR(retval);
1335 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1337 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1340 /* Called with genl_mutex and optionally with RTNL lock also. */
1341 static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
1342 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1344 struct datapath *dp;
1346 if (!a[OVS_DP_ATTR_NAME])
1347 dp = get_dp(ovs_header->dp_ifindex);
1349 struct vport *vport;
1352 vport = vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
1353 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1356 return dp ? dp : ERR_PTR(-ENODEV);
1359 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1361 struct nlattr **a = info->attrs;
1362 struct vport_parms parms;
1363 struct sk_buff *reply;
1364 struct datapath *dp;
1365 struct vport *vport;
1369 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1372 err = ovs_dp_cmd_validate(a);
1378 if (!try_module_get(THIS_MODULE))
1379 goto err_unlock_rtnl;
1382 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1384 goto err_put_module;
1385 INIT_LIST_HEAD(&dp->port_list);
1387 /* Initialize kobject for bridge. This will be added as
1388 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1389 dp->ifobj.kset = NULL;
1390 kobject_init(&dp->ifobj, &dp_ktype);
1392 /* Allocate table. */
1394 rcu_assign_pointer(dp->table, flow_tbl_alloc(TBL_MIN_BUCKETS));
1398 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1399 if (!dp->stats_percpu) {
1401 goto err_destroy_table;
1404 /* Set up our datapath device. */
1405 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1406 parms.type = OVS_VPORT_TYPE_INTERNAL;
1407 parms.options = NULL;
1409 parms.port_no = OVSP_LOCAL;
1410 parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1412 vport = new_vport(&parms);
1413 if (IS_ERR(vport)) {
1414 err = PTR_ERR(vport);
1418 goto err_destroy_percpu;
1421 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1422 info->snd_seq, OVS_DP_CMD_NEW);
1423 err = PTR_ERR(reply);
1425 goto err_destroy_local_port;
1427 list_add_tail(&dp->list_node, &dps);
1428 dp_sysfs_add_dp(dp);
1432 genl_notify(reply, genl_info_net(info), info->snd_pid,
1433 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1436 err_destroy_local_port:
1437 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1439 free_percpu(dp->stats_percpu);
1441 flow_tbl_destroy(get_table_protected(dp));
1445 module_put(THIS_MODULE);
1452 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1454 struct vport *vport, *next_vport;
1455 struct sk_buff *reply;
1456 struct datapath *dp;
1459 err = ovs_dp_cmd_validate(info->attrs);
1464 dp = lookup_datapath(info->userhdr, info->attrs);
1469 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1470 info->snd_seq, OVS_DP_CMD_DEL);
1471 err = PTR_ERR(reply);
1475 list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
1476 if (vport->port_no != OVSP_LOCAL)
1477 dp_detach_port(vport);
1479 dp_sysfs_del_dp(dp);
1480 list_del(&dp->list_node);
1481 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1483 /* rtnl_unlock() will wait until all the references to devices that
1484 * are pending unregistration have been dropped. We do it here to
1485 * ensure that any internal devices (which contain DP pointers) are
1486 * fully destroyed before freeing the datapath.
1490 call_rcu(&dp->rcu, destroy_dp_rcu);
1491 module_put(THIS_MODULE);
1493 genl_notify(reply, genl_info_net(info), info->snd_pid,
1494 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1504 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1506 struct sk_buff *reply;
1507 struct datapath *dp;
1510 err = ovs_dp_cmd_validate(info->attrs);
1514 dp = lookup_datapath(info->userhdr, info->attrs);
1518 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1519 info->snd_seq, OVS_DP_CMD_NEW);
1520 if (IS_ERR(reply)) {
1521 err = PTR_ERR(reply);
1522 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1523 dp_datapath_multicast_group.id, err);
1527 genl_notify(reply, genl_info_net(info), info->snd_pid,
1528 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1532 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1534 struct sk_buff *reply;
1535 struct datapath *dp;
1538 err = ovs_dp_cmd_validate(info->attrs);
1542 dp = lookup_datapath(info->userhdr, info->attrs);
1546 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1547 info->snd_seq, OVS_DP_CMD_NEW);
1549 return PTR_ERR(reply);
1551 return genlmsg_reply(reply, info);
1554 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1556 struct datapath *dp;
1557 int skip = cb->args[0];
1560 list_for_each_entry(dp, &dps, list_node) {
1563 if (ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1564 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1565 OVS_DP_CMD_NEW) < 0)
1575 static struct genl_ops dp_datapath_genl_ops[] = {
1576 { .cmd = OVS_DP_CMD_NEW,
1577 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1578 .policy = datapath_policy,
1579 .doit = ovs_dp_cmd_new
1581 { .cmd = OVS_DP_CMD_DEL,
1582 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1583 .policy = datapath_policy,
1584 .doit = ovs_dp_cmd_del
1586 { .cmd = OVS_DP_CMD_GET,
1587 .flags = 0, /* OK for unprivileged users. */
1588 .policy = datapath_policy,
1589 .doit = ovs_dp_cmd_get,
1590 .dumpit = ovs_dp_cmd_dump
1592 { .cmd = OVS_DP_CMD_SET,
1593 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1594 .policy = datapath_policy,
1595 .doit = ovs_dp_cmd_set,
1599 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1600 #ifdef HAVE_NLA_NUL_STRING
1601 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1602 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1603 [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1605 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1606 [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1608 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1609 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1610 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1611 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1614 static struct genl_family dp_vport_genl_family = {
1615 .id = GENL_ID_GENERATE,
1616 .hdrsize = sizeof(struct ovs_header),
1617 .name = OVS_VPORT_FAMILY,
1618 .version = OVS_VPORT_VERSION,
1619 .maxattr = OVS_VPORT_ATTR_MAX
1622 struct genl_multicast_group dp_vport_multicast_group = {
1623 .name = OVS_VPORT_MCGROUP
1626 /* Called with RTNL lock or RCU read lock. */
1627 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1628 u32 pid, u32 seq, u32 flags, u8 cmd)
1630 struct ovs_header *ovs_header;
1634 ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1639 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1641 NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1642 NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type);
1643 NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport));
1644 NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1646 nla = nla_reserve(skb, OVS_VPORT_ATTR_STATS,
1647 sizeof(struct ovs_vport_stats));
1649 goto nla_put_failure;
1651 vport_get_stats(vport, nla_data(nla));
1653 NLA_PUT(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1654 vport->ops->get_addr(vport));
1656 err = vport_get_options(vport, skb);
1657 if (err == -EMSGSIZE)
1660 return genlmsg_end(skb, ovs_header);
1665 genlmsg_cancel(skb, ovs_header);
1669 /* Called with RTNL lock or RCU read lock. */
1670 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1673 struct sk_buff *skb;
1676 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1678 return ERR_PTR(-ENOMEM);
1680 retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1683 return ERR_PTR(retval);
1688 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1690 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1693 /* Called with RTNL lock or RCU read lock. */
1694 static struct vport *lookup_vport(struct ovs_header *ovs_header,
1695 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1697 struct datapath *dp;
1698 struct vport *vport;
1700 if (a[OVS_VPORT_ATTR_NAME]) {
1701 vport = vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
1703 return ERR_PTR(-ENODEV);
1705 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1706 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1708 if (port_no >= DP_MAX_PORTS)
1709 return ERR_PTR(-EFBIG);
1711 dp = get_dp(ovs_header->dp_ifindex);
1713 return ERR_PTR(-ENODEV);
1715 vport = get_vport_protected(dp, port_no);
1717 return ERR_PTR(-ENOENT);
1720 return ERR_PTR(-EINVAL);
1723 /* Called with RTNL lock. */
1724 static int change_vport(struct vport *vport,
1725 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1729 if (a[OVS_VPORT_ATTR_STATS])
1730 vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1732 if (a[OVS_VPORT_ATTR_ADDRESS])
1733 err = vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1738 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1740 struct nlattr **a = info->attrs;
1741 struct ovs_header *ovs_header = info->userhdr;
1742 struct vport_parms parms;
1743 struct sk_buff *reply;
1744 struct vport *vport;
1745 struct datapath *dp;
1750 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1751 !a[OVS_VPORT_ATTR_UPCALL_PID])
1754 err = ovs_vport_cmd_validate(a);
1759 dp = get_dp(ovs_header->dp_ifindex);
1764 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1765 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1768 if (port_no >= DP_MAX_PORTS)
1771 vport = get_vport_protected(dp, port_no);
1776 for (port_no = 1; ; port_no++) {
1777 if (port_no >= DP_MAX_PORTS) {
1781 vport = get_vport_protected(dp, port_no);
1787 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1788 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1789 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1791 parms.port_no = port_no;
1792 parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1794 vport = new_vport(&parms);
1795 err = PTR_ERR(vport);
1799 dp_sysfs_add_if(vport);
1801 err = change_vport(vport, a);
1803 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1807 err = PTR_ERR(reply);
1810 dp_detach_port(vport);
1813 genl_notify(reply, genl_info_net(info), info->snd_pid,
1814 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1823 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1825 struct nlattr **a = info->attrs;
1826 struct sk_buff *reply;
1827 struct vport *vport;
1830 err = ovs_vport_cmd_validate(a);
1835 vport = lookup_vport(info->userhdr, a);
1836 err = PTR_ERR(vport);
1841 if (a[OVS_VPORT_ATTR_TYPE] &&
1842 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1845 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1846 err = vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1848 err = change_vport(vport, a);
1849 if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1850 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1852 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1854 if (IS_ERR(reply)) {
1855 err = PTR_ERR(reply);
1856 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1857 dp_vport_multicast_group.id, err);
1861 genl_notify(reply, genl_info_net(info), info->snd_pid,
1862 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1870 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1872 struct nlattr **a = info->attrs;
1873 struct sk_buff *reply;
1874 struct vport *vport;
1877 err = ovs_vport_cmd_validate(a);
1882 vport = lookup_vport(info->userhdr, a);
1883 err = PTR_ERR(vport);
1887 if (vport->port_no == OVSP_LOCAL) {
1892 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1894 err = PTR_ERR(reply);
1898 dp_detach_port(vport);
1900 genl_notify(reply, genl_info_net(info), info->snd_pid,
1901 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1909 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1911 struct nlattr **a = info->attrs;
1912 struct ovs_header *ovs_header = info->userhdr;
1913 struct sk_buff *reply;
1914 struct vport *vport;
1917 err = ovs_vport_cmd_validate(a);
1922 vport = lookup_vport(ovs_header, a);
1923 err = PTR_ERR(vport);
1927 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1929 err = PTR_ERR(reply);
1935 return genlmsg_reply(reply, info);
1943 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1945 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1946 struct datapath *dp;
1950 dp = get_dp(ovs_header->dp_ifindex);
1955 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1956 struct vport *vport;
1958 vport = get_vport_protected(dp, port_no);
1962 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1963 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1964 OVS_VPORT_CMD_NEW) < 0)
1969 cb->args[0] = port_no;
1975 static struct genl_ops dp_vport_genl_ops[] = {
1976 { .cmd = OVS_VPORT_CMD_NEW,
1977 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1978 .policy = vport_policy,
1979 .doit = ovs_vport_cmd_new
1981 { .cmd = OVS_VPORT_CMD_DEL,
1982 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1983 .policy = vport_policy,
1984 .doit = ovs_vport_cmd_del
1986 { .cmd = OVS_VPORT_CMD_GET,
1987 .flags = 0, /* OK for unprivileged users. */
1988 .policy = vport_policy,
1989 .doit = ovs_vport_cmd_get,
1990 .dumpit = ovs_vport_cmd_dump
1992 { .cmd = OVS_VPORT_CMD_SET,
1993 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1994 .policy = vport_policy,
1995 .doit = ovs_vport_cmd_set,
1999 struct genl_family_and_ops {
2000 struct genl_family *family;
2001 struct genl_ops *ops;
2003 struct genl_multicast_group *group;
2006 static const struct genl_family_and_ops dp_genl_families[] = {
2007 { &dp_datapath_genl_family,
2008 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2009 &dp_datapath_multicast_group },
2010 { &dp_vport_genl_family,
2011 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2012 &dp_vport_multicast_group },
2013 { &dp_flow_genl_family,
2014 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2015 &dp_flow_multicast_group },
2016 { &dp_packet_genl_family,
2017 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2021 static void dp_unregister_genl(int n_families)
2025 for (i = 0; i < n_families; i++)
2026 genl_unregister_family(dp_genl_families[i].family);
2029 static int dp_register_genl(void)
2036 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2037 const struct genl_family_and_ops *f = &dp_genl_families[i];
2039 err = genl_register_family_with_ops(f->family, f->ops,
2046 err = genl_register_mc_group(f->family, f->group);
2055 dp_unregister_genl(n_registered);
2059 static int __init dp_init(void)
2061 struct sk_buff *dummy_skb;
2064 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2066 pr_info("Open vSwitch %s, built "__DATE__" "__TIME__"\n",
2075 goto error_tnl_exit;
2079 goto error_flow_exit;
2081 err = register_netdevice_notifier(&dp_device_notifier);
2083 goto error_vport_exit;
2085 err = dp_register_genl();
2087 goto error_unreg_notifier;
2091 error_unreg_notifier:
2092 unregister_netdevice_notifier(&dp_device_notifier);
2103 static void dp_cleanup(void)
2106 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2107 unregister_netdevice_notifier(&dp_device_notifier);
2113 module_init(dp_init);
2114 module_exit(dp_cleanup);
2116 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2117 MODULE_LICENSE("GPL");