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"
55 #include "vport-internal_dev.h"
57 int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
58 EXPORT_SYMBOL(dp_ioctl_hook);
63 * Writes to device state (add/remove datapath, port, set operations on vports,
64 * etc.) are protected by RTNL.
66 * Writes to other state (flow table modifications, set miscellaneous datapath
67 * parameters such as drop frags, etc.) are protected by genl_mutex. The RTNL
68 * lock nests inside genl_mutex.
70 * Reads are protected by RCU.
72 * There are a few special cases (mostly stats) that have their own
73 * synchronization but they nest under all of above and don't interact with
77 /* Global list of datapaths to enable dumping them all out.
78 * Protected by genl_mutex.
80 static LIST_HEAD(dps);
82 static struct vport *new_vport(const struct vport_parms *);
83 static int queue_control_packets(struct datapath *, struct sk_buff *,
84 const struct dp_upcall_info *);
86 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
87 struct datapath *get_dp(int dp_ifindex)
89 struct datapath *dp = NULL;
90 struct net_device *dev;
93 dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
95 struct vport *vport = internal_dev_get_vport(dev);
103 EXPORT_SYMBOL_GPL(get_dp);
105 /* Must be called with genl_mutex. */
106 static struct tbl *get_table_protected(struct datapath *dp)
108 return rcu_dereference_protected(dp->table, lockdep_genl_is_held());
111 /* Must be called with rcu_read_lock or RTNL lock. */
112 static struct vport *get_vport_protected(struct datapath *dp, u16 port_no)
114 return rcu_dereference_rtnl(dp->ports[port_no]);
117 /* Must be called with rcu_read_lock or RTNL lock. */
118 const char *dp_name(const struct datapath *dp)
120 return vport_get_name(rcu_dereference_rtnl(dp->ports[ODPP_LOCAL]));
123 static inline size_t br_nlmsg_size(void)
125 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
126 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
127 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
128 + nla_total_size(4) /* IFLA_MASTER */
129 + nla_total_size(4) /* IFLA_MTU */
130 + nla_total_size(4) /* IFLA_LINK */
131 + nla_total_size(1); /* IFLA_OPERSTATE */
134 /* Caller must hold RTNL lock. */
135 static int dp_fill_ifinfo(struct sk_buff *skb,
136 const struct vport *port,
137 int event, unsigned int flags)
139 struct datapath *dp = port->dp;
140 int ifindex = vport_get_ifindex(port);
141 int iflink = vport_get_iflink(port);
142 struct ifinfomsg *hdr;
143 struct nlmsghdr *nlh;
151 nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
155 hdr = nlmsg_data(nlh);
156 hdr->ifi_family = AF_BRIDGE;
158 hdr->ifi_type = ARPHRD_ETHER;
159 hdr->ifi_index = ifindex;
160 hdr->ifi_flags = vport_get_flags(port);
163 NLA_PUT_STRING(skb, IFLA_IFNAME, vport_get_name(port));
164 NLA_PUT_U32(skb, IFLA_MASTER,
165 vport_get_ifindex(get_vport_protected(dp, ODPP_LOCAL)));
166 NLA_PUT_U32(skb, IFLA_MTU, vport_get_mtu(port));
167 #ifdef IFLA_OPERSTATE
168 NLA_PUT_U8(skb, IFLA_OPERSTATE,
169 vport_is_running(port)
170 ? vport_get_operstate(port)
174 NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, vport_get_addr(port));
176 if (ifindex != iflink)
177 NLA_PUT_U32(skb, IFLA_LINK,iflink);
179 return nlmsg_end(skb, nlh);
182 nlmsg_cancel(skb, nlh);
186 /* Caller must hold RTNL lock. */
187 static void dp_ifinfo_notify(int event, struct vport *port)
192 skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
196 err = dp_fill_ifinfo(skb, port, event, 0);
198 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
199 WARN_ON(err == -EMSGSIZE);
203 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
207 rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
210 static void release_dp(struct kobject *kobj)
212 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
216 static struct kobj_type dp_ktype = {
217 .release = release_dp
220 static void destroy_dp_rcu(struct rcu_head *rcu)
222 struct datapath *dp = container_of(rcu, struct datapath, rcu);
224 tbl_destroy((struct tbl __force *)dp->table, flow_free_tbl);
225 free_percpu(dp->stats_percpu);
226 kobject_put(&dp->ifobj);
229 /* Called with RTNL lock and genl_lock. */
230 static struct vport *new_vport(const struct vport_parms *parms)
234 vport = vport_add(parms);
235 if (!IS_ERR(vport)) {
236 struct datapath *dp = parms->dp;
238 rcu_assign_pointer(dp->ports[parms->port_no], vport);
239 list_add(&vport->node, &dp->port_list);
241 dp_ifinfo_notify(RTM_NEWLINK, vport);
247 /* Called with RTNL lock. */
248 int dp_detach_port(struct vport *p)
252 if (p->port_no != ODPP_LOCAL)
254 dp_ifinfo_notify(RTM_DELLINK, p);
256 /* First drop references to device. */
258 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
260 /* Then destroy it. */
264 /* Must be called with rcu_read_lock. */
265 void dp_process_received_packet(struct vport *p, struct sk_buff *skb)
267 struct datapath *dp = p->dp;
268 struct dp_stats_percpu *stats;
269 int stats_counter_off;
270 struct sw_flow_actions *acts;
271 struct loop_counter *loop;
274 OVS_CB(skb)->vport = p;
276 if (!OVS_CB(skb)->flow) {
277 struct sw_flow_key key;
278 struct tbl_node *flow_node;
281 /* Extract flow from 'skb' into 'key'. */
282 error = flow_extract(skb, p->port_no, &key, &is_frag);
283 if (unlikely(error)) {
288 if (is_frag && dp->drop_frags) {
290 stats_counter_off = offsetof(struct dp_stats_percpu, n_frags);
295 flow_node = tbl_lookup(rcu_dereference(dp->table), &key,
296 flow_hash(&key), flow_cmp);
297 if (unlikely(!flow_node)) {
298 struct dp_upcall_info upcall;
300 upcall.cmd = ODP_PACKET_CMD_MISS;
303 upcall.sample_pool = 0;
304 upcall.actions = NULL;
305 upcall.actions_len = 0;
306 dp_upcall(dp, skb, &upcall);
307 stats_counter_off = offsetof(struct dp_stats_percpu, n_missed);
311 OVS_CB(skb)->flow = flow_cast(flow_node);
314 stats_counter_off = offsetof(struct dp_stats_percpu, n_hit);
315 flow_used(OVS_CB(skb)->flow, skb);
317 acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
319 /* Check whether we've looped too much. */
320 loop = loop_get_counter();
321 if (unlikely(++loop->count > MAX_LOOPS))
322 loop->looping = true;
323 if (unlikely(loop->looping)) {
324 loop_suppress(dp, acts);
329 /* Execute actions. */
330 execute_actions(dp, skb, &OVS_CB(skb)->flow->key, acts->actions,
333 /* Check whether sub-actions looped too much. */
334 if (unlikely(loop->looping))
335 loop_suppress(dp, acts);
338 /* Decrement loop counter. */
340 loop->looping = false;
344 /* Update datapath statistics. */
346 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
348 write_seqcount_begin(&stats->seqlock);
349 (*(u64 *)((u8 *)stats + stats_counter_off))++;
350 write_seqcount_end(&stats->seqlock);
355 static void copy_and_csum_skb(struct sk_buff *skb, void *to)
357 u16 csum_start, csum_offset;
360 get_skb_csum_pointers(skb, &csum_start, &csum_offset);
361 csum_start -= skb_headroom(skb);
363 skb_copy_bits(skb, 0, to, csum_start);
365 csum = skb_copy_and_csum_bits(skb, csum_start, to + csum_start,
366 skb->len - csum_start, 0);
367 *(__sum16 *)(to + csum_start + csum_offset) = csum_fold(csum);
370 static struct genl_family dp_packet_genl_family = {
371 .id = GENL_ID_GENERATE,
372 .hdrsize = sizeof(struct odp_header),
373 .name = ODP_PACKET_FAMILY,
375 .maxattr = ODP_PACKET_ATTR_MAX
378 /* Generic Netlink multicast groups for upcalls.
380 * We really want three unique multicast groups per datapath, but we can't even
381 * get one, because genl_register_mc_group() takes genl_lock, which is also
382 * held during Generic Netlink message processing, so trying to acquire
383 * multicast groups during ODP_DP_NEW processing deadlocks. Instead, we
384 * preallocate a few groups and use them round-robin for datapaths. Collision
385 * isn't fatal--multicast listeners should check that the family is the one
386 * that they want and discard others--but it wastes time and memory to receive
389 #define PACKET_N_MC_GROUPS 16
390 static struct genl_multicast_group packet_mc_groups[PACKET_N_MC_GROUPS];
392 static u32 packet_mc_group(struct datapath *dp, u8 cmd)
395 BUILD_BUG_ON_NOT_POWER_OF_2(PACKET_N_MC_GROUPS);
397 idx = jhash_2words(dp->dp_ifindex, cmd, 0) & (PACKET_N_MC_GROUPS - 1);
398 return packet_mc_groups[idx].id;
401 static int packet_register_mc_groups(void)
405 for (i = 0; i < PACKET_N_MC_GROUPS; i++) {
406 struct genl_multicast_group *group = &packet_mc_groups[i];
409 sprintf(group->name, "packet%d", i);
410 error = genl_register_mc_group(&dp_packet_genl_family, group);
417 int dp_upcall(struct datapath *dp, struct sk_buff *skb, const struct dp_upcall_info *upcall_info)
419 struct dp_stats_percpu *stats;
422 WARN_ON_ONCE(skb_shared(skb));
424 forward_ip_summed(skb);
426 err = vswitch_skb_checksum_setup(skb);
430 /* Break apart GSO packets into their component pieces. Otherwise
431 * userspace may try to stuff a 64kB packet into a 1500-byte MTU. */
432 if (skb_is_gso(skb)) {
433 struct sk_buff *nskb = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
443 err = queue_control_packets(dp, skb, upcall_info);
453 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
455 write_seqcount_begin(&stats->seqlock);
457 write_seqcount_end(&stats->seqlock);
464 /* Send each packet in the 'skb' list to userspace for 'dp' as directed by
465 * 'upcall_info'. There will be only one packet unless we broke up a GSO
468 static int queue_control_packets(struct datapath *dp, struct sk_buff *skb,
469 const struct dp_upcall_info *upcall_info)
471 u32 group = packet_mc_group(dp, upcall_info->cmd);
472 struct sk_buff *nskb;
476 if (OVS_CB(skb)->vport)
477 port_no = OVS_CB(skb)->vport->port_no;
479 port_no = ODPP_LOCAL;
482 struct odp_header *upcall;
483 struct sk_buff *user_skb; /* to be queued to userspace */
490 err = vlan_deaccel_tag(skb);
494 if (nla_attr_size(skb->len) > USHRT_MAX)
497 len = sizeof(struct odp_header);
498 len += nla_total_size(skb->len);
499 len += nla_total_size(FLOW_BUFSIZE);
500 if (upcall_info->userdata)
501 len += nla_total_size(8);
502 if (upcall_info->sample_pool)
503 len += nla_total_size(4);
504 if (upcall_info->actions_len)
505 len += nla_total_size(upcall_info->actions_len);
507 user_skb = genlmsg_new(len, GFP_ATOMIC);
509 netlink_set_err(INIT_NET_GENL_SOCK, 0, group, -ENOBUFS);
513 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, 0, upcall_info->cmd);
514 upcall->dp_ifindex = dp->dp_ifindex;
516 nla = nla_nest_start(user_skb, ODP_PACKET_ATTR_KEY);
517 flow_to_nlattrs(upcall_info->key, user_skb);
518 nla_nest_end(user_skb, nla);
520 if (upcall_info->userdata)
521 nla_put_u64(user_skb, ODP_PACKET_ATTR_USERDATA, upcall_info->userdata);
522 if (upcall_info->sample_pool)
523 nla_put_u32(user_skb, ODP_PACKET_ATTR_SAMPLE_POOL, upcall_info->sample_pool);
524 if (upcall_info->actions_len) {
525 const struct nlattr *actions = upcall_info->actions;
526 u32 actions_len = upcall_info->actions_len;
528 nla = nla_nest_start(user_skb, ODP_PACKET_ATTR_ACTIONS);
529 memcpy(__skb_put(user_skb, actions_len), actions, actions_len);
530 nla_nest_end(user_skb, nla);
533 nla = __nla_reserve(user_skb, ODP_PACKET_ATTR_PACKET, skb->len);
534 if (skb->ip_summed == CHECKSUM_PARTIAL)
535 copy_and_csum_skb(skb, nla_data(nla));
537 skb_copy_bits(skb, 0, nla_data(nla), skb->len);
539 err = genlmsg_multicast(user_skb, 0, group, GFP_ATOMIC);
550 while ((skb = nskb) != NULL) {
557 /* Called with genl_mutex. */
558 static int flush_flows(int dp_ifindex)
560 struct tbl *old_table;
561 struct tbl *new_table;
564 dp = get_dp(dp_ifindex);
568 old_table = get_table_protected(dp);
569 new_table = tbl_create(TBL_MIN_BUCKETS);
573 rcu_assign_pointer(dp->table, new_table);
575 tbl_deferred_destroy(old_table, flow_free_tbl);
580 static int validate_actions(const struct nlattr *attr)
582 const struct nlattr *a;
585 nla_for_each_nested(a, attr, rem) {
586 static const u32 action_lens[ODP_ACTION_ATTR_MAX + 1] = {
587 [ODP_ACTION_ATTR_OUTPUT] = 4,
588 [ODP_ACTION_ATTR_CONTROLLER] = 8,
589 [ODP_ACTION_ATTR_SET_DL_TCI] = 2,
590 [ODP_ACTION_ATTR_STRIP_VLAN] = 0,
591 [ODP_ACTION_ATTR_SET_DL_SRC] = ETH_ALEN,
592 [ODP_ACTION_ATTR_SET_DL_DST] = ETH_ALEN,
593 [ODP_ACTION_ATTR_SET_NW_SRC] = 4,
594 [ODP_ACTION_ATTR_SET_NW_DST] = 4,
595 [ODP_ACTION_ATTR_SET_NW_TOS] = 1,
596 [ODP_ACTION_ATTR_SET_TP_SRC] = 2,
597 [ODP_ACTION_ATTR_SET_TP_DST] = 2,
598 [ODP_ACTION_ATTR_SET_TUNNEL] = 8,
599 [ODP_ACTION_ATTR_SET_PRIORITY] = 4,
600 [ODP_ACTION_ATTR_POP_PRIORITY] = 0,
601 [ODP_ACTION_ATTR_DROP_SPOOFED_ARP] = 0,
603 int type = nla_type(a);
605 if (type > ODP_ACTION_ATTR_MAX || nla_len(a) != action_lens[type])
609 case ODP_ACTION_ATTR_UNSPEC:
612 case ODP_ACTION_ATTR_CONTROLLER:
613 case ODP_ACTION_ATTR_STRIP_VLAN:
614 case ODP_ACTION_ATTR_SET_DL_SRC:
615 case ODP_ACTION_ATTR_SET_DL_DST:
616 case ODP_ACTION_ATTR_SET_NW_SRC:
617 case ODP_ACTION_ATTR_SET_NW_DST:
618 case ODP_ACTION_ATTR_SET_TP_SRC:
619 case ODP_ACTION_ATTR_SET_TP_DST:
620 case ODP_ACTION_ATTR_SET_TUNNEL:
621 case ODP_ACTION_ATTR_SET_PRIORITY:
622 case ODP_ACTION_ATTR_POP_PRIORITY:
623 case ODP_ACTION_ATTR_DROP_SPOOFED_ARP:
624 /* No validation needed. */
627 case ODP_ACTION_ATTR_OUTPUT:
628 if (nla_get_u32(a) >= DP_MAX_PORTS)
632 case ODP_ACTION_ATTR_SET_DL_TCI:
633 if (nla_get_be16(a) & htons(VLAN_CFI_MASK))
637 case ODP_ACTION_ATTR_SET_NW_TOS:
638 if (nla_get_u8(a) & INET_ECN_MASK)
652 static void clear_stats(struct sw_flow *flow)
656 flow->packet_count = 0;
657 flow->byte_count = 0;
660 /* Called with genl_mutex. */
661 static int expand_table(struct datapath *dp)
663 struct tbl *old_table = get_table_protected(dp);
664 struct tbl *new_table;
666 new_table = tbl_expand(old_table);
667 if (IS_ERR(new_table))
668 return PTR_ERR(new_table);
670 rcu_assign_pointer(dp->table, new_table);
671 tbl_deferred_destroy(old_table, NULL);
676 static int odp_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
678 struct odp_header *odp_header = info->userhdr;
679 struct nlattr **a = info->attrs;
680 struct sk_buff *packet;
681 struct sw_flow_key key;
688 if (!a[ODP_PACKET_ATTR_PACKET] || !a[ODP_PACKET_ATTR_ACTIONS] ||
689 nla_len(a[ODP_PACKET_ATTR_PACKET]) < ETH_HLEN)
692 err = validate_actions(a[ODP_PACKET_ATTR_ACTIONS]);
696 packet = skb_clone(skb, GFP_KERNEL);
700 packet->data = nla_data(a[ODP_PACKET_ATTR_PACKET]);
701 packet->len = nla_len(a[ODP_PACKET_ATTR_PACKET]);
703 skb_reset_mac_header(packet);
704 eth = eth_hdr(packet);
706 /* Normally, setting the skb 'protocol' field would be handled by a
707 * call to eth_type_trans(), but it assumes there's a sending
708 * device, which we may not have. */
709 if (ntohs(eth->h_proto) >= 1536)
710 packet->protocol = eth->h_proto;
712 packet->protocol = htons(ETH_P_802_2);
714 /* Initialize OVS_CB (it came from Netlink so might not be zeroed). */
715 memset(OVS_CB(packet), 0, sizeof(struct ovs_skb_cb));
717 err = flow_extract(packet, -1, &key, &is_frag);
722 dp = get_dp(odp_header->dp_ifindex);
726 err = execute_actions(dp, packet, &key,
727 nla_data(a[ODP_PACKET_ATTR_ACTIONS]),
728 nla_len(a[ODP_PACKET_ATTR_ACTIONS]));
740 static const struct nla_policy packet_policy[ODP_PACKET_ATTR_MAX + 1] = {
741 [ODP_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
742 [ODP_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
745 static struct genl_ops dp_packet_genl_ops[] = {
746 { .cmd = ODP_PACKET_CMD_EXECUTE,
747 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
748 .policy = packet_policy,
749 .doit = odp_packet_cmd_execute
753 static void get_dp_stats(struct datapath *dp, struct odp_stats *stats)
757 stats->n_frags = stats->n_hit = stats->n_missed = stats->n_lost = 0;
758 for_each_possible_cpu(i) {
759 const struct dp_stats_percpu *percpu_stats;
760 struct dp_stats_percpu local_stats;
763 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
766 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
767 local_stats = *percpu_stats;
768 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
770 stats->n_frags += local_stats.n_frags;
771 stats->n_hit += local_stats.n_hit;
772 stats->n_missed += local_stats.n_missed;
773 stats->n_lost += local_stats.n_lost;
777 /* MTU of the dp pseudo-device: ETH_DATA_LEN or the minimum of the ports.
778 * Called with RTNL lock.
780 int dp_min_mtu(const struct datapath *dp)
787 list_for_each_entry (p, &dp->port_list, node) {
790 /* Skip any internal ports, since that's what we're trying to
792 if (is_internal_vport(p))
795 dev_mtu = vport_get_mtu(p);
798 if (!mtu || dev_mtu < mtu)
802 return mtu ? mtu : ETH_DATA_LEN;
805 /* Sets the MTU of all datapath devices to the minimum of the ports
806 * Called with RTNL lock.
808 void set_internal_devs_mtu(const struct datapath *dp)
815 mtu = dp_min_mtu(dp);
817 list_for_each_entry (p, &dp->port_list, node) {
818 if (is_internal_vport(p))
819 vport_set_mtu(p, mtu);
823 static const struct nla_policy flow_policy[ODP_FLOW_ATTR_MAX + 1] = {
824 [ODP_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
825 [ODP_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
826 [ODP_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
829 static struct genl_family dp_flow_genl_family = {
830 .id = GENL_ID_GENERATE,
831 .hdrsize = sizeof(struct odp_header),
832 .name = ODP_FLOW_FAMILY,
834 .maxattr = ODP_FLOW_ATTR_MAX
837 static struct genl_multicast_group dp_flow_multicast_group = {
838 .name = ODP_FLOW_MCGROUP
841 /* Called with genl_lock. */
842 static int odp_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
843 struct sk_buff *skb, u32 pid, u32 seq, u32 flags, u8 cmd)
845 const int skb_orig_len = skb->len;
846 const struct sw_flow_actions *sf_acts;
847 struct odp_flow_stats stats;
848 struct odp_header *odp_header;
854 sf_acts = rcu_dereference_protected(flow->sf_acts,
855 lockdep_genl_is_held());
857 odp_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
861 odp_header->dp_ifindex = dp->dp_ifindex;
863 nla = nla_nest_start(skb, ODP_FLOW_ATTR_KEY);
865 goto nla_put_failure;
866 err = flow_to_nlattrs(&flow->key, skb);
869 nla_nest_end(skb, nla);
871 spin_lock_bh(&flow->lock);
873 stats.n_packets = flow->packet_count;
874 stats.n_bytes = flow->byte_count;
875 tcp_flags = flow->tcp_flags;
876 spin_unlock_bh(&flow->lock);
879 NLA_PUT_U64(skb, ODP_FLOW_ATTR_USED, flow_used_time(used));
882 NLA_PUT(skb, ODP_FLOW_ATTR_STATS, sizeof(struct odp_flow_stats), &stats);
885 NLA_PUT_U8(skb, ODP_FLOW_ATTR_TCP_FLAGS, tcp_flags);
887 /* If ODP_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
888 * this is the first flow to be dumped into 'skb'. This is unusual for
889 * Netlink but individual action lists can be longer than
890 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
891 * The userspace caller can always fetch the actions separately if it
892 * really wants them. (Most userspace callers in fact don't care.)
894 * This can only fail for dump operations because the skb is always
895 * properly sized for single flows.
897 err = nla_put(skb, ODP_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
899 if (err < 0 && skb_orig_len)
902 return genlmsg_end(skb, odp_header);
907 genlmsg_cancel(skb, odp_header);
911 static struct sk_buff *odp_flow_cmd_alloc_info(struct sw_flow *flow)
913 const struct sw_flow_actions *sf_acts;
916 sf_acts = rcu_dereference_protected(flow->sf_acts,
917 lockdep_genl_is_held());
919 len = nla_total_size(FLOW_BUFSIZE); /* ODP_FLOW_ATTR_KEY */
920 len += nla_total_size(sf_acts->actions_len); /* ODP_FLOW_ATTR_ACTIONS */
921 len += nla_total_size(sizeof(struct odp_flow_stats)); /* ODP_FLOW_ATTR_STATS */
922 len += nla_total_size(1); /* ODP_FLOW_ATTR_TCP_FLAGS */
923 len += nla_total_size(8); /* ODP_FLOW_ATTR_USED */
924 return genlmsg_new(NLMSG_ALIGN(sizeof(struct odp_header)) + len, GFP_KERNEL);
927 static struct sk_buff *odp_flow_cmd_build_info(struct sw_flow *flow, struct datapath *dp,
928 u32 pid, u32 seq, u8 cmd)
933 skb = odp_flow_cmd_alloc_info(flow);
935 return ERR_PTR(-ENOMEM);
937 retval = odp_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
942 static int odp_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
944 struct nlattr **a = info->attrs;
945 struct odp_header *odp_header = info->userhdr;
946 struct tbl_node *flow_node;
947 struct sw_flow_key key;
948 struct sw_flow *flow;
949 struct sk_buff *reply;
957 if (!a[ODP_FLOW_ATTR_KEY])
959 error = flow_from_nlattrs(&key, a[ODP_FLOW_ATTR_KEY]);
963 /* Validate actions. */
964 if (a[ODP_FLOW_ATTR_ACTIONS]) {
965 error = validate_actions(a[ODP_FLOW_ATTR_ACTIONS]);
968 } else if (info->genlhdr->cmd == ODP_FLOW_CMD_NEW) {
973 dp = get_dp(odp_header->dp_ifindex);
978 hash = flow_hash(&key);
979 table = get_table_protected(dp);
980 flow_node = tbl_lookup(table, &key, hash, flow_cmp);
982 struct sw_flow_actions *acts;
984 /* Bail out if we're not allowed to create a new flow. */
986 if (info->genlhdr->cmd == ODP_FLOW_CMD_SET)
989 /* Expand table, if necessary, to make room. */
990 if (tbl_count(table) >= tbl_n_buckets(table)) {
991 error = expand_table(dp);
994 table = get_table_protected(dp);
1000 error = PTR_ERR(flow);
1006 /* Obtain actions. */
1007 acts = flow_actions_alloc(a[ODP_FLOW_ATTR_ACTIONS]);
1008 error = PTR_ERR(acts);
1010 goto error_free_flow;
1011 rcu_assign_pointer(flow->sf_acts, acts);
1013 /* Put flow in bucket. */
1014 error = tbl_insert(table, &flow->tbl_node, hash);
1016 goto error_free_flow;
1018 reply = odp_flow_cmd_build_info(flow, dp, info->snd_pid,
1019 info->snd_seq, ODP_FLOW_CMD_NEW);
1021 /* We found a matching flow. */
1022 struct sw_flow_actions *old_acts;
1024 /* Bail out if we're not allowed to modify an existing flow.
1025 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1026 * because Generic Netlink treats the latter as a dump
1027 * request. We also accept NLM_F_EXCL in case that bug ever
1031 if (info->genlhdr->cmd == ODP_FLOW_CMD_NEW &&
1032 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1035 /* Update actions. */
1036 flow = flow_cast(flow_node);
1037 old_acts = rcu_dereference_protected(flow->sf_acts,
1038 lockdep_genl_is_held());
1039 if (a[ODP_FLOW_ATTR_ACTIONS] &&
1040 (old_acts->actions_len != nla_len(a[ODP_FLOW_ATTR_ACTIONS]) ||
1041 memcmp(old_acts->actions, nla_data(a[ODP_FLOW_ATTR_ACTIONS]),
1042 old_acts->actions_len))) {
1043 struct sw_flow_actions *new_acts;
1045 new_acts = flow_actions_alloc(a[ODP_FLOW_ATTR_ACTIONS]);
1046 error = PTR_ERR(new_acts);
1047 if (IS_ERR(new_acts))
1050 rcu_assign_pointer(flow->sf_acts, new_acts);
1051 flow_deferred_free_acts(old_acts);
1054 reply = odp_flow_cmd_build_info(flow, dp, info->snd_pid,
1055 info->snd_seq, ODP_FLOW_CMD_NEW);
1058 if (a[ODP_FLOW_ATTR_CLEAR]) {
1059 spin_lock_bh(&flow->lock);
1061 spin_unlock_bh(&flow->lock);
1066 genl_notify(reply, genl_info_net(info), info->snd_pid,
1067 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1069 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1070 dp_flow_multicast_group.id, PTR_ERR(reply));
1079 static int odp_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1081 struct nlattr **a = info->attrs;
1082 struct odp_header *odp_header = info->userhdr;
1083 struct sw_flow_key key;
1084 struct tbl_node *flow_node;
1085 struct sk_buff *reply;
1086 struct sw_flow *flow;
1087 struct datapath *dp;
1091 if (!a[ODP_FLOW_ATTR_KEY])
1093 err = flow_from_nlattrs(&key, a[ODP_FLOW_ATTR_KEY]);
1097 dp = get_dp(odp_header->dp_ifindex);
1101 table = get_table_protected(dp);
1102 flow_node = tbl_lookup(table, &key, flow_hash(&key), flow_cmp);
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;
1126 if (!a[ODP_FLOW_ATTR_KEY])
1127 return flush_flows(odp_header->dp_ifindex);
1128 err = flow_from_nlattrs(&key, a[ODP_FLOW_ATTR_KEY]);
1132 dp = get_dp(odp_header->dp_ifindex);
1136 table = get_table_protected(dp);
1137 flow_node = tbl_lookup(table, &key, flow_hash(&key), flow_cmp);
1140 flow = flow_cast(flow_node);
1142 reply = odp_flow_cmd_alloc_info(flow);
1146 err = tbl_remove(table, flow_node);
1152 err = odp_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1153 info->snd_seq, 0, ODP_FLOW_CMD_DEL);
1156 flow_deferred_free(flow);
1158 genl_notify(reply, genl_info_net(info), info->snd_pid,
1159 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1163 static int odp_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1165 struct odp_header *odp_header = genlmsg_data(nlmsg_data(cb->nlh));
1166 struct datapath *dp;
1168 dp = get_dp(odp_header->dp_ifindex);
1173 struct tbl_node *flow_node;
1174 struct sw_flow *flow;
1177 bucket = cb->args[0];
1179 flow_node = tbl_next(get_table_protected(dp), &bucket, &obj);
1183 flow = flow_cast(flow_node);
1184 if (odp_flow_cmd_fill_info(flow, dp, skb, NETLINK_CB(cb->skb).pid,
1185 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1186 ODP_FLOW_CMD_NEW) < 0)
1189 cb->args[0] = bucket;
1195 static struct genl_ops dp_flow_genl_ops[] = {
1196 { .cmd = ODP_FLOW_CMD_NEW,
1197 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1198 .policy = flow_policy,
1199 .doit = odp_flow_cmd_new_or_set
1201 { .cmd = ODP_FLOW_CMD_DEL,
1202 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1203 .policy = flow_policy,
1204 .doit = odp_flow_cmd_del
1206 { .cmd = ODP_FLOW_CMD_GET,
1207 .flags = 0, /* OK for unprivileged users. */
1208 .policy = flow_policy,
1209 .doit = odp_flow_cmd_get,
1210 .dumpit = odp_flow_cmd_dump
1212 { .cmd = ODP_FLOW_CMD_SET,
1213 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1214 .policy = flow_policy,
1215 .doit = odp_flow_cmd_new_or_set,
1219 static const struct nla_policy datapath_policy[ODP_DP_ATTR_MAX + 1] = {
1220 #ifdef HAVE_NLA_NUL_STRING
1221 [ODP_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1223 [ODP_DP_ATTR_IPV4_FRAGS] = { .type = NLA_U32 },
1224 [ODP_DP_ATTR_SAMPLING] = { .type = NLA_U32 },
1227 static struct genl_family dp_datapath_genl_family = {
1228 .id = GENL_ID_GENERATE,
1229 .hdrsize = sizeof(struct odp_header),
1230 .name = ODP_DATAPATH_FAMILY,
1232 .maxattr = ODP_DP_ATTR_MAX
1235 static struct genl_multicast_group dp_datapath_multicast_group = {
1236 .name = ODP_DATAPATH_MCGROUP
1239 static int odp_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1240 u32 pid, u32 seq, u32 flags, u8 cmd)
1242 struct odp_header *odp_header;
1246 odp_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1251 odp_header->dp_ifindex = dp->dp_ifindex;
1254 err = nla_put_string(skb, ODP_DP_ATTR_NAME, dp_name(dp));
1257 goto nla_put_failure;
1259 nla = nla_reserve(skb, ODP_DP_ATTR_STATS, sizeof(struct odp_stats));
1261 goto nla_put_failure;
1262 get_dp_stats(dp, nla_data(nla));
1264 NLA_PUT_U32(skb, ODP_DP_ATTR_IPV4_FRAGS,
1265 dp->drop_frags ? ODP_DP_FRAG_DROP : ODP_DP_FRAG_ZERO);
1267 if (dp->sflow_probability)
1268 NLA_PUT_U32(skb, ODP_DP_ATTR_SAMPLING, dp->sflow_probability);
1270 nla = nla_nest_start(skb, ODP_DP_ATTR_MCGROUPS);
1272 goto nla_put_failure;
1273 NLA_PUT_U32(skb, ODP_PACKET_CMD_MISS, packet_mc_group(dp, ODP_PACKET_CMD_MISS));
1274 NLA_PUT_U32(skb, ODP_PACKET_CMD_ACTION, packet_mc_group(dp, ODP_PACKET_CMD_ACTION));
1275 NLA_PUT_U32(skb, ODP_PACKET_CMD_SAMPLE, packet_mc_group(dp, ODP_PACKET_CMD_SAMPLE));
1276 nla_nest_end(skb, nla);
1278 return genlmsg_end(skb, odp_header);
1281 genlmsg_cancel(skb, odp_header);
1286 static struct sk_buff *odp_dp_cmd_build_info(struct datapath *dp, u32 pid,
1289 struct sk_buff *skb;
1292 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1294 return ERR_PTR(-ENOMEM);
1296 retval = odp_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1299 return ERR_PTR(retval);
1304 static int odp_dp_cmd_validate(struct nlattr *a[ODP_DP_ATTR_MAX + 1])
1306 if (a[ODP_DP_ATTR_IPV4_FRAGS]) {
1307 u32 frags = nla_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]);
1309 if (frags != ODP_DP_FRAG_ZERO && frags != ODP_DP_FRAG_DROP)
1313 return CHECK_NUL_STRING(a[ODP_DP_ATTR_NAME], IFNAMSIZ - 1);
1316 /* Called with genl_mutex and optionally with RTNL lock also. */
1317 static struct datapath *lookup_datapath(struct odp_header *odp_header, struct nlattr *a[ODP_DP_ATTR_MAX + 1])
1319 struct datapath *dp;
1321 if (!a[ODP_DP_ATTR_NAME])
1322 dp = get_dp(odp_header->dp_ifindex);
1324 struct vport *vport;
1327 vport = vport_locate(nla_data(a[ODP_DP_ATTR_NAME]));
1328 dp = vport && vport->port_no == ODPP_LOCAL ? vport->dp : NULL;
1331 return dp ? dp : ERR_PTR(-ENODEV);
1334 /* Called with genl_mutex. */
1335 static void change_datapath(struct datapath *dp, struct nlattr *a[ODP_DP_ATTR_MAX + 1])
1337 if (a[ODP_DP_ATTR_IPV4_FRAGS])
1338 dp->drop_frags = nla_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]) == ODP_DP_FRAG_DROP;
1339 if (a[ODP_DP_ATTR_SAMPLING])
1340 dp->sflow_probability = nla_get_u32(a[ODP_DP_ATTR_SAMPLING]);
1343 static int odp_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1345 struct nlattr **a = info->attrs;
1346 struct vport_parms parms;
1347 struct sk_buff *reply;
1348 struct datapath *dp;
1349 struct vport *vport;
1353 if (!a[ODP_DP_ATTR_NAME])
1356 err = odp_dp_cmd_validate(a);
1362 if (!try_module_get(THIS_MODULE))
1363 goto err_unlock_rtnl;
1366 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1368 goto err_put_module;
1369 INIT_LIST_HEAD(&dp->port_list);
1371 /* Initialize kobject for bridge. This will be added as
1372 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1373 dp->ifobj.kset = NULL;
1374 kobject_init(&dp->ifobj, &dp_ktype);
1376 /* Allocate table. */
1378 rcu_assign_pointer(dp->table, tbl_create(TBL_MIN_BUCKETS));
1382 /* Set up our datapath device. */
1383 parms.name = nla_data(a[ODP_DP_ATTR_NAME]);
1384 parms.type = ODP_VPORT_TYPE_INTERNAL;
1385 parms.options = NULL;
1387 parms.port_no = ODPP_LOCAL;
1388 vport = new_vport(&parms);
1389 if (IS_ERR(vport)) {
1390 err = PTR_ERR(vport);
1394 goto err_destroy_table;
1396 dp->dp_ifindex = vport_get_ifindex(vport);
1399 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1400 if (!dp->stats_percpu) {
1402 goto err_destroy_local_port;
1405 change_datapath(dp, a);
1407 reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
1408 err = PTR_ERR(reply);
1410 goto err_destroy_local_port;
1412 list_add_tail(&dp->list_node, &dps);
1413 dp_sysfs_add_dp(dp);
1417 genl_notify(reply, genl_info_net(info), info->snd_pid,
1418 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1421 err_destroy_local_port:
1422 dp_detach_port(get_vport_protected(dp, ODPP_LOCAL));
1424 tbl_destroy(get_table_protected(dp), NULL);
1428 module_put(THIS_MODULE);
1435 static int odp_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 = odp_dp_cmd_validate(info->attrs);
1447 dp = lookup_datapath(info->userhdr, info->attrs);
1452 reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_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 != ODPP_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, ODPP_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 odp_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1488 struct sk_buff *reply;
1489 struct datapath *dp;
1492 err = odp_dp_cmd_validate(info->attrs);
1496 dp = lookup_datapath(info->userhdr, info->attrs);
1500 change_datapath(dp, info->attrs);
1502 reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_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 odp_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1517 struct sk_buff *reply;
1518 struct datapath *dp;
1521 err = odp_dp_cmd_validate(info->attrs);
1525 dp = lookup_datapath(info->userhdr, info->attrs);
1529 reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
1531 return PTR_ERR(reply);
1533 return genlmsg_reply(reply, info);
1536 static int odp_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 (odp_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1546 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1547 ODP_DP_CMD_NEW) < 0)
1557 static struct genl_ops dp_datapath_genl_ops[] = {
1558 { .cmd = ODP_DP_CMD_NEW,
1559 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1560 .policy = datapath_policy,
1561 .doit = odp_dp_cmd_new
1563 { .cmd = ODP_DP_CMD_DEL,
1564 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1565 .policy = datapath_policy,
1566 .doit = odp_dp_cmd_del
1568 { .cmd = ODP_DP_CMD_GET,
1569 .flags = 0, /* OK for unprivileged users. */
1570 .policy = datapath_policy,
1571 .doit = odp_dp_cmd_get,
1572 .dumpit = odp_dp_cmd_dump
1574 { .cmd = ODP_DP_CMD_SET,
1575 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1576 .policy = datapath_policy,
1577 .doit = odp_dp_cmd_set,
1581 static const struct nla_policy vport_policy[ODP_VPORT_ATTR_MAX + 1] = {
1582 #ifdef HAVE_NLA_NUL_STRING
1583 [ODP_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1584 [ODP_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1585 [ODP_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1586 [ODP_VPORT_ATTR_STATS] = { .len = sizeof(struct rtnl_link_stats64) },
1587 [ODP_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1589 [ODP_VPORT_ATTR_STATS] = { .minlen = sizeof(struct rtnl_link_stats64) },
1590 [ODP_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1592 [ODP_VPORT_ATTR_MTU] = { .type = NLA_U32 },
1593 [ODP_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1596 static struct genl_family dp_vport_genl_family = {
1597 .id = GENL_ID_GENERATE,
1598 .hdrsize = sizeof(struct odp_header),
1599 .name = ODP_VPORT_FAMILY,
1601 .maxattr = ODP_VPORT_ATTR_MAX
1604 static struct genl_multicast_group dp_vport_multicast_group = {
1605 .name = ODP_VPORT_MCGROUP
1608 /* Called with RTNL lock or RCU read lock. */
1609 static int odp_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1610 u32 pid, u32 seq, u32 flags, u8 cmd)
1612 struct odp_header *odp_header;
1614 int ifindex, iflink;
1618 odp_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1623 odp_header->dp_ifindex = vport->dp->dp_ifindex;
1625 NLA_PUT_U32(skb, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
1626 NLA_PUT_U32(skb, ODP_VPORT_ATTR_TYPE, vport_get_type(vport));
1627 NLA_PUT_STRING(skb, ODP_VPORT_ATTR_NAME, vport_get_name(vport));
1629 nla = nla_reserve(skb, ODP_VPORT_ATTR_STATS, sizeof(struct rtnl_link_stats64));
1631 goto nla_put_failure;
1632 if (vport_get_stats(vport, nla_data(nla)))
1633 __skb_trim(skb, skb->len - nla->nla_len);
1635 NLA_PUT(skb, ODP_VPORT_ATTR_ADDRESS, ETH_ALEN, vport_get_addr(vport));
1637 mtu = vport_get_mtu(vport);
1639 NLA_PUT_U32(skb, ODP_VPORT_ATTR_MTU, mtu);
1641 err = vport_get_options(vport, skb);
1642 if (err == -EMSGSIZE)
1645 ifindex = vport_get_ifindex(vport);
1647 NLA_PUT_U32(skb, ODP_VPORT_ATTR_IFINDEX, ifindex);
1649 iflink = vport_get_iflink(vport);
1651 NLA_PUT_U32(skb, ODP_VPORT_ATTR_IFLINK, iflink);
1653 return genlmsg_end(skb, odp_header);
1658 genlmsg_cancel(skb, odp_header);
1662 /* Called with RTNL lock or RCU read lock. */
1663 static struct sk_buff *odp_vport_cmd_build_info(struct vport *vport, u32 pid,
1666 struct sk_buff *skb;
1669 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1671 return ERR_PTR(-ENOMEM);
1673 retval = odp_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1676 return ERR_PTR(retval);
1681 static int odp_vport_cmd_validate(struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1683 return CHECK_NUL_STRING(a[ODP_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1686 /* Called with RTNL lock or RCU read lock. */
1687 static struct vport *lookup_vport(struct odp_header *odp_header,
1688 struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1690 struct datapath *dp;
1691 struct vport *vport;
1693 if (a[ODP_VPORT_ATTR_NAME]) {
1694 vport = vport_locate(nla_data(a[ODP_VPORT_ATTR_NAME]));
1696 return ERR_PTR(-ENODEV);
1698 } else if (a[ODP_VPORT_ATTR_PORT_NO]) {
1699 u32 port_no = nla_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1701 if (port_no >= DP_MAX_PORTS)
1702 return ERR_PTR(-EFBIG);
1704 dp = get_dp(odp_header->dp_ifindex);
1706 return ERR_PTR(-ENODEV);
1708 vport = get_vport_protected(dp, port_no);
1710 return ERR_PTR(-ENOENT);
1713 return ERR_PTR(-EINVAL);
1716 /* Called with RTNL lock. */
1717 static int change_vport(struct vport *vport, struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1720 if (a[ODP_VPORT_ATTR_STATS])
1721 err = vport_set_stats(vport, nla_data(a[ODP_VPORT_ATTR_STATS]));
1722 if (!err && a[ODP_VPORT_ATTR_ADDRESS])
1723 err = vport_set_addr(vport, nla_data(a[ODP_VPORT_ATTR_ADDRESS]));
1724 if (!err && a[ODP_VPORT_ATTR_MTU])
1725 err = vport_set_mtu(vport, nla_get_u32(a[ODP_VPORT_ATTR_MTU]));
1729 static int odp_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1731 struct nlattr **a = info->attrs;
1732 struct odp_header *odp_header = info->userhdr;
1733 struct vport_parms parms;
1734 struct sk_buff *reply;
1735 struct vport *vport;
1736 struct datapath *dp;
1741 if (!a[ODP_VPORT_ATTR_NAME] || !a[ODP_VPORT_ATTR_TYPE])
1744 err = odp_vport_cmd_validate(a);
1749 dp = get_dp(odp_header->dp_ifindex);
1754 if (a[ODP_VPORT_ATTR_PORT_NO]) {
1755 port_no = nla_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1758 if (port_no >= DP_MAX_PORTS)
1761 vport = get_vport_protected(dp, port_no);
1766 for (port_no = 1; ; port_no++) {
1767 if (port_no >= DP_MAX_PORTS) {
1771 vport = get_vport_protected(dp, port_no);
1777 parms.name = nla_data(a[ODP_VPORT_ATTR_NAME]);
1778 parms.type = nla_get_u32(a[ODP_VPORT_ATTR_TYPE]);
1779 parms.options = a[ODP_VPORT_ATTR_OPTIONS];
1781 parms.port_no = port_no;
1783 vport = new_vport(&parms);
1784 err = PTR_ERR(vport);
1788 set_internal_devs_mtu(dp);
1789 dp_sysfs_add_if(vport);
1791 err = change_vport(vport, a);
1793 reply = odp_vport_cmd_build_info(vport, info->snd_pid,
1794 info->snd_seq, ODP_VPORT_CMD_NEW);
1796 err = PTR_ERR(reply);
1799 dp_detach_port(vport);
1802 genl_notify(reply, genl_info_net(info), info->snd_pid,
1803 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1812 static int odp_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1814 struct nlattr **a = info->attrs;
1815 struct sk_buff *reply;
1816 struct vport *vport;
1819 err = odp_vport_cmd_validate(a);
1824 vport = lookup_vport(info->userhdr, a);
1825 err = PTR_ERR(vport);
1830 if (a[ODP_VPORT_ATTR_OPTIONS])
1831 err = vport_set_options(vport, a[ODP_VPORT_ATTR_OPTIONS]);
1833 err = change_vport(vport, a);
1835 reply = odp_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1837 if (IS_ERR(reply)) {
1838 err = PTR_ERR(reply);
1839 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1840 dp_vport_multicast_group.id, err);
1844 genl_notify(reply, genl_info_net(info), info->snd_pid,
1845 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1853 static int odp_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1855 struct nlattr **a = info->attrs;
1856 struct sk_buff *reply;
1857 struct vport *vport;
1860 err = odp_vport_cmd_validate(a);
1865 vport = lookup_vport(info->userhdr, a);
1866 err = PTR_ERR(vport);
1870 if (vport->port_no == ODPP_LOCAL) {
1875 reply = odp_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1877 err = PTR_ERR(reply);
1881 err = dp_detach_port(vport);
1883 genl_notify(reply, genl_info_net(info), info->snd_pid,
1884 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1892 static int odp_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1894 struct nlattr **a = info->attrs;
1895 struct odp_header *odp_header = info->userhdr;
1896 struct sk_buff *reply;
1897 struct vport *vport;
1900 err = odp_vport_cmd_validate(a);
1905 vport = lookup_vport(odp_header, a);
1906 err = PTR_ERR(vport);
1910 reply = odp_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1912 err = PTR_ERR(reply);
1916 err = genlmsg_reply(reply, info);
1924 static int odp_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1926 struct odp_header *odp_header = genlmsg_data(nlmsg_data(cb->nlh));
1927 struct datapath *dp;
1931 dp = get_dp(odp_header->dp_ifindex);
1936 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1937 struct vport *vport;
1939 vport = get_vport_protected(dp, port_no);
1943 if (odp_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1944 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1945 ODP_VPORT_CMD_NEW) < 0)
1950 cb->args[0] = port_no;
1956 static struct genl_ops dp_vport_genl_ops[] = {
1957 { .cmd = ODP_VPORT_CMD_NEW,
1958 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1959 .policy = vport_policy,
1960 .doit = odp_vport_cmd_new
1962 { .cmd = ODP_VPORT_CMD_DEL,
1963 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1964 .policy = vport_policy,
1965 .doit = odp_vport_cmd_del
1967 { .cmd = ODP_VPORT_CMD_GET,
1968 .flags = 0, /* OK for unprivileged users. */
1969 .policy = vport_policy,
1970 .doit = odp_vport_cmd_get,
1971 .dumpit = odp_vport_cmd_dump
1973 { .cmd = ODP_VPORT_CMD_SET,
1974 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1975 .policy = vport_policy,
1976 .doit = odp_vport_cmd_set,
1980 struct genl_family_and_ops {
1981 struct genl_family *family;
1982 struct genl_ops *ops;
1984 struct genl_multicast_group *group;
1987 static const struct genl_family_and_ops dp_genl_families[] = {
1988 { &dp_datapath_genl_family,
1989 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1990 &dp_datapath_multicast_group },
1991 { &dp_vport_genl_family,
1992 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1993 &dp_vport_multicast_group },
1994 { &dp_flow_genl_family,
1995 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1996 &dp_flow_multicast_group },
1997 { &dp_packet_genl_family,
1998 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2002 static void dp_unregister_genl(int n_families)
2006 for (i = 0; i < n_families; i++) {
2007 genl_unregister_family(dp_genl_families[i].family);
2011 static int dp_register_genl(void)
2018 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2019 const struct genl_family_and_ops *f = &dp_genl_families[i];
2021 err = genl_register_family_with_ops(f->family, f->ops,
2028 err = genl_register_mc_group(f->family, f->group);
2034 err = packet_register_mc_groups();
2040 dp_unregister_genl(n_registered);
2044 static int __init dp_init(void)
2046 struct sk_buff *dummy_skb;
2049 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2051 printk("Open vSwitch %s, built "__DATE__" "__TIME__"\n", VERSION BUILDNR);
2059 goto error_flow_exit;
2061 err = register_netdevice_notifier(&dp_device_notifier);
2063 goto error_vport_exit;
2065 err = dp_register_genl();
2067 goto error_unreg_notifier;
2071 error_unreg_notifier:
2072 unregister_netdevice_notifier(&dp_device_notifier);
2081 static void dp_cleanup(void)
2084 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2085 unregister_netdevice_notifier(&dp_device_notifier);
2090 module_init(dp_init);
2091 module_exit(dp_cleanup);
2093 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2094 MODULE_LICENSE("GPL");