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>
37 #include <linux/highmem.h>
38 #include <linux/netfilter_bridge.h>
39 #include <linux/netfilter_ipv4.h>
40 #include <linux/inetdevice.h>
41 #include <linux/list.h>
42 #include <linux/openvswitch.h>
43 #include <linux/rculist.h>
44 #include <linux/dmi.h>
45 #include <net/inet_ecn.h>
46 #include <net/genetlink.h>
54 #include "vport-internal_dev.h"
56 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
57 LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0)
58 #error Kernels before 2.6.18 or after 3.1 are not supported by this version of Open vSwitch.
61 int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
62 EXPORT_SYMBOL(dp_ioctl_hook);
67 * Writes to device state (add/remove datapath, port, set operations on vports,
68 * etc.) are protected by RTNL.
70 * Writes to other state (flow table modifications, set miscellaneous datapath
71 * parameters such as drop frags, etc.) are protected by genl_mutex. The RTNL
72 * lock nests inside genl_mutex.
74 * Reads are protected by RCU.
76 * There are a few special cases (mostly stats) that have their own
77 * synchronization but they nest under all of above and don't interact with
81 /* Global list of datapaths to enable dumping them all out.
82 * Protected by genl_mutex.
84 static LIST_HEAD(dps);
86 static struct vport *new_vport(const struct vport_parms *);
87 static int queue_userspace_packets(struct datapath *, 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 return vport_get_name(rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]));
127 static int get_dpifindex(struct datapath *dp)
134 local = get_vport_protected(dp, OVSP_LOCAL);
136 ifindex = vport_get_ifindex(local);
145 static inline size_t br_nlmsg_size(void)
147 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
148 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
149 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
150 + nla_total_size(4) /* IFLA_MASTER */
151 + nla_total_size(4) /* IFLA_MTU */
152 + nla_total_size(1); /* IFLA_OPERSTATE */
155 /* Caller must hold RTNL lock. */
156 static int dp_fill_ifinfo(struct sk_buff *skb,
157 const struct vport *port,
158 int event, unsigned int flags)
160 struct datapath *dp = port->dp;
161 int ifindex = vport_get_ifindex(port);
162 struct ifinfomsg *hdr;
163 struct nlmsghdr *nlh;
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 = ifindex;
177 hdr->ifi_flags = vport_get_flags(port);
180 NLA_PUT_STRING(skb, IFLA_IFNAME, vport_get_name(port));
181 NLA_PUT_U32(skb, IFLA_MASTER, get_dpifindex(dp));
182 NLA_PUT_U32(skb, IFLA_MTU, vport_get_mtu(port));
183 #ifdef IFLA_OPERSTATE
184 NLA_PUT_U8(skb, IFLA_OPERSTATE,
185 vport_is_running(port)
186 ? vport_get_operstate(port)
190 NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, vport_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);
209 err = dp_fill_ifinfo(skb, port, event, 0);
211 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
212 WARN_ON(err == -EMSGSIZE);
216 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
220 rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
223 static void release_dp(struct kobject *kobj)
225 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
229 static struct kobj_type dp_ktype = {
230 .release = release_dp
233 static void destroy_dp_rcu(struct rcu_head *rcu)
235 struct datapath *dp = container_of(rcu, struct datapath, rcu);
237 flow_tbl_destroy(dp->table);
238 free_percpu(dp->stats_percpu);
239 kobject_put(&dp->ifobj);
242 /* Called with RTNL lock and genl_lock. */
243 static struct vport *new_vport(const struct vport_parms *parms)
247 vport = vport_add(parms);
248 if (!IS_ERR(vport)) {
249 struct datapath *dp = parms->dp;
251 rcu_assign_pointer(dp->ports[parms->port_no], vport);
252 list_add(&vport->node, &dp->port_list);
254 dp_ifinfo_notify(RTM_NEWLINK, vport);
260 /* Called with RTNL lock. */
261 void dp_detach_port(struct vport *p)
265 if (p->port_no != OVSP_LOCAL)
267 dp_ifinfo_notify(RTM_DELLINK, p);
269 /* First drop references to device. */
271 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
273 /* Then destroy it. */
277 /* Must be called with rcu_read_lock. */
278 void dp_process_received_packet(struct vport *p, struct sk_buff *skb)
280 struct datapath *dp = p->dp;
281 struct sw_flow *flow;
282 struct dp_stats_percpu *stats;
286 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
287 OVS_CB(skb)->vport = p;
289 if (!OVS_CB(skb)->flow) {
290 struct sw_flow_key key;
294 /* Extract flow from 'skb' into 'key'. */
295 error = flow_extract(skb, p->port_no, &key, &key_len, &is_frag);
296 if (unlikely(error)) {
301 if (is_frag && dp->drop_frags) {
303 stats_counter = &stats->n_frags;
308 flow = flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
309 if (unlikely(!flow)) {
310 struct dp_upcall_info upcall;
312 upcall.cmd = OVS_PACKET_CMD_MISS;
314 upcall.userdata = NULL;
315 upcall.pid = p->upcall_pid;
316 dp_upcall(dp, skb, &upcall);
318 stats_counter = &stats->n_missed;
322 OVS_CB(skb)->flow = flow;
325 stats_counter = &stats->n_hit;
326 flow_used(OVS_CB(skb)->flow, skb);
327 execute_actions(dp, skb);
330 /* Update datapath statistics. */
332 write_seqcount_begin(&stats->seqlock);
334 write_seqcount_end(&stats->seqlock);
337 static void copy_and_csum_skb(struct sk_buff *skb, void *to)
339 u16 csum_start, csum_offset;
342 get_skb_csum_pointers(skb, &csum_start, &csum_offset);
343 csum_start -= skb_headroom(skb);
345 skb_copy_bits(skb, 0, to, csum_start);
347 csum = skb_copy_and_csum_bits(skb, csum_start, to + csum_start,
348 skb->len - csum_start, 0);
349 *(__sum16 *)(to + csum_start + csum_offset) = csum_fold(csum);
352 static struct genl_family dp_packet_genl_family = {
353 .id = GENL_ID_GENERATE,
354 .hdrsize = sizeof(struct ovs_header),
355 .name = OVS_PACKET_FAMILY,
357 .maxattr = OVS_PACKET_ATTR_MAX
360 int dp_upcall(struct datapath *dp, struct sk_buff *skb,
361 const struct dp_upcall_info *upcall_info)
363 struct sk_buff *segs = NULL;
364 struct dp_stats_percpu *stats;
367 if (upcall_info->pid == 0) {
372 forward_ip_summed(skb, true);
374 /* Break apart GSO packets into their component pieces. Otherwise
375 * userspace may try to stuff a 64kB packet into a 1500-byte MTU. */
376 if (skb_is_gso(skb)) {
377 segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
386 err = queue_userspace_packets(dp, skb, upcall_info);
388 struct sk_buff *next;
389 /* Free GSO-segments */
393 } while ((segs = next) != NULL);
402 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
404 write_seqcount_begin(&stats->seqlock);
406 write_seqcount_end(&stats->seqlock);
411 /* Send each packet in the 'skb' list to userspace for 'dp' as directed by
412 * 'upcall_info'. There will be only one packet unless we broke up a GSO
415 static int queue_userspace_packets(struct datapath *dp, struct sk_buff *skb,
416 const struct dp_upcall_info *upcall_info)
420 dp_ifindex = get_dpifindex(dp);
425 struct ovs_header *upcall;
426 struct sk_buff *user_skb; /* to be queued to userspace */
431 err = vlan_deaccel_tag(skb);
435 if (nla_attr_size(skb->len) > USHRT_MAX)
438 len = sizeof(struct ovs_header);
439 len += nla_total_size(skb->len);
440 len += nla_total_size(FLOW_BUFSIZE);
441 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
442 len += nla_total_size(8);
444 user_skb = genlmsg_new(len, GFP_ATOMIC);
448 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
449 0, upcall_info->cmd);
450 upcall->dp_ifindex = dp_ifindex;
452 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
453 flow_to_nlattrs(upcall_info->key, user_skb);
454 nla_nest_end(user_skb, nla);
456 if (upcall_info->userdata)
457 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
458 nla_get_u64(upcall_info->userdata));
460 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
461 if (skb->ip_summed == CHECKSUM_PARTIAL)
462 copy_and_csum_skb(skb, nla_data(nla));
464 skb_copy_bits(skb, 0, nla_data(nla), skb->len);
466 err = genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
470 } while ((skb = skb->next));
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 static const struct nla_policy sample_policy[OVS_SAMPLE_ATTR_MAX + 1] =
505 [OVS_SAMPLE_ATTR_PROBABILITY] = {.type = NLA_U32 },
506 [OVS_SAMPLE_ATTR_ACTIONS] = {.type = NLA_UNSPEC },
508 struct nlattr *a[OVS_SAMPLE_ATTR_MAX + 1];
511 error = nla_parse_nested(a, OVS_SAMPLE_ATTR_MAX, attr, sample_policy);
515 if (!a[OVS_SAMPLE_ATTR_PROBABILITY])
517 if (!a[OVS_SAMPLE_ATTR_ACTIONS])
520 return validate_actions(a[OVS_SAMPLE_ATTR_ACTIONS], key, (depth + 1));
523 static int validate_action_key(const struct nlattr *a,
524 const struct sw_flow_key *flow_key)
526 int act_type = nla_type(a);
527 const struct nlattr *ovs_key = nla_data(a);
528 int key_type = nla_type(ovs_key);
530 /* There can be only one key in a action */
531 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
534 if (key_type > OVS_KEY_ATTR_MAX ||
535 nla_len(ovs_key) != ovs_key_lens[key_type])
538 #define ACTION(act, key) ((act << 8) | key)
540 switch(ACTION(act_type, key_type)) {
541 const struct ovs_key_ipv4 *ipv4_key;
542 const struct ovs_key_8021q *q_key;
544 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_TUN_ID):
545 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_ETHERNET):
548 case ACTION(OVS_ACTION_ATTR_PUSH, OVS_KEY_ATTR_8021Q):
549 q_key = nla_data(ovs_key);
550 if (q_key->q_tpid != htons(ETH_P_8021Q))
553 if (q_key->q_tci & htons(VLAN_TAG_PRESENT))
557 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_IPV4):
558 if (flow_key->eth.type != htons(ETH_P_IP))
561 if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
564 ipv4_key = nla_data(ovs_key);
565 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
568 if (ipv4_key->ipv4_tos & INET_ECN_MASK)
572 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_TCP):
573 if (flow_key->ip.proto != IPPROTO_TCP)
576 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
581 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_UDP):
582 if (flow_key->ip.proto != IPPROTO_UDP)
585 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
596 static int validate_userspace(const struct nlattr *attr)
598 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] =
600 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
601 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
603 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
606 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, attr, userspace_policy);
610 if (!a[OVS_USERSPACE_ATTR_PID] || !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
616 static int validate_actions(const struct nlattr *attr,
617 const struct sw_flow_key *key, int depth)
619 const struct nlattr *a;
622 if (depth >= SAMPLE_ACTION_DEPTH)
625 nla_for_each_nested(a, attr, rem) {
626 /* Expected argument lengths, (u32)-1 for variable length. */
627 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
628 [OVS_ACTION_ATTR_OUTPUT] = 4,
629 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
630 [OVS_ACTION_ATTR_PUSH] = (u32)-1,
631 [OVS_ACTION_ATTR_POP] = 2,
632 [OVS_ACTION_ATTR_SET] = (u32)-1,
633 [OVS_ACTION_ATTR_SET_PRIORITY] = 4,
634 [OVS_ACTION_ATTR_POP_PRIORITY] = 0,
635 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
637 int type = nla_type(a);
639 if (type > OVS_ACTION_ATTR_MAX ||
640 (action_lens[type] != nla_len(a) &&
641 action_lens[type] != (u32)-1))
645 case OVS_ACTION_ATTR_UNSPEC:
648 case OVS_ACTION_ATTR_SET_PRIORITY:
649 case OVS_ACTION_ATTR_POP_PRIORITY:
650 /* No validation needed. */
653 case OVS_ACTION_ATTR_USERSPACE:
654 err = validate_userspace(a);
659 case OVS_ACTION_ATTR_OUTPUT:
660 if (nla_get_u32(a) >= DP_MAX_PORTS)
665 case OVS_ACTION_ATTR_POP:
666 if (nla_get_u16(a) != OVS_KEY_ATTR_8021Q)
670 case OVS_ACTION_ATTR_SET:
671 case OVS_ACTION_ATTR_PUSH:
672 err = validate_action_key(a, key);
677 case OVS_ACTION_ATTR_SAMPLE:
678 err = validate_sample(a, key, depth);
694 static void clear_stats(struct sw_flow *flow)
698 flow->packet_count = 0;
699 flow->byte_count = 0;
702 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
704 struct ovs_header *ovs_header = info->userhdr;
705 struct nlattr **a = info->attrs;
706 struct sw_flow_actions *acts;
707 struct sk_buff *packet;
708 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, &is_frag);
752 err = flow_metadata_from_nlattrs(&flow->key.eth.in_port,
753 &flow->key.eth.tun_id,
754 a[OVS_PACKET_ATTR_KEY]);
758 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
762 flow->hash = flow_hash(&flow->key, key_len);
764 acts = flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
768 rcu_assign_pointer(flow->sf_acts, acts);
770 OVS_CB(packet)->flow = flow;
773 dp = get_dp(ovs_header->dp_ifindex);
778 if (flow->key.eth.in_port < DP_MAX_PORTS)
779 OVS_CB(packet)->vport = get_vport_protected(dp,
780 flow->key.eth.in_port);
783 err = execute_actions(dp, packet);
800 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
801 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
802 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
803 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
806 static struct genl_ops dp_packet_genl_ops[] = {
807 { .cmd = OVS_PACKET_CMD_EXECUTE,
808 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
809 .policy = packet_policy,
810 .doit = ovs_packet_cmd_execute
814 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
817 struct flow_table *table = get_table_protected(dp);
819 stats->n_flows = flow_tbl_count(table);
821 stats->n_frags = stats->n_hit = stats->n_missed = stats->n_lost = 0;
822 for_each_possible_cpu(i) {
823 const struct dp_stats_percpu *percpu_stats;
824 struct dp_stats_percpu local_stats;
827 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
830 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
831 local_stats = *percpu_stats;
832 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
834 stats->n_frags += local_stats.n_frags;
835 stats->n_hit += local_stats.n_hit;
836 stats->n_missed += local_stats.n_missed;
837 stats->n_lost += local_stats.n_lost;
841 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
842 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
843 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
844 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
847 static struct genl_family dp_flow_genl_family = {
848 .id = GENL_ID_GENERATE,
849 .hdrsize = sizeof(struct ovs_header),
850 .name = OVS_FLOW_FAMILY,
852 .maxattr = OVS_FLOW_ATTR_MAX
855 static struct genl_multicast_group dp_flow_multicast_group = {
856 .name = OVS_FLOW_MCGROUP
859 /* Called with genl_lock. */
860 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
861 struct sk_buff *skb, u32 pid, u32 seq, u32 flags, u8 cmd)
863 const int skb_orig_len = skb->len;
864 const struct sw_flow_actions *sf_acts;
865 struct ovs_flow_stats stats;
866 struct ovs_header *ovs_header;
872 sf_acts = rcu_dereference_protected(flow->sf_acts,
873 lockdep_genl_is_held());
875 ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
879 ovs_header->dp_ifindex = get_dpifindex(dp);
881 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
883 goto nla_put_failure;
884 err = flow_to_nlattrs(&flow->key, skb);
887 nla_nest_end(skb, nla);
889 spin_lock_bh(&flow->lock);
891 stats.n_packets = flow->packet_count;
892 stats.n_bytes = flow->byte_count;
893 tcp_flags = flow->tcp_flags;
894 spin_unlock_bh(&flow->lock);
897 NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, flow_used_time(used));
900 NLA_PUT(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats);
903 NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
905 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
906 * this is the first flow to be dumped into 'skb'. This is unusual for
907 * Netlink but individual action lists can be longer than
908 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
909 * The userspace caller can always fetch the actions separately if it
910 * really wants them. (Most userspace callers in fact don't care.)
912 * This can only fail for dump operations because the skb is always
913 * properly sized for single flows.
915 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
917 if (err < 0 && skb_orig_len)
920 return genlmsg_end(skb, ovs_header);
925 genlmsg_cancel(skb, ovs_header);
929 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
931 const struct sw_flow_actions *sf_acts;
934 sf_acts = rcu_dereference_protected(flow->sf_acts,
935 lockdep_genl_is_held());
937 len = nla_total_size(FLOW_BUFSIZE); /* OVS_FLOW_ATTR_KEY */
938 len += nla_total_size(sf_acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
939 len += nla_total_size(sizeof(struct ovs_flow_stats)); /* OVS_FLOW_ATTR_STATS */
940 len += nla_total_size(1); /* OVS_FLOW_ATTR_TCP_FLAGS */
941 len += nla_total_size(8); /* OVS_FLOW_ATTR_USED */
942 return genlmsg_new(NLMSG_ALIGN(sizeof(struct ovs_header)) + len, GFP_KERNEL);
945 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow, struct datapath *dp,
946 u32 pid, u32 seq, u8 cmd)
951 skb = ovs_flow_cmd_alloc_info(flow);
953 return ERR_PTR(-ENOMEM);
955 retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
960 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
962 struct nlattr **a = info->attrs;
963 struct ovs_header *ovs_header = info->userhdr;
964 struct sw_flow_key key;
965 struct sw_flow *flow;
966 struct sk_buff *reply;
968 struct flow_table *table;
974 if (!a[OVS_FLOW_ATTR_KEY])
976 error = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
980 /* Validate actions. */
981 if (a[OVS_FLOW_ATTR_ACTIONS]) {
982 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
985 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
990 dp = get_dp(ovs_header->dp_ifindex);
995 table = get_table_protected(dp);
996 flow = flow_tbl_lookup(table, &key, key_len);
998 struct sw_flow_actions *acts;
1000 /* Bail out if we're not allowed to create a new flow. */
1002 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1005 /* Expand table, if necessary, to make room. */
1006 if (flow_tbl_need_to_expand(table)) {
1007 struct flow_table *new_table;
1009 new_table = flow_tbl_expand(table);
1010 if (!IS_ERR(new_table)) {
1011 rcu_assign_pointer(dp->table, new_table);
1012 flow_tbl_deferred_destroy(table);
1013 table = get_table_protected(dp);
1017 /* Allocate flow. */
1018 flow = flow_alloc();
1020 error = PTR_ERR(flow);
1026 /* Obtain actions. */
1027 acts = flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1028 error = PTR_ERR(acts);
1030 goto error_free_flow;
1031 rcu_assign_pointer(flow->sf_acts, acts);
1033 /* Put flow in bucket. */
1034 flow->hash = flow_hash(&key, key_len);
1035 flow_tbl_insert(table, flow);
1037 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1038 info->snd_seq, OVS_FLOW_CMD_NEW);
1040 /* We found a matching flow. */
1041 struct sw_flow_actions *old_acts;
1043 /* Bail out if we're not allowed to modify an existing flow.
1044 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1045 * because Generic Netlink treats the latter as a dump
1046 * request. We also accept NLM_F_EXCL in case that bug ever
1050 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1051 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1054 /* Update actions. */
1055 old_acts = rcu_dereference_protected(flow->sf_acts,
1056 lockdep_genl_is_held());
1057 if (a[OVS_FLOW_ATTR_ACTIONS] &&
1058 (old_acts->actions_len != nla_len(a[OVS_FLOW_ATTR_ACTIONS]) ||
1059 memcmp(old_acts->actions, nla_data(a[OVS_FLOW_ATTR_ACTIONS]),
1060 old_acts->actions_len))) {
1061 struct sw_flow_actions *new_acts;
1063 new_acts = flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1064 error = PTR_ERR(new_acts);
1065 if (IS_ERR(new_acts))
1068 rcu_assign_pointer(flow->sf_acts, new_acts);
1069 flow_deferred_free_acts(old_acts);
1072 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1073 info->snd_seq, OVS_FLOW_CMD_NEW);
1076 if (a[OVS_FLOW_ATTR_CLEAR]) {
1077 spin_lock_bh(&flow->lock);
1079 spin_unlock_bh(&flow->lock);
1084 genl_notify(reply, genl_info_net(info), info->snd_pid,
1085 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1087 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1088 dp_flow_multicast_group.id, PTR_ERR(reply));
1097 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1099 struct nlattr **a = info->attrs;
1100 struct ovs_header *ovs_header = info->userhdr;
1101 struct sw_flow_key key;
1102 struct sk_buff *reply;
1103 struct sw_flow *flow;
1104 struct datapath *dp;
1105 struct flow_table *table;
1109 if (!a[OVS_FLOW_ATTR_KEY])
1111 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1115 dp = get_dp(ovs_header->dp_ifindex);
1119 table = get_table_protected(dp);
1120 flow = flow_tbl_lookup(table, &key, key_len);
1124 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid, info->snd_seq, OVS_FLOW_CMD_NEW);
1126 return PTR_ERR(reply);
1128 return genlmsg_reply(reply, info);
1131 static int ovs_flow_cmd_del(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])
1144 return flush_flows(ovs_header->dp_ifindex);
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_alloc_info(flow);
1162 flow_tbl_remove(table, flow);
1164 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1165 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1168 flow_deferred_free(flow);
1170 genl_notify(reply, genl_info_net(info), info->snd_pid,
1171 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1175 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1177 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1178 struct datapath *dp;
1180 dp = get_dp(ovs_header->dp_ifindex);
1185 struct sw_flow *flow;
1188 bucket = cb->args[0];
1190 flow = flow_tbl_next(get_table_protected(dp), &bucket, &obj);
1194 if (ovs_flow_cmd_fill_info(flow, dp, skb, NETLINK_CB(cb->skb).pid,
1195 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1196 OVS_FLOW_CMD_NEW) < 0)
1199 cb->args[0] = bucket;
1205 static struct genl_ops dp_flow_genl_ops[] = {
1206 { .cmd = OVS_FLOW_CMD_NEW,
1207 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1208 .policy = flow_policy,
1209 .doit = ovs_flow_cmd_new_or_set
1211 { .cmd = OVS_FLOW_CMD_DEL,
1212 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1213 .policy = flow_policy,
1214 .doit = ovs_flow_cmd_del
1216 { .cmd = OVS_FLOW_CMD_GET,
1217 .flags = 0, /* OK for unprivileged users. */
1218 .policy = flow_policy,
1219 .doit = ovs_flow_cmd_get,
1220 .dumpit = ovs_flow_cmd_dump
1222 { .cmd = OVS_FLOW_CMD_SET,
1223 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1224 .policy = flow_policy,
1225 .doit = ovs_flow_cmd_new_or_set,
1229 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1230 #ifdef HAVE_NLA_NUL_STRING
1231 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1233 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1234 [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NLA_U32 },
1237 static struct genl_family dp_datapath_genl_family = {
1238 .id = GENL_ID_GENERATE,
1239 .hdrsize = sizeof(struct ovs_header),
1240 .name = OVS_DATAPATH_FAMILY,
1242 .maxattr = OVS_DP_ATTR_MAX
1245 static struct genl_multicast_group dp_datapath_multicast_group = {
1246 .name = OVS_DATAPATH_MCGROUP
1249 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1250 u32 pid, u32 seq, u32 flags, u8 cmd)
1252 struct ovs_header *ovs_header;
1256 ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1261 ovs_header->dp_ifindex = get_dpifindex(dp);
1264 err = nla_put_string(skb, OVS_DP_ATTR_NAME, dp_name(dp));
1267 goto nla_put_failure;
1269 nla = nla_reserve(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats));
1271 goto nla_put_failure;
1272 get_dp_stats(dp, nla_data(nla));
1274 NLA_PUT_U32(skb, OVS_DP_ATTR_IPV4_FRAGS,
1275 dp->drop_frags ? OVS_DP_FRAG_DROP : OVS_DP_FRAG_ZERO);
1277 return genlmsg_end(skb, ovs_header);
1280 genlmsg_cancel(skb, ovs_header);
1285 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1288 struct sk_buff *skb;
1291 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1293 return ERR_PTR(-ENOMEM);
1295 retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1298 return ERR_PTR(retval);
1303 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1305 if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1306 u32 frags = nla_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1308 if (frags != OVS_DP_FRAG_ZERO && frags != OVS_DP_FRAG_DROP)
1312 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1315 /* Called with genl_mutex and optionally with RTNL lock also. */
1316 static struct datapath *lookup_datapath(struct ovs_header *ovs_header, struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1318 struct datapath *dp;
1320 if (!a[OVS_DP_ATTR_NAME])
1321 dp = get_dp(ovs_header->dp_ifindex);
1323 struct vport *vport;
1326 vport = vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
1327 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1330 return dp ? dp : ERR_PTR(-ENODEV);
1333 /* Called with genl_mutex. */
1334 static void change_datapath(struct datapath *dp, struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1336 if (a[OVS_DP_ATTR_IPV4_FRAGS])
1337 dp->drop_frags = nla_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]) == OVS_DP_FRAG_DROP;
1340 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1342 struct nlattr **a = info->attrs;
1343 struct vport_parms parms;
1344 struct sk_buff *reply;
1345 struct datapath *dp;
1346 struct vport *vport;
1350 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1353 err = ovs_dp_cmd_validate(a);
1359 if (!try_module_get(THIS_MODULE))
1360 goto err_unlock_rtnl;
1363 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1365 goto err_put_module;
1366 INIT_LIST_HEAD(&dp->port_list);
1368 /* Initialize kobject for bridge. This will be added as
1369 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1370 dp->ifobj.kset = NULL;
1371 kobject_init(&dp->ifobj, &dp_ktype);
1373 /* Allocate table. */
1375 rcu_assign_pointer(dp->table, flow_tbl_alloc(TBL_MIN_BUCKETS));
1380 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1381 if (!dp->stats_percpu) {
1383 goto err_destroy_table;
1386 change_datapath(dp, a);
1388 /* Set up our datapath device. */
1389 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1390 parms.type = OVS_VPORT_TYPE_INTERNAL;
1391 parms.options = NULL;
1393 parms.port_no = OVSP_LOCAL;
1394 parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1396 vport = new_vport(&parms);
1397 if (IS_ERR(vport)) {
1398 err = PTR_ERR(vport);
1402 goto err_destroy_percpu;
1405 reply = ovs_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, OVS_DP_CMD_NEW);
1406 err = PTR_ERR(reply);
1408 goto err_destroy_local_port;
1410 list_add_tail(&dp->list_node, &dps);
1411 dp_sysfs_add_dp(dp);
1415 genl_notify(reply, genl_info_net(info), info->snd_pid,
1416 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1419 err_destroy_local_port:
1420 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1422 free_percpu(dp->stats_percpu);
1424 flow_tbl_destroy(get_table_protected(dp));
1428 module_put(THIS_MODULE);
1435 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1437 struct vport *vport, *next_vport;
1438 struct sk_buff *reply;
1439 struct datapath *dp;
1442 err = ovs_dp_cmd_validate(info->attrs);
1447 dp = lookup_datapath(info->userhdr, info->attrs);
1452 reply = ovs_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, OVS_DP_CMD_DEL);
1453 err = PTR_ERR(reply);
1457 list_for_each_entry_safe (vport, next_vport, &dp->port_list, node)
1458 if (vport->port_no != OVSP_LOCAL)
1459 dp_detach_port(vport);
1461 dp_sysfs_del_dp(dp);
1462 list_del(&dp->list_node);
1463 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1465 /* rtnl_unlock() will wait until all the references to devices that
1466 * are pending unregistration have been dropped. We do it here to
1467 * ensure that any internal devices (which contain DP pointers) are
1468 * fully destroyed before freeing the datapath.
1472 call_rcu(&dp->rcu, destroy_dp_rcu);
1473 module_put(THIS_MODULE);
1475 genl_notify(reply, genl_info_net(info), info->snd_pid,
1476 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1486 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1488 struct sk_buff *reply;
1489 struct datapath *dp;
1492 err = ovs_dp_cmd_validate(info->attrs);
1496 dp = lookup_datapath(info->userhdr, info->attrs);
1500 change_datapath(dp, info->attrs);
1502 reply = ovs_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, OVS_DP_CMD_NEW);
1503 if (IS_ERR(reply)) {
1504 err = PTR_ERR(reply);
1505 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1506 dp_datapath_multicast_group.id, err);
1510 genl_notify(reply, genl_info_net(info), info->snd_pid,
1511 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1515 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1517 struct sk_buff *reply;
1518 struct datapath *dp;
1521 err = ovs_dp_cmd_validate(info->attrs);
1525 dp = lookup_datapath(info->userhdr, info->attrs);
1529 reply = ovs_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, OVS_DP_CMD_NEW);
1531 return PTR_ERR(reply);
1533 return genlmsg_reply(reply, info);
1536 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1538 struct datapath *dp;
1539 int skip = cb->args[0];
1542 list_for_each_entry (dp, &dps, list_node) {
1545 if (ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1546 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1547 OVS_DP_CMD_NEW) < 0)
1557 static struct genl_ops dp_datapath_genl_ops[] = {
1558 { .cmd = OVS_DP_CMD_NEW,
1559 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1560 .policy = datapath_policy,
1561 .doit = ovs_dp_cmd_new
1563 { .cmd = OVS_DP_CMD_DEL,
1564 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1565 .policy = datapath_policy,
1566 .doit = ovs_dp_cmd_del
1568 { .cmd = OVS_DP_CMD_GET,
1569 .flags = 0, /* OK for unprivileged users. */
1570 .policy = datapath_policy,
1571 .doit = ovs_dp_cmd_get,
1572 .dumpit = ovs_dp_cmd_dump
1574 { .cmd = OVS_DP_CMD_SET,
1575 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1576 .policy = datapath_policy,
1577 .doit = ovs_dp_cmd_set,
1581 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1582 #ifdef HAVE_NLA_NUL_STRING
1583 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1584 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1585 [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1587 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1588 [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1590 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1591 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1592 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1593 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1596 static struct genl_family dp_vport_genl_family = {
1597 .id = GENL_ID_GENERATE,
1598 .hdrsize = sizeof(struct ovs_header),
1599 .name = OVS_VPORT_FAMILY,
1601 .maxattr = OVS_VPORT_ATTR_MAX
1604 struct genl_multicast_group dp_vport_multicast_group = {
1605 .name = OVS_VPORT_MCGROUP
1608 /* Called with RTNL lock or RCU read lock. */
1609 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1610 u32 pid, u32 seq, u32 flags, u8 cmd)
1612 struct ovs_header *ovs_header;
1616 ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1621 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1623 NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1624 NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport_get_type(vport));
1625 NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport_get_name(vport));
1626 NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1628 nla = nla_reserve(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats));
1630 goto nla_put_failure;
1632 vport_get_stats(vport, nla_data(nla));
1634 NLA_PUT(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN, vport_get_addr(vport));
1636 err = vport_get_options(vport, skb);
1637 if (err == -EMSGSIZE)
1640 return genlmsg_end(skb, ovs_header);
1645 genlmsg_cancel(skb, ovs_header);
1649 /* Called with RTNL lock or RCU read lock. */
1650 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1653 struct sk_buff *skb;
1656 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1658 return ERR_PTR(-ENOMEM);
1660 retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1663 return ERR_PTR(retval);
1668 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1670 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1673 /* Called with RTNL lock or RCU read lock. */
1674 static struct vport *lookup_vport(struct ovs_header *ovs_header,
1675 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1677 struct datapath *dp;
1678 struct vport *vport;
1680 if (a[OVS_VPORT_ATTR_NAME]) {
1681 vport = vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
1683 return ERR_PTR(-ENODEV);
1685 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1686 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1688 if (port_no >= DP_MAX_PORTS)
1689 return ERR_PTR(-EFBIG);
1691 dp = get_dp(ovs_header->dp_ifindex);
1693 return ERR_PTR(-ENODEV);
1695 vport = get_vport_protected(dp, port_no);
1697 return ERR_PTR(-ENOENT);
1700 return ERR_PTR(-EINVAL);
1703 /* Called with RTNL lock. */
1704 static int change_vport(struct vport *vport, struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1708 if (a[OVS_VPORT_ATTR_STATS])
1709 vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1711 if (a[OVS_VPORT_ATTR_ADDRESS])
1712 err = vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1717 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1719 struct nlattr **a = info->attrs;
1720 struct ovs_header *ovs_header = info->userhdr;
1721 struct vport_parms parms;
1722 struct sk_buff *reply;
1723 struct vport *vport;
1724 struct datapath *dp;
1729 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1730 !a[OVS_VPORT_ATTR_UPCALL_PID])
1733 err = ovs_vport_cmd_validate(a);
1738 dp = get_dp(ovs_header->dp_ifindex);
1743 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1744 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1747 if (port_no >= DP_MAX_PORTS)
1750 vport = get_vport_protected(dp, port_no);
1755 for (port_no = 1; ; port_no++) {
1756 if (port_no >= DP_MAX_PORTS) {
1760 vport = get_vport_protected(dp, port_no);
1766 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1767 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1768 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1770 parms.port_no = port_no;
1771 parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1773 vport = new_vport(&parms);
1774 err = PTR_ERR(vport);
1778 dp_sysfs_add_if(vport);
1780 err = change_vport(vport, a);
1782 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1783 info->snd_seq, OVS_VPORT_CMD_NEW);
1785 err = PTR_ERR(reply);
1788 dp_detach_port(vport);
1791 genl_notify(reply, genl_info_net(info), info->snd_pid,
1792 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1801 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1803 struct nlattr **a = info->attrs;
1804 struct sk_buff *reply;
1805 struct vport *vport;
1808 err = ovs_vport_cmd_validate(a);
1813 vport = lookup_vport(info->userhdr, a);
1814 err = PTR_ERR(vport);
1819 if (a[OVS_VPORT_ATTR_TYPE] && nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport_get_type(vport))
1821 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1822 err = vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1824 err = change_vport(vport, a);
1825 if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1826 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1828 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1830 if (IS_ERR(reply)) {
1831 err = PTR_ERR(reply);
1832 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1833 dp_vport_multicast_group.id, err);
1837 genl_notify(reply, genl_info_net(info), info->snd_pid,
1838 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1846 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1848 struct nlattr **a = info->attrs;
1849 struct sk_buff *reply;
1850 struct vport *vport;
1853 err = ovs_vport_cmd_validate(a);
1858 vport = lookup_vport(info->userhdr, a);
1859 err = PTR_ERR(vport);
1863 if (vport->port_no == OVSP_LOCAL) {
1868 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1870 err = PTR_ERR(reply);
1874 dp_detach_port(vport);
1876 genl_notify(reply, genl_info_net(info), info->snd_pid,
1877 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1885 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1887 struct nlattr **a = info->attrs;
1888 struct ovs_header *ovs_header = info->userhdr;
1889 struct sk_buff *reply;
1890 struct vport *vport;
1893 err = ovs_vport_cmd_validate(a);
1898 vport = lookup_vport(ovs_header, a);
1899 err = PTR_ERR(vport);
1903 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1905 err = PTR_ERR(reply);
1911 return genlmsg_reply(reply, info);
1919 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1921 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1922 struct datapath *dp;
1926 dp = get_dp(ovs_header->dp_ifindex);
1931 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1932 struct vport *vport;
1934 vport = get_vport_protected(dp, port_no);
1938 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1939 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1940 OVS_VPORT_CMD_NEW) < 0)
1945 cb->args[0] = port_no;
1951 static struct genl_ops dp_vport_genl_ops[] = {
1952 { .cmd = OVS_VPORT_CMD_NEW,
1953 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1954 .policy = vport_policy,
1955 .doit = ovs_vport_cmd_new
1957 { .cmd = OVS_VPORT_CMD_DEL,
1958 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1959 .policy = vport_policy,
1960 .doit = ovs_vport_cmd_del
1962 { .cmd = OVS_VPORT_CMD_GET,
1963 .flags = 0, /* OK for unprivileged users. */
1964 .policy = vport_policy,
1965 .doit = ovs_vport_cmd_get,
1966 .dumpit = ovs_vport_cmd_dump
1968 { .cmd = OVS_VPORT_CMD_SET,
1969 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1970 .policy = vport_policy,
1971 .doit = ovs_vport_cmd_set,
1975 struct genl_family_and_ops {
1976 struct genl_family *family;
1977 struct genl_ops *ops;
1979 struct genl_multicast_group *group;
1982 static const struct genl_family_and_ops dp_genl_families[] = {
1983 { &dp_datapath_genl_family,
1984 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1985 &dp_datapath_multicast_group },
1986 { &dp_vport_genl_family,
1987 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1988 &dp_vport_multicast_group },
1989 { &dp_flow_genl_family,
1990 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1991 &dp_flow_multicast_group },
1992 { &dp_packet_genl_family,
1993 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1997 static void dp_unregister_genl(int n_families)
2001 for (i = 0; i < n_families; i++)
2002 genl_unregister_family(dp_genl_families[i].family);
2005 static int dp_register_genl(void)
2012 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2013 const struct genl_family_and_ops *f = &dp_genl_families[i];
2015 err = genl_register_family_with_ops(f->family, f->ops,
2022 err = genl_register_mc_group(f->family, f->group);
2031 dp_unregister_genl(n_registered);
2035 static int __init dp_init(void)
2037 struct sk_buff *dummy_skb;
2040 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2042 printk("Open vSwitch %s, built "__DATE__" "__TIME__"\n", VERSION BUILDNR);
2050 goto error_tnl_exit;
2054 goto error_flow_exit;
2056 err = register_netdevice_notifier(&dp_device_notifier);
2058 goto error_vport_exit;
2060 err = dp_register_genl();
2062 goto error_unreg_notifier;
2066 error_unreg_notifier:
2067 unregister_netdevice_notifier(&dp_device_notifier);
2078 static void dp_cleanup(void)
2081 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2082 unregister_netdevice_notifier(&dp_device_notifier);
2088 module_init(dp_init);
2089 module_exit(dp_cleanup);
2091 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2092 MODULE_LICENSE("GPL");