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"
52 #include "loop_counter.h"
54 #include "vport-internal_dev.h"
56 int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
57 EXPORT_SYMBOL(dp_ioctl_hook);
62 * Writes to device state (add/remove datapath, port, set operations on vports,
63 * etc.) are protected by RTNL.
65 * Writes to other state (flow table modifications, set miscellaneous datapath
66 * parameters such as drop frags, etc.) are protected by genl_mutex. The RTNL
67 * lock nests inside genl_mutex.
69 * Reads are protected by RCU.
71 * There are a few special cases (mostly stats) that have their own
72 * synchronization but they nest under all of above and don't interact with
76 /* Global list of datapaths to enable dumping them all out.
77 * Protected by genl_mutex.
79 static LIST_HEAD(dps);
81 static struct vport *new_vport(const struct vport_parms *);
83 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
84 struct datapath *get_dp(int dp_ifindex)
86 struct datapath *dp = NULL;
87 struct net_device *dev;
90 dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
92 struct vport *vport = internal_dev_get_vport(dev);
100 EXPORT_SYMBOL_GPL(get_dp);
102 /* Must be called with genl_mutex. */
103 static struct tbl *get_table_protected(struct datapath *dp)
105 return rcu_dereference_protected(dp->table, lockdep_genl_is_held());
108 /* Must be called with rcu_read_lock or RTNL lock. */
109 static struct vport *get_vport_protected(struct datapath *dp, u16 port_no)
111 return rcu_dereference_rtnl(dp->ports[port_no]);
114 /* Must be called with rcu_read_lock or RTNL lock. */
115 const char *dp_name(const struct datapath *dp)
117 return vport_get_name(rcu_dereference_rtnl(dp->ports[ODPP_LOCAL]));
120 static inline size_t br_nlmsg_size(void)
122 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
123 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
124 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
125 + nla_total_size(4) /* IFLA_MASTER */
126 + nla_total_size(4) /* IFLA_MTU */
127 + nla_total_size(4) /* IFLA_LINK */
128 + nla_total_size(1); /* IFLA_OPERSTATE */
131 /* Caller must hold RTNL lock. */
132 static int dp_fill_ifinfo(struct sk_buff *skb,
133 const struct vport *port,
134 int event, unsigned int flags)
136 struct datapath *dp = port->dp;
137 int ifindex = vport_get_ifindex(port);
138 int iflink = vport_get_iflink(port);
139 struct ifinfomsg *hdr;
140 struct nlmsghdr *nlh;
148 nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
152 hdr = nlmsg_data(nlh);
153 hdr->ifi_family = AF_BRIDGE;
155 hdr->ifi_type = ARPHRD_ETHER;
156 hdr->ifi_index = ifindex;
157 hdr->ifi_flags = vport_get_flags(port);
160 NLA_PUT_STRING(skb, IFLA_IFNAME, vport_get_name(port));
161 NLA_PUT_U32(skb, IFLA_MASTER,
162 vport_get_ifindex(get_vport_protected(dp, ODPP_LOCAL)));
163 NLA_PUT_U32(skb, IFLA_MTU, vport_get_mtu(port));
164 #ifdef IFLA_OPERSTATE
165 NLA_PUT_U8(skb, IFLA_OPERSTATE,
166 vport_is_running(port)
167 ? vport_get_operstate(port)
171 NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, vport_get_addr(port));
173 if (ifindex != iflink)
174 NLA_PUT_U32(skb, IFLA_LINK,iflink);
176 return nlmsg_end(skb, nlh);
179 nlmsg_cancel(skb, nlh);
183 /* Caller must hold RTNL lock. */
184 static void dp_ifinfo_notify(int event, struct vport *port)
189 skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
193 err = dp_fill_ifinfo(skb, port, event, 0);
195 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
196 WARN_ON(err == -EMSGSIZE);
200 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
204 rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
207 static void release_dp(struct kobject *kobj)
209 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
213 static struct kobj_type dp_ktype = {
214 .release = release_dp
217 static void destroy_dp_rcu(struct rcu_head *rcu)
219 struct datapath *dp = container_of(rcu, struct datapath, rcu);
221 tbl_destroy((struct tbl __force *)dp->table, flow_free_tbl);
222 free_percpu(dp->stats_percpu);
223 kobject_put(&dp->ifobj);
226 /* Called with RTNL lock and genl_lock. */
227 static struct vport *new_vport(const struct vport_parms *parms)
231 vport = vport_add(parms);
232 if (!IS_ERR(vport)) {
233 struct datapath *dp = parms->dp;
235 rcu_assign_pointer(dp->ports[parms->port_no], vport);
236 list_add(&vport->node, &dp->port_list);
238 dp_ifinfo_notify(RTM_NEWLINK, vport);
244 /* Called with RTNL lock. */
245 int dp_detach_port(struct vport *p)
249 if (p->port_no != ODPP_LOCAL)
251 dp_ifinfo_notify(RTM_DELLINK, p);
253 /* First drop references to device. */
255 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
257 /* Then destroy it. */
261 /* Must be called with rcu_read_lock. */
262 void dp_process_received_packet(struct vport *p, struct sk_buff *skb)
264 struct datapath *dp = p->dp;
265 struct dp_stats_percpu *stats;
266 int stats_counter_off;
267 struct sw_flow_actions *acts;
268 struct loop_counter *loop;
271 OVS_CB(skb)->vport = p;
273 if (!OVS_CB(skb)->flow) {
274 struct sw_flow_key key;
275 struct tbl_node *flow_node;
278 /* Extract flow from 'skb' into 'key'. */
279 error = flow_extract(skb, p->port_no, &key, &is_frag);
280 if (unlikely(error)) {
285 if (is_frag && dp->drop_frags) {
287 stats_counter_off = offsetof(struct dp_stats_percpu, n_frags);
292 flow_node = tbl_lookup(rcu_dereference(dp->table), &key,
293 flow_hash(&key), flow_cmp);
294 if (unlikely(!flow_node)) {
295 struct dp_upcall_info upcall;
297 upcall.cmd = ODP_PACKET_CMD_MISS;
300 upcall.sample_pool = 0;
301 upcall.actions = NULL;
302 upcall.actions_len = 0;
303 dp_upcall(dp, skb, &upcall);
304 stats_counter_off = offsetof(struct dp_stats_percpu, n_missed);
308 OVS_CB(skb)->flow = flow_cast(flow_node);
311 stats_counter_off = offsetof(struct dp_stats_percpu, n_hit);
312 flow_used(OVS_CB(skb)->flow, skb);
314 acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
316 /* Check whether we've looped too much. */
317 loop = loop_get_counter();
318 if (unlikely(++loop->count > MAX_LOOPS))
319 loop->looping = true;
320 if (unlikely(loop->looping)) {
321 loop_suppress(dp, acts);
326 /* Execute actions. */
327 execute_actions(dp, skb, &OVS_CB(skb)->flow->key, acts->actions,
330 /* Check whether sub-actions looped too much. */
331 if (unlikely(loop->looping))
332 loop_suppress(dp, acts);
335 /* Decrement loop counter. */
337 loop->looping = false;
341 /* Update datapath statistics. */
343 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
345 write_seqcount_begin(&stats->seqlock);
346 (*(u64 *)((u8 *)stats + stats_counter_off))++;
347 write_seqcount_end(&stats->seqlock);
352 static void copy_and_csum_skb(struct sk_buff *skb, void *to)
354 u16 csum_start, csum_offset;
357 get_skb_csum_pointers(skb, &csum_start, &csum_offset);
358 csum_start -= skb_headroom(skb);
359 BUG_ON(csum_start >= skb_headlen(skb));
361 skb_copy_bits(skb, 0, to, csum_start);
363 csum = skb_copy_and_csum_bits(skb, csum_start, to + csum_start,
364 skb->len - csum_start, 0);
365 *(__sum16 *)(to + csum_start + csum_offset) = csum_fold(csum);
368 static struct genl_family dp_packet_genl_family;
369 #define PACKET_N_MC_GROUPS 16
371 static int packet_mc_group(struct datapath *dp, u8 cmd)
373 BUILD_BUG_ON_NOT_POWER_OF_2(PACKET_N_MC_GROUPS);
374 return jhash_2words(dp->dp_ifindex, cmd, 0) & (PACKET_N_MC_GROUPS - 1);
377 /* Send each packet in the 'skb' list to userspace for 'dp' as directed by
378 * 'upcall_info'. There will be only one packet unless we broke up a GSO
381 static int queue_control_packets(struct datapath *dp, struct sk_buff *skb,
382 const struct dp_upcall_info *upcall_info)
384 u32 group = packet_mc_group(dp, upcall_info->cmd);
385 struct sk_buff *nskb;
389 if (OVS_CB(skb)->vport)
390 port_no = OVS_CB(skb)->vport->port_no;
392 port_no = ODPP_LOCAL;
395 struct odp_header *upcall;
396 struct sk_buff *user_skb; /* to be queued to userspace */
403 len = sizeof(struct odp_header);
404 len += nla_total_size(4); /* ODP_PACKET_ATTR_TYPE. */
405 len += nla_total_size(skb->len);
406 len += nla_total_size(FLOW_BUFSIZE);
407 if (upcall_info->userdata)
408 len += nla_total_size(8);
409 if (upcall_info->sample_pool)
410 len += nla_total_size(4);
411 if (upcall_info->actions_len)
412 len += nla_total_size(upcall_info->actions_len);
414 user_skb = genlmsg_new(len, GFP_ATOMIC);
416 netlink_set_err(INIT_NET_GENL_SOCK, 0, group, -ENOBUFS);
420 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, 0, upcall_info->cmd);
421 upcall->dp_ifindex = dp->dp_ifindex;
423 nla = nla_nest_start(user_skb, ODP_PACKET_ATTR_KEY);
424 flow_to_nlattrs(upcall_info->key, user_skb);
425 nla_nest_end(user_skb, nla);
427 if (upcall_info->userdata)
428 nla_put_u64(user_skb, ODP_PACKET_ATTR_USERDATA, upcall_info->userdata);
429 if (upcall_info->sample_pool)
430 nla_put_u32(user_skb, ODP_PACKET_ATTR_SAMPLE_POOL, upcall_info->sample_pool);
431 if (upcall_info->actions_len) {
432 const struct nlattr *actions = upcall_info->actions;
433 u32 actions_len = upcall_info->actions_len;
435 nla = nla_nest_start(user_skb, ODP_PACKET_ATTR_ACTIONS);
436 memcpy(__skb_put(user_skb, actions_len), actions, actions_len);
437 nla_nest_end(user_skb, nla);
440 nla = __nla_reserve(user_skb, ODP_PACKET_ATTR_PACKET, skb->len);
441 if (skb->ip_summed == CHECKSUM_PARTIAL)
442 copy_and_csum_skb(skb, nla_data(nla));
444 skb_copy_bits(skb, 0, nla_data(nla), skb->len);
446 err = genlmsg_multicast(user_skb, 0, group, GFP_ATOMIC);
457 while ((skb = nskb) != NULL) {
464 /* Generic Netlink multicast groups for upcalls.
466 * We really want three unique multicast groups per datapath, but we can't even
467 * get one, because genl_register_mc_group() takes genl_lock, which is also
468 * held during Generic Netlink message processing, so trying to acquire
469 * multicast groups during ODP_DP_NEW processing deadlocks. Instead, we
470 * preallocate a few groups and use them round-robin for datapaths. Collision
471 * isn't fatal--multicast listeners should check that the family is the one
472 * that they want and discard others--but it wastes time and memory to receive
475 static struct genl_multicast_group packet_mc_groups[PACKET_N_MC_GROUPS];
477 static struct genl_family dp_packet_genl_family = {
478 .id = GENL_ID_GENERATE,
479 .hdrsize = sizeof(struct odp_header),
480 .name = ODP_PACKET_FAMILY,
482 .maxattr = ODP_PACKET_ATTR_MAX
485 static int packet_register_mc_groups(void)
489 for (i = 0; i < PACKET_N_MC_GROUPS; i++) {
490 struct genl_multicast_group *group = &packet_mc_groups[i];
493 sprintf(group->name, "packet%d", i);
494 error = genl_register_mc_group(&dp_packet_genl_family, group);
501 int dp_upcall(struct datapath *dp, struct sk_buff *skb, const struct dp_upcall_info *upcall_info)
503 struct dp_stats_percpu *stats;
506 WARN_ON_ONCE(skb_shared(skb));
508 forward_ip_summed(skb);
510 err = vswitch_skb_checksum_setup(skb);
514 /* Break apart GSO packets into their component pieces. Otherwise
515 * userspace may try to stuff a 64kB packet into a 1500-byte MTU. */
516 if (skb_is_gso(skb)) {
517 struct sk_buff *nskb = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
527 return queue_control_packets(dp, skb, upcall_info);
533 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
535 write_seqcount_begin(&stats->seqlock);
537 write_seqcount_end(&stats->seqlock);
544 /* Called with genl_mutex. */
545 static int flush_flows(int dp_ifindex)
547 struct tbl *old_table;
548 struct tbl *new_table;
551 dp = get_dp(dp_ifindex);
555 old_table = get_table_protected(dp);
556 new_table = tbl_create(TBL_MIN_BUCKETS);
560 rcu_assign_pointer(dp->table, new_table);
562 tbl_deferred_destroy(old_table, flow_free_tbl);
567 static int validate_actions(const struct nlattr *attr)
569 const struct nlattr *a;
572 nla_for_each_nested(a, attr, rem) {
573 static const u32 action_lens[ODP_ACTION_ATTR_MAX + 1] = {
574 [ODP_ACTION_ATTR_OUTPUT] = 4,
575 [ODP_ACTION_ATTR_CONTROLLER] = 8,
576 [ODP_ACTION_ATTR_SET_DL_TCI] = 2,
577 [ODP_ACTION_ATTR_STRIP_VLAN] = 0,
578 [ODP_ACTION_ATTR_SET_DL_SRC] = ETH_ALEN,
579 [ODP_ACTION_ATTR_SET_DL_DST] = ETH_ALEN,
580 [ODP_ACTION_ATTR_SET_NW_SRC] = 4,
581 [ODP_ACTION_ATTR_SET_NW_DST] = 4,
582 [ODP_ACTION_ATTR_SET_NW_TOS] = 1,
583 [ODP_ACTION_ATTR_SET_TP_SRC] = 2,
584 [ODP_ACTION_ATTR_SET_TP_DST] = 2,
585 [ODP_ACTION_ATTR_SET_TUNNEL] = 8,
586 [ODP_ACTION_ATTR_SET_PRIORITY] = 4,
587 [ODP_ACTION_ATTR_POP_PRIORITY] = 0,
588 [ODP_ACTION_ATTR_DROP_SPOOFED_ARP] = 0,
590 int type = nla_type(a);
592 if (type > ODP_ACTION_ATTR_MAX || nla_len(a) != action_lens[type])
596 case ODP_ACTION_ATTR_UNSPEC:
599 case ODP_ACTION_ATTR_CONTROLLER:
600 case ODP_ACTION_ATTR_STRIP_VLAN:
601 case ODP_ACTION_ATTR_SET_DL_SRC:
602 case ODP_ACTION_ATTR_SET_DL_DST:
603 case ODP_ACTION_ATTR_SET_NW_SRC:
604 case ODP_ACTION_ATTR_SET_NW_DST:
605 case ODP_ACTION_ATTR_SET_TP_SRC:
606 case ODP_ACTION_ATTR_SET_TP_DST:
607 case ODP_ACTION_ATTR_SET_TUNNEL:
608 case ODP_ACTION_ATTR_SET_PRIORITY:
609 case ODP_ACTION_ATTR_POP_PRIORITY:
610 case ODP_ACTION_ATTR_DROP_SPOOFED_ARP:
611 /* No validation needed. */
614 case ODP_ACTION_ATTR_OUTPUT:
615 if (nla_get_u32(a) >= DP_MAX_PORTS)
619 case ODP_ACTION_ATTR_SET_DL_TCI:
620 if (nla_get_be16(a) & htons(VLAN_CFI_MASK))
624 case ODP_ACTION_ATTR_SET_NW_TOS:
625 if (nla_get_u8(a) & INET_ECN_MASK)
639 static void clear_stats(struct sw_flow *flow)
643 flow->packet_count = 0;
644 flow->byte_count = 0;
647 /* Called with genl_mutex. */
648 static int expand_table(struct datapath *dp)
650 struct tbl *old_table = get_table_protected(dp);
651 struct tbl *new_table;
653 new_table = tbl_expand(old_table);
654 if (IS_ERR(new_table))
655 return PTR_ERR(new_table);
657 rcu_assign_pointer(dp->table, new_table);
658 tbl_deferred_destroy(old_table, NULL);
663 static int odp_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
665 struct odp_header *odp_header = info->userhdr;
666 struct nlattr **a = info->attrs;
667 struct sk_buff *packet;
668 struct sw_flow_key key;
675 if (!a[ODP_PACKET_ATTR_PACKET] || !a[ODP_PACKET_ATTR_ACTIONS] ||
676 nla_len(a[ODP_PACKET_ATTR_PACKET]) < ETH_HLEN)
679 err = validate_actions(a[ODP_PACKET_ATTR_ACTIONS]);
683 packet = skb_clone(skb, GFP_KERNEL);
687 packet->data = nla_data(a[ODP_PACKET_ATTR_PACKET]);
688 packet->len = nla_len(a[ODP_PACKET_ATTR_PACKET]);
690 skb_reset_mac_header(packet);
691 eth = eth_hdr(packet);
693 /* Normally, setting the skb 'protocol' field would be handled by a
694 * call to eth_type_trans(), but it assumes there's a sending
695 * device, which we may not have. */
696 if (ntohs(eth->h_proto) >= 1536)
697 packet->protocol = eth->h_proto;
699 packet->protocol = htons(ETH_P_802_2);
701 err = flow_extract(packet, -1, &key, &is_frag);
706 dp = get_dp(odp_header->dp_ifindex);
709 err = execute_actions(dp, packet, &key,
710 nla_data(a[ODP_PACKET_ATTR_ACTIONS]),
711 nla_len(a[ODP_PACKET_ATTR_ACTIONS]));
718 static const struct nla_policy packet_policy[ODP_PACKET_ATTR_MAX + 1] = {
719 [ODP_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
720 [ODP_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
723 static struct genl_ops dp_packet_genl_ops[] = {
724 { .cmd = ODP_PACKET_CMD_EXECUTE,
725 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
726 .policy = packet_policy,
727 .doit = odp_packet_cmd_execute
731 static void get_dp_stats(struct datapath *dp, struct odp_stats *stats)
735 stats->n_frags = stats->n_hit = stats->n_missed = stats->n_lost = 0;
736 for_each_possible_cpu(i) {
737 const struct dp_stats_percpu *percpu_stats;
738 struct dp_stats_percpu local_stats;
741 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
744 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
745 local_stats = *percpu_stats;
746 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
748 stats->n_frags += local_stats.n_frags;
749 stats->n_hit += local_stats.n_hit;
750 stats->n_missed += local_stats.n_missed;
751 stats->n_lost += local_stats.n_lost;
755 /* MTU of the dp pseudo-device: ETH_DATA_LEN or the minimum of the ports.
756 * Called with RTNL lock.
758 int dp_min_mtu(const struct datapath *dp)
765 list_for_each_entry (p, &dp->port_list, node) {
768 /* Skip any internal ports, since that's what we're trying to
770 if (is_internal_vport(p))
773 dev_mtu = vport_get_mtu(p);
774 if (!mtu || dev_mtu < mtu)
778 return mtu ? mtu : ETH_DATA_LEN;
781 /* Sets the MTU of all datapath devices to the minimum of the ports
782 * Called with RTNL lock.
784 void set_internal_devs_mtu(const struct datapath *dp)
791 mtu = dp_min_mtu(dp);
793 list_for_each_entry (p, &dp->port_list, node) {
794 if (is_internal_vport(p))
795 vport_set_mtu(p, mtu);
799 static const struct nla_policy flow_policy[ODP_FLOW_ATTR_MAX + 1] = {
800 [ODP_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
801 [ODP_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
802 [ODP_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
805 static struct genl_family dp_flow_genl_family = {
806 .id = GENL_ID_GENERATE,
807 .hdrsize = sizeof(struct odp_header),
808 .name = ODP_FLOW_FAMILY,
810 .maxattr = ODP_FLOW_ATTR_MAX
813 static struct genl_multicast_group dp_flow_multicast_group = {
814 .name = ODP_FLOW_MCGROUP
817 /* Called with genl_lock. */
818 static int odp_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
819 struct sk_buff *skb, u32 pid, u32 seq, u32 flags, u8 cmd)
821 const int skb_orig_len = skb->len;
822 const struct sw_flow_actions *sf_acts;
823 struct odp_flow_stats stats;
824 struct odp_header *odp_header;
831 sf_acts = rcu_dereference_protected(flow->sf_acts,
832 lockdep_genl_is_held());
834 odp_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
838 odp_header->dp_ifindex = dp->dp_ifindex;
840 nla = nla_nest_start(skb, ODP_FLOW_ATTR_KEY);
842 goto nla_put_failure;
843 err = flow_to_nlattrs(&flow->key, skb);
846 nla_nest_end(skb, nla);
848 spin_lock_bh(&flow->lock);
850 stats.n_packets = flow->packet_count;
851 stats.n_bytes = flow->byte_count;
852 tcp_flags = flow->tcp_flags;
853 spin_unlock_bh(&flow->lock);
856 NLA_PUT_MSECS(skb, ODP_FLOW_ATTR_USED, used);
859 NLA_PUT(skb, ODP_FLOW_ATTR_STATS, sizeof(struct odp_flow_stats), &stats);
862 NLA_PUT_U8(skb, ODP_FLOW_ATTR_TCP_FLAGS, tcp_flags);
864 /* If ODP_FLOW_ATTR_ACTIONS doesn't fit, and this is the first flow to
865 * be dumped into 'skb', then expand the skb. This is unusual for
866 * Netlink but individual action lists can be longer than a page and
867 * thus entirely undumpable if we didn't do this. */
868 nla_len = nla_total_size(sf_acts->actions_len);
869 if (nla_len > skb_tailroom(skb) && !skb_orig_len) {
870 int hdr_off = (unsigned char *)odp_header - skb->data;
872 err = pskb_expand_head(skb, 0, nla_len - skb_tailroom(skb), GFP_KERNEL);
876 odp_header = (struct odp_header *)(skb->data + hdr_off);
878 nla = nla_nest_start(skb, ODP_FLOW_ATTR_ACTIONS);
879 memcpy(__skb_put(skb, sf_acts->actions_len), sf_acts->actions, sf_acts->actions_len);
880 nla_nest_end(skb, nla);
882 return genlmsg_end(skb, odp_header);
887 genlmsg_cancel(skb, odp_header);
891 static struct sk_buff *odp_flow_cmd_alloc_info(struct sw_flow *flow)
893 const struct sw_flow_actions *sf_acts;
896 sf_acts = rcu_dereference_protected(flow->sf_acts,
897 lockdep_genl_is_held());
899 len = nla_total_size(FLOW_BUFSIZE); /* ODP_FLOW_ATTR_KEY */
900 len += nla_total_size(sf_acts->actions_len); /* ODP_FLOW_ATTR_ACTIONS */
901 len += nla_total_size(sizeof(struct odp_flow_stats)); /* ODP_FLOW_ATTR_STATS */
902 len += nla_total_size(1); /* ODP_FLOW_ATTR_TCP_FLAGS */
903 len += nla_total_size(8); /* ODP_FLOW_ATTR_USED */
904 return genlmsg_new(NLMSG_ALIGN(sizeof(struct odp_header)) + len, GFP_KERNEL);
907 static struct sk_buff *odp_flow_cmd_build_info(struct sw_flow *flow, struct datapath *dp,
908 u32 pid, u32 seq, u8 cmd)
913 skb = odp_flow_cmd_alloc_info(flow);
915 return ERR_PTR(-ENOMEM);
917 retval = odp_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
922 static int odp_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
924 struct nlattr **a = info->attrs;
925 struct odp_header *odp_header = info->userhdr;
926 struct tbl_node *flow_node;
927 struct sw_flow_key key;
928 struct sw_flow *flow;
929 struct sk_buff *reply;
937 if (!a[ODP_FLOW_ATTR_KEY])
939 error = flow_from_nlattrs(&key, a[ODP_FLOW_ATTR_KEY]);
943 /* Validate actions. */
944 if (a[ODP_FLOW_ATTR_ACTIONS]) {
945 error = validate_actions(a[ODP_FLOW_ATTR_ACTIONS]);
948 } else if (info->genlhdr->cmd == ODP_FLOW_CMD_NEW) {
953 dp = get_dp(odp_header->dp_ifindex);
958 hash = flow_hash(&key);
959 table = get_table_protected(dp);
960 flow_node = tbl_lookup(table, &key, hash, flow_cmp);
962 struct sw_flow_actions *acts;
964 /* Bail out if we're not allowed to create a new flow. */
966 if (info->genlhdr->cmd == ODP_FLOW_CMD_SET)
969 /* Expand table, if necessary, to make room. */
970 if (tbl_count(table) >= tbl_n_buckets(table)) {
971 error = expand_table(dp);
974 table = get_table_protected(dp);
980 error = PTR_ERR(flow);
986 /* Obtain actions. */
987 acts = flow_actions_alloc(a[ODP_FLOW_ATTR_ACTIONS]);
988 error = PTR_ERR(acts);
990 goto error_free_flow;
991 rcu_assign_pointer(flow->sf_acts, acts);
993 /* Put flow in bucket. */
994 error = tbl_insert(table, &flow->tbl_node, hash);
996 goto error_free_flow;
998 reply = odp_flow_cmd_build_info(flow, dp, info->snd_pid,
999 info->snd_seq, ODP_FLOW_CMD_NEW);
1001 /* We found a matching flow. */
1002 struct sw_flow_actions *old_acts;
1004 /* Bail out if we're not allowed to modify an existing flow.
1005 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1006 * because Generic Netlink treats the latter as a dump
1007 * request. We also accept NLM_F_EXCL in case that bug ever
1011 if (info->genlhdr->cmd == ODP_FLOW_CMD_NEW &&
1012 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1015 /* Update actions. */
1016 flow = flow_cast(flow_node);
1017 old_acts = rcu_dereference_protected(flow->sf_acts,
1018 lockdep_genl_is_held());
1019 if (a[ODP_FLOW_ATTR_ACTIONS] &&
1020 (old_acts->actions_len != nla_len(a[ODP_FLOW_ATTR_ACTIONS]) ||
1021 memcmp(old_acts->actions, nla_data(a[ODP_FLOW_ATTR_ACTIONS]),
1022 old_acts->actions_len))) {
1023 struct sw_flow_actions *new_acts;
1025 new_acts = flow_actions_alloc(a[ODP_FLOW_ATTR_ACTIONS]);
1026 error = PTR_ERR(new_acts);
1027 if (IS_ERR(new_acts))
1030 rcu_assign_pointer(flow->sf_acts, new_acts);
1031 flow_deferred_free_acts(old_acts);
1034 reply = odp_flow_cmd_build_info(flow, dp, info->snd_pid,
1035 info->snd_seq, ODP_FLOW_CMD_NEW);
1038 if (a[ODP_FLOW_ATTR_CLEAR]) {
1039 spin_lock_bh(&flow->lock);
1041 spin_unlock_bh(&flow->lock);
1046 genl_notify(reply, genl_info_net(info), info->snd_pid,
1047 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1049 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1050 dp_flow_multicast_group.id, PTR_ERR(reply));
1059 static int odp_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1061 struct nlattr **a = info->attrs;
1062 struct odp_header *odp_header = info->userhdr;
1063 struct sw_flow_key key;
1064 struct tbl_node *flow_node;
1065 struct sk_buff *reply;
1066 struct sw_flow *flow;
1067 struct datapath *dp;
1071 if (!a[ODP_FLOW_ATTR_KEY])
1073 err = flow_from_nlattrs(&key, a[ODP_FLOW_ATTR_KEY]);
1077 dp = get_dp(odp_header->dp_ifindex);
1081 table = get_table_protected(dp);
1082 flow_node = tbl_lookup(table, &key, flow_hash(&key), flow_cmp);
1086 flow = flow_cast(flow_node);
1087 reply = odp_flow_cmd_build_info(flow, dp, info->snd_pid, info->snd_seq, ODP_FLOW_CMD_NEW);
1089 return PTR_ERR(reply);
1091 return genlmsg_reply(reply, info);
1094 static int odp_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1096 struct nlattr **a = info->attrs;
1097 struct odp_header *odp_header = info->userhdr;
1098 struct sw_flow_key key;
1099 struct tbl_node *flow_node;
1100 struct sk_buff *reply;
1101 struct sw_flow *flow;
1102 struct datapath *dp;
1106 if (!a[ODP_FLOW_ATTR_KEY])
1107 return flush_flows(odp_header->dp_ifindex);
1108 err = flow_from_nlattrs(&key, a[ODP_FLOW_ATTR_KEY]);
1112 dp = get_dp(odp_header->dp_ifindex);
1116 table = get_table_protected(dp);
1117 flow_node = tbl_lookup(table, &key, flow_hash(&key), flow_cmp);
1120 flow = flow_cast(flow_node);
1122 reply = odp_flow_cmd_alloc_info(flow);
1126 err = tbl_remove(table, flow_node);
1132 err = odp_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1133 info->snd_seq, 0, ODP_FLOW_CMD_DEL);
1136 flow_deferred_free(flow);
1138 genl_notify(reply, genl_info_net(info), info->snd_pid,
1139 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1143 static int odp_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1145 struct odp_header *odp_header = genlmsg_data(nlmsg_data(cb->nlh));
1146 struct datapath *dp;
1148 dp = get_dp(odp_header->dp_ifindex);
1153 struct tbl_node *flow_node;
1154 struct sw_flow *flow;
1157 bucket = cb->args[0];
1159 flow_node = tbl_next(get_table_protected(dp), &bucket, &obj);
1163 flow = flow_cast(flow_node);
1164 if (odp_flow_cmd_fill_info(flow, dp, skb, NETLINK_CB(cb->skb).pid,
1165 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1166 ODP_FLOW_CMD_NEW) < 0)
1169 cb->args[0] = bucket;
1175 static struct genl_ops dp_flow_genl_ops[] = {
1176 { .cmd = ODP_FLOW_CMD_NEW,
1177 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1178 .policy = flow_policy,
1179 .doit = odp_flow_cmd_new_or_set
1181 { .cmd = ODP_FLOW_CMD_DEL,
1182 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1183 .policy = flow_policy,
1184 .doit = odp_flow_cmd_del
1186 { .cmd = ODP_FLOW_CMD_GET,
1187 .flags = 0, /* OK for unprivileged users. */
1188 .policy = flow_policy,
1189 .doit = odp_flow_cmd_get,
1190 .dumpit = odp_flow_cmd_dump
1192 { .cmd = ODP_FLOW_CMD_SET,
1193 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1194 .policy = flow_policy,
1195 .doit = odp_flow_cmd_new_or_set,
1199 static const struct nla_policy datapath_policy[ODP_DP_ATTR_MAX + 1] = {
1200 #ifdef HAVE_NLA_NUL_STRING
1201 [ODP_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1203 [ODP_DP_ATTR_IPV4_FRAGS] = { .type = NLA_U32 },
1204 [ODP_DP_ATTR_SAMPLING] = { .type = NLA_U32 },
1207 static struct genl_family dp_datapath_genl_family = {
1208 .id = GENL_ID_GENERATE,
1209 .hdrsize = sizeof(struct odp_header),
1210 .name = ODP_DATAPATH_FAMILY,
1212 .maxattr = ODP_DP_ATTR_MAX
1215 static struct genl_multicast_group dp_datapath_multicast_group = {
1216 .name = ODP_DATAPATH_MCGROUP
1219 static int odp_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1220 u32 pid, u32 seq, u32 flags, u8 cmd)
1222 struct odp_header *odp_header;
1226 odp_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1231 odp_header->dp_ifindex = dp->dp_ifindex;
1234 err = nla_put_string(skb, ODP_DP_ATTR_NAME, dp_name(dp));
1237 goto nla_put_failure;
1239 nla = nla_reserve(skb, ODP_DP_ATTR_STATS, sizeof(struct odp_stats));
1241 goto nla_put_failure;
1242 get_dp_stats(dp, nla_data(nla));
1244 NLA_PUT_U32(skb, ODP_DP_ATTR_IPV4_FRAGS,
1245 dp->drop_frags ? ODP_DP_FRAG_DROP : ODP_DP_FRAG_ZERO);
1247 if (dp->sflow_probability)
1248 NLA_PUT_U32(skb, ODP_DP_ATTR_SAMPLING, dp->sflow_probability);
1250 nla = nla_nest_start(skb, ODP_DP_ATTR_MCGROUPS);
1252 goto nla_put_failure;
1253 NLA_PUT_U32(skb, ODP_PACKET_CMD_MISS, packet_mc_group(dp, ODP_PACKET_CMD_MISS));
1254 NLA_PUT_U32(skb, ODP_PACKET_CMD_ACTION, packet_mc_group(dp, ODP_PACKET_CMD_ACTION));
1255 NLA_PUT_U32(skb, ODP_PACKET_CMD_SAMPLE, packet_mc_group(dp, ODP_PACKET_CMD_SAMPLE));
1256 nla_nest_end(skb, nla);
1258 return genlmsg_end(skb, odp_header);
1261 genlmsg_cancel(skb, odp_header);
1266 static struct sk_buff *odp_dp_cmd_build_info(struct datapath *dp, u32 pid,
1269 struct sk_buff *skb;
1272 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1274 return ERR_PTR(-ENOMEM);
1276 retval = odp_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1279 return ERR_PTR(retval);
1284 static int odp_dp_cmd_validate(struct nlattr *a[ODP_DP_ATTR_MAX + 1])
1286 if (a[ODP_DP_ATTR_IPV4_FRAGS]) {
1287 u32 frags = nla_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]);
1289 if (frags != ODP_DP_FRAG_ZERO && frags != ODP_DP_FRAG_DROP)
1293 return VERIFY_NUL_STRING(a[ODP_DP_ATTR_NAME], IFNAMSIZ - 1);
1296 /* Called with genl_mutex and optionally with RTNL lock also. */
1297 static struct datapath *lookup_datapath(struct odp_header *odp_header, struct nlattr *a[ODP_DP_ATTR_MAX + 1])
1299 struct datapath *dp;
1301 if (!a[ODP_DP_ATTR_NAME])
1302 dp = get_dp(odp_header->dp_ifindex);
1304 struct vport *vport;
1307 vport = vport_locate(nla_data(a[ODP_DP_ATTR_NAME]));
1308 dp = vport && vport->port_no == ODPP_LOCAL ? vport->dp : NULL;
1311 return dp ? dp : ERR_PTR(-ENODEV);
1314 /* Called with genl_mutex. */
1315 static void change_datapath(struct datapath *dp, struct nlattr *a[ODP_DP_ATTR_MAX + 1])
1317 if (a[ODP_DP_ATTR_IPV4_FRAGS])
1318 dp->drop_frags = nla_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]) == ODP_DP_FRAG_DROP;
1319 if (a[ODP_DP_ATTR_SAMPLING])
1320 dp->sflow_probability = nla_get_u32(a[ODP_DP_ATTR_SAMPLING]);
1323 static int odp_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1325 struct nlattr **a = info->attrs;
1326 struct vport_parms parms;
1327 struct sk_buff *reply;
1328 struct datapath *dp;
1329 struct vport *vport;
1333 if (!a[ODP_DP_ATTR_NAME])
1336 err = odp_dp_cmd_validate(a);
1342 if (!try_module_get(THIS_MODULE))
1343 goto err_unlock_rtnl;
1346 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1348 goto err_put_module;
1349 INIT_LIST_HEAD(&dp->port_list);
1351 /* Initialize kobject for bridge. This will be added as
1352 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1353 dp->ifobj.kset = NULL;
1354 kobject_init(&dp->ifobj, &dp_ktype);
1356 /* Allocate table. */
1358 rcu_assign_pointer(dp->table, tbl_create(TBL_MIN_BUCKETS));
1362 /* Set up our datapath device. */
1363 parms.name = nla_data(a[ODP_DP_ATTR_NAME]);
1364 parms.type = ODP_VPORT_TYPE_INTERNAL;
1365 parms.options = NULL;
1367 parms.port_no = ODPP_LOCAL;
1368 vport = new_vport(&parms);
1369 if (IS_ERR(vport)) {
1370 err = PTR_ERR(vport);
1374 goto err_destroy_table;
1376 dp->dp_ifindex = vport_get_ifindex(vport);
1379 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1380 if (!dp->stats_percpu) {
1382 goto err_destroy_local_port;
1385 change_datapath(dp, a);
1387 reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
1388 err = PTR_ERR(reply);
1390 goto err_destroy_local_port;
1392 list_add_tail(&dp->list_node, &dps);
1393 dp_sysfs_add_dp(dp);
1397 genl_notify(reply, genl_info_net(info), info->snd_pid,
1398 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1401 err_destroy_local_port:
1402 dp_detach_port(get_vport_protected(dp, ODPP_LOCAL));
1404 tbl_destroy(get_table_protected(dp), NULL);
1408 module_put(THIS_MODULE);
1415 static int odp_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1417 struct vport *vport, *next_vport;
1418 struct sk_buff *reply;
1419 struct datapath *dp;
1422 err = odp_dp_cmd_validate(info->attrs);
1427 dp = lookup_datapath(info->userhdr, info->attrs);
1432 reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_DEL);
1433 err = PTR_ERR(reply);
1437 list_for_each_entry_safe (vport, next_vport, &dp->port_list, node)
1438 if (vport->port_no != ODPP_LOCAL)
1439 dp_detach_port(vport);
1441 dp_sysfs_del_dp(dp);
1442 list_del(&dp->list_node);
1443 dp_detach_port(get_vport_protected(dp, ODPP_LOCAL));
1445 call_rcu(&dp->rcu, destroy_dp_rcu);
1446 module_put(THIS_MODULE);
1448 genl_notify(reply, genl_info_net(info), info->snd_pid,
1449 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1458 static int odp_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1460 struct sk_buff *reply;
1461 struct datapath *dp;
1464 err = odp_dp_cmd_validate(info->attrs);
1468 dp = lookup_datapath(info->userhdr, info->attrs);
1472 change_datapath(dp, info->attrs);
1474 reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
1475 if (IS_ERR(reply)) {
1476 err = PTR_ERR(reply);
1477 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1478 dp_datapath_multicast_group.id, err);
1482 genl_notify(reply, genl_info_net(info), info->snd_pid,
1483 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1487 static int odp_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1489 struct sk_buff *reply;
1490 struct datapath *dp;
1493 err = odp_dp_cmd_validate(info->attrs);
1497 dp = lookup_datapath(info->userhdr, info->attrs);
1501 reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
1503 return PTR_ERR(reply);
1505 return genlmsg_reply(reply, info);
1508 static int odp_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1510 struct datapath *dp;
1511 int skip = cb->args[0];
1514 list_for_each_entry (dp, &dps, list_node) {
1517 if (odp_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1518 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1519 ODP_DP_CMD_NEW) < 0)
1529 static struct genl_ops dp_datapath_genl_ops[] = {
1530 { .cmd = ODP_DP_CMD_NEW,
1531 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1532 .policy = datapath_policy,
1533 .doit = odp_dp_cmd_new
1535 { .cmd = ODP_DP_CMD_DEL,
1536 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1537 .policy = datapath_policy,
1538 .doit = odp_dp_cmd_del
1540 { .cmd = ODP_DP_CMD_GET,
1541 .flags = 0, /* OK for unprivileged users. */
1542 .policy = datapath_policy,
1543 .doit = odp_dp_cmd_get,
1544 .dumpit = odp_dp_cmd_dump
1546 { .cmd = ODP_DP_CMD_SET,
1547 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1548 .policy = datapath_policy,
1549 .doit = odp_dp_cmd_set,
1553 static const struct nla_policy vport_policy[ODP_VPORT_ATTR_MAX + 1] = {
1554 #ifdef HAVE_NLA_NUL_STRING
1555 [ODP_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1556 [ODP_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1557 [ODP_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1558 [ODP_VPORT_ATTR_STATS] = { .len = sizeof(struct rtnl_link_stats64) },
1559 [ODP_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1561 [ODP_VPORT_ATTR_STATS] = { .minlen = sizeof(struct rtnl_link_stats64) },
1562 [ODP_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1564 [ODP_VPORT_ATTR_MTU] = { .type = NLA_U32 },
1565 [ODP_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1568 static struct genl_family dp_vport_genl_family = {
1569 .id = GENL_ID_GENERATE,
1570 .hdrsize = sizeof(struct odp_header),
1571 .name = ODP_VPORT_FAMILY,
1573 .maxattr = ODP_VPORT_ATTR_MAX
1576 static struct genl_multicast_group dp_vport_multicast_group = {
1577 .name = ODP_VPORT_MCGROUP
1580 /* Called with RTNL lock or RCU read lock. */
1581 static int odp_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1582 u32 pid, u32 seq, u32 flags, u8 cmd)
1584 struct odp_header *odp_header;
1586 int ifindex, iflink;
1589 odp_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1594 odp_header->dp_ifindex = vport->dp->dp_ifindex;
1596 NLA_PUT_U32(skb, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
1597 NLA_PUT_U32(skb, ODP_VPORT_ATTR_TYPE, vport_get_type(vport));
1598 NLA_PUT_STRING(skb, ODP_VPORT_ATTR_NAME, vport_get_name(vport));
1600 nla = nla_reserve(skb, ODP_VPORT_ATTR_STATS, sizeof(struct rtnl_link_stats64));
1602 goto nla_put_failure;
1603 if (vport_get_stats(vport, nla_data(nla)))
1604 __skb_trim(skb, skb->len - nla->nla_len);
1606 NLA_PUT(skb, ODP_VPORT_ATTR_ADDRESS, ETH_ALEN, vport_get_addr(vport));
1608 NLA_PUT_U32(skb, ODP_VPORT_ATTR_MTU, vport_get_mtu(vport));
1610 err = vport_get_options(vport, skb);
1611 if (err == -EMSGSIZE)
1614 ifindex = vport_get_ifindex(vport);
1616 NLA_PUT_U32(skb, ODP_VPORT_ATTR_IFINDEX, ifindex);
1618 iflink = vport_get_iflink(vport);
1620 NLA_PUT_U32(skb, ODP_VPORT_ATTR_IFLINK, iflink);
1622 return genlmsg_end(skb, odp_header);
1627 genlmsg_cancel(skb, odp_header);
1631 /* Called with RTNL lock or RCU read lock. */
1632 static struct sk_buff *odp_vport_cmd_build_info(struct vport *vport, u32 pid,
1635 struct sk_buff *skb;
1638 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1640 return ERR_PTR(-ENOMEM);
1642 retval = odp_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1645 return ERR_PTR(retval);
1650 static int odp_vport_cmd_validate(struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1652 return VERIFY_NUL_STRING(a[ODP_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1655 /* Called with RTNL lock or RCU read lock. */
1656 static struct vport *lookup_vport(struct odp_header *odp_header,
1657 struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1659 struct datapath *dp;
1660 struct vport *vport;
1662 if (a[ODP_VPORT_ATTR_NAME]) {
1663 vport = vport_locate(nla_data(a[ODP_VPORT_ATTR_NAME]));
1665 return ERR_PTR(-ENODEV);
1667 } else if (a[ODP_VPORT_ATTR_PORT_NO]) {
1668 u32 port_no = nla_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1670 if (port_no >= DP_MAX_PORTS)
1671 return ERR_PTR(-EFBIG);
1673 dp = get_dp(odp_header->dp_ifindex);
1675 return ERR_PTR(-ENODEV);
1677 vport = get_vport_protected(dp, port_no);
1679 return ERR_PTR(-ENOENT);
1682 return ERR_PTR(-EINVAL);
1685 /* Called with RTNL lock. */
1686 static int change_vport(struct vport *vport, struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1689 if (a[ODP_VPORT_ATTR_STATS])
1690 err = vport_set_stats(vport, nla_data(a[ODP_VPORT_ATTR_STATS]));
1691 if (!err && a[ODP_VPORT_ATTR_ADDRESS])
1692 err = vport_set_addr(vport, nla_data(a[ODP_VPORT_ATTR_ADDRESS]));
1693 if (!err && a[ODP_VPORT_ATTR_MTU])
1694 err = vport_set_mtu(vport, nla_get_u32(a[ODP_VPORT_ATTR_MTU]));
1698 static int odp_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1700 struct nlattr **a = info->attrs;
1701 struct odp_header *odp_header = info->userhdr;
1702 struct vport_parms parms;
1703 struct sk_buff *reply;
1704 struct vport *vport;
1705 struct datapath *dp;
1710 if (!a[ODP_VPORT_ATTR_NAME] || !a[ODP_VPORT_ATTR_TYPE])
1713 err = odp_vport_cmd_validate(a);
1718 dp = get_dp(odp_header->dp_ifindex);
1723 if (a[ODP_VPORT_ATTR_PORT_NO]) {
1724 port_no = nla_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1727 if (port_no >= DP_MAX_PORTS)
1730 vport = get_vport_protected(dp, port_no);
1735 for (port_no = 1; ; port_no++) {
1736 if (port_no >= DP_MAX_PORTS) {
1740 vport = get_vport_protected(dp, port_no);
1746 parms.name = nla_data(a[ODP_VPORT_ATTR_NAME]);
1747 parms.type = nla_get_u32(a[ODP_VPORT_ATTR_TYPE]);
1748 parms.options = a[ODP_VPORT_ATTR_OPTIONS];
1750 parms.port_no = port_no;
1752 vport = new_vport(&parms);
1753 err = PTR_ERR(vport);
1757 set_internal_devs_mtu(dp);
1758 dp_sysfs_add_if(vport);
1760 err = change_vport(vport, a);
1762 reply = odp_vport_cmd_build_info(vport, info->snd_pid,
1763 info->snd_seq, ODP_VPORT_CMD_NEW);
1765 err = PTR_ERR(reply);
1768 dp_detach_port(vport);
1771 genl_notify(reply, genl_info_net(info), info->snd_pid,
1772 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1781 static int odp_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1783 struct nlattr **a = info->attrs;
1784 struct sk_buff *reply;
1785 struct vport *vport;
1788 err = odp_vport_cmd_validate(a);
1793 vport = lookup_vport(info->userhdr, a);
1794 err = PTR_ERR(vport);
1799 if (a[ODP_VPORT_ATTR_OPTIONS])
1800 err = vport_set_options(vport, a[ODP_VPORT_ATTR_OPTIONS]);
1802 err = change_vport(vport, a);
1804 reply = odp_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1806 if (IS_ERR(reply)) {
1807 err = PTR_ERR(reply);
1808 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1809 dp_vport_multicast_group.id, err);
1813 genl_notify(reply, genl_info_net(info), info->snd_pid,
1814 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1822 static int odp_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1824 struct nlattr **a = info->attrs;
1825 struct sk_buff *reply;
1826 struct vport *vport;
1829 err = odp_vport_cmd_validate(a);
1834 vport = lookup_vport(info->userhdr, a);
1835 err = PTR_ERR(vport);
1839 if (vport->port_no == ODPP_LOCAL) {
1844 reply = odp_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1846 err = PTR_ERR(reply);
1850 err = dp_detach_port(vport);
1852 genl_notify(reply, genl_info_net(info), info->snd_pid,
1853 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1861 static int odp_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1863 struct nlattr **a = info->attrs;
1864 struct odp_header *odp_header = info->userhdr;
1865 struct sk_buff *reply;
1866 struct vport *vport;
1869 err = odp_vport_cmd_validate(a);
1874 vport = lookup_vport(odp_header, a);
1875 err = PTR_ERR(vport);
1879 reply = odp_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1881 err = PTR_ERR(reply);
1885 err = genlmsg_reply(reply, info);
1893 static int odp_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1895 struct odp_header *odp_header = genlmsg_data(nlmsg_data(cb->nlh));
1896 struct datapath *dp;
1900 dp = get_dp(odp_header->dp_ifindex);
1905 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1906 struct vport *vport;
1908 vport = get_vport_protected(dp, port_no);
1912 if (odp_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1913 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1914 ODP_VPORT_CMD_NEW) < 0)
1919 cb->args[0] = port_no;
1925 static struct genl_ops dp_vport_genl_ops[] = {
1926 { .cmd = ODP_VPORT_CMD_NEW,
1927 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1928 .policy = vport_policy,
1929 .doit = odp_vport_cmd_new
1931 { .cmd = ODP_VPORT_CMD_DEL,
1932 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1933 .policy = vport_policy,
1934 .doit = odp_vport_cmd_del
1936 { .cmd = ODP_VPORT_CMD_GET,
1937 .flags = 0, /* OK for unprivileged users. */
1938 .policy = vport_policy,
1939 .doit = odp_vport_cmd_get,
1940 .dumpit = odp_vport_cmd_dump
1942 { .cmd = ODP_VPORT_CMD_SET,
1943 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1944 .policy = vport_policy,
1945 .doit = odp_vport_cmd_set,
1949 struct genl_family_and_ops {
1950 struct genl_family *family;
1951 struct genl_ops *ops;
1953 struct genl_multicast_group *group;
1956 static const struct genl_family_and_ops dp_genl_families[] = {
1957 { &dp_datapath_genl_family,
1958 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1959 &dp_datapath_multicast_group },
1960 { &dp_vport_genl_family,
1961 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1962 &dp_vport_multicast_group },
1963 { &dp_flow_genl_family,
1964 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1965 &dp_flow_multicast_group },
1966 { &dp_packet_genl_family,
1967 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1971 static void dp_unregister_genl(int n_families)
1975 for (i = 0; i < n_families; i++) {
1976 genl_unregister_family(dp_genl_families[i].family);
1980 static int dp_register_genl(void)
1987 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1988 const struct genl_family_and_ops *f = &dp_genl_families[i];
1990 err = genl_register_family_with_ops(f->family, f->ops,
1997 err = genl_register_mc_group(f->family, f->group);
2003 err = packet_register_mc_groups();
2009 dp_unregister_genl(n_registered);
2013 static int __init dp_init(void)
2015 struct sk_buff *dummy_skb;
2018 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2020 printk("Open vSwitch %s, built "__DATE__" "__TIME__"\n", VERSION BUILDNR);
2028 goto error_flow_exit;
2030 err = register_netdevice_notifier(&dp_device_notifier);
2032 goto error_vport_exit;
2034 err = dp_register_genl();
2036 goto error_unreg_notifier;
2040 error_unreg_notifier:
2041 unregister_netdevice_notifier(&dp_device_notifier);
2050 static void dp_cleanup(void)
2053 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2054 unregister_netdevice_notifier(&dp_device_notifier);
2059 module_init(dp_init);
2060 module_exit(dp_cleanup);
2062 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2063 MODULE_LICENSE("GPL");