2 * Distributed under the terms of the GNU GPL version 2.
3 * Copyright (c) 2007 The Board of Trustees of The Leland Stanford Junior Univer
7 /* Functions for managing the dp interface/device. */
9 #include <linux/module.h>
10 #include <linux/if_arp.h>
11 #include <linux/if_bridge.h>
12 #include <linux/if_vlan.h>
14 #include <net/genetlink.h>
16 #include <linux/etherdevice.h>
17 #include <linux/kernel.h>
18 #include <linux/mutex.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/timer.h>
21 #include <linux/rcupdate.h>
22 #include <linux/version.h>
23 #include <linux/ethtool.h>
24 #include <linux/random.h>
25 #include <asm/system.h>
26 #include <linux/netfilter_bridge.h>
27 #include <linux/inetdevice.h>
28 #include <linux/list.h>
30 #include "openflow-netlink.h"
36 #include "datapath_t.h"
41 /* Number of seconds between runs of the flow expiration code. */
44 #define BRIDGE_PORT_NO_FLOOD 0x00000001
46 #define UINT32_MAX 4294967295U
48 struct net_bridge_port {
52 struct net_device *dev;
53 struct list_head node; /* Element in datapath.ports. */
56 static struct genl_family dp_genl_family;
57 static struct genl_multicast_group mc_group;
59 int dp_dev_setup(struct net_device *dev);
61 /* It's hard to imagine wanting more than one datapath, but... */
64 /* datapaths. Protected on the read side by rcu_read_lock, on the write side
67 * It is safe to access the datapath and net_bridge_port structures with just
68 * the dp_mutex, but to access the chain you need to take the rcu_read_lock
69 * also (because dp_mutex doesn't prevent flows from being destroyed).
71 static struct datapath *dps[DP_MAX];
72 static DEFINE_MUTEX(dp_mutex);
74 static void dp_timer_handler(unsigned long arg);
75 static int send_port_status(struct net_bridge_port *p, uint8_t status);
78 /* nla_unreserve - reduce amount of space reserved by nla_reserve
79 * @skb: socket buffer from which to recover room
80 * @nla: netlink attribute to adjust
81 * @len: amount by which to reduce attribute payload
83 * Reduces amount of space reserved by a call to nla_reserve.
85 * No other attributes may be added between calling nla_reserve and this
86 * function, since it will create a hole in the message.
88 void nla_unreserve(struct sk_buff *skb, struct nlattr *nla, int len)
96 /* Generates a unique datapath id. It incorporates the datapath index
97 * and a hardware address, if available. If not, it generates a random
101 uint64_t gen_datapath_id(uint16_t dp_idx)
105 struct net_device *dev;
107 /* The top 16 bits are used to identify the datapath. The lower 48 bits
108 * use an interface address. */
109 id = (uint64_t)dp_idx << 48;
110 if ((dev = dev_get_by_name(&init_net, "ctl0"))
111 || (dev = dev_get_by_name(&init_net, "eth0"))) {
112 for (i=0; i<ETH_ALEN; i++) {
113 id |= (uint64_t)dev->dev_addr[i] << (8*(ETH_ALEN-1 - i));
117 /* Randomly choose the lower 48 bits if we cannot find an
118 * address and mark the most significant bit to indicate that
119 * this was randomly generated. */
120 uint8_t rand[ETH_ALEN];
121 get_random_bytes(rand, ETH_ALEN);
122 id |= (uint64_t)1 << 63;
123 for (i=0; i<ETH_ALEN; i++) {
124 id |= (uint64_t)rand[i] << (8*(ETH_ALEN-1 - i));
131 /* Creates a new datapath numbered 'dp_idx'. Returns 0 for success or a
132 * negative error code.
134 * Not called with any locks. */
135 static int new_dp(int dp_idx)
140 if (dp_idx < 0 || dp_idx >= DP_MAX)
143 if (!try_module_get(THIS_MODULE))
146 mutex_lock(&dp_mutex);
147 dp = rcu_dereference(dps[dp_idx]);
154 dp = kzalloc(sizeof *dp, GFP_KERNEL);
159 dp->id = gen_datapath_id(dp_idx);
160 dp->chain = chain_create(dp);
161 if (dp->chain == NULL)
163 INIT_LIST_HEAD(&dp->port_list);
166 /* Setup our "of" device */
169 err = dp_dev_setup(&dp->dev);
172 printk("datapath: problem setting up 'of' device\n");
175 dp->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
177 setup_timer(&dp->timer, dp_timer_handler, (unsigned long) dp);
178 mod_timer(&dp->timer, round_jiffies(jiffies + (EXPIRE_SECS * HZ)));
180 rcu_assign_pointer(dps[dp_idx], dp);
181 mutex_unlock(&dp_mutex);
188 mutex_unlock(&dp_mutex);
189 module_put(THIS_MODULE);
193 /* Find and return a free port number under 'dp'. Called under dp_mutex. */
194 static int find_portno(struct datapath *dp)
197 for (i = 0; i < OFPP_MAX; i++)
198 if (dp->ports[i] == NULL)
203 static struct net_bridge_port *new_nbp(struct datapath *dp,
204 struct net_device *dev)
206 struct net_bridge_port *p;
209 port_no = find_portno(dp);
211 return ERR_PTR(port_no);
213 p = kzalloc(sizeof(*p), GFP_KERNEL);
215 return ERR_PTR(-ENOMEM);
220 p->port_no = port_no;
225 /* Called with dp_mutex. */
226 int add_switch_port(struct datapath *dp, struct net_device *dev)
228 struct net_bridge_port *p;
230 if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER)
233 if (dev->br_port != NULL)
236 p = new_nbp(dp, dev);
241 rcu_assign_pointer(dev->br_port, p);
243 dev_set_promiscuity(dev, 1);
246 rcu_assign_pointer(dp->ports[p->port_no], p);
247 list_add_rcu(&p->node, &dp->port_list);
249 /* Notify the ctlpath that this port has been added */
250 send_port_status(p, OFPPR_ADD);
255 /* Delete 'p' from switch.
256 * Called with dp_mutex. */
257 static int del_switch_port(struct net_bridge_port *p)
259 /* First drop references to device. */
261 dev_set_promiscuity(p->dev, -1);
263 list_del_rcu(&p->node);
264 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
265 rcu_assign_pointer(p->dev->br_port, NULL);
267 /* Then wait until no one is still using it, and destroy it. */
270 /* Notify the ctlpath that this port no longer exists */
271 send_port_status(p, OFPPR_DELETE);
279 /* Called with dp_mutex. */
280 static void del_dp(struct datapath *dp)
282 struct net_bridge_port *p, *n;
285 /* Unregister the "of" device of this dp */
287 unregister_netdevice(&dp->dev);
291 /* Drop references to DP. */
292 list_for_each_entry_safe (p, n, &dp->port_list, node)
294 del_timer_sync(&dp->timer);
295 rcu_assign_pointer(dps[dp->dp_idx], NULL);
297 /* Wait until no longer in use, then destroy it. */
299 chain_destroy(dp->chain);
301 module_put(THIS_MODULE);
304 static void dp_timer_handler(unsigned long arg)
306 struct datapath *dp = (struct datapath *) arg;
308 chain_timeout(dp->chain);
310 int count = chain_timeout(dp->chain);
311 chain_print_stats(dp->chain);
313 printk("%d flows timed out\n", count);
315 mod_timer(&dp->timer, round_jiffies(jiffies + (EXPIRE_SECS * HZ)));
319 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
320 * different set of devices!) Returns 0 if *pskb should be processed further,
321 * 1 if *pskb is handled. */
322 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
323 /* Called with rcu_read_lock. */
324 static struct sk_buff *dp_frame_hook(struct net_bridge_port *p,
327 struct ethhdr *eh = eth_hdr(skb);
328 struct sk_buff *skb_local = NULL;
331 if (compare_ether_addr(eh->h_dest, skb->dev->dev_addr) == 0)
334 if (is_broadcast_ether_addr(eh->h_dest)
335 || is_multicast_ether_addr(eh->h_dest)
336 || is_local_ether_addr(eh->h_dest))
337 skb_local = skb_clone(skb, GFP_ATOMIC);
339 /* Push the Ethernet header back on. */
340 if (skb->protocol == htons(ETH_P_8021Q))
341 skb_push(skb, VLAN_ETH_HLEN);
343 skb_push(skb, ETH_HLEN);
345 fwd_port_input(p->dp->chain, skb, p->port_no);
349 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
350 static int dp_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
352 /* Push the Ethernet header back on. */
353 if ((*pskb)->protocol == htons(ETH_P_8021Q))
354 skb_push(*pskb, VLAN_ETH_HLEN);
356 skb_push(*pskb, ETH_HLEN);
358 fwd_port_input(p->dp->chain, *pskb, p->port_no);
362 /* NB: This has only been tested on 2.4.35 */
364 /* Called without any locks (?) */
365 static void dp_frame_hook(struct sk_buff *skb)
367 struct net_bridge_port *p = skb->dev->br_port;
369 /* Push the Ethernet header back on. */
370 if (skb->protocol == htons(ETH_P_8021Q))
371 skb_push(skb, VLAN_ETH_HLEN);
373 skb_push(skb, ETH_HLEN);
377 fwd_port_input(p->dp->chain, skb, p->port_no);
384 /* Forwarding output path.
385 * Based on net/bridge/br_forward.c. */
387 /* Don't forward packets to originating port or with flooding disabled */
388 static inline int should_deliver(const struct net_bridge_port *p,
389 const struct sk_buff *skb)
391 if ((skb->dev == p->dev) || (p->flags & BRIDGE_PORT_NO_FLOOD)) {
398 static inline unsigned packet_length(const struct sk_buff *skb)
400 int length = skb->len - ETH_HLEN;
401 if (skb->protocol == htons(ETH_P_8021Q))
407 flood(struct datapath *dp, struct sk_buff *skb)
409 struct net_bridge_port *p;
413 list_for_each_entry_rcu (p, &dp->port_list, node) {
414 if (!should_deliver(p, skb))
416 if (prev_port != -1) {
417 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
422 dp_output_port(dp, clone, prev_port);
424 prev_port = p->port_no;
427 dp_output_port(dp, skb, prev_port);
434 /* Marks 'skb' as having originated from 'in_port' in 'dp'.
435 FIXME: how are devices reference counted? */
436 int dp_set_origin(struct datapath *dp, uint16_t in_port,
439 if (in_port < OFPP_MAX && dp->ports[in_port]) {
440 skb->dev = dp->ports[in_port]->dev;
446 /* Takes ownership of 'skb' and transmits it to 'out_port' on 'dp'.
448 int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port)
450 struct net_bridge_port *p;
454 if (out_port == OFPP_FLOOD)
455 return flood(dp, skb);
456 else if (out_port == OFPP_CONTROLLER)
457 return dp_output_control(dp, skb, fwd_save_skb(skb), 0,
459 else if (out_port >= OFPP_MAX)
462 p = dp->ports[out_port];
467 if (packet_length(skb) > skb->dev->mtu) {
468 printk("dropped over-mtu packet: %d > %d\n",
469 packet_length(skb), skb->dev->mtu);
481 printk("can't forward to bad port %d\n", out_port);
485 /* Takes ownership of 'skb' and transmits it to 'dp''s control path. If
486 * 'buffer_id' != -1, then only the first 64 bytes of 'skb' are sent;
487 * otherwise, all of 'skb' is sent. 'reason' indicates why 'skb' is being
488 * sent. 'max_len' sets the maximum number of bytes that the caller
489 * wants to be sent; a value of 0 indicates the entire packet should be
492 dp_output_control(struct datapath *dp, struct sk_buff *skb,
493 uint32_t buffer_id, size_t max_len, int reason)
495 /* FIXME? packet_rcv_spkt in net/packet/af_packet.c does some stuff
496 that we should possibly be doing here too. */
497 /* FIXME? Can we avoid creating a new skbuff in the case where we
498 * forward the whole packet? */
499 struct sk_buff *f_skb;
501 struct ofp_packet_in *opi;
508 if ((buffer_id != (uint32_t) -1) && max_len)
509 fwd_len = min(fwd_len, max_len);
511 len = nla_total_size(offsetof(struct ofp_packet_in, data) + fwd_len)
512 + nla_total_size(sizeof(uint32_t));
514 f_skb = genlmsg_new(len, GFP_ATOMIC);
518 data = genlmsg_put(f_skb, 0, 0, &dp_genl_family, 0,
521 goto error_free_f_skb;
523 NLA_PUT_U32(f_skb, DP_GENL_A_DP_IDX, dp->dp_idx);
525 opi_len = offsetof(struct ofp_packet_in, data) + fwd_len;
526 attr = nla_reserve(f_skb, DP_GENL_A_OPENFLOW, opi_len);
528 goto error_free_f_skb;
529 opi = nla_data(attr);
530 opi->header.version = OFP_VERSION;
531 opi->header.type = OFPT_PACKET_IN;
532 opi->header.length = htons(opi_len);
533 opi->header.xid = htonl(0);
535 opi->buffer_id = htonl(buffer_id);
536 opi->total_len = htons(skb->len);
537 opi->in_port = htons(skb->dev->br_port->port_no);
538 opi->reason = reason;
540 SKB_LINEAR_ASSERT(skb);
541 memcpy(opi->data, skb_mac_header(skb), fwd_len);
543 err = genlmsg_end(f_skb, data);
545 goto error_free_f_skb;
547 err = genlmsg_multicast(f_skb, 0, mc_group.id, GFP_ATOMIC);
548 if (err && net_ratelimit())
549 printk(KERN_WARNING "dp_output_control: genlmsg_multicast failed: %d\n", err);
561 printk(KERN_ERR "dp_output_control: failed to send: %d\n", err);
565 static void fill_port_desc(struct net_bridge_port *p, struct ofp_phy_port *desc)
567 desc->port_no = htons(p->port_no);
568 strncpy(desc->name, p->dev->name, OFP_MAX_PORT_NAME_LEN);
569 desc->name[OFP_MAX_PORT_NAME_LEN-1] = '\0';
570 memcpy(desc->hw_addr, p->dev->dev_addr, ETH_ALEN);
571 desc->flags = htonl(p->flags);
575 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,24)
576 if (p->dev->ethtool_ops && p->dev->ethtool_ops->get_settings) {
577 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
579 if (!p->dev->ethtool_ops->get_settings(p->dev, &ecmd)) {
580 if (ecmd.supported & SUPPORTED_10baseT_Half)
581 desc->features |= OFPPF_10MB_HD;
582 if (ecmd.supported & SUPPORTED_10baseT_Full)
583 desc->features |= OFPPF_10MB_FD;
584 if (ecmd.supported & SUPPORTED_100baseT_Half)
585 desc->features |= OFPPF_100MB_HD;
586 if (ecmd.supported & SUPPORTED_100baseT_Full)
587 desc->features |= OFPPF_100MB_FD;
588 if (ecmd.supported & SUPPORTED_1000baseT_Half)
589 desc->features |= OFPPF_1GB_HD;
590 if (ecmd.supported & SUPPORTED_1000baseT_Full)
591 desc->features |= OFPPF_1GB_FD;
592 /* 10Gbps half-duplex doesn't exist... */
593 if (ecmd.supported & SUPPORTED_10000baseT_Full)
594 desc->features |= OFPPF_10GB_FD;
596 desc->features = htonl(desc->features);
597 desc->speed = htonl(ecmd.speed);
604 fill_data_hello(struct datapath *dp, struct ofp_data_hello *odh)
606 struct net_bridge_port *p;
609 odh->header.version = OFP_VERSION;
610 odh->header.type = OFPT_DATA_HELLO;
611 odh->header.xid = htonl(0);
612 odh->datapath_id = cpu_to_be64(dp->id);
614 odh->n_exact = htonl(2 * TABLE_HASH_MAX_FLOWS);
615 odh->n_mac_only = htonl(TABLE_MAC_MAX_FLOWS);
616 odh->n_compression = 0; /* Not supported */
617 odh->n_general = htonl(TABLE_LINEAR_MAX_FLOWS);
618 odh->buffer_mb = htonl(UINT32_MAX);
619 odh->n_buffers = htonl(N_PKT_BUFFERS);
620 odh->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
621 odh->actions = htonl(OFP_SUPPORTED_ACTIONS);
622 odh->miss_send_len = htons(dp->miss_send_len);
624 list_for_each_entry_rcu (p, &dp->port_list, node) {
625 fill_port_desc(p, &odh->ports[port_count]);
633 dp_send_hello(struct datapath *dp)
637 struct ofp_data_hello *odh;
638 size_t odh_max_len, odh_len, port_max_len, len;
644 /* Overallocate, since we can't reliably determine the number of
646 port_max_len = sizeof(struct ofp_phy_port) * OFPP_MAX;
648 len = nla_total_size(sizeof(*odh) + port_max_len)
649 + nla_total_size(sizeof(uint32_t));
651 skb = genlmsg_new(len, GFP_ATOMIC);
654 printk("dp_send_hello: genlmsg_new failed\n");
658 data = genlmsg_put(skb, 0, 0, &dp_genl_family, 0,
662 printk("dp_send_hello: genlmsg_put failed\n");
666 NLA_PUT_U32(skb, DP_GENL_A_DP_IDX, dp->dp_idx);
668 odh_max_len = sizeof(*odh) + port_max_len;
669 attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, odh_max_len);
672 printk("dp_send_hello: nla_reserve failed\n");
675 odh = nla_data(attr);
676 port_count = fill_data_hello(dp, odh);
678 /* Only now that we know how many ports we've added can we say
679 * say something about the length. */
680 odh_len = sizeof(*odh) + (sizeof(struct ofp_phy_port) * port_count);
681 odh->header.length = htons(odh_len);
683 /* Take back the unused part that was reserved */
684 nla_unreserve(skb, attr, (odh_max_len - odh_len));
686 err = genlmsg_end(skb, data);
689 printk("dp_send_hello: genlmsg_end failed\n");
693 err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
694 if (err && net_ratelimit())
695 printk(KERN_WARNING "dp_send_hello: genlmsg_multicast failed: %d\n", err);
703 printk(KERN_ERR "dp_send_hello: failed to send: %d\n", err);
708 dp_update_port_flags(struct datapath *dp, const struct ofp_phy_port *opp)
710 struct net_bridge_port *p;
712 p = dp->ports[htons(opp->port_no)];
714 /* Make sure the port id hasn't changed since this was sent */
715 if (!p || memcmp(opp->hw_addr, p->dev->dev_addr, ETH_ALEN) != 0)
718 p->flags = htonl(opp->flags);
725 send_port_status(struct net_bridge_port *p, uint8_t status)
729 struct ofp_port_status *ops;
734 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
737 printk("send_port_status: genlmsg_new failed\n");
741 data = genlmsg_put(skb, 0, 0, &dp_genl_family, 0,
745 printk("send_port_status: genlmsg_put failed\n");
749 NLA_PUT_U32(skb, DP_GENL_A_DP_IDX, p->dp->dp_idx);
751 attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, sizeof(*ops));
754 printk("send_port_status: nla_reserve failed\n");
758 ops = nla_data(attr);
759 ops->header.version = OFP_VERSION;
760 ops->header.type = OFPT_PORT_STATUS;
761 ops->header.length = htons(sizeof(*ops));
762 ops->header.xid = htonl(0);
764 ops->reason = status;
765 fill_port_desc(p, &ops->desc);
767 err = genlmsg_end(skb, data);
770 printk("send_port_status: genlmsg_end failed\n");
774 err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
775 if (err && net_ratelimit())
776 printk(KERN_WARNING "send_port_status: genlmsg_multicast failed: %d\n", err);
784 printk(KERN_ERR "send_port_status: failed to send: %d\n", err);
789 dp_send_flow_expired(struct datapath *dp, struct sw_flow *flow)
793 struct ofp_flow_expired *ofe;
795 unsigned long duration_j;
799 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
802 printk("dp_send_flow_expired: genlmsg_new failed\n");
806 data = genlmsg_put(skb, 0, 0, &dp_genl_family, 0,
810 printk("dp_send_flow_expired: genlmsg_put failed\n");
814 NLA_PUT_U32(skb, DP_GENL_A_DP_IDX, dp->dp_idx);
816 attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, sizeof(*ofe));
819 printk("dp_send_flow_expired: nla_reserve failed\n");
823 ofe = nla_data(attr);
824 ofe->header.version = OFP_VERSION;
825 ofe->header.type = OFPT_FLOW_EXPIRED;
826 ofe->header.length = htons(sizeof(*ofe));
827 ofe->header.xid = htonl(0);
829 flow_fill_match(&ofe->match, &flow->key);
830 duration_j = (flow->timeout - HZ * flow->max_idle) - flow->init_time;
831 ofe->duration = htonl(duration_j / HZ);
832 ofe->packet_count = cpu_to_be64(flow->packet_count);
833 ofe->byte_count = cpu_to_be64(flow->byte_count);
835 err = genlmsg_end(skb, data);
838 printk("dp_send_flow_expired: genlmsg_end failed\n");
842 err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
843 if (err && net_ratelimit())
844 printk(KERN_WARNING "send_flow_expired: genlmsg_multicast failed: %d\n", err);
852 printk(KERN_ERR "send_flow_expired: failed to send: %d\n", err);
856 /* Generic Netlink interface.
858 * See netlink(7) for an introduction to netlink. See
859 * http://linux-net.osdl.org/index.php/Netlink for more information and
860 * pointers on how to work with netlink and Generic Netlink in the kernel and
863 static struct genl_family dp_genl_family = {
864 .id = GENL_ID_GENERATE,
866 .name = DP_GENL_FAMILY_NAME,
868 .maxattr = DP_GENL_A_MAX,
871 /* Attribute policy: what each attribute may contain. */
872 static struct nla_policy dp_genl_policy[DP_GENL_A_MAX + 1] = {
873 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
874 [DP_GENL_A_MC_GROUP] = { .type = NLA_U32 },
875 [DP_GENL_A_PORTNAME] = { .type = NLA_STRING }
878 static int dp_genl_add(struct sk_buff *skb, struct genl_info *info)
880 if (!info->attrs[DP_GENL_A_DP_IDX])
883 return new_dp(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
886 static struct genl_ops dp_genl_ops_add_dp = {
887 .cmd = DP_GENL_C_ADD_DP,
888 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
889 .policy = dp_genl_policy,
894 struct datapath *dp_get(int dp_idx)
896 if (dp_idx < 0 || dp_idx > DP_MAX)
898 return rcu_dereference(dps[dp_idx]);
901 static int dp_genl_del(struct sk_buff *skb, struct genl_info *info)
906 if (!info->attrs[DP_GENL_A_DP_IDX])
909 mutex_lock(&dp_mutex);
910 dp = dp_get(nla_get_u32((info->attrs[DP_GENL_A_DP_IDX])));
917 mutex_unlock(&dp_mutex);
921 static struct genl_ops dp_genl_ops_del_dp = {
922 .cmd = DP_GENL_C_DEL_DP,
923 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
924 .policy = dp_genl_policy,
929 /* Queries a datapath for related information. Currently the only relevant
930 * information is the datapath's multicast group ID. Really we want one
931 * multicast group per datapath, but because of locking issues[*] we can't
932 * easily get one. Thus, every datapath will currently return the same
933 * global multicast group ID, but in the future it would be nice to fix that.
935 * [*] dp_genl_add, to add a new datapath, is called under the genl_lock
936 * mutex, and genl_register_mc_group, called to acquire a new multicast
937 * group ID, also acquires genl_lock, thus deadlock.
939 static int dp_genl_query(struct sk_buff *skb, struct genl_info *info)
942 struct sk_buff *ans_skb = NULL;
946 if (!info->attrs[DP_GENL_A_DP_IDX])
950 dp_idx = nla_get_u32((info->attrs[DP_GENL_A_DP_IDX]));
956 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
961 data = genlmsg_put_reply(ans_skb, info, &dp_genl_family,
962 0, DP_GENL_C_QUERY_DP);
967 NLA_PUT_U32(ans_skb, DP_GENL_A_DP_IDX, dp_idx);
968 NLA_PUT_U32(ans_skb, DP_GENL_A_MC_GROUP, mc_group.id);
970 genlmsg_end(ans_skb, data);
971 err = genlmsg_reply(ans_skb, info);
984 * Fill flow entry for nl flow query. Called with rcu_lock
989 dp_fill_flow(struct ofp_flow_mod* ofm, struct swt_iterator* iter)
991 ofm->header.version = OFP_VERSION;
992 ofm->header.type = OFPT_FLOW_MOD;
993 ofm->header.length = htons(sizeof(struct ofp_flow_mod)
994 + sizeof(ofm->actions[0]));
995 ofm->header.xid = htonl(0);
997 ofm->match.wildcards = htons(iter->flow->key.wildcards);
998 ofm->match.in_port = iter->flow->key.in_port;
999 ofm->match.dl_vlan = iter->flow->key.dl_vlan;
1000 memcpy(ofm->match.dl_src, iter->flow->key.dl_src, ETH_ALEN);
1001 memcpy(ofm->match.dl_dst, iter->flow->key.dl_dst, ETH_ALEN);
1002 ofm->match.dl_type = iter->flow->key.dl_type;
1003 ofm->match.nw_src = iter->flow->key.nw_src;
1004 ofm->match.nw_dst = iter->flow->key.nw_dst;
1005 ofm->match.nw_proto = iter->flow->key.nw_proto;
1006 ofm->match.tp_src = iter->flow->key.tp_src;
1007 ofm->match.tp_dst = iter->flow->key.tp_dst;
1008 ofm->group_id = iter->flow->group_id;
1009 ofm->max_idle = iter->flow->max_idle;
1010 /* TODO support multiple actions */
1011 ofm->actions[0] = iter->flow->actions[0];
1016 static int dp_genl_show(struct sk_buff *skb, struct genl_info *info)
1018 struct datapath *dp;
1020 struct sk_buff *ans_skb = NULL;
1022 struct nlattr *attr;
1023 struct ofp_data_hello *odh;
1024 size_t odh_max_len, odh_len, port_max_len, len;
1027 if (!info->attrs[DP_GENL_A_DP_IDX])
1030 mutex_lock(&dp_mutex);
1031 dp = dp_get(nla_get_u32((info->attrs[DP_GENL_A_DP_IDX])));
1035 /* Overallocate, since we can't reliably determine the number of
1036 * ports a priori. */
1037 port_max_len = sizeof(struct ofp_phy_port) * OFPP_MAX;
1039 len = nla_total_size(sizeof(*odh) + port_max_len)
1040 + nla_total_size(sizeof(uint32_t));
1042 ans_skb = nlmsg_new(len, GFP_KERNEL);
1046 data = genlmsg_put_reply(ans_skb, info, &dp_genl_family,
1047 0, DP_GENL_C_SHOW_DP);
1051 NLA_PUT_U32(ans_skb, DP_GENL_A_DP_IDX, dp->dp_idx);
1053 odh_max_len = sizeof(*odh) + port_max_len;
1054 attr = nla_reserve(ans_skb, DP_GENL_A_DP_INFO, odh_max_len);
1057 odh = nla_data(attr);
1058 port_count = fill_data_hello(dp, odh);
1060 /* Only now that we know how many ports we've added can we say
1061 * say something about the length. */
1062 odh_len = sizeof(*odh) + (sizeof(struct ofp_phy_port) * port_count);
1063 odh->header.length = htons(odh_len);
1065 /* Take back the unused part that was reserved */
1066 nla_unreserve(ans_skb, attr, (odh_max_len - odh_len));
1068 genlmsg_end(ans_skb, data);
1069 err = genlmsg_reply(ans_skb, info);
1077 mutex_unlock(&dp_mutex);
1081 static struct genl_ops dp_genl_ops_show_dp = {
1082 .cmd = DP_GENL_C_SHOW_DP,
1083 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1084 .policy = dp_genl_policy,
1085 .doit = dp_genl_show,
1089 /* Convenience function */
1092 dp_init_nl_flow_msg(uint32_t dp_idx, uint16_t table_idx,
1093 struct genl_info *info, struct sk_buff* skb)
1097 data = genlmsg_put_reply(skb, info, &dp_genl_family, 0,
1098 DP_GENL_C_QUERY_FLOW);
1101 NLA_PUT_U32(skb, DP_GENL_A_DP_IDX, dp_idx);
1102 NLA_PUT_U16(skb, DP_GENL_A_TABLEIDX, table_idx);
1110 /* Iterate through the specified table and send all flow entries over
1111 * netlink to userspace. Each flow message has the following format:
1115 * 32bit number of flows
1116 * openflow-flow-entries
1118 * The full table may require multiple messages. A message with 0 flows
1119 * signifies end-of message.
1124 dp_dump_table(struct datapath *dp, uint16_t table_idx, struct genl_info *info, struct ofp_flow_mod* matchme)
1126 struct sk_buff *skb = 0;
1127 struct sw_table *table = 0;
1128 struct swt_iterator iter;
1129 struct sw_flow_key in_flow;
1130 struct nlattr *attr;
1131 int count = 0, sum_count = 0;
1133 uint8_t* ofm_ptr = 0;
1134 struct nlattr *num_attr;
1137 table = dp->chain->tables[table_idx];
1138 if ( table == NULL ) {
1139 dprintk("dp::dp_dump_table error, non-existant table at position %d\n", table_idx);
1143 if (!table->iterator(table, &iter)) {
1144 dprintk("dp::dp_dump_table couldn't initialize empty table iterator\n");
1150 /* verify that we can fit all NL_FLOWS_PER_MESSAGE in a single
1152 if( (sizeof(dp_genl_family) + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint32_t) +
1153 (NL_FLOWS_PER_MESSAGE * sizeof(struct ofp_flow_mod))) > (8192 - 64)){
1154 dprintk("dp::dp_dump_table NL_FLOWS_PER_MESSAGE may cause overrun in skbuf\n");
1158 skb = nlmsg_new(8192 - 64, GFP_ATOMIC);
1163 data = dp_init_nl_flow_msg(dp->dp_idx, table_idx, info, skb);
1166 goto error_free_skb;
1169 /* reserve space to put the number of flows for this message, to
1170 * be filled after the loop*/
1171 num_attr = nla_reserve(skb, DP_GENL_A_NUMFLOWS, sizeof(uint32_t));
1174 goto error_free_skb;
1177 /* Only load NL_FLOWS_PER_MESSAGE flows at a time */
1178 attr = nla_reserve(skb, DP_GENL_A_FLOW,
1179 (sizeof(struct ofp_flow_mod) + sizeof(struct ofp_action)) * NL_FLOWS_PER_MESSAGE);
1182 goto error_free_skb;
1185 /* internal loop to fill NL_FLOWS_PER_MESSAGE flows */
1186 ofm_ptr = nla_data(attr);
1187 flow_extract_match(&in_flow, &matchme->match);
1188 while (iter.flow && count < NL_FLOWS_PER_MESSAGE) {
1189 if(flow_matches(&in_flow, &iter.flow->key)){
1190 if((err = dp_fill_flow((struct ofp_flow_mod*)ofm_ptr, &iter)))
1191 goto error_free_skb;
1193 /* TODO support multiple actions */
1194 ofm_ptr += sizeof(struct ofp_flow_mod) + sizeof(struct ofp_action);
1196 table->iterator_next(&iter);
1199 *((uint32_t*)nla_data(num_attr)) = count;
1200 genlmsg_end(skb, data);
1205 err = genlmsg_unicast(skb, info->snd_pid);
1209 /* send a sentinal message saying we're done */
1210 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
1214 data = dp_init_nl_flow_msg(dp->dp_idx, table_idx, info, skb);
1217 goto error_free_skb;
1220 NLA_PUT_U32(skb, DP_GENL_A_NUMFLOWS, 0);
1221 /* dummy flow so nl doesn't complain */
1222 attr = nla_reserve(skb, DP_GENL_A_FLOW, sizeof(struct ofp_flow_mod));
1225 goto error_free_skb;
1227 genlmsg_end(skb, data);
1228 err = genlmsg_reply(skb, info); skb = 0;
1237 /* Helper function to query_table which creates and sends a message packed with
1238 * table stats. Message form is:
1242 * OFP_TABLE (list of OFP_TABLES)
1248 dp_dump_table_stats(struct datapath *dp, int dp_idx, struct genl_info *info)
1250 struct sk_buff *skb = 0;
1251 struct ofp_table *ot = 0;
1252 struct nlattr *attr;
1253 struct sw_table_stats stats;
1257 int nt = dp->chain->n_tables;
1259 /* u32 IDX, u32 NUMTABLES, list-of-tables */
1260 skb = nlmsg_new(4 + 4 + (sizeof(struct ofp_table) * nt), GFP_ATOMIC);
1265 data = genlmsg_put_reply(skb, info, &dp_genl_family, 0,
1266 DP_GENL_C_QUERY_TABLE);
1271 NLA_PUT_U32(skb, DP_GENL_A_DP_IDX, dp_idx);
1272 NLA_PUT_U32(skb, DP_GENL_A_NUMTABLES, nt);
1274 /* ... we assume that all tables can fit in a single message.
1275 * Probably a reasonable assumption seeing that we only have
1277 attr = nla_reserve(skb, DP_GENL_A_TABLE, (sizeof(struct ofp_table) * nt));
1280 goto error_free_skb;
1283 ot = nla_data(attr);
1285 for (i = 0; i < nt; ++i) {
1286 dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
1287 ot->header.version = OFP_VERSION;
1288 ot->header.type = OFPT_TABLE;
1289 ot->header.length = htons(sizeof(struct ofp_table));
1290 ot->header.xid = htonl(0);
1292 strncpy(ot->name, stats.name, OFP_MAX_TABLE_NAME_LEN);
1293 ot->table_id = htons(i);
1294 ot->n_flows = htonl(stats.n_flows);
1295 ot->max_flows = htonl(stats.max_flows);
1300 genlmsg_end(skb, data);
1301 err = genlmsg_reply(skb, info); skb = 0;
1311 * Queries a datapath for flow-table statistics
1315 static int dp_genl_table_query(struct sk_buff *skb, struct genl_info *info)
1317 struct datapath* dp;
1320 if (!info->attrs[DP_GENL_A_DP_IDX]) {
1321 dprintk("dp::dp_genl_table_query received message with missing attributes\n");
1326 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1332 err = dp_dump_table_stats(dp, nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]), info);
1340 * Queries a datapath for flow-table entries.
1343 static int dp_genl_flow_query(struct sk_buff *skb, struct genl_info *info)
1345 struct datapath* dp;
1346 struct ofp_flow_mod* ofm;
1350 if (!info->attrs[DP_GENL_A_DP_IDX]
1351 || !info->attrs[DP_GENL_A_TABLEIDX]
1352 || !info->attrs[DP_GENL_A_FLOW]) {
1353 dprintk("dp::dp_genl_flow_query received message with missing attributes\n");
1358 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1364 table_idx = nla_get_u16(info->attrs[DP_GENL_A_TABLEIDX]);
1366 if (dp->chain->n_tables <= table_idx){
1367 printk("table index %d invalid (dp has %d tables)\n",
1368 table_idx, dp->chain->n_tables);
1373 ofm = nla_data(info->attrs[DP_GENL_A_FLOW]);
1374 err = dp_dump_table(dp, table_idx, info, ofm);
1381 static struct nla_policy dp_genl_flow_policy[DP_GENL_A_MAX + 1] = {
1382 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1383 [DP_GENL_A_TABLEIDX] = { .type = NLA_U16 },
1384 [DP_GENL_A_NUMFLOWS] = { .type = NLA_U32 },
1387 static struct genl_ops dp_genl_ops_query_flow = {
1388 .cmd = DP_GENL_C_QUERY_FLOW,
1389 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1390 .policy = dp_genl_flow_policy,
1391 .doit = dp_genl_flow_query,
1395 static struct nla_policy dp_genl_table_policy[DP_GENL_A_MAX + 1] = {
1396 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1399 static struct genl_ops dp_genl_ops_query_table = {
1400 .cmd = DP_GENL_C_QUERY_TABLE,
1401 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1402 .policy = dp_genl_table_policy,
1403 .doit = dp_genl_table_query,
1408 static struct genl_ops dp_genl_ops_query_dp = {
1409 .cmd = DP_GENL_C_QUERY_DP,
1410 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1411 .policy = dp_genl_policy,
1412 .doit = dp_genl_query,
1416 static int dp_genl_add_del_port(struct sk_buff *skb, struct genl_info *info)
1418 struct datapath *dp;
1419 struct net_device *port;
1422 if (!info->attrs[DP_GENL_A_DP_IDX] || !info->attrs[DP_GENL_A_PORTNAME])
1426 mutex_lock(&dp_mutex);
1427 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1433 /* Get interface to add/remove. */
1434 port = dev_get_by_name(&init_net,
1435 nla_data(info->attrs[DP_GENL_A_PORTNAME]));
1441 /* Execute operation. */
1442 if (info->genlhdr->cmd == DP_GENL_C_ADD_PORT)
1443 err = add_switch_port(dp, port);
1445 if (port->br_port == NULL || port->br_port->dp != dp) {
1449 err = del_switch_port(port->br_port);
1455 mutex_unlock(&dp_mutex);
1459 static struct genl_ops dp_genl_ops_add_port = {
1460 .cmd = DP_GENL_C_ADD_PORT,
1461 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1462 .policy = dp_genl_policy,
1463 .doit = dp_genl_add_del_port,
1467 static struct genl_ops dp_genl_ops_del_port = {
1468 .cmd = DP_GENL_C_DEL_PORT,
1469 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1470 .policy = dp_genl_policy,
1471 .doit = dp_genl_add_del_port,
1475 static int dp_genl_openflow(struct sk_buff *skb, struct genl_info *info)
1477 struct nlattr *va = info->attrs[DP_GENL_A_OPENFLOW];
1478 struct datapath *dp;
1481 if (!info->attrs[DP_GENL_A_DP_IDX] || !va)
1485 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1491 va = info->attrs[DP_GENL_A_OPENFLOW];
1493 err = fwd_control_input(dp->chain, nla_data(va), nla_len(va));
1500 static struct nla_policy dp_genl_openflow_policy[DP_GENL_A_MAX + 1] = {
1501 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1504 static struct genl_ops dp_genl_ops_openflow = {
1505 .cmd = DP_GENL_C_OPENFLOW,
1506 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1507 .policy = dp_genl_openflow_policy,
1508 .doit = dp_genl_openflow,
1512 static struct nla_policy dp_genl_benchmark_policy[DP_GENL_A_MAX + 1] = {
1513 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1514 [DP_GENL_A_NPACKETS] = { .type = NLA_U32 },
1515 [DP_GENL_A_PSIZE] = { .type = NLA_U32 },
1518 static struct genl_ops dp_genl_ops_benchmark_nl = {
1519 .cmd = DP_GENL_C_BENCHMARK_NL,
1520 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1521 .policy = dp_genl_benchmark_policy,
1522 .doit = dp_genl_benchmark_nl,
1526 static struct genl_ops *dp_genl_all_ops[] = {
1527 /* Keep this operation first. Generic Netlink dispatching
1528 * looks up operations with linear search, so we want it at the
1530 &dp_genl_ops_openflow,
1532 &dp_genl_ops_query_flow,
1533 &dp_genl_ops_query_table,
1534 &dp_genl_ops_show_dp,
1535 &dp_genl_ops_add_dp,
1536 &dp_genl_ops_del_dp,
1537 &dp_genl_ops_query_dp,
1538 &dp_genl_ops_add_port,
1539 &dp_genl_ops_del_port,
1540 &dp_genl_ops_benchmark_nl,
1543 static int dp_init_netlink(void)
1548 err = genl_register_family(&dp_genl_family);
1552 for (i = 0; i < ARRAY_SIZE(dp_genl_all_ops); i++) {
1553 err = genl_register_ops(&dp_genl_family, dp_genl_all_ops[i]);
1555 goto err_unregister;
1558 strcpy(mc_group.name, "openflow");
1559 err = genl_register_mc_group(&dp_genl_family, &mc_group);
1561 goto err_unregister;
1566 genl_unregister_family(&dp_genl_family);
1570 static void dp_uninit_netlink(void)
1572 genl_unregister_family(&dp_genl_family);
1575 #define DRV_NAME "openflow"
1576 #define DRV_VERSION VERSION
1577 #define DRV_DESCRIPTION "OpenFlow switching datapath implementation"
1578 #define DRV_COPYRIGHT "Copyright (c) 2007 The Board of Trustees of The Leland Stanford Junior University"
1581 static int __init dp_init(void)
1585 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION "\n");
1586 printk(KERN_INFO DRV_NAME ": " VERSION" built on "__DATE__" "__TIME__"\n");
1587 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
1593 err = dp_init_netlink();
1595 goto error_flow_exit;
1597 /* Hook into callback used by the bridge to intercept packets.
1598 * Parasites we are. */
1599 if (br_handle_frame_hook)
1600 printk("openflow: hijacking bridge hook\n");
1601 br_handle_frame_hook = dp_frame_hook;
1608 printk(KERN_EMERG "openflow: failed to install!");
1612 static void dp_cleanup(void)
1615 dp_uninit_netlink();
1617 br_handle_frame_hook = NULL;
1620 module_init(dp_init);
1621 module_exit(dp_cleanup);
1623 MODULE_DESCRIPTION(DRV_DESCRIPTION);
1624 MODULE_AUTHOR(DRV_COPYRIGHT);
1625 MODULE_LICENSE("GPL");