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;
539 SKB_LINEAR_ASSERT(skb);
540 memcpy(opi->data, skb_mac_header(skb), fwd_len);
542 err = genlmsg_end(f_skb, data);
544 goto error_free_f_skb;
546 err = genlmsg_multicast(f_skb, 0, mc_group.id, GFP_ATOMIC);
547 if (err && net_ratelimit())
548 printk(KERN_WARNING "dp_output_control: genlmsg_multicast failed: %d\n", err);
560 printk(KERN_ERR "dp_output_control: failed to send: %d\n", err);
564 static void fill_port_desc(struct net_bridge_port *p, struct ofp_phy_port *desc)
566 desc->port_no = htons(p->port_no);
567 strncpy(desc->name, p->dev->name, OFP_MAX_PORT_NAME_LEN);
568 desc->name[OFP_MAX_PORT_NAME_LEN-1] = '\0';
569 memcpy(desc->hw_addr, p->dev->dev_addr, ETH_ALEN);
570 desc->flags = htonl(p->flags);
574 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,24)
575 if (p->dev->ethtool_ops && p->dev->ethtool_ops->get_settings) {
576 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
578 if (!p->dev->ethtool_ops->get_settings(p->dev, &ecmd)) {
579 if (ecmd.supported & SUPPORTED_10baseT_Half)
580 desc->features |= OFPPF_10MB_HD;
581 if (ecmd.supported & SUPPORTED_10baseT_Full)
582 desc->features |= OFPPF_10MB_FD;
583 if (ecmd.supported & SUPPORTED_100baseT_Half)
584 desc->features |= OFPPF_100MB_HD;
585 if (ecmd.supported & SUPPORTED_100baseT_Full)
586 desc->features |= OFPPF_100MB_FD;
587 if (ecmd.supported & SUPPORTED_1000baseT_Half)
588 desc->features |= OFPPF_1GB_HD;
589 if (ecmd.supported & SUPPORTED_1000baseT_Full)
590 desc->features |= OFPPF_1GB_FD;
591 /* 10Gbps half-duplex doesn't exist... */
592 if (ecmd.supported & SUPPORTED_10000baseT_Full)
593 desc->features |= OFPPF_10GB_FD;
595 desc->features = htonl(desc->features);
596 desc->speed = htonl(ecmd.speed);
603 fill_data_hello(struct datapath *dp, struct ofp_data_hello *odh)
605 struct net_bridge_port *p;
608 odh->header.version = OFP_VERSION;
609 odh->header.type = OFPT_DATA_HELLO;
610 odh->header.xid = htonl(0);
611 odh->datapath_id = cpu_to_be64(dp->id);
613 odh->n_exact = htonl(2 * TABLE_HASH_MAX_FLOWS);
614 odh->n_mac_only = htonl(TABLE_MAC_MAX_FLOWS);
615 odh->n_compression = 0; /* Not supported */
616 odh->n_general = htonl(TABLE_LINEAR_MAX_FLOWS);
617 odh->buffer_mb = htonl(UINT32_MAX);
618 odh->n_buffers = htonl(N_PKT_BUFFERS);
619 odh->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
620 odh->actions = htonl(OFP_SUPPORTED_ACTIONS);
621 odh->miss_send_len = htons(dp->miss_send_len);
623 list_for_each_entry_rcu (p, &dp->port_list, node) {
624 fill_port_desc(p, &odh->ports[port_count]);
632 dp_send_hello(struct datapath *dp)
636 struct ofp_data_hello *odh;
637 size_t odh_max_len, odh_len, port_max_len, len;
643 /* Overallocate, since we can't reliably determine the number of
645 port_max_len = sizeof(struct ofp_phy_port) * OFPP_MAX;
647 len = nla_total_size(sizeof(*odh) + port_max_len)
648 + nla_total_size(sizeof(uint32_t));
650 skb = genlmsg_new(len, GFP_ATOMIC);
653 printk("dp_send_hello: genlmsg_new failed\n");
657 data = genlmsg_put(skb, 0, 0, &dp_genl_family, 0,
661 printk("dp_send_hello: genlmsg_put failed\n");
665 NLA_PUT_U32(skb, DP_GENL_A_DP_IDX, dp->dp_idx);
667 odh_max_len = sizeof(*odh) + port_max_len;
668 attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, odh_max_len);
671 printk("dp_send_hello: nla_reserve failed\n");
674 odh = nla_data(attr);
675 port_count = fill_data_hello(dp, odh);
677 /* Only now that we know how many ports we've added can we say
678 * say something about the length. */
679 odh_len = sizeof(*odh) + (sizeof(struct ofp_phy_port) * port_count);
680 odh->header.length = htons(odh_len);
682 /* Take back the unused part that was reserved */
683 nla_unreserve(skb, attr, (odh_max_len - odh_len));
685 err = genlmsg_end(skb, data);
688 printk("dp_send_hello: genlmsg_end failed\n");
692 err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
693 if (err && net_ratelimit())
694 printk(KERN_WARNING "dp_send_hello: genlmsg_multicast failed: %d\n", err);
702 printk(KERN_ERR "dp_send_hello: failed to send: %d\n", err);
707 dp_update_port_flags(struct datapath *dp, const struct ofp_phy_port *opp)
709 struct net_bridge_port *p;
711 p = dp->ports[htons(opp->port_no)];
713 /* Make sure the port id hasn't changed since this was sent */
714 if (!p || memcmp(opp->hw_addr, p->dev->dev_addr, ETH_ALEN) != 0)
717 p->flags = htonl(opp->flags);
724 send_port_status(struct net_bridge_port *p, uint8_t status)
728 struct ofp_port_status *ops;
733 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
736 printk("send_port_status: genlmsg_new failed\n");
740 data = genlmsg_put(skb, 0, 0, &dp_genl_family, 0,
744 printk("send_port_status: genlmsg_put failed\n");
748 NLA_PUT_U32(skb, DP_GENL_A_DP_IDX, p->dp->dp_idx);
750 attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, sizeof(*ops));
753 printk("send_port_status: nla_reserve failed\n");
757 ops = nla_data(attr);
758 ops->header.version = OFP_VERSION;
759 ops->header.type = OFPT_PORT_STATUS;
760 ops->header.length = htons(sizeof(*ops));
761 ops->header.xid = htonl(0);
763 ops->reason = status;
764 fill_port_desc(p, &ops->desc);
766 err = genlmsg_end(skb, data);
769 printk("send_port_status: genlmsg_end failed\n");
773 err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
774 if (err && net_ratelimit())
775 printk(KERN_WARNING "send_port_status: genlmsg_multicast failed: %d\n", err);
783 printk(KERN_ERR "send_port_status: failed to send: %d\n", err);
788 dp_send_flow_expired(struct datapath *dp, struct sw_flow *flow)
792 struct ofp_flow_expired *ofe;
794 unsigned long duration_j;
798 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
801 printk("dp_send_flow_expired: genlmsg_new failed\n");
805 data = genlmsg_put(skb, 0, 0, &dp_genl_family, 0,
809 printk("dp_send_flow_expired: genlmsg_put failed\n");
813 NLA_PUT_U32(skb, DP_GENL_A_DP_IDX, dp->dp_idx);
815 attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, sizeof(*ofe));
818 printk("dp_send_flow_expired: nla_reserve failed\n");
822 ofe = nla_data(attr);
823 ofe->header.version = OFP_VERSION;
824 ofe->header.type = OFPT_FLOW_EXPIRED;
825 ofe->header.length = htons(sizeof(*ofe));
826 ofe->header.xid = htonl(0);
828 flow_fill_match(&ofe->match, &flow->key);
829 duration_j = (flow->timeout - HZ * flow->max_idle) - flow->init_time;
830 ofe->duration = htonl(duration_j / HZ);
831 ofe->packet_count = cpu_to_be64(flow->packet_count);
832 ofe->byte_count = cpu_to_be64(flow->byte_count);
834 err = genlmsg_end(skb, data);
837 printk("dp_send_flow_expired: genlmsg_end failed\n");
841 err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
842 if (err && net_ratelimit())
843 printk(KERN_WARNING "send_flow_expired: genlmsg_multicast failed: %d\n", err);
851 printk(KERN_ERR "send_flow_expired: failed to send: %d\n", err);
855 /* Generic Netlink interface.
857 * See netlink(7) for an introduction to netlink. See
858 * http://linux-net.osdl.org/index.php/Netlink for more information and
859 * pointers on how to work with netlink and Generic Netlink in the kernel and
862 static struct genl_family dp_genl_family = {
863 .id = GENL_ID_GENERATE,
865 .name = DP_GENL_FAMILY_NAME,
867 .maxattr = DP_GENL_A_MAX,
870 /* Attribute policy: what each attribute may contain. */
871 static struct nla_policy dp_genl_policy[DP_GENL_A_MAX + 1] = {
872 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
873 [DP_GENL_A_MC_GROUP] = { .type = NLA_U32 },
874 [DP_GENL_A_PORTNAME] = { .type = NLA_STRING }
877 static int dp_genl_add(struct sk_buff *skb, struct genl_info *info)
879 if (!info->attrs[DP_GENL_A_DP_IDX])
882 return new_dp(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
885 static struct genl_ops dp_genl_ops_add_dp = {
886 .cmd = DP_GENL_C_ADD_DP,
887 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
888 .policy = dp_genl_policy,
893 struct datapath *dp_get(int dp_idx)
895 if (dp_idx < 0 || dp_idx > DP_MAX)
897 return rcu_dereference(dps[dp_idx]);
900 static int dp_genl_del(struct sk_buff *skb, struct genl_info *info)
905 if (!info->attrs[DP_GENL_A_DP_IDX])
908 mutex_lock(&dp_mutex);
909 dp = dp_get(nla_get_u32((info->attrs[DP_GENL_A_DP_IDX])));
916 mutex_unlock(&dp_mutex);
920 static struct genl_ops dp_genl_ops_del_dp = {
921 .cmd = DP_GENL_C_DEL_DP,
922 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
923 .policy = dp_genl_policy,
928 /* Queries a datapath for related information. Currently the only relevant
929 * information is the datapath's multicast group ID. Really we want one
930 * multicast group per datapath, but because of locking issues[*] we can't
931 * easily get one. Thus, every datapath will currently return the same
932 * global multicast group ID, but in the future it would be nice to fix that.
934 * [*] dp_genl_add, to add a new datapath, is called under the genl_lock
935 * mutex, and genl_register_mc_group, called to acquire a new multicast
936 * group ID, also acquires genl_lock, thus deadlock.
938 static int dp_genl_query(struct sk_buff *skb, struct genl_info *info)
941 struct sk_buff *ans_skb = NULL;
945 if (!info->attrs[DP_GENL_A_DP_IDX])
949 dp_idx = nla_get_u32((info->attrs[DP_GENL_A_DP_IDX]));
955 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
960 data = genlmsg_put_reply(ans_skb, info, &dp_genl_family,
961 0, DP_GENL_C_QUERY_DP);
966 NLA_PUT_U32(ans_skb, DP_GENL_A_DP_IDX, dp_idx);
967 NLA_PUT_U32(ans_skb, DP_GENL_A_MC_GROUP, mc_group.id);
969 genlmsg_end(ans_skb, data);
970 err = genlmsg_reply(ans_skb, info);
983 * Fill flow entry for nl flow query. Called with rcu_lock
988 dp_fill_flow(struct ofp_flow_mod* ofm, struct swt_iterator* iter)
990 ofm->header.version = OFP_VERSION;
991 ofm->header.type = OFPT_FLOW_MOD;
992 ofm->header.length = htons(sizeof(struct ofp_flow_mod)
993 + sizeof(ofm->actions[0]));
994 ofm->header.xid = htonl(0);
996 ofm->match.wildcards = htons(iter->flow->key.wildcards);
997 ofm->match.in_port = iter->flow->key.in_port;
998 ofm->match.dl_vlan = iter->flow->key.dl_vlan;
999 memcpy(ofm->match.dl_src, iter->flow->key.dl_src, ETH_ALEN);
1000 memcpy(ofm->match.dl_dst, iter->flow->key.dl_dst, ETH_ALEN);
1001 ofm->match.dl_type = iter->flow->key.dl_type;
1002 ofm->match.nw_src = iter->flow->key.nw_src;
1003 ofm->match.nw_dst = iter->flow->key.nw_dst;
1004 ofm->match.nw_proto = iter->flow->key.nw_proto;
1005 ofm->match.tp_src = iter->flow->key.tp_src;
1006 ofm->match.tp_dst = iter->flow->key.tp_dst;
1007 ofm->group_id = iter->flow->group_id;
1008 ofm->max_idle = iter->flow->max_idle;
1009 /* TODO support multiple actions */
1010 ofm->actions[0] = iter->flow->actions[0];
1015 static int dp_genl_show(struct sk_buff *skb, struct genl_info *info)
1017 struct datapath *dp;
1019 struct sk_buff *ans_skb = NULL;
1021 struct nlattr *attr;
1022 struct ofp_data_hello *odh;
1023 size_t odh_max_len, odh_len, port_max_len, len;
1026 if (!info->attrs[DP_GENL_A_DP_IDX])
1029 mutex_lock(&dp_mutex);
1030 dp = dp_get(nla_get_u32((info->attrs[DP_GENL_A_DP_IDX])));
1034 /* Overallocate, since we can't reliably determine the number of
1035 * ports a priori. */
1036 port_max_len = sizeof(struct ofp_phy_port) * OFPP_MAX;
1038 len = nla_total_size(sizeof(*odh) + port_max_len)
1039 + nla_total_size(sizeof(uint32_t));
1041 ans_skb = nlmsg_new(len, GFP_KERNEL);
1045 data = genlmsg_put_reply(ans_skb, info, &dp_genl_family,
1046 0, DP_GENL_C_SHOW_DP);
1050 NLA_PUT_U32(ans_skb, DP_GENL_A_DP_IDX, dp->dp_idx);
1052 odh_max_len = sizeof(*odh) + port_max_len;
1053 attr = nla_reserve(ans_skb, DP_GENL_A_DP_INFO, odh_max_len);
1056 odh = nla_data(attr);
1057 port_count = fill_data_hello(dp, odh);
1059 /* Only now that we know how many ports we've added can we say
1060 * say something about the length. */
1061 odh_len = sizeof(*odh) + (sizeof(struct ofp_phy_port) * port_count);
1062 odh->header.length = htons(odh_len);
1064 /* Take back the unused part that was reserved */
1065 nla_unreserve(ans_skb, attr, (odh_max_len - odh_len));
1067 genlmsg_end(ans_skb, data);
1068 err = genlmsg_reply(ans_skb, info);
1076 mutex_unlock(&dp_mutex);
1080 static struct genl_ops dp_genl_ops_show_dp = {
1081 .cmd = DP_GENL_C_SHOW_DP,
1082 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1083 .policy = dp_genl_policy,
1084 .doit = dp_genl_show,
1088 /* Convenience function */
1091 dp_init_nl_flow_msg(uint32_t dp_idx, uint16_t table_idx,
1092 struct genl_info *info, struct sk_buff* skb)
1096 data = genlmsg_put_reply(skb, info, &dp_genl_family, 0,
1097 DP_GENL_C_QUERY_FLOW);
1100 NLA_PUT_U32(skb, DP_GENL_A_DP_IDX, dp_idx);
1101 NLA_PUT_U16(skb, DP_GENL_A_TABLEIDX, table_idx);
1109 /* Iterate through the specified table and send all flow entries over
1110 * netlink to userspace. Each flow message has the following format:
1114 * 32bit number of flows
1115 * openflow-flow-entries
1117 * The full table may require multiple messages. A message with 0 flows
1118 * signifies end-of message.
1123 dp_dump_table(struct datapath *dp, uint16_t table_idx, struct genl_info *info, struct ofp_flow_mod* matchme)
1125 struct sk_buff *skb = 0;
1126 struct sw_table *table = 0;
1127 struct swt_iterator iter;
1128 struct sw_flow_key in_flow;
1129 struct nlattr *attr;
1130 int count = 0, sum_count = 0;
1132 uint8_t* ofm_ptr = 0;
1133 struct nlattr *num_attr;
1136 table = dp->chain->tables[table_idx];
1137 if ( table == NULL ) {
1138 dprintk("dp::dp_dump_table error, non-existant table at position %d\n", table_idx);
1142 if (!table->iterator(table, &iter)) {
1143 dprintk("dp::dp_dump_table couldn't initialize empty table iterator\n");
1149 /* verify that we can fit all NL_FLOWS_PER_MESSAGE in a single
1151 if( (sizeof(dp_genl_family) + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint32_t) +
1152 (NL_FLOWS_PER_MESSAGE * sizeof(struct ofp_flow_mod))) > (8192 - 64)){
1153 dprintk("dp::dp_dump_table NL_FLOWS_PER_MESSAGE may cause overrun in skbuf\n");
1157 skb = nlmsg_new(8192 - 64, GFP_ATOMIC);
1162 data = dp_init_nl_flow_msg(dp->dp_idx, table_idx, info, skb);
1165 goto error_free_skb;
1168 /* reserve space to put the number of flows for this message, to
1169 * be filled after the loop*/
1170 num_attr = nla_reserve(skb, DP_GENL_A_NUMFLOWS, sizeof(uint32_t));
1173 goto error_free_skb;
1176 /* Only load NL_FLOWS_PER_MESSAGE flows at a time */
1177 attr = nla_reserve(skb, DP_GENL_A_FLOW,
1178 (sizeof(struct ofp_flow_mod) + sizeof(struct ofp_action)) * NL_FLOWS_PER_MESSAGE);
1181 goto error_free_skb;
1184 /* internal loop to fill NL_FLOWS_PER_MESSAGE flows */
1185 ofm_ptr = nla_data(attr);
1186 flow_extract_match(&in_flow, &matchme->match);
1187 while (iter.flow && count < NL_FLOWS_PER_MESSAGE) {
1188 if(flow_matches(&in_flow, &iter.flow->key)){
1189 if((err = dp_fill_flow((struct ofp_flow_mod*)ofm_ptr, &iter)))
1190 goto error_free_skb;
1192 /* TODO support multiple actions */
1193 ofm_ptr += sizeof(struct ofp_flow_mod) + sizeof(struct ofp_action);
1195 table->iterator_next(&iter);
1198 *((uint32_t*)nla_data(num_attr)) = count;
1199 genlmsg_end(skb, data);
1204 err = genlmsg_unicast(skb, info->snd_pid);
1208 /* send a sentinal message saying we're done */
1209 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
1213 data = dp_init_nl_flow_msg(dp->dp_idx, table_idx, info, skb);
1216 goto error_free_skb;
1219 NLA_PUT_U32(skb, DP_GENL_A_NUMFLOWS, 0);
1220 /* dummy flow so nl doesn't complain */
1221 attr = nla_reserve(skb, DP_GENL_A_FLOW, sizeof(struct ofp_flow_mod));
1224 goto error_free_skb;
1226 genlmsg_end(skb, data);
1227 err = genlmsg_reply(skb, info); skb = 0;
1236 /* Helper function to query_table which creates and sends a message packed with
1237 * table stats. Message form is:
1241 * OFP_TABLE (list of OFP_TABLES)
1247 dp_dump_table_stats(struct datapath *dp, int dp_idx, struct genl_info *info)
1249 struct sk_buff *skb = 0;
1250 struct ofp_table *ot = 0;
1251 struct nlattr *attr;
1252 struct sw_table_stats stats;
1256 int nt = dp->chain->n_tables;
1258 /* u32 IDX, u32 NUMTABLES, list-of-tables */
1259 skb = nlmsg_new(4 + 4 + (sizeof(struct ofp_table) * nt), GFP_ATOMIC);
1264 data = genlmsg_put_reply(skb, info, &dp_genl_family, 0,
1265 DP_GENL_C_QUERY_TABLE);
1270 NLA_PUT_U32(skb, DP_GENL_A_DP_IDX, dp_idx);
1271 NLA_PUT_U32(skb, DP_GENL_A_NUMTABLES, nt);
1273 /* ... we assume that all tables can fit in a single message.
1274 * Probably a reasonable assumption seeing that we only have
1276 attr = nla_reserve(skb, DP_GENL_A_TABLE, (sizeof(struct ofp_table) * nt));
1279 goto error_free_skb;
1282 ot = nla_data(attr);
1284 for (i = 0; i < nt; ++i) {
1285 dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
1286 ot->header.version = OFP_VERSION;
1287 ot->header.type = OFPT_TABLE;
1288 ot->header.length = htons(sizeof(struct ofp_table));
1289 ot->header.xid = htonl(0);
1291 strncpy(ot->name, stats.name, OFP_MAX_TABLE_NAME_LEN);
1292 ot->table_id = htons(i);
1293 ot->n_flows = htonl(stats.n_flows);
1294 ot->max_flows = htonl(stats.max_flows);
1299 genlmsg_end(skb, data);
1300 err = genlmsg_reply(skb, info); skb = 0;
1310 * Queries a datapath for flow-table statistics
1314 static int dp_genl_table_query(struct sk_buff *skb, struct genl_info *info)
1316 struct datapath* dp;
1319 if (!info->attrs[DP_GENL_A_DP_IDX]) {
1320 dprintk("dp::dp_genl_table_query received message with missing attributes\n");
1325 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1331 err = dp_dump_table_stats(dp, nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]), info);
1339 * Queries a datapath for flow-table entries.
1342 static int dp_genl_flow_query(struct sk_buff *skb, struct genl_info *info)
1344 struct datapath* dp;
1345 struct ofp_flow_mod* ofm;
1349 if (!info->attrs[DP_GENL_A_DP_IDX]
1350 || !info->attrs[DP_GENL_A_TABLEIDX]
1351 || !info->attrs[DP_GENL_A_FLOW]) {
1352 dprintk("dp::dp_genl_flow_query received message with missing attributes\n");
1357 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1363 table_idx = nla_get_u16(info->attrs[DP_GENL_A_TABLEIDX]);
1365 if (dp->chain->n_tables <= table_idx){
1366 printk("table index %d invalid (dp has %d tables)\n",
1367 table_idx, dp->chain->n_tables);
1372 ofm = nla_data(info->attrs[DP_GENL_A_FLOW]);
1373 err = dp_dump_table(dp, table_idx, info, ofm);
1380 static struct nla_policy dp_genl_flow_policy[DP_GENL_A_MAX + 1] = {
1381 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1382 [DP_GENL_A_TABLEIDX] = { .type = NLA_U16 },
1383 [DP_GENL_A_NUMFLOWS] = { .type = NLA_U32 },
1386 static struct genl_ops dp_genl_ops_query_flow = {
1387 .cmd = DP_GENL_C_QUERY_FLOW,
1388 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1389 .policy = dp_genl_flow_policy,
1390 .doit = dp_genl_flow_query,
1394 static struct nla_policy dp_genl_table_policy[DP_GENL_A_MAX + 1] = {
1395 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1398 static struct genl_ops dp_genl_ops_query_table = {
1399 .cmd = DP_GENL_C_QUERY_TABLE,
1400 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1401 .policy = dp_genl_table_policy,
1402 .doit = dp_genl_table_query,
1407 static struct genl_ops dp_genl_ops_query_dp = {
1408 .cmd = DP_GENL_C_QUERY_DP,
1409 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1410 .policy = dp_genl_policy,
1411 .doit = dp_genl_query,
1415 static int dp_genl_add_del_port(struct sk_buff *skb, struct genl_info *info)
1417 struct datapath *dp;
1418 struct net_device *port;
1421 if (!info->attrs[DP_GENL_A_DP_IDX] || !info->attrs[DP_GENL_A_PORTNAME])
1425 mutex_lock(&dp_mutex);
1426 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1432 /* Get interface to add/remove. */
1433 port = dev_get_by_name(&init_net,
1434 nla_data(info->attrs[DP_GENL_A_PORTNAME]));
1440 /* Execute operation. */
1441 if (info->genlhdr->cmd == DP_GENL_C_ADD_PORT)
1442 err = add_switch_port(dp, port);
1444 if (port->br_port == NULL || port->br_port->dp != dp) {
1448 err = del_switch_port(port->br_port);
1454 mutex_unlock(&dp_mutex);
1458 static struct genl_ops dp_genl_ops_add_port = {
1459 .cmd = DP_GENL_C_ADD_PORT,
1460 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1461 .policy = dp_genl_policy,
1462 .doit = dp_genl_add_del_port,
1466 static struct genl_ops dp_genl_ops_del_port = {
1467 .cmd = DP_GENL_C_DEL_PORT,
1468 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1469 .policy = dp_genl_policy,
1470 .doit = dp_genl_add_del_port,
1474 static int dp_genl_openflow(struct sk_buff *skb, struct genl_info *info)
1476 struct nlattr *va = info->attrs[DP_GENL_A_OPENFLOW];
1477 struct datapath *dp;
1480 if (!info->attrs[DP_GENL_A_DP_IDX] || !va)
1484 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1490 va = info->attrs[DP_GENL_A_OPENFLOW];
1492 err = fwd_control_input(dp->chain, nla_data(va), nla_len(va));
1499 static struct nla_policy dp_genl_openflow_policy[DP_GENL_A_MAX + 1] = {
1500 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1503 static struct genl_ops dp_genl_ops_openflow = {
1504 .cmd = DP_GENL_C_OPENFLOW,
1505 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1506 .policy = dp_genl_openflow_policy,
1507 .doit = dp_genl_openflow,
1511 static struct nla_policy dp_genl_benchmark_policy[DP_GENL_A_MAX + 1] = {
1512 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1513 [DP_GENL_A_NPACKETS] = { .type = NLA_U32 },
1514 [DP_GENL_A_PSIZE] = { .type = NLA_U32 },
1517 static struct genl_ops dp_genl_ops_benchmark_nl = {
1518 .cmd = DP_GENL_C_BENCHMARK_NL,
1519 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1520 .policy = dp_genl_benchmark_policy,
1521 .doit = dp_genl_benchmark_nl,
1525 static struct genl_ops *dp_genl_all_ops[] = {
1526 /* Keep this operation first. Generic Netlink dispatching
1527 * looks up operations with linear search, so we want it at the
1529 &dp_genl_ops_openflow,
1531 &dp_genl_ops_query_flow,
1532 &dp_genl_ops_query_table,
1533 &dp_genl_ops_show_dp,
1534 &dp_genl_ops_add_dp,
1535 &dp_genl_ops_del_dp,
1536 &dp_genl_ops_query_dp,
1537 &dp_genl_ops_add_port,
1538 &dp_genl_ops_del_port,
1539 &dp_genl_ops_benchmark_nl,
1542 static int dp_init_netlink(void)
1547 err = genl_register_family(&dp_genl_family);
1551 for (i = 0; i < ARRAY_SIZE(dp_genl_all_ops); i++) {
1552 err = genl_register_ops(&dp_genl_family, dp_genl_all_ops[i]);
1554 goto err_unregister;
1557 strcpy(mc_group.name, "openflow");
1558 err = genl_register_mc_group(&dp_genl_family, &mc_group);
1560 goto err_unregister;
1565 genl_unregister_family(&dp_genl_family);
1569 static void dp_uninit_netlink(void)
1571 genl_unregister_family(&dp_genl_family);
1574 #define DRV_NAME "openflow"
1575 #define DRV_VERSION VERSION
1576 #define DRV_DESCRIPTION "OpenFlow switching datapath implementation"
1577 #define DRV_COPYRIGHT "Copyright (c) 2007 The Board of Trustees of The Leland Stanford Junior University"
1580 static int __init dp_init(void)
1584 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION "\n");
1585 printk(KERN_INFO DRV_NAME ": " VERSION" built on "__DATE__" "__TIME__"\n");
1586 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
1592 err = dp_init_netlink();
1594 goto error_flow_exit;
1596 /* Hook into callback used by the bridge to intercept packets.
1597 * Parasites we are. */
1598 if (br_handle_frame_hook)
1599 printk("openflow: hijacking bridge hook\n");
1600 br_handle_frame_hook = dp_frame_hook;
1607 printk(KERN_EMERG "openflow: failed to install!");
1611 static void dp_cleanup(void)
1614 dp_uninit_netlink();
1616 br_handle_frame_hook = NULL;
1619 module_init(dp_init);
1620 module_exit(dp_cleanup);
1622 MODULE_DESCRIPTION(DRV_DESCRIPTION);
1623 MODULE_AUTHOR(DRV_COPYRIGHT);
1624 MODULE_LICENSE("GPL");