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 *);
82 static int queue_control_packets(struct datapath *, struct sk_buff *,
83 const struct dp_upcall_info *);
85 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
86 struct datapath *get_dp(int dp_ifindex)
88 struct datapath *dp = NULL;
89 struct net_device *dev;
92 dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
94 struct vport *vport = internal_dev_get_vport(dev);
102 EXPORT_SYMBOL_GPL(get_dp);
104 /* Must be called with genl_mutex. */
105 static struct tbl *get_table_protected(struct datapath *dp)
107 return rcu_dereference_protected(dp->table, lockdep_genl_is_held());
110 /* Must be called with rcu_read_lock or RTNL lock. */
111 static struct vport *get_vport_protected(struct datapath *dp, u16 port_no)
113 return rcu_dereference_rtnl(dp->ports[port_no]);
116 /* Must be called with rcu_read_lock or RTNL lock. */
117 const char *dp_name(const struct datapath *dp)
119 return vport_get_name(rcu_dereference_rtnl(dp->ports[ODPP_LOCAL]));
122 static inline size_t br_nlmsg_size(void)
124 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
125 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
126 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
127 + nla_total_size(4) /* IFLA_MASTER */
128 + nla_total_size(4) /* IFLA_MTU */
129 + nla_total_size(4) /* IFLA_LINK */
130 + nla_total_size(1); /* IFLA_OPERSTATE */
133 /* Caller must hold RTNL lock. */
134 static int dp_fill_ifinfo(struct sk_buff *skb,
135 const struct vport *port,
136 int event, unsigned int flags)
138 struct datapath *dp = port->dp;
139 int ifindex = vport_get_ifindex(port);
140 int iflink = vport_get_iflink(port);
141 struct ifinfomsg *hdr;
142 struct nlmsghdr *nlh;
150 nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
154 hdr = nlmsg_data(nlh);
155 hdr->ifi_family = AF_BRIDGE;
157 hdr->ifi_type = ARPHRD_ETHER;
158 hdr->ifi_index = ifindex;
159 hdr->ifi_flags = vport_get_flags(port);
162 NLA_PUT_STRING(skb, IFLA_IFNAME, vport_get_name(port));
163 NLA_PUT_U32(skb, IFLA_MASTER,
164 vport_get_ifindex(get_vport_protected(dp, ODPP_LOCAL)));
165 NLA_PUT_U32(skb, IFLA_MTU, vport_get_mtu(port));
166 #ifdef IFLA_OPERSTATE
167 NLA_PUT_U8(skb, IFLA_OPERSTATE,
168 vport_is_running(port)
169 ? vport_get_operstate(port)
173 NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, vport_get_addr(port));
175 if (ifindex != iflink)
176 NLA_PUT_U32(skb, IFLA_LINK,iflink);
178 return nlmsg_end(skb, nlh);
181 nlmsg_cancel(skb, nlh);
185 /* Caller must hold RTNL lock. */
186 static void dp_ifinfo_notify(int event, struct vport *port)
191 skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
195 err = dp_fill_ifinfo(skb, port, event, 0);
197 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
198 WARN_ON(err == -EMSGSIZE);
202 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
206 rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
209 static void release_dp(struct kobject *kobj)
211 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
215 static struct kobj_type dp_ktype = {
216 .release = release_dp
219 static void destroy_dp_rcu(struct rcu_head *rcu)
221 struct datapath *dp = container_of(rcu, struct datapath, rcu);
223 tbl_destroy((struct tbl __force *)dp->table, flow_free_tbl);
224 free_percpu(dp->stats_percpu);
225 kobject_put(&dp->ifobj);
228 /* Called with RTNL lock and genl_lock. */
229 static struct vport *new_vport(const struct vport_parms *parms)
233 vport = vport_add(parms);
234 if (!IS_ERR(vport)) {
235 struct datapath *dp = parms->dp;
237 rcu_assign_pointer(dp->ports[parms->port_no], vport);
238 list_add(&vport->node, &dp->port_list);
240 dp_ifinfo_notify(RTM_NEWLINK, vport);
246 /* Called with RTNL lock. */
247 int dp_detach_port(struct vport *p)
251 if (p->port_no != ODPP_LOCAL)
253 dp_ifinfo_notify(RTM_DELLINK, p);
255 /* First drop references to device. */
257 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
259 /* Then destroy it. */
263 /* Must be called with rcu_read_lock. */
264 void dp_process_received_packet(struct vport *p, struct sk_buff *skb)
266 struct datapath *dp = p->dp;
267 struct dp_stats_percpu *stats;
268 int stats_counter_off;
269 struct sw_flow_actions *acts;
270 struct loop_counter *loop;
273 OVS_CB(skb)->vport = p;
275 if (!OVS_CB(skb)->flow) {
276 struct sw_flow_key key;
277 struct tbl_node *flow_node;
280 /* Extract flow from 'skb' into 'key'. */
281 error = flow_extract(skb, p->port_no, &key, &is_frag);
282 if (unlikely(error)) {
287 if (is_frag && dp->drop_frags) {
289 stats_counter_off = offsetof(struct dp_stats_percpu, n_frags);
294 flow_node = tbl_lookup(rcu_dereference(dp->table), &key,
295 flow_hash(&key), flow_cmp);
296 if (unlikely(!flow_node)) {
297 struct dp_upcall_info upcall;
299 upcall.cmd = ODP_PACKET_CMD_MISS;
302 upcall.sample_pool = 0;
303 upcall.actions = NULL;
304 upcall.actions_len = 0;
305 dp_upcall(dp, skb, &upcall);
306 stats_counter_off = offsetof(struct dp_stats_percpu, n_missed);
310 OVS_CB(skb)->flow = flow_cast(flow_node);
313 stats_counter_off = offsetof(struct dp_stats_percpu, n_hit);
314 flow_used(OVS_CB(skb)->flow, skb);
316 acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
318 /* Check whether we've looped too much. */
319 loop = loop_get_counter();
320 if (unlikely(++loop->count > MAX_LOOPS))
321 loop->looping = true;
322 if (unlikely(loop->looping)) {
323 loop_suppress(dp, acts);
328 /* Execute actions. */
329 execute_actions(dp, skb, &OVS_CB(skb)->flow->key, acts->actions,
332 /* Check whether sub-actions looped too much. */
333 if (unlikely(loop->looping))
334 loop_suppress(dp, acts);
337 /* Decrement loop counter. */
339 loop->looping = false;
343 /* Update datapath statistics. */
345 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
347 write_seqcount_begin(&stats->seqlock);
348 (*(u64 *)((u8 *)stats + stats_counter_off))++;
349 write_seqcount_end(&stats->seqlock);
354 static void copy_and_csum_skb(struct sk_buff *skb, void *to)
356 u16 csum_start, csum_offset;
359 get_skb_csum_pointers(skb, &csum_start, &csum_offset);
360 csum_start -= skb_headroom(skb);
361 BUG_ON(csum_start >= skb_headlen(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 return queue_control_packets(dp, skb, upcall_info);
449 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
451 write_seqcount_begin(&stats->seqlock);
453 write_seqcount_end(&stats->seqlock);
460 /* Send each packet in the 'skb' list to userspace for 'dp' as directed by
461 * 'upcall_info'. There will be only one packet unless we broke up a GSO
464 static int queue_control_packets(struct datapath *dp, struct sk_buff *skb,
465 const struct dp_upcall_info *upcall_info)
467 u32 group = packet_mc_group(dp, upcall_info->cmd);
468 struct sk_buff *nskb;
472 if (OVS_CB(skb)->vport)
473 port_no = OVS_CB(skb)->vport->port_no;
475 port_no = ODPP_LOCAL;
478 struct odp_header *upcall;
479 struct sk_buff *user_skb; /* to be queued to userspace */
486 len = sizeof(struct odp_header);
487 len += nla_total_size(skb->len);
488 len += nla_total_size(FLOW_BUFSIZE);
489 if (upcall_info->userdata)
490 len += nla_total_size(8);
491 if (upcall_info->sample_pool)
492 len += nla_total_size(4);
493 if (upcall_info->actions_len)
494 len += nla_total_size(upcall_info->actions_len);
496 user_skb = genlmsg_new(len, GFP_ATOMIC);
498 netlink_set_err(INIT_NET_GENL_SOCK, 0, group, -ENOBUFS);
502 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, 0, upcall_info->cmd);
503 upcall->dp_ifindex = dp->dp_ifindex;
505 nla = nla_nest_start(user_skb, ODP_PACKET_ATTR_KEY);
506 flow_to_nlattrs(upcall_info->key, user_skb);
507 nla_nest_end(user_skb, nla);
509 if (upcall_info->userdata)
510 nla_put_u64(user_skb, ODP_PACKET_ATTR_USERDATA, upcall_info->userdata);
511 if (upcall_info->sample_pool)
512 nla_put_u32(user_skb, ODP_PACKET_ATTR_SAMPLE_POOL, upcall_info->sample_pool);
513 if (upcall_info->actions_len) {
514 const struct nlattr *actions = upcall_info->actions;
515 u32 actions_len = upcall_info->actions_len;
517 nla = nla_nest_start(user_skb, ODP_PACKET_ATTR_ACTIONS);
518 memcpy(__skb_put(user_skb, actions_len), actions, actions_len);
519 nla_nest_end(user_skb, nla);
522 nla = __nla_reserve(user_skb, ODP_PACKET_ATTR_PACKET, skb->len);
523 if (skb->ip_summed == CHECKSUM_PARTIAL)
524 copy_and_csum_skb(skb, nla_data(nla));
526 skb_copy_bits(skb, 0, nla_data(nla), skb->len);
528 err = genlmsg_multicast(user_skb, 0, group, GFP_ATOMIC);
539 while ((skb = nskb) != NULL) {
546 /* Called with genl_mutex. */
547 static int flush_flows(int dp_ifindex)
549 struct tbl *old_table;
550 struct tbl *new_table;
553 dp = get_dp(dp_ifindex);
557 old_table = get_table_protected(dp);
558 new_table = tbl_create(TBL_MIN_BUCKETS);
562 rcu_assign_pointer(dp->table, new_table);
564 tbl_deferred_destroy(old_table, flow_free_tbl);
569 static int validate_actions(const struct nlattr *attr)
571 const struct nlattr *a;
574 nla_for_each_nested(a, attr, rem) {
575 static const u32 action_lens[ODP_ACTION_ATTR_MAX + 1] = {
576 [ODP_ACTION_ATTR_OUTPUT] = 4,
577 [ODP_ACTION_ATTR_CONTROLLER] = 8,
578 [ODP_ACTION_ATTR_SET_DL_TCI] = 2,
579 [ODP_ACTION_ATTR_STRIP_VLAN] = 0,
580 [ODP_ACTION_ATTR_SET_DL_SRC] = ETH_ALEN,
581 [ODP_ACTION_ATTR_SET_DL_DST] = ETH_ALEN,
582 [ODP_ACTION_ATTR_SET_NW_SRC] = 4,
583 [ODP_ACTION_ATTR_SET_NW_DST] = 4,
584 [ODP_ACTION_ATTR_SET_NW_TOS] = 1,
585 [ODP_ACTION_ATTR_SET_TP_SRC] = 2,
586 [ODP_ACTION_ATTR_SET_TP_DST] = 2,
587 [ODP_ACTION_ATTR_SET_TUNNEL] = 8,
588 [ODP_ACTION_ATTR_SET_PRIORITY] = 4,
589 [ODP_ACTION_ATTR_POP_PRIORITY] = 0,
590 [ODP_ACTION_ATTR_DROP_SPOOFED_ARP] = 0,
592 int type = nla_type(a);
594 if (type > ODP_ACTION_ATTR_MAX || nla_len(a) != action_lens[type])
598 case ODP_ACTION_ATTR_UNSPEC:
601 case ODP_ACTION_ATTR_CONTROLLER:
602 case ODP_ACTION_ATTR_STRIP_VLAN:
603 case ODP_ACTION_ATTR_SET_DL_SRC:
604 case ODP_ACTION_ATTR_SET_DL_DST:
605 case ODP_ACTION_ATTR_SET_NW_SRC:
606 case ODP_ACTION_ATTR_SET_NW_DST:
607 case ODP_ACTION_ATTR_SET_TP_SRC:
608 case ODP_ACTION_ATTR_SET_TP_DST:
609 case ODP_ACTION_ATTR_SET_TUNNEL:
610 case ODP_ACTION_ATTR_SET_PRIORITY:
611 case ODP_ACTION_ATTR_POP_PRIORITY:
612 case ODP_ACTION_ATTR_DROP_SPOOFED_ARP:
613 /* No validation needed. */
616 case ODP_ACTION_ATTR_OUTPUT:
617 if (nla_get_u32(a) >= DP_MAX_PORTS)
621 case ODP_ACTION_ATTR_SET_DL_TCI:
622 if (nla_get_be16(a) & htons(VLAN_CFI_MASK))
626 case ODP_ACTION_ATTR_SET_NW_TOS:
627 if (nla_get_u8(a) & INET_ECN_MASK)
641 static void clear_stats(struct sw_flow *flow)
645 flow->packet_count = 0;
646 flow->byte_count = 0;
649 /* Called with genl_mutex. */
650 static int expand_table(struct datapath *dp)
652 struct tbl *old_table = get_table_protected(dp);
653 struct tbl *new_table;
655 new_table = tbl_expand(old_table);
656 if (IS_ERR(new_table))
657 return PTR_ERR(new_table);
659 rcu_assign_pointer(dp->table, new_table);
660 tbl_deferred_destroy(old_table, NULL);
665 static int odp_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
667 struct odp_header *odp_header = info->userhdr;
668 struct nlattr **a = info->attrs;
669 struct sk_buff *packet;
670 struct sw_flow_key key;
677 if (!a[ODP_PACKET_ATTR_PACKET] || !a[ODP_PACKET_ATTR_ACTIONS] ||
678 nla_len(a[ODP_PACKET_ATTR_PACKET]) < ETH_HLEN)
681 err = validate_actions(a[ODP_PACKET_ATTR_ACTIONS]);
685 packet = skb_clone(skb, GFP_KERNEL);
689 packet->data = nla_data(a[ODP_PACKET_ATTR_PACKET]);
690 packet->len = nla_len(a[ODP_PACKET_ATTR_PACKET]);
692 skb_reset_mac_header(packet);
693 eth = eth_hdr(packet);
695 /* Normally, setting the skb 'protocol' field would be handled by a
696 * call to eth_type_trans(), but it assumes there's a sending
697 * device, which we may not have. */
698 if (ntohs(eth->h_proto) >= 1536)
699 packet->protocol = eth->h_proto;
701 packet->protocol = htons(ETH_P_802_2);
703 err = flow_extract(packet, -1, &key, &is_frag);
708 dp = get_dp(odp_header->dp_ifindex);
711 err = execute_actions(dp, packet, &key,
712 nla_data(a[ODP_PACKET_ATTR_ACTIONS]),
713 nla_len(a[ODP_PACKET_ATTR_ACTIONS]));
720 static const struct nla_policy packet_policy[ODP_PACKET_ATTR_MAX + 1] = {
721 [ODP_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
722 [ODP_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
725 static struct genl_ops dp_packet_genl_ops[] = {
726 { .cmd = ODP_PACKET_CMD_EXECUTE,
727 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
728 .policy = packet_policy,
729 .doit = odp_packet_cmd_execute
733 static void get_dp_stats(struct datapath *dp, struct odp_stats *stats)
737 stats->n_frags = stats->n_hit = stats->n_missed = stats->n_lost = 0;
738 for_each_possible_cpu(i) {
739 const struct dp_stats_percpu *percpu_stats;
740 struct dp_stats_percpu local_stats;
743 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
746 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
747 local_stats = *percpu_stats;
748 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
750 stats->n_frags += local_stats.n_frags;
751 stats->n_hit += local_stats.n_hit;
752 stats->n_missed += local_stats.n_missed;
753 stats->n_lost += local_stats.n_lost;
757 /* MTU of the dp pseudo-device: ETH_DATA_LEN or the minimum of the ports.
758 * Called with RTNL lock.
760 int dp_min_mtu(const struct datapath *dp)
767 list_for_each_entry (p, &dp->port_list, node) {
770 /* Skip any internal ports, since that's what we're trying to
772 if (is_internal_vport(p))
775 dev_mtu = vport_get_mtu(p);
776 if (!mtu || dev_mtu < mtu)
780 return mtu ? mtu : ETH_DATA_LEN;
783 /* Sets the MTU of all datapath devices to the minimum of the ports
784 * Called with RTNL lock.
786 void set_internal_devs_mtu(const struct datapath *dp)
793 mtu = dp_min_mtu(dp);
795 list_for_each_entry (p, &dp->port_list, node) {
796 if (is_internal_vport(p))
797 vport_set_mtu(p, mtu);
801 static const struct nla_policy flow_policy[ODP_FLOW_ATTR_MAX + 1] = {
802 [ODP_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
803 [ODP_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
804 [ODP_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
807 static struct genl_family dp_flow_genl_family = {
808 .id = GENL_ID_GENERATE,
809 .hdrsize = sizeof(struct odp_header),
810 .name = ODP_FLOW_FAMILY,
812 .maxattr = ODP_FLOW_ATTR_MAX
815 static struct genl_multicast_group dp_flow_multicast_group = {
816 .name = ODP_FLOW_MCGROUP
819 /* Called with genl_lock. */
820 static int odp_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
821 struct sk_buff *skb, u32 pid, u32 seq, u32 flags, u8 cmd)
823 const int skb_orig_len = skb->len;
824 const struct sw_flow_actions *sf_acts;
825 struct odp_flow_stats stats;
826 struct odp_header *odp_header;
832 sf_acts = rcu_dereference_protected(flow->sf_acts,
833 lockdep_genl_is_held());
835 odp_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
839 odp_header->dp_ifindex = dp->dp_ifindex;
841 nla = nla_nest_start(skb, ODP_FLOW_ATTR_KEY);
843 goto nla_put_failure;
844 err = flow_to_nlattrs(&flow->key, skb);
847 nla_nest_end(skb, nla);
849 spin_lock_bh(&flow->lock);
851 stats.n_packets = flow->packet_count;
852 stats.n_bytes = flow->byte_count;
853 tcp_flags = flow->tcp_flags;
854 spin_unlock_bh(&flow->lock);
857 NLA_PUT_U64(skb, ODP_FLOW_ATTR_USED, flow_used_time(used));
860 NLA_PUT(skb, ODP_FLOW_ATTR_STATS, sizeof(struct odp_flow_stats), &stats);
863 NLA_PUT_U8(skb, ODP_FLOW_ATTR_TCP_FLAGS, tcp_flags);
865 /* If ODP_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
866 * this is the first flow to be dumped into 'skb'. This is unusual for
867 * Netlink but individual action lists can be longer than
868 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
869 * The userspace caller can always fetch the actions separately if it
870 * really wants them. (Most userspace callers in fact don't care.)
872 * This can only fail for dump operations because the skb is always
873 * properly sized for single flows.
875 err = nla_put(skb, ODP_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
877 if (err < 0 && skb_orig_len)
880 return genlmsg_end(skb, odp_header);
885 genlmsg_cancel(skb, odp_header);
889 static struct sk_buff *odp_flow_cmd_alloc_info(struct sw_flow *flow)
891 const struct sw_flow_actions *sf_acts;
894 sf_acts = rcu_dereference_protected(flow->sf_acts,
895 lockdep_genl_is_held());
897 len = nla_total_size(FLOW_BUFSIZE); /* ODP_FLOW_ATTR_KEY */
898 len += nla_total_size(sf_acts->actions_len); /* ODP_FLOW_ATTR_ACTIONS */
899 len += nla_total_size(sizeof(struct odp_flow_stats)); /* ODP_FLOW_ATTR_STATS */
900 len += nla_total_size(1); /* ODP_FLOW_ATTR_TCP_FLAGS */
901 len += nla_total_size(8); /* ODP_FLOW_ATTR_USED */
902 return genlmsg_new(NLMSG_ALIGN(sizeof(struct odp_header)) + len, GFP_KERNEL);
905 static struct sk_buff *odp_flow_cmd_build_info(struct sw_flow *flow, struct datapath *dp,
906 u32 pid, u32 seq, u8 cmd)
911 skb = odp_flow_cmd_alloc_info(flow);
913 return ERR_PTR(-ENOMEM);
915 retval = odp_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
920 static int odp_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
922 struct nlattr **a = info->attrs;
923 struct odp_header *odp_header = info->userhdr;
924 struct tbl_node *flow_node;
925 struct sw_flow_key key;
926 struct sw_flow *flow;
927 struct sk_buff *reply;
935 if (!a[ODP_FLOW_ATTR_KEY])
937 error = flow_from_nlattrs(&key, a[ODP_FLOW_ATTR_KEY]);
941 /* Validate actions. */
942 if (a[ODP_FLOW_ATTR_ACTIONS]) {
943 error = validate_actions(a[ODP_FLOW_ATTR_ACTIONS]);
946 } else if (info->genlhdr->cmd == ODP_FLOW_CMD_NEW) {
951 dp = get_dp(odp_header->dp_ifindex);
956 hash = flow_hash(&key);
957 table = get_table_protected(dp);
958 flow_node = tbl_lookup(table, &key, hash, flow_cmp);
960 struct sw_flow_actions *acts;
962 /* Bail out if we're not allowed to create a new flow. */
964 if (info->genlhdr->cmd == ODP_FLOW_CMD_SET)
967 /* Expand table, if necessary, to make room. */
968 if (tbl_count(table) >= tbl_n_buckets(table)) {
969 error = expand_table(dp);
972 table = get_table_protected(dp);
978 error = PTR_ERR(flow);
984 /* Obtain actions. */
985 acts = flow_actions_alloc(a[ODP_FLOW_ATTR_ACTIONS]);
986 error = PTR_ERR(acts);
988 goto error_free_flow;
989 rcu_assign_pointer(flow->sf_acts, acts);
991 /* Put flow in bucket. */
992 error = tbl_insert(table, &flow->tbl_node, hash);
994 goto error_free_flow;
996 reply = odp_flow_cmd_build_info(flow, dp, info->snd_pid,
997 info->snd_seq, ODP_FLOW_CMD_NEW);
999 /* We found a matching flow. */
1000 struct sw_flow_actions *old_acts;
1002 /* Bail out if we're not allowed to modify an existing flow.
1003 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1004 * because Generic Netlink treats the latter as a dump
1005 * request. We also accept NLM_F_EXCL in case that bug ever
1009 if (info->genlhdr->cmd == ODP_FLOW_CMD_NEW &&
1010 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1013 /* Update actions. */
1014 flow = flow_cast(flow_node);
1015 old_acts = rcu_dereference_protected(flow->sf_acts,
1016 lockdep_genl_is_held());
1017 if (a[ODP_FLOW_ATTR_ACTIONS] &&
1018 (old_acts->actions_len != nla_len(a[ODP_FLOW_ATTR_ACTIONS]) ||
1019 memcmp(old_acts->actions, nla_data(a[ODP_FLOW_ATTR_ACTIONS]),
1020 old_acts->actions_len))) {
1021 struct sw_flow_actions *new_acts;
1023 new_acts = flow_actions_alloc(a[ODP_FLOW_ATTR_ACTIONS]);
1024 error = PTR_ERR(new_acts);
1025 if (IS_ERR(new_acts))
1028 rcu_assign_pointer(flow->sf_acts, new_acts);
1029 flow_deferred_free_acts(old_acts);
1032 reply = odp_flow_cmd_build_info(flow, dp, info->snd_pid,
1033 info->snd_seq, ODP_FLOW_CMD_NEW);
1036 if (a[ODP_FLOW_ATTR_CLEAR]) {
1037 spin_lock_bh(&flow->lock);
1039 spin_unlock_bh(&flow->lock);
1044 genl_notify(reply, genl_info_net(info), info->snd_pid,
1045 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1047 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1048 dp_flow_multicast_group.id, PTR_ERR(reply));
1057 static int odp_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1059 struct nlattr **a = info->attrs;
1060 struct odp_header *odp_header = info->userhdr;
1061 struct sw_flow_key key;
1062 struct tbl_node *flow_node;
1063 struct sk_buff *reply;
1064 struct sw_flow *flow;
1065 struct datapath *dp;
1069 if (!a[ODP_FLOW_ATTR_KEY])
1071 err = flow_from_nlattrs(&key, a[ODP_FLOW_ATTR_KEY]);
1075 dp = get_dp(odp_header->dp_ifindex);
1079 table = get_table_protected(dp);
1080 flow_node = tbl_lookup(table, &key, flow_hash(&key), flow_cmp);
1084 flow = flow_cast(flow_node);
1085 reply = odp_flow_cmd_build_info(flow, dp, info->snd_pid, info->snd_seq, ODP_FLOW_CMD_NEW);
1087 return PTR_ERR(reply);
1089 return genlmsg_reply(reply, info);
1092 static int odp_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1094 struct nlattr **a = info->attrs;
1095 struct odp_header *odp_header = info->userhdr;
1096 struct sw_flow_key key;
1097 struct tbl_node *flow_node;
1098 struct sk_buff *reply;
1099 struct sw_flow *flow;
1100 struct datapath *dp;
1104 if (!a[ODP_FLOW_ATTR_KEY])
1105 return flush_flows(odp_header->dp_ifindex);
1106 err = flow_from_nlattrs(&key, a[ODP_FLOW_ATTR_KEY]);
1110 dp = get_dp(odp_header->dp_ifindex);
1114 table = get_table_protected(dp);
1115 flow_node = tbl_lookup(table, &key, flow_hash(&key), flow_cmp);
1118 flow = flow_cast(flow_node);
1120 reply = odp_flow_cmd_alloc_info(flow);
1124 err = tbl_remove(table, flow_node);
1130 err = odp_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1131 info->snd_seq, 0, ODP_FLOW_CMD_DEL);
1134 flow_deferred_free(flow);
1136 genl_notify(reply, genl_info_net(info), info->snd_pid,
1137 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1141 static int odp_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1143 struct odp_header *odp_header = genlmsg_data(nlmsg_data(cb->nlh));
1144 struct datapath *dp;
1146 dp = get_dp(odp_header->dp_ifindex);
1151 struct tbl_node *flow_node;
1152 struct sw_flow *flow;
1155 bucket = cb->args[0];
1157 flow_node = tbl_next(get_table_protected(dp), &bucket, &obj);
1161 flow = flow_cast(flow_node);
1162 if (odp_flow_cmd_fill_info(flow, dp, skb, NETLINK_CB(cb->skb).pid,
1163 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1164 ODP_FLOW_CMD_NEW) < 0)
1167 cb->args[0] = bucket;
1173 static struct genl_ops dp_flow_genl_ops[] = {
1174 { .cmd = ODP_FLOW_CMD_NEW,
1175 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1176 .policy = flow_policy,
1177 .doit = odp_flow_cmd_new_or_set
1179 { .cmd = ODP_FLOW_CMD_DEL,
1180 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1181 .policy = flow_policy,
1182 .doit = odp_flow_cmd_del
1184 { .cmd = ODP_FLOW_CMD_GET,
1185 .flags = 0, /* OK for unprivileged users. */
1186 .policy = flow_policy,
1187 .doit = odp_flow_cmd_get,
1188 .dumpit = odp_flow_cmd_dump
1190 { .cmd = ODP_FLOW_CMD_SET,
1191 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1192 .policy = flow_policy,
1193 .doit = odp_flow_cmd_new_or_set,
1197 static const struct nla_policy datapath_policy[ODP_DP_ATTR_MAX + 1] = {
1198 #ifdef HAVE_NLA_NUL_STRING
1199 [ODP_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1201 [ODP_DP_ATTR_IPV4_FRAGS] = { .type = NLA_U32 },
1202 [ODP_DP_ATTR_SAMPLING] = { .type = NLA_U32 },
1205 static struct genl_family dp_datapath_genl_family = {
1206 .id = GENL_ID_GENERATE,
1207 .hdrsize = sizeof(struct odp_header),
1208 .name = ODP_DATAPATH_FAMILY,
1210 .maxattr = ODP_DP_ATTR_MAX
1213 static struct genl_multicast_group dp_datapath_multicast_group = {
1214 .name = ODP_DATAPATH_MCGROUP
1217 static int odp_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1218 u32 pid, u32 seq, u32 flags, u8 cmd)
1220 struct odp_header *odp_header;
1224 odp_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1229 odp_header->dp_ifindex = dp->dp_ifindex;
1232 err = nla_put_string(skb, ODP_DP_ATTR_NAME, dp_name(dp));
1235 goto nla_put_failure;
1237 nla = nla_reserve(skb, ODP_DP_ATTR_STATS, sizeof(struct odp_stats));
1239 goto nla_put_failure;
1240 get_dp_stats(dp, nla_data(nla));
1242 NLA_PUT_U32(skb, ODP_DP_ATTR_IPV4_FRAGS,
1243 dp->drop_frags ? ODP_DP_FRAG_DROP : ODP_DP_FRAG_ZERO);
1245 if (dp->sflow_probability)
1246 NLA_PUT_U32(skb, ODP_DP_ATTR_SAMPLING, dp->sflow_probability);
1248 nla = nla_nest_start(skb, ODP_DP_ATTR_MCGROUPS);
1250 goto nla_put_failure;
1251 NLA_PUT_U32(skb, ODP_PACKET_CMD_MISS, packet_mc_group(dp, ODP_PACKET_CMD_MISS));
1252 NLA_PUT_U32(skb, ODP_PACKET_CMD_ACTION, packet_mc_group(dp, ODP_PACKET_CMD_ACTION));
1253 NLA_PUT_U32(skb, ODP_PACKET_CMD_SAMPLE, packet_mc_group(dp, ODP_PACKET_CMD_SAMPLE));
1254 nla_nest_end(skb, nla);
1256 return genlmsg_end(skb, odp_header);
1259 genlmsg_cancel(skb, odp_header);
1264 static struct sk_buff *odp_dp_cmd_build_info(struct datapath *dp, u32 pid,
1267 struct sk_buff *skb;
1270 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1272 return ERR_PTR(-ENOMEM);
1274 retval = odp_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1277 return ERR_PTR(retval);
1282 static int odp_dp_cmd_validate(struct nlattr *a[ODP_DP_ATTR_MAX + 1])
1284 if (a[ODP_DP_ATTR_IPV4_FRAGS]) {
1285 u32 frags = nla_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]);
1287 if (frags != ODP_DP_FRAG_ZERO && frags != ODP_DP_FRAG_DROP)
1291 return VERIFY_NUL_STRING(a[ODP_DP_ATTR_NAME], IFNAMSIZ - 1);
1294 /* Called with genl_mutex and optionally with RTNL lock also. */
1295 static struct datapath *lookup_datapath(struct odp_header *odp_header, struct nlattr *a[ODP_DP_ATTR_MAX + 1])
1297 struct datapath *dp;
1299 if (!a[ODP_DP_ATTR_NAME])
1300 dp = get_dp(odp_header->dp_ifindex);
1302 struct vport *vport;
1305 vport = vport_locate(nla_data(a[ODP_DP_ATTR_NAME]));
1306 dp = vport && vport->port_no == ODPP_LOCAL ? vport->dp : NULL;
1309 return dp ? dp : ERR_PTR(-ENODEV);
1312 /* Called with genl_mutex. */
1313 static void change_datapath(struct datapath *dp, struct nlattr *a[ODP_DP_ATTR_MAX + 1])
1315 if (a[ODP_DP_ATTR_IPV4_FRAGS])
1316 dp->drop_frags = nla_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]) == ODP_DP_FRAG_DROP;
1317 if (a[ODP_DP_ATTR_SAMPLING])
1318 dp->sflow_probability = nla_get_u32(a[ODP_DP_ATTR_SAMPLING]);
1321 static int odp_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1323 struct nlattr **a = info->attrs;
1324 struct vport_parms parms;
1325 struct sk_buff *reply;
1326 struct datapath *dp;
1327 struct vport *vport;
1331 if (!a[ODP_DP_ATTR_NAME])
1334 err = odp_dp_cmd_validate(a);
1340 if (!try_module_get(THIS_MODULE))
1341 goto err_unlock_rtnl;
1344 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1346 goto err_put_module;
1347 INIT_LIST_HEAD(&dp->port_list);
1349 /* Initialize kobject for bridge. This will be added as
1350 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1351 dp->ifobj.kset = NULL;
1352 kobject_init(&dp->ifobj, &dp_ktype);
1354 /* Allocate table. */
1356 rcu_assign_pointer(dp->table, tbl_create(TBL_MIN_BUCKETS));
1360 /* Set up our datapath device. */
1361 parms.name = nla_data(a[ODP_DP_ATTR_NAME]);
1362 parms.type = ODP_VPORT_TYPE_INTERNAL;
1363 parms.options = NULL;
1365 parms.port_no = ODPP_LOCAL;
1366 vport = new_vport(&parms);
1367 if (IS_ERR(vport)) {
1368 err = PTR_ERR(vport);
1372 goto err_destroy_table;
1374 dp->dp_ifindex = vport_get_ifindex(vport);
1377 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1378 if (!dp->stats_percpu) {
1380 goto err_destroy_local_port;
1383 change_datapath(dp, a);
1385 reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
1386 err = PTR_ERR(reply);
1388 goto err_destroy_local_port;
1390 list_add_tail(&dp->list_node, &dps);
1391 dp_sysfs_add_dp(dp);
1395 genl_notify(reply, genl_info_net(info), info->snd_pid,
1396 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1399 err_destroy_local_port:
1400 dp_detach_port(get_vport_protected(dp, ODPP_LOCAL));
1402 tbl_destroy(get_table_protected(dp), NULL);
1406 module_put(THIS_MODULE);
1413 static int odp_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1415 struct vport *vport, *next_vport;
1416 struct sk_buff *reply;
1417 struct datapath *dp;
1420 err = odp_dp_cmd_validate(info->attrs);
1425 dp = lookup_datapath(info->userhdr, info->attrs);
1430 reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_DEL);
1431 err = PTR_ERR(reply);
1435 list_for_each_entry_safe (vport, next_vport, &dp->port_list, node)
1436 if (vport->port_no != ODPP_LOCAL)
1437 dp_detach_port(vport);
1439 dp_sysfs_del_dp(dp);
1440 list_del(&dp->list_node);
1441 dp_detach_port(get_vport_protected(dp, ODPP_LOCAL));
1443 call_rcu(&dp->rcu, destroy_dp_rcu);
1444 module_put(THIS_MODULE);
1446 genl_notify(reply, genl_info_net(info), info->snd_pid,
1447 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1456 static int odp_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1458 struct sk_buff *reply;
1459 struct datapath *dp;
1462 err = odp_dp_cmd_validate(info->attrs);
1466 dp = lookup_datapath(info->userhdr, info->attrs);
1470 change_datapath(dp, info->attrs);
1472 reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
1473 if (IS_ERR(reply)) {
1474 err = PTR_ERR(reply);
1475 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1476 dp_datapath_multicast_group.id, err);
1480 genl_notify(reply, genl_info_net(info), info->snd_pid,
1481 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1485 static int odp_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1487 struct sk_buff *reply;
1488 struct datapath *dp;
1491 err = odp_dp_cmd_validate(info->attrs);
1495 dp = lookup_datapath(info->userhdr, info->attrs);
1499 reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
1501 return PTR_ERR(reply);
1503 return genlmsg_reply(reply, info);
1506 static int odp_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1508 struct datapath *dp;
1509 int skip = cb->args[0];
1512 list_for_each_entry (dp, &dps, list_node) {
1515 if (odp_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1516 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1517 ODP_DP_CMD_NEW) < 0)
1527 static struct genl_ops dp_datapath_genl_ops[] = {
1528 { .cmd = ODP_DP_CMD_NEW,
1529 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1530 .policy = datapath_policy,
1531 .doit = odp_dp_cmd_new
1533 { .cmd = ODP_DP_CMD_DEL,
1534 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1535 .policy = datapath_policy,
1536 .doit = odp_dp_cmd_del
1538 { .cmd = ODP_DP_CMD_GET,
1539 .flags = 0, /* OK for unprivileged users. */
1540 .policy = datapath_policy,
1541 .doit = odp_dp_cmd_get,
1542 .dumpit = odp_dp_cmd_dump
1544 { .cmd = ODP_DP_CMD_SET,
1545 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1546 .policy = datapath_policy,
1547 .doit = odp_dp_cmd_set,
1551 static const struct nla_policy vport_policy[ODP_VPORT_ATTR_MAX + 1] = {
1552 #ifdef HAVE_NLA_NUL_STRING
1553 [ODP_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1554 [ODP_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1555 [ODP_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1556 [ODP_VPORT_ATTR_STATS] = { .len = sizeof(struct rtnl_link_stats64) },
1557 [ODP_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1559 [ODP_VPORT_ATTR_STATS] = { .minlen = sizeof(struct rtnl_link_stats64) },
1560 [ODP_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1562 [ODP_VPORT_ATTR_MTU] = { .type = NLA_U32 },
1563 [ODP_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1566 static struct genl_family dp_vport_genl_family = {
1567 .id = GENL_ID_GENERATE,
1568 .hdrsize = sizeof(struct odp_header),
1569 .name = ODP_VPORT_FAMILY,
1571 .maxattr = ODP_VPORT_ATTR_MAX
1574 static struct genl_multicast_group dp_vport_multicast_group = {
1575 .name = ODP_VPORT_MCGROUP
1578 /* Called with RTNL lock or RCU read lock. */
1579 static int odp_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1580 u32 pid, u32 seq, u32 flags, u8 cmd)
1582 struct odp_header *odp_header;
1584 int ifindex, iflink;
1587 odp_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1592 odp_header->dp_ifindex = vport->dp->dp_ifindex;
1594 NLA_PUT_U32(skb, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
1595 NLA_PUT_U32(skb, ODP_VPORT_ATTR_TYPE, vport_get_type(vport));
1596 NLA_PUT_STRING(skb, ODP_VPORT_ATTR_NAME, vport_get_name(vport));
1598 nla = nla_reserve(skb, ODP_VPORT_ATTR_STATS, sizeof(struct rtnl_link_stats64));
1600 goto nla_put_failure;
1601 if (vport_get_stats(vport, nla_data(nla)))
1602 __skb_trim(skb, skb->len - nla->nla_len);
1604 NLA_PUT(skb, ODP_VPORT_ATTR_ADDRESS, ETH_ALEN, vport_get_addr(vport));
1606 NLA_PUT_U32(skb, ODP_VPORT_ATTR_MTU, vport_get_mtu(vport));
1608 err = vport_get_options(vport, skb);
1609 if (err == -EMSGSIZE)
1612 ifindex = vport_get_ifindex(vport);
1614 NLA_PUT_U32(skb, ODP_VPORT_ATTR_IFINDEX, ifindex);
1616 iflink = vport_get_iflink(vport);
1618 NLA_PUT_U32(skb, ODP_VPORT_ATTR_IFLINK, iflink);
1620 return genlmsg_end(skb, odp_header);
1625 genlmsg_cancel(skb, odp_header);
1629 /* Called with RTNL lock or RCU read lock. */
1630 static struct sk_buff *odp_vport_cmd_build_info(struct vport *vport, u32 pid,
1633 struct sk_buff *skb;
1636 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1638 return ERR_PTR(-ENOMEM);
1640 retval = odp_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1643 return ERR_PTR(retval);
1648 static int odp_vport_cmd_validate(struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1650 return VERIFY_NUL_STRING(a[ODP_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1653 /* Called with RTNL lock or RCU read lock. */
1654 static struct vport *lookup_vport(struct odp_header *odp_header,
1655 struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1657 struct datapath *dp;
1658 struct vport *vport;
1660 if (a[ODP_VPORT_ATTR_NAME]) {
1661 vport = vport_locate(nla_data(a[ODP_VPORT_ATTR_NAME]));
1663 return ERR_PTR(-ENODEV);
1665 } else if (a[ODP_VPORT_ATTR_PORT_NO]) {
1666 u32 port_no = nla_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1668 if (port_no >= DP_MAX_PORTS)
1669 return ERR_PTR(-EFBIG);
1671 dp = get_dp(odp_header->dp_ifindex);
1673 return ERR_PTR(-ENODEV);
1675 vport = get_vport_protected(dp, port_no);
1677 return ERR_PTR(-ENOENT);
1680 return ERR_PTR(-EINVAL);
1683 /* Called with RTNL lock. */
1684 static int change_vport(struct vport *vport, struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1687 if (a[ODP_VPORT_ATTR_STATS])
1688 err = vport_set_stats(vport, nla_data(a[ODP_VPORT_ATTR_STATS]));
1689 if (!err && a[ODP_VPORT_ATTR_ADDRESS])
1690 err = vport_set_addr(vport, nla_data(a[ODP_VPORT_ATTR_ADDRESS]));
1691 if (!err && a[ODP_VPORT_ATTR_MTU])
1692 err = vport_set_mtu(vport, nla_get_u32(a[ODP_VPORT_ATTR_MTU]));
1696 static int odp_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1698 struct nlattr **a = info->attrs;
1699 struct odp_header *odp_header = info->userhdr;
1700 struct vport_parms parms;
1701 struct sk_buff *reply;
1702 struct vport *vport;
1703 struct datapath *dp;
1708 if (!a[ODP_VPORT_ATTR_NAME] || !a[ODP_VPORT_ATTR_TYPE])
1711 err = odp_vport_cmd_validate(a);
1716 dp = get_dp(odp_header->dp_ifindex);
1721 if (a[ODP_VPORT_ATTR_PORT_NO]) {
1722 port_no = nla_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1725 if (port_no >= DP_MAX_PORTS)
1728 vport = get_vport_protected(dp, port_no);
1733 for (port_no = 1; ; port_no++) {
1734 if (port_no >= DP_MAX_PORTS) {
1738 vport = get_vport_protected(dp, port_no);
1744 parms.name = nla_data(a[ODP_VPORT_ATTR_NAME]);
1745 parms.type = nla_get_u32(a[ODP_VPORT_ATTR_TYPE]);
1746 parms.options = a[ODP_VPORT_ATTR_OPTIONS];
1748 parms.port_no = port_no;
1750 vport = new_vport(&parms);
1751 err = PTR_ERR(vport);
1755 set_internal_devs_mtu(dp);
1756 dp_sysfs_add_if(vport);
1758 err = change_vport(vport, a);
1760 reply = odp_vport_cmd_build_info(vport, info->snd_pid,
1761 info->snd_seq, ODP_VPORT_CMD_NEW);
1763 err = PTR_ERR(reply);
1766 dp_detach_port(vport);
1769 genl_notify(reply, genl_info_net(info), info->snd_pid,
1770 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1779 static int odp_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1781 struct nlattr **a = info->attrs;
1782 struct sk_buff *reply;
1783 struct vport *vport;
1786 err = odp_vport_cmd_validate(a);
1791 vport = lookup_vport(info->userhdr, a);
1792 err = PTR_ERR(vport);
1797 if (a[ODP_VPORT_ATTR_OPTIONS])
1798 err = vport_set_options(vport, a[ODP_VPORT_ATTR_OPTIONS]);
1800 err = change_vport(vport, a);
1802 reply = odp_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1804 if (IS_ERR(reply)) {
1805 err = PTR_ERR(reply);
1806 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1807 dp_vport_multicast_group.id, err);
1811 genl_notify(reply, genl_info_net(info), info->snd_pid,
1812 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1820 static int odp_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1822 struct nlattr **a = info->attrs;
1823 struct sk_buff *reply;
1824 struct vport *vport;
1827 err = odp_vport_cmd_validate(a);
1832 vport = lookup_vport(info->userhdr, a);
1833 err = PTR_ERR(vport);
1837 if (vport->port_no == ODPP_LOCAL) {
1842 reply = odp_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1844 err = PTR_ERR(reply);
1848 err = dp_detach_port(vport);
1850 genl_notify(reply, genl_info_net(info), info->snd_pid,
1851 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1859 static int odp_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1861 struct nlattr **a = info->attrs;
1862 struct odp_header *odp_header = info->userhdr;
1863 struct sk_buff *reply;
1864 struct vport *vport;
1867 err = odp_vport_cmd_validate(a);
1872 vport = lookup_vport(odp_header, a);
1873 err = PTR_ERR(vport);
1877 reply = odp_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1879 err = PTR_ERR(reply);
1883 err = genlmsg_reply(reply, info);
1891 static int odp_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1893 struct odp_header *odp_header = genlmsg_data(nlmsg_data(cb->nlh));
1894 struct datapath *dp;
1898 dp = get_dp(odp_header->dp_ifindex);
1903 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1904 struct vport *vport;
1906 vport = get_vport_protected(dp, port_no);
1910 if (odp_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1911 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1912 ODP_VPORT_CMD_NEW) < 0)
1917 cb->args[0] = port_no;
1923 static struct genl_ops dp_vport_genl_ops[] = {
1924 { .cmd = ODP_VPORT_CMD_NEW,
1925 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1926 .policy = vport_policy,
1927 .doit = odp_vport_cmd_new
1929 { .cmd = ODP_VPORT_CMD_DEL,
1930 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1931 .policy = vport_policy,
1932 .doit = odp_vport_cmd_del
1934 { .cmd = ODP_VPORT_CMD_GET,
1935 .flags = 0, /* OK for unprivileged users. */
1936 .policy = vport_policy,
1937 .doit = odp_vport_cmd_get,
1938 .dumpit = odp_vport_cmd_dump
1940 { .cmd = ODP_VPORT_CMD_SET,
1941 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1942 .policy = vport_policy,
1943 .doit = odp_vport_cmd_set,
1947 struct genl_family_and_ops {
1948 struct genl_family *family;
1949 struct genl_ops *ops;
1951 struct genl_multicast_group *group;
1954 static const struct genl_family_and_ops dp_genl_families[] = {
1955 { &dp_datapath_genl_family,
1956 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1957 &dp_datapath_multicast_group },
1958 { &dp_vport_genl_family,
1959 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1960 &dp_vport_multicast_group },
1961 { &dp_flow_genl_family,
1962 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1963 &dp_flow_multicast_group },
1964 { &dp_packet_genl_family,
1965 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1969 static void dp_unregister_genl(int n_families)
1973 for (i = 0; i < n_families; i++) {
1974 genl_unregister_family(dp_genl_families[i].family);
1978 static int dp_register_genl(void)
1985 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1986 const struct genl_family_and_ops *f = &dp_genl_families[i];
1988 err = genl_register_family_with_ops(f->family, f->ops,
1995 err = genl_register_mc_group(f->family, f->group);
2001 err = packet_register_mc_groups();
2007 dp_unregister_genl(n_registered);
2011 static int __init dp_init(void)
2013 struct sk_buff *dummy_skb;
2016 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2018 printk("Open vSwitch %s, built "__DATE__" "__TIME__"\n", VERSION BUILDNR);
2026 goto error_flow_exit;
2028 err = register_netdevice_notifier(&dp_device_notifier);
2030 goto error_vport_exit;
2032 err = dp_register_genl();
2034 goto error_unreg_notifier;
2038 error_unreg_notifier:
2039 unregister_netdevice_notifier(&dp_device_notifier);
2048 static void dp_cleanup(void)
2051 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2052 unregister_netdevice_notifier(&dp_device_notifier);
2057 module_init(dp_init);
2058 module_exit(dp_cleanup);
2060 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2061 MODULE_LICENSE("GPL");