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/rculist.h>
43 #include <linux/dmi.h>
44 #include <net/inet_ecn.h>
45 #include <net/genetlink.h>
47 #include "openvswitch/datapath-protocol.h"
54 #include "vport-internal_dev.h"
56 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
57 LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)
58 #error Kernels before 2.6.18 or after 3.0 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 tbl *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[ODPP_LOCAL]));
127 static inline size_t br_nlmsg_size(void)
129 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
130 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
131 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
132 + nla_total_size(4) /* IFLA_MASTER */
133 + nla_total_size(4) /* IFLA_MTU */
134 + nla_total_size(4) /* IFLA_LINK */
135 + nla_total_size(1); /* IFLA_OPERSTATE */
138 /* Caller must hold RTNL lock. */
139 static int dp_fill_ifinfo(struct sk_buff *skb,
140 const struct vport *port,
141 int event, unsigned int flags)
143 struct datapath *dp = port->dp;
144 int ifindex = vport_get_ifindex(port);
145 int iflink = vport_get_iflink(port);
146 struct ifinfomsg *hdr;
147 struct nlmsghdr *nlh;
155 nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
159 hdr = nlmsg_data(nlh);
160 hdr->ifi_family = AF_BRIDGE;
162 hdr->ifi_type = ARPHRD_ETHER;
163 hdr->ifi_index = ifindex;
164 hdr->ifi_flags = vport_get_flags(port);
167 NLA_PUT_STRING(skb, IFLA_IFNAME, vport_get_name(port));
168 NLA_PUT_U32(skb, IFLA_MASTER,
169 vport_get_ifindex(get_vport_protected(dp, ODPP_LOCAL)));
170 NLA_PUT_U32(skb, IFLA_MTU, vport_get_mtu(port));
171 #ifdef IFLA_OPERSTATE
172 NLA_PUT_U8(skb, IFLA_OPERSTATE,
173 vport_is_running(port)
174 ? vport_get_operstate(port)
178 NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, vport_get_addr(port));
180 if (ifindex != iflink)
181 NLA_PUT_U32(skb, IFLA_LINK,iflink);
183 return nlmsg_end(skb, nlh);
186 nlmsg_cancel(skb, nlh);
190 /* Caller must hold RTNL lock. */
191 static void dp_ifinfo_notify(int event, struct vport *port)
196 skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
200 err = dp_fill_ifinfo(skb, port, event, 0);
202 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
203 WARN_ON(err == -EMSGSIZE);
207 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
211 rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
214 static void release_dp(struct kobject *kobj)
216 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
220 static struct kobj_type dp_ktype = {
221 .release = release_dp
224 static void destroy_dp_rcu(struct rcu_head *rcu)
226 struct datapath *dp = container_of(rcu, struct datapath, rcu);
228 tbl_destroy((struct tbl __force *)dp->table, flow_free_tbl);
229 free_percpu(dp->stats_percpu);
230 kobject_put(&dp->ifobj);
233 /* Called with RTNL lock and genl_lock. */
234 static struct vport *new_vport(const struct vport_parms *parms)
238 vport = vport_add(parms);
239 if (!IS_ERR(vport)) {
240 struct datapath *dp = parms->dp;
242 rcu_assign_pointer(dp->ports[parms->port_no], vport);
243 list_add(&vport->node, &dp->port_list);
245 dp_ifinfo_notify(RTM_NEWLINK, vport);
251 /* Called with RTNL lock. */
252 int dp_detach_port(struct vport *p)
256 if (p->port_no != ODPP_LOCAL)
258 dp_ifinfo_notify(RTM_DELLINK, p);
260 /* First drop references to device. */
262 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
264 /* Then destroy it. */
268 /* Must be called with rcu_read_lock. */
269 void dp_process_received_packet(struct vport *p, struct sk_buff *skb)
271 struct datapath *dp = p->dp;
272 struct dp_stats_percpu *stats;
273 int stats_counter_off;
276 OVS_CB(skb)->vport = p;
278 if (!OVS_CB(skb)->flow) {
279 struct sw_flow_key key;
280 struct tbl_node *flow_node;
284 /* Extract flow from 'skb' into 'key'. */
285 error = flow_extract(skb, p->port_no, &key, &key_len, &is_frag);
286 if (unlikely(error)) {
291 if (is_frag && dp->drop_frags) {
293 stats_counter_off = offsetof(struct dp_stats_percpu, n_frags);
298 flow_node = tbl_lookup(rcu_dereference(dp->table), &key, key_len,
299 flow_hash(&key, key_len), flow_cmp);
300 if (unlikely(!flow_node)) {
301 struct dp_upcall_info upcall;
303 upcall.cmd = ODP_PACKET_CMD_MISS;
306 upcall.sample_pool = 0;
307 upcall.actions = NULL;
308 upcall.actions_len = 0;
309 dp_upcall(dp, skb, &upcall);
310 stats_counter_off = offsetof(struct dp_stats_percpu, n_missed);
314 OVS_CB(skb)->flow = flow_cast(flow_node);
317 stats_counter_off = offsetof(struct dp_stats_percpu, n_hit);
318 flow_used(OVS_CB(skb)->flow, skb);
319 execute_actions(dp, skb);
322 /* Update datapath statistics. */
324 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
326 write_seqcount_begin(&stats->seqlock);
327 (*(u64 *)((u8 *)stats + stats_counter_off))++;
328 write_seqcount_end(&stats->seqlock);
333 static void copy_and_csum_skb(struct sk_buff *skb, void *to)
335 u16 csum_start, csum_offset;
338 get_skb_csum_pointers(skb, &csum_start, &csum_offset);
339 csum_start -= skb_headroom(skb);
341 skb_copy_bits(skb, 0, to, csum_start);
343 csum = skb_copy_and_csum_bits(skb, csum_start, to + csum_start,
344 skb->len - csum_start, 0);
345 *(__sum16 *)(to + csum_start + csum_offset) = csum_fold(csum);
348 static struct genl_family dp_packet_genl_family = {
349 .id = GENL_ID_GENERATE,
350 .hdrsize = sizeof(struct odp_header),
351 .name = ODP_PACKET_FAMILY,
353 .maxattr = ODP_PACKET_ATTR_MAX
356 /* Generic Netlink multicast groups for upcalls.
358 * We really want three unique multicast groups per datapath, but we can't even
359 * get one, because genl_register_mc_group() takes genl_lock, which is also
360 * held during Generic Netlink message processing, so trying to acquire
361 * multicast groups during ODP_DP_NEW processing deadlocks. Instead, we
362 * preallocate a few groups and use them round-robin for datapaths. Collision
363 * isn't fatal--multicast listeners should check that the family is the one
364 * that they want and discard others--but it wastes time and memory to receive
367 #define PACKET_N_MC_GROUPS 16
368 static struct genl_multicast_group packet_mc_groups[PACKET_N_MC_GROUPS];
370 static u32 packet_mc_group(struct datapath *dp, u8 cmd)
373 BUILD_BUG_ON_NOT_POWER_OF_2(PACKET_N_MC_GROUPS);
375 idx = jhash_2words(dp->dp_ifindex, cmd, 0) & (PACKET_N_MC_GROUPS - 1);
376 return packet_mc_groups[idx].id;
379 static int packet_register_mc_groups(void)
383 for (i = 0; i < PACKET_N_MC_GROUPS; i++) {
384 struct genl_multicast_group *group = &packet_mc_groups[i];
387 sprintf(group->name, "packet%d", i);
388 error = genl_register_mc_group(&dp_packet_genl_family, group);
395 int dp_upcall(struct datapath *dp, struct sk_buff *skb, const struct dp_upcall_info *upcall_info)
397 struct dp_stats_percpu *stats;
400 WARN_ON_ONCE(skb_shared(skb));
402 forward_ip_summed(skb, true);
404 /* Break apart GSO packets into their component pieces. Otherwise
405 * userspace may try to stuff a 64kB packet into a 1500-byte MTU. */
406 if (skb_is_gso(skb)) {
407 struct sk_buff *nskb = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
418 err = queue_userspace_packets(dp, skb, upcall_info);
426 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
428 write_seqcount_begin(&stats->seqlock);
430 write_seqcount_end(&stats->seqlock);
437 /* Send each packet in the 'skb' list to userspace for 'dp' as directed by
438 * 'upcall_info'. There will be only one packet unless we broke up a GSO
441 static int queue_userspace_packets(struct datapath *dp, struct sk_buff *skb,
442 const struct dp_upcall_info *upcall_info)
444 u32 group = packet_mc_group(dp, upcall_info->cmd);
445 struct sk_buff *nskb;
449 struct odp_header *upcall;
450 struct sk_buff *user_skb; /* to be queued to userspace */
457 err = vlan_deaccel_tag(skb);
461 if (nla_attr_size(skb->len) > USHRT_MAX)
464 len = sizeof(struct odp_header);
465 len += nla_total_size(skb->len);
466 len += nla_total_size(FLOW_BUFSIZE);
467 if (upcall_info->userdata)
468 len += nla_total_size(8);
469 if (upcall_info->sample_pool)
470 len += nla_total_size(4);
471 if (upcall_info->actions_len)
472 len += nla_total_size(upcall_info->actions_len);
474 user_skb = genlmsg_new(len, GFP_ATOMIC);
476 netlink_set_err(INIT_NET_GENL_SOCK, 0, group, -ENOBUFS);
480 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, 0, upcall_info->cmd);
481 upcall->dp_ifindex = dp->dp_ifindex;
483 nla = nla_nest_start(user_skb, ODP_PACKET_ATTR_KEY);
484 flow_to_nlattrs(upcall_info->key, user_skb);
485 nla_nest_end(user_skb, nla);
487 if (upcall_info->userdata)
488 nla_put_u64(user_skb, ODP_PACKET_ATTR_USERDATA, upcall_info->userdata);
489 if (upcall_info->sample_pool)
490 nla_put_u32(user_skb, ODP_PACKET_ATTR_SAMPLE_POOL, upcall_info->sample_pool);
491 if (upcall_info->actions_len) {
492 const struct nlattr *actions = upcall_info->actions;
493 u32 actions_len = upcall_info->actions_len;
495 nla = nla_nest_start(user_skb, ODP_PACKET_ATTR_ACTIONS);
496 memcpy(__skb_put(user_skb, actions_len), actions, actions_len);
497 nla_nest_end(user_skb, nla);
500 nla = __nla_reserve(user_skb, ODP_PACKET_ATTR_PACKET, skb->len);
501 if (skb->ip_summed == CHECKSUM_PARTIAL)
502 copy_and_csum_skb(skb, nla_data(nla));
504 skb_copy_bits(skb, 0, nla_data(nla), skb->len);
506 err = genlmsg_multicast(user_skb, 0, group, GFP_ATOMIC);
517 while ((skb = nskb) != NULL) {
524 /* Called with genl_mutex. */
525 static int flush_flows(int dp_ifindex)
527 struct tbl *old_table;
528 struct tbl *new_table;
531 dp = get_dp(dp_ifindex);
535 old_table = get_table_protected(dp);
536 new_table = tbl_create(TBL_MIN_BUCKETS);
540 rcu_assign_pointer(dp->table, new_table);
542 tbl_deferred_destroy(old_table, flow_free_tbl);
547 static int validate_actions(const struct nlattr *attr)
549 const struct nlattr *a;
552 nla_for_each_nested(a, attr, rem) {
553 static const u32 action_lens[ODP_ACTION_ATTR_MAX + 1] = {
554 [ODP_ACTION_ATTR_OUTPUT] = 4,
555 [ODP_ACTION_ATTR_USERSPACE] = 8,
556 [ODP_ACTION_ATTR_SET_DL_TCI] = 2,
557 [ODP_ACTION_ATTR_STRIP_VLAN] = 0,
558 [ODP_ACTION_ATTR_SET_DL_SRC] = ETH_ALEN,
559 [ODP_ACTION_ATTR_SET_DL_DST] = ETH_ALEN,
560 [ODP_ACTION_ATTR_SET_NW_SRC] = 4,
561 [ODP_ACTION_ATTR_SET_NW_DST] = 4,
562 [ODP_ACTION_ATTR_SET_NW_TOS] = 1,
563 [ODP_ACTION_ATTR_SET_TP_SRC] = 2,
564 [ODP_ACTION_ATTR_SET_TP_DST] = 2,
565 [ODP_ACTION_ATTR_SET_TUNNEL] = 8,
566 [ODP_ACTION_ATTR_SET_PRIORITY] = 4,
567 [ODP_ACTION_ATTR_POP_PRIORITY] = 0,
569 int type = nla_type(a);
571 if (type > ODP_ACTION_ATTR_MAX || nla_len(a) != action_lens[type])
575 case ODP_ACTION_ATTR_UNSPEC:
578 case ODP_ACTION_ATTR_USERSPACE:
579 case ODP_ACTION_ATTR_STRIP_VLAN:
580 case ODP_ACTION_ATTR_SET_DL_SRC:
581 case ODP_ACTION_ATTR_SET_DL_DST:
582 case ODP_ACTION_ATTR_SET_NW_SRC:
583 case ODP_ACTION_ATTR_SET_NW_DST:
584 case ODP_ACTION_ATTR_SET_TP_SRC:
585 case ODP_ACTION_ATTR_SET_TP_DST:
586 case ODP_ACTION_ATTR_SET_TUNNEL:
587 case ODP_ACTION_ATTR_SET_PRIORITY:
588 case ODP_ACTION_ATTR_POP_PRIORITY:
589 /* No validation needed. */
592 case ODP_ACTION_ATTR_OUTPUT:
593 if (nla_get_u32(a) >= DP_MAX_PORTS)
597 case ODP_ACTION_ATTR_SET_DL_TCI:
598 if (nla_get_be16(a) & htons(VLAN_CFI_MASK))
602 case ODP_ACTION_ATTR_SET_NW_TOS:
603 if (nla_get_u8(a) & INET_ECN_MASK)
617 static void clear_stats(struct sw_flow *flow)
621 flow->packet_count = 0;
622 flow->byte_count = 0;
625 /* Called with genl_mutex. */
626 static int expand_table(struct datapath *dp)
628 struct tbl *old_table = get_table_protected(dp);
629 struct tbl *new_table;
631 new_table = tbl_expand(old_table);
632 if (IS_ERR(new_table)) {
633 if (PTR_ERR(new_table) != -ENOSPC)
634 return PTR_ERR(new_table);
636 rcu_assign_pointer(dp->table, new_table);
637 tbl_deferred_destroy(old_table, NULL);
643 static int odp_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
645 struct odp_header *odp_header = info->userhdr;
646 struct nlattr **a = info->attrs;
647 struct sw_flow_actions *acts;
648 struct sk_buff *packet;
649 struct sw_flow *flow;
658 if (!a[ODP_PACKET_ATTR_PACKET] || !a[ODP_PACKET_ATTR_KEY] ||
659 !a[ODP_PACKET_ATTR_ACTIONS] ||
660 nla_len(a[ODP_PACKET_ATTR_PACKET]) < ETH_HLEN)
663 err = validate_actions(a[ODP_PACKET_ATTR_ACTIONS]);
667 len = nla_len(a[ODP_PACKET_ATTR_PACKET]);
668 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
672 skb_reserve(packet, NET_IP_ALIGN);
674 memcpy(__skb_put(packet, len), nla_data(a[ODP_PACKET_ATTR_PACKET]), len);
676 skb_reset_mac_header(packet);
677 eth = eth_hdr(packet);
679 /* Normally, setting the skb 'protocol' field would be handled by a
680 * call to eth_type_trans(), but it assumes there's a sending
681 * device, which we may not have. */
682 if (ntohs(eth->h_proto) >= 1536)
683 packet->protocol = eth->h_proto;
685 packet->protocol = htons(ETH_P_802_2);
687 /* Build an sw_flow for sending this packet. */
693 err = flow_extract(packet, -1, &flow->key, &key_len, &is_frag);
696 flow->tbl_node.hash = flow_hash(&flow->key, key_len);
698 err = flow_metadata_from_nlattrs(&flow->key.eth.in_port,
699 &flow->key.eth.tun_id,
700 a[ODP_PACKET_ATTR_KEY]);
704 acts = flow_actions_alloc(a[ODP_PACKET_ATTR_ACTIONS]);
708 rcu_assign_pointer(flow->sf_acts, acts);
710 OVS_CB(packet)->flow = flow;
713 dp = get_dp(odp_header->dp_ifindex);
717 err = execute_actions(dp, packet);
733 static const struct nla_policy packet_policy[ODP_PACKET_ATTR_MAX + 1] = {
734 [ODP_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
735 [ODP_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
736 [ODP_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
739 static struct genl_ops dp_packet_genl_ops[] = {
740 { .cmd = ODP_PACKET_CMD_EXECUTE,
741 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
742 .policy = packet_policy,
743 .doit = odp_packet_cmd_execute
747 static void get_dp_stats(struct datapath *dp, struct odp_stats *stats)
750 struct tbl *table = get_table_protected(dp);
752 stats->n_flows = tbl_count(table);
754 stats->n_frags = stats->n_hit = stats->n_missed = stats->n_lost = 0;
755 for_each_possible_cpu(i) {
756 const struct dp_stats_percpu *percpu_stats;
757 struct dp_stats_percpu local_stats;
760 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
763 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
764 local_stats = *percpu_stats;
765 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
767 stats->n_frags += local_stats.n_frags;
768 stats->n_hit += local_stats.n_hit;
769 stats->n_missed += local_stats.n_missed;
770 stats->n_lost += local_stats.n_lost;
774 /* MTU of the dp pseudo-device: ETH_DATA_LEN or the minimum of the ports.
775 * Called with RTNL lock.
777 int dp_min_mtu(const struct datapath *dp)
784 list_for_each_entry (p, &dp->port_list, node) {
787 /* Skip any internal ports, since that's what we're trying to
789 if (is_internal_vport(p))
792 dev_mtu = vport_get_mtu(p);
795 if (!mtu || dev_mtu < mtu)
799 return mtu ? mtu : ETH_DATA_LEN;
802 /* Sets the MTU of all datapath devices to the minimum of the ports
803 * Called with RTNL lock.
805 void set_internal_devs_mtu(const struct datapath *dp)
812 mtu = dp_min_mtu(dp);
814 list_for_each_entry (p, &dp->port_list, node) {
815 if (is_internal_vport(p))
816 vport_set_mtu(p, mtu);
820 static const struct nla_policy flow_policy[ODP_FLOW_ATTR_MAX + 1] = {
821 [ODP_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
822 [ODP_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
823 [ODP_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
826 static struct genl_family dp_flow_genl_family = {
827 .id = GENL_ID_GENERATE,
828 .hdrsize = sizeof(struct odp_header),
829 .name = ODP_FLOW_FAMILY,
831 .maxattr = ODP_FLOW_ATTR_MAX
834 static struct genl_multicast_group dp_flow_multicast_group = {
835 .name = ODP_FLOW_MCGROUP
838 /* Called with genl_lock. */
839 static int odp_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
840 struct sk_buff *skb, u32 pid, u32 seq, u32 flags, u8 cmd)
842 const int skb_orig_len = skb->len;
843 const struct sw_flow_actions *sf_acts;
844 struct odp_flow_stats stats;
845 struct odp_header *odp_header;
851 sf_acts = rcu_dereference_protected(flow->sf_acts,
852 lockdep_genl_is_held());
854 odp_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
858 odp_header->dp_ifindex = dp->dp_ifindex;
860 nla = nla_nest_start(skb, ODP_FLOW_ATTR_KEY);
862 goto nla_put_failure;
863 err = flow_to_nlattrs(&flow->key, skb);
866 nla_nest_end(skb, nla);
868 spin_lock_bh(&flow->lock);
870 stats.n_packets = flow->packet_count;
871 stats.n_bytes = flow->byte_count;
872 tcp_flags = flow->tcp_flags;
873 spin_unlock_bh(&flow->lock);
876 NLA_PUT_U64(skb, ODP_FLOW_ATTR_USED, flow_used_time(used));
879 NLA_PUT(skb, ODP_FLOW_ATTR_STATS, sizeof(struct odp_flow_stats), &stats);
882 NLA_PUT_U8(skb, ODP_FLOW_ATTR_TCP_FLAGS, tcp_flags);
884 /* If ODP_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
885 * this is the first flow to be dumped into 'skb'. This is unusual for
886 * Netlink but individual action lists can be longer than
887 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
888 * The userspace caller can always fetch the actions separately if it
889 * really wants them. (Most userspace callers in fact don't care.)
891 * This can only fail for dump operations because the skb is always
892 * properly sized for single flows.
894 err = nla_put(skb, ODP_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
896 if (err < 0 && skb_orig_len)
899 return genlmsg_end(skb, odp_header);
904 genlmsg_cancel(skb, odp_header);
908 static struct sk_buff *odp_flow_cmd_alloc_info(struct sw_flow *flow)
910 const struct sw_flow_actions *sf_acts;
913 sf_acts = rcu_dereference_protected(flow->sf_acts,
914 lockdep_genl_is_held());
916 len = nla_total_size(FLOW_BUFSIZE); /* ODP_FLOW_ATTR_KEY */
917 len += nla_total_size(sf_acts->actions_len); /* ODP_FLOW_ATTR_ACTIONS */
918 len += nla_total_size(sizeof(struct odp_flow_stats)); /* ODP_FLOW_ATTR_STATS */
919 len += nla_total_size(1); /* ODP_FLOW_ATTR_TCP_FLAGS */
920 len += nla_total_size(8); /* ODP_FLOW_ATTR_USED */
921 return genlmsg_new(NLMSG_ALIGN(sizeof(struct odp_header)) + len, GFP_KERNEL);
924 static struct sk_buff *odp_flow_cmd_build_info(struct sw_flow *flow, struct datapath *dp,
925 u32 pid, u32 seq, u8 cmd)
930 skb = odp_flow_cmd_alloc_info(flow);
932 return ERR_PTR(-ENOMEM);
934 retval = odp_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
939 static int odp_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
941 struct nlattr **a = info->attrs;
942 struct odp_header *odp_header = info->userhdr;
943 struct tbl_node *flow_node;
944 struct sw_flow_key key;
945 struct sw_flow *flow;
946 struct sk_buff *reply;
955 if (!a[ODP_FLOW_ATTR_KEY])
957 error = flow_from_nlattrs(&key, &key_len, a[ODP_FLOW_ATTR_KEY]);
961 /* Validate actions. */
962 if (a[ODP_FLOW_ATTR_ACTIONS]) {
963 error = validate_actions(a[ODP_FLOW_ATTR_ACTIONS]);
966 } else if (info->genlhdr->cmd == ODP_FLOW_CMD_NEW) {
971 dp = get_dp(odp_header->dp_ifindex);
976 hash = flow_hash(&key, key_len);
977 table = get_table_protected(dp);
978 flow_node = tbl_lookup(table, &key, key_len, hash, flow_cmp);
980 struct sw_flow_actions *acts;
982 /* Bail out if we're not allowed to create a new flow. */
984 if (info->genlhdr->cmd == ODP_FLOW_CMD_SET)
987 /* Expand table, if necessary, to make room. */
988 if (tbl_count(table) >= tbl_n_buckets(table)) {
989 error = expand_table(dp);
992 table = get_table_protected(dp);
998 error = PTR_ERR(flow);
1004 /* Obtain actions. */
1005 acts = flow_actions_alloc(a[ODP_FLOW_ATTR_ACTIONS]);
1006 error = PTR_ERR(acts);
1008 goto error_free_flow;
1009 rcu_assign_pointer(flow->sf_acts, acts);
1011 /* Put flow in bucket. */
1012 error = tbl_insert(table, &flow->tbl_node, hash);
1014 goto error_free_flow;
1016 reply = odp_flow_cmd_build_info(flow, dp, info->snd_pid,
1017 info->snd_seq, ODP_FLOW_CMD_NEW);
1019 /* We found a matching flow. */
1020 struct sw_flow_actions *old_acts;
1022 /* Bail out if we're not allowed to modify an existing flow.
1023 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1024 * because Generic Netlink treats the latter as a dump
1025 * request. We also accept NLM_F_EXCL in case that bug ever
1029 if (info->genlhdr->cmd == ODP_FLOW_CMD_NEW &&
1030 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1033 /* Update actions. */
1034 flow = flow_cast(flow_node);
1035 old_acts = rcu_dereference_protected(flow->sf_acts,
1036 lockdep_genl_is_held());
1037 if (a[ODP_FLOW_ATTR_ACTIONS] &&
1038 (old_acts->actions_len != nla_len(a[ODP_FLOW_ATTR_ACTIONS]) ||
1039 memcmp(old_acts->actions, nla_data(a[ODP_FLOW_ATTR_ACTIONS]),
1040 old_acts->actions_len))) {
1041 struct sw_flow_actions *new_acts;
1043 new_acts = flow_actions_alloc(a[ODP_FLOW_ATTR_ACTIONS]);
1044 error = PTR_ERR(new_acts);
1045 if (IS_ERR(new_acts))
1048 rcu_assign_pointer(flow->sf_acts, new_acts);
1049 flow_deferred_free_acts(old_acts);
1052 reply = odp_flow_cmd_build_info(flow, dp, info->snd_pid,
1053 info->snd_seq, ODP_FLOW_CMD_NEW);
1056 if (a[ODP_FLOW_ATTR_CLEAR]) {
1057 spin_lock_bh(&flow->lock);
1059 spin_unlock_bh(&flow->lock);
1064 genl_notify(reply, genl_info_net(info), info->snd_pid,
1065 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1067 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1068 dp_flow_multicast_group.id, PTR_ERR(reply));
1077 static int odp_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1079 struct nlattr **a = info->attrs;
1080 struct odp_header *odp_header = info->userhdr;
1081 struct sw_flow_key key;
1082 struct tbl_node *flow_node;
1083 struct sk_buff *reply;
1084 struct sw_flow *flow;
1085 struct datapath *dp;
1090 if (!a[ODP_FLOW_ATTR_KEY])
1092 err = flow_from_nlattrs(&key, &key_len, a[ODP_FLOW_ATTR_KEY]);
1096 dp = get_dp(odp_header->dp_ifindex);
1100 table = get_table_protected(dp);
1101 flow_node = tbl_lookup(table, &key, key_len, flow_hash(&key, key_len),
1106 flow = flow_cast(flow_node);
1107 reply = odp_flow_cmd_build_info(flow, dp, info->snd_pid, info->snd_seq, ODP_FLOW_CMD_NEW);
1109 return PTR_ERR(reply);
1111 return genlmsg_reply(reply, info);
1114 static int odp_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1116 struct nlattr **a = info->attrs;
1117 struct odp_header *odp_header = info->userhdr;
1118 struct sw_flow_key key;
1119 struct tbl_node *flow_node;
1120 struct sk_buff *reply;
1121 struct sw_flow *flow;
1122 struct datapath *dp;
1127 if (!a[ODP_FLOW_ATTR_KEY])
1128 return flush_flows(odp_header->dp_ifindex);
1129 err = flow_from_nlattrs(&key, &key_len, a[ODP_FLOW_ATTR_KEY]);
1133 dp = get_dp(odp_header->dp_ifindex);
1137 table = get_table_protected(dp);
1138 flow_node = tbl_lookup(table, &key, key_len, flow_hash(&key, key_len),
1142 flow = flow_cast(flow_node);
1144 reply = odp_flow_cmd_alloc_info(flow);
1148 err = tbl_remove(table, flow_node);
1154 err = odp_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1155 info->snd_seq, 0, ODP_FLOW_CMD_DEL);
1158 flow_deferred_free(flow);
1160 genl_notify(reply, genl_info_net(info), info->snd_pid,
1161 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1165 static int odp_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1167 struct odp_header *odp_header = genlmsg_data(nlmsg_data(cb->nlh));
1168 struct datapath *dp;
1170 dp = get_dp(odp_header->dp_ifindex);
1175 struct tbl_node *flow_node;
1176 struct sw_flow *flow;
1179 bucket = cb->args[0];
1181 flow_node = tbl_next(get_table_protected(dp), &bucket, &obj);
1185 flow = flow_cast(flow_node);
1186 if (odp_flow_cmd_fill_info(flow, dp, skb, NETLINK_CB(cb->skb).pid,
1187 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1188 ODP_FLOW_CMD_NEW) < 0)
1191 cb->args[0] = bucket;
1197 static struct genl_ops dp_flow_genl_ops[] = {
1198 { .cmd = ODP_FLOW_CMD_NEW,
1199 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1200 .policy = flow_policy,
1201 .doit = odp_flow_cmd_new_or_set
1203 { .cmd = ODP_FLOW_CMD_DEL,
1204 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1205 .policy = flow_policy,
1206 .doit = odp_flow_cmd_del
1208 { .cmd = ODP_FLOW_CMD_GET,
1209 .flags = 0, /* OK for unprivileged users. */
1210 .policy = flow_policy,
1211 .doit = odp_flow_cmd_get,
1212 .dumpit = odp_flow_cmd_dump
1214 { .cmd = ODP_FLOW_CMD_SET,
1215 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1216 .policy = flow_policy,
1217 .doit = odp_flow_cmd_new_or_set,
1221 static const struct nla_policy datapath_policy[ODP_DP_ATTR_MAX + 1] = {
1222 #ifdef HAVE_NLA_NUL_STRING
1223 [ODP_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1225 [ODP_DP_ATTR_IPV4_FRAGS] = { .type = NLA_U32 },
1226 [ODP_DP_ATTR_SAMPLING] = { .type = NLA_U32 },
1229 static struct genl_family dp_datapath_genl_family = {
1230 .id = GENL_ID_GENERATE,
1231 .hdrsize = sizeof(struct odp_header),
1232 .name = ODP_DATAPATH_FAMILY,
1234 .maxattr = ODP_DP_ATTR_MAX
1237 static struct genl_multicast_group dp_datapath_multicast_group = {
1238 .name = ODP_DATAPATH_MCGROUP
1241 static int odp_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1242 u32 pid, u32 seq, u32 flags, u8 cmd)
1244 struct odp_header *odp_header;
1248 odp_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1253 odp_header->dp_ifindex = dp->dp_ifindex;
1256 err = nla_put_string(skb, ODP_DP_ATTR_NAME, dp_name(dp));
1259 goto nla_put_failure;
1261 nla = nla_reserve(skb, ODP_DP_ATTR_STATS, sizeof(struct odp_stats));
1263 goto nla_put_failure;
1264 get_dp_stats(dp, nla_data(nla));
1266 NLA_PUT_U32(skb, ODP_DP_ATTR_IPV4_FRAGS,
1267 dp->drop_frags ? ODP_DP_FRAG_DROP : ODP_DP_FRAG_ZERO);
1269 if (dp->sflow_probability)
1270 NLA_PUT_U32(skb, ODP_DP_ATTR_SAMPLING, dp->sflow_probability);
1272 nla = nla_nest_start(skb, ODP_DP_ATTR_MCGROUPS);
1274 goto nla_put_failure;
1275 NLA_PUT_U32(skb, ODP_PACKET_CMD_MISS, packet_mc_group(dp, ODP_PACKET_CMD_MISS));
1276 NLA_PUT_U32(skb, ODP_PACKET_CMD_ACTION, packet_mc_group(dp, ODP_PACKET_CMD_ACTION));
1277 NLA_PUT_U32(skb, ODP_PACKET_CMD_SAMPLE, packet_mc_group(dp, ODP_PACKET_CMD_SAMPLE));
1278 nla_nest_end(skb, nla);
1280 return genlmsg_end(skb, odp_header);
1283 genlmsg_cancel(skb, odp_header);
1288 static struct sk_buff *odp_dp_cmd_build_info(struct datapath *dp, u32 pid,
1291 struct sk_buff *skb;
1294 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1296 return ERR_PTR(-ENOMEM);
1298 retval = odp_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1301 return ERR_PTR(retval);
1306 static int odp_dp_cmd_validate(struct nlattr *a[ODP_DP_ATTR_MAX + 1])
1308 if (a[ODP_DP_ATTR_IPV4_FRAGS]) {
1309 u32 frags = nla_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]);
1311 if (frags != ODP_DP_FRAG_ZERO && frags != ODP_DP_FRAG_DROP)
1315 return CHECK_NUL_STRING(a[ODP_DP_ATTR_NAME], IFNAMSIZ - 1);
1318 /* Called with genl_mutex and optionally with RTNL lock also. */
1319 static struct datapath *lookup_datapath(struct odp_header *odp_header, struct nlattr *a[ODP_DP_ATTR_MAX + 1])
1321 struct datapath *dp;
1323 if (!a[ODP_DP_ATTR_NAME])
1324 dp = get_dp(odp_header->dp_ifindex);
1326 struct vport *vport;
1329 vport = vport_locate(nla_data(a[ODP_DP_ATTR_NAME]));
1330 dp = vport && vport->port_no == ODPP_LOCAL ? vport->dp : NULL;
1333 return dp ? dp : ERR_PTR(-ENODEV);
1336 /* Called with genl_mutex. */
1337 static void change_datapath(struct datapath *dp, struct nlattr *a[ODP_DP_ATTR_MAX + 1])
1339 if (a[ODP_DP_ATTR_IPV4_FRAGS])
1340 dp->drop_frags = nla_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]) == ODP_DP_FRAG_DROP;
1341 if (a[ODP_DP_ATTR_SAMPLING])
1342 dp->sflow_probability = nla_get_u32(a[ODP_DP_ATTR_SAMPLING]);
1345 static int odp_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1347 struct nlattr **a = info->attrs;
1348 struct vport_parms parms;
1349 struct sk_buff *reply;
1350 struct datapath *dp;
1351 struct vport *vport;
1355 if (!a[ODP_DP_ATTR_NAME])
1358 err = odp_dp_cmd_validate(a);
1364 if (!try_module_get(THIS_MODULE))
1365 goto err_unlock_rtnl;
1368 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1370 goto err_put_module;
1371 INIT_LIST_HEAD(&dp->port_list);
1373 /* Initialize kobject for bridge. This will be added as
1374 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1375 dp->ifobj.kset = NULL;
1376 kobject_init(&dp->ifobj, &dp_ktype);
1378 /* Allocate table. */
1380 rcu_assign_pointer(dp->table, tbl_create(TBL_MIN_BUCKETS));
1384 /* Set up our datapath device. */
1385 parms.name = nla_data(a[ODP_DP_ATTR_NAME]);
1386 parms.type = ODP_VPORT_TYPE_INTERNAL;
1387 parms.options = NULL;
1389 parms.port_no = ODPP_LOCAL;
1390 vport = new_vport(&parms);
1391 if (IS_ERR(vport)) {
1392 err = PTR_ERR(vport);
1396 goto err_destroy_table;
1398 dp->dp_ifindex = vport_get_ifindex(vport);
1401 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1402 if (!dp->stats_percpu) {
1404 goto err_destroy_local_port;
1407 change_datapath(dp, a);
1409 reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
1410 err = PTR_ERR(reply);
1412 goto err_destroy_local_port;
1414 list_add_tail(&dp->list_node, &dps);
1415 dp_sysfs_add_dp(dp);
1419 genl_notify(reply, genl_info_net(info), info->snd_pid,
1420 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1423 err_destroy_local_port:
1424 dp_detach_port(get_vport_protected(dp, ODPP_LOCAL));
1426 tbl_destroy(get_table_protected(dp), NULL);
1430 module_put(THIS_MODULE);
1437 static int odp_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1439 struct vport *vport, *next_vport;
1440 struct sk_buff *reply;
1441 struct datapath *dp;
1444 err = odp_dp_cmd_validate(info->attrs);
1449 dp = lookup_datapath(info->userhdr, info->attrs);
1454 reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_DEL);
1455 err = PTR_ERR(reply);
1459 list_for_each_entry_safe (vport, next_vport, &dp->port_list, node)
1460 if (vport->port_no != ODPP_LOCAL)
1461 dp_detach_port(vport);
1463 dp_sysfs_del_dp(dp);
1464 list_del(&dp->list_node);
1465 dp_detach_port(get_vport_protected(dp, ODPP_LOCAL));
1467 /* rtnl_unlock() will wait until all the references to devices that
1468 * are pending unregistration have been dropped. We do it here to
1469 * ensure that any internal devices (which contain DP pointers) are
1470 * fully destroyed before freeing the datapath.
1474 call_rcu(&dp->rcu, destroy_dp_rcu);
1475 module_put(THIS_MODULE);
1477 genl_notify(reply, genl_info_net(info), info->snd_pid,
1478 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1488 static int odp_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1490 struct sk_buff *reply;
1491 struct datapath *dp;
1494 err = odp_dp_cmd_validate(info->attrs);
1498 dp = lookup_datapath(info->userhdr, info->attrs);
1502 change_datapath(dp, info->attrs);
1504 reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
1505 if (IS_ERR(reply)) {
1506 err = PTR_ERR(reply);
1507 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1508 dp_datapath_multicast_group.id, err);
1512 genl_notify(reply, genl_info_net(info), info->snd_pid,
1513 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1517 static int odp_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1519 struct sk_buff *reply;
1520 struct datapath *dp;
1523 err = odp_dp_cmd_validate(info->attrs);
1527 dp = lookup_datapath(info->userhdr, info->attrs);
1531 reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
1533 return PTR_ERR(reply);
1535 return genlmsg_reply(reply, info);
1538 static int odp_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1540 struct datapath *dp;
1541 int skip = cb->args[0];
1544 list_for_each_entry (dp, &dps, list_node) {
1547 if (odp_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1548 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1549 ODP_DP_CMD_NEW) < 0)
1559 static struct genl_ops dp_datapath_genl_ops[] = {
1560 { .cmd = ODP_DP_CMD_NEW,
1561 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1562 .policy = datapath_policy,
1563 .doit = odp_dp_cmd_new
1565 { .cmd = ODP_DP_CMD_DEL,
1566 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1567 .policy = datapath_policy,
1568 .doit = odp_dp_cmd_del
1570 { .cmd = ODP_DP_CMD_GET,
1571 .flags = 0, /* OK for unprivileged users. */
1572 .policy = datapath_policy,
1573 .doit = odp_dp_cmd_get,
1574 .dumpit = odp_dp_cmd_dump
1576 { .cmd = ODP_DP_CMD_SET,
1577 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1578 .policy = datapath_policy,
1579 .doit = odp_dp_cmd_set,
1583 static const struct nla_policy vport_policy[ODP_VPORT_ATTR_MAX + 1] = {
1584 #ifdef HAVE_NLA_NUL_STRING
1585 [ODP_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1586 [ODP_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1587 [ODP_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1588 [ODP_VPORT_ATTR_STATS] = { .len = sizeof(struct rtnl_link_stats64) },
1589 [ODP_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1591 [ODP_VPORT_ATTR_STATS] = { .minlen = sizeof(struct rtnl_link_stats64) },
1592 [ODP_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1594 [ODP_VPORT_ATTR_MTU] = { .type = NLA_U32 },
1595 [ODP_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1598 static struct genl_family dp_vport_genl_family = {
1599 .id = GENL_ID_GENERATE,
1600 .hdrsize = sizeof(struct odp_header),
1601 .name = ODP_VPORT_FAMILY,
1603 .maxattr = ODP_VPORT_ATTR_MAX
1606 static struct genl_multicast_group dp_vport_multicast_group = {
1607 .name = ODP_VPORT_MCGROUP
1610 /* Called with RTNL lock or RCU read lock. */
1611 static int odp_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1612 u32 pid, u32 seq, u32 flags, u8 cmd)
1614 struct odp_header *odp_header;
1616 int ifindex, iflink;
1620 odp_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1625 odp_header->dp_ifindex = vport->dp->dp_ifindex;
1627 NLA_PUT_U32(skb, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
1628 NLA_PUT_U32(skb, ODP_VPORT_ATTR_TYPE, vport_get_type(vport));
1629 NLA_PUT_STRING(skb, ODP_VPORT_ATTR_NAME, vport_get_name(vport));
1631 nla = nla_reserve(skb, ODP_VPORT_ATTR_STATS, sizeof(struct rtnl_link_stats64));
1633 goto nla_put_failure;
1634 if (vport_get_stats(vport, nla_data(nla)))
1635 __skb_trim(skb, skb->len - nla->nla_len);
1637 NLA_PUT(skb, ODP_VPORT_ATTR_ADDRESS, ETH_ALEN, vport_get_addr(vport));
1639 mtu = vport_get_mtu(vport);
1641 NLA_PUT_U32(skb, ODP_VPORT_ATTR_MTU, mtu);
1643 err = vport_get_options(vport, skb);
1644 if (err == -EMSGSIZE)
1647 ifindex = vport_get_ifindex(vport);
1649 NLA_PUT_U32(skb, ODP_VPORT_ATTR_IFINDEX, ifindex);
1651 iflink = vport_get_iflink(vport);
1653 NLA_PUT_U32(skb, ODP_VPORT_ATTR_IFLINK, iflink);
1655 return genlmsg_end(skb, odp_header);
1660 genlmsg_cancel(skb, odp_header);
1664 /* Called with RTNL lock or RCU read lock. */
1665 static struct sk_buff *odp_vport_cmd_build_info(struct vport *vport, u32 pid,
1668 struct sk_buff *skb;
1671 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1673 return ERR_PTR(-ENOMEM);
1675 retval = odp_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1678 return ERR_PTR(retval);
1683 static int odp_vport_cmd_validate(struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1685 return CHECK_NUL_STRING(a[ODP_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1688 /* Called with RTNL lock or RCU read lock. */
1689 static struct vport *lookup_vport(struct odp_header *odp_header,
1690 struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1692 struct datapath *dp;
1693 struct vport *vport;
1695 if (a[ODP_VPORT_ATTR_NAME]) {
1696 vport = vport_locate(nla_data(a[ODP_VPORT_ATTR_NAME]));
1698 return ERR_PTR(-ENODEV);
1700 } else if (a[ODP_VPORT_ATTR_PORT_NO]) {
1701 u32 port_no = nla_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1703 if (port_no >= DP_MAX_PORTS)
1704 return ERR_PTR(-EFBIG);
1706 dp = get_dp(odp_header->dp_ifindex);
1708 return ERR_PTR(-ENODEV);
1710 vport = get_vport_protected(dp, port_no);
1712 return ERR_PTR(-ENOENT);
1715 return ERR_PTR(-EINVAL);
1718 /* Called with RTNL lock. */
1719 static int change_vport(struct vport *vport, struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1722 if (a[ODP_VPORT_ATTR_STATS])
1723 err = vport_set_stats(vport, nla_data(a[ODP_VPORT_ATTR_STATS]));
1724 if (!err && a[ODP_VPORT_ATTR_ADDRESS])
1725 err = vport_set_addr(vport, nla_data(a[ODP_VPORT_ATTR_ADDRESS]));
1726 if (!err && a[ODP_VPORT_ATTR_MTU])
1727 err = vport_set_mtu(vport, nla_get_u32(a[ODP_VPORT_ATTR_MTU]));
1731 static int odp_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1733 struct nlattr **a = info->attrs;
1734 struct odp_header *odp_header = info->userhdr;
1735 struct vport_parms parms;
1736 struct sk_buff *reply;
1737 struct vport *vport;
1738 struct datapath *dp;
1743 if (!a[ODP_VPORT_ATTR_NAME] || !a[ODP_VPORT_ATTR_TYPE])
1746 err = odp_vport_cmd_validate(a);
1751 dp = get_dp(odp_header->dp_ifindex);
1756 if (a[ODP_VPORT_ATTR_PORT_NO]) {
1757 port_no = nla_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1760 if (port_no >= DP_MAX_PORTS)
1763 vport = get_vport_protected(dp, port_no);
1768 for (port_no = 1; ; port_no++) {
1769 if (port_no >= DP_MAX_PORTS) {
1773 vport = get_vport_protected(dp, port_no);
1779 parms.name = nla_data(a[ODP_VPORT_ATTR_NAME]);
1780 parms.type = nla_get_u32(a[ODP_VPORT_ATTR_TYPE]);
1781 parms.options = a[ODP_VPORT_ATTR_OPTIONS];
1783 parms.port_no = port_no;
1785 vport = new_vport(&parms);
1786 err = PTR_ERR(vport);
1790 set_internal_devs_mtu(dp);
1791 dp_sysfs_add_if(vport);
1793 err = change_vport(vport, a);
1795 reply = odp_vport_cmd_build_info(vport, info->snd_pid,
1796 info->snd_seq, ODP_VPORT_CMD_NEW);
1798 err = PTR_ERR(reply);
1801 dp_detach_port(vport);
1804 genl_notify(reply, genl_info_net(info), info->snd_pid,
1805 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1814 static int odp_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1816 struct nlattr **a = info->attrs;
1817 struct sk_buff *reply;
1818 struct vport *vport;
1821 err = odp_vport_cmd_validate(a);
1826 vport = lookup_vport(info->userhdr, a);
1827 err = PTR_ERR(vport);
1832 if (a[ODP_VPORT_ATTR_OPTIONS])
1833 err = vport_set_options(vport, a[ODP_VPORT_ATTR_OPTIONS]);
1835 err = change_vport(vport, a);
1837 reply = odp_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1839 if (IS_ERR(reply)) {
1840 err = PTR_ERR(reply);
1841 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1842 dp_vport_multicast_group.id, err);
1846 genl_notify(reply, genl_info_net(info), info->snd_pid,
1847 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1855 static int odp_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1857 struct nlattr **a = info->attrs;
1858 struct sk_buff *reply;
1859 struct vport *vport;
1862 err = odp_vport_cmd_validate(a);
1867 vport = lookup_vport(info->userhdr, a);
1868 err = PTR_ERR(vport);
1872 if (vport->port_no == ODPP_LOCAL) {
1877 reply = odp_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1879 err = PTR_ERR(reply);
1883 err = dp_detach_port(vport);
1885 genl_notify(reply, genl_info_net(info), info->snd_pid,
1886 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1894 static int odp_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1896 struct nlattr **a = info->attrs;
1897 struct odp_header *odp_header = info->userhdr;
1898 struct sk_buff *reply;
1899 struct vport *vport;
1902 err = odp_vport_cmd_validate(a);
1907 vport = lookup_vport(odp_header, a);
1908 err = PTR_ERR(vport);
1912 reply = odp_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1914 err = PTR_ERR(reply);
1920 return genlmsg_reply(reply, info);
1928 static int odp_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1930 struct odp_header *odp_header = genlmsg_data(nlmsg_data(cb->nlh));
1931 struct datapath *dp;
1935 dp = get_dp(odp_header->dp_ifindex);
1940 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1941 struct vport *vport;
1943 vport = get_vport_protected(dp, port_no);
1947 if (odp_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1948 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1949 ODP_VPORT_CMD_NEW) < 0)
1954 cb->args[0] = port_no;
1960 static struct genl_ops dp_vport_genl_ops[] = {
1961 { .cmd = ODP_VPORT_CMD_NEW,
1962 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1963 .policy = vport_policy,
1964 .doit = odp_vport_cmd_new
1966 { .cmd = ODP_VPORT_CMD_DEL,
1967 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1968 .policy = vport_policy,
1969 .doit = odp_vport_cmd_del
1971 { .cmd = ODP_VPORT_CMD_GET,
1972 .flags = 0, /* OK for unprivileged users. */
1973 .policy = vport_policy,
1974 .doit = odp_vport_cmd_get,
1975 .dumpit = odp_vport_cmd_dump
1977 { .cmd = ODP_VPORT_CMD_SET,
1978 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1979 .policy = vport_policy,
1980 .doit = odp_vport_cmd_set,
1984 struct genl_family_and_ops {
1985 struct genl_family *family;
1986 struct genl_ops *ops;
1988 struct genl_multicast_group *group;
1991 static const struct genl_family_and_ops dp_genl_families[] = {
1992 { &dp_datapath_genl_family,
1993 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1994 &dp_datapath_multicast_group },
1995 { &dp_vport_genl_family,
1996 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1997 &dp_vport_multicast_group },
1998 { &dp_flow_genl_family,
1999 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2000 &dp_flow_multicast_group },
2001 { &dp_packet_genl_family,
2002 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2006 static void dp_unregister_genl(int n_families)
2010 for (i = 0; i < n_families; i++) {
2011 genl_unregister_family(dp_genl_families[i].family);
2015 static int dp_register_genl(void)
2022 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2023 const struct genl_family_and_ops *f = &dp_genl_families[i];
2025 err = genl_register_family_with_ops(f->family, f->ops,
2032 err = genl_register_mc_group(f->family, f->group);
2038 err = packet_register_mc_groups();
2044 dp_unregister_genl(n_registered);
2048 static int __init dp_init(void)
2050 struct sk_buff *dummy_skb;
2053 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2055 printk("Open vSwitch %s, built "__DATE__" "__TIME__"\n", VERSION BUILDNR);
2063 goto error_flow_exit;
2065 err = register_netdevice_notifier(&dp_device_notifier);
2067 goto error_vport_exit;
2069 err = dp_register_genl();
2071 goto error_unreg_notifier;
2075 error_unreg_notifier:
2076 unregister_netdevice_notifier(&dp_device_notifier);
2085 static void dp_cleanup(void)
2088 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2089 unregister_netdevice_notifier(&dp_device_notifier);
2094 module_init(dp_init);
2095 module_exit(dp_cleanup);
2097 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2098 MODULE_LICENSE("GPL");