2 * Distributed under the terms of the GNU GPL version 2.
3 * Copyright (c) 2007, 2008 The Board of Trustees of The Leland
4 * Stanford Junior University
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/delay.h>
17 #include <linux/etherdevice.h>
18 #include <linux/kernel.h>
19 #include <linux/kthread.h>
20 #include <linux/mutex.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/rcupdate.h>
23 #include <linux/version.h>
24 #include <linux/ethtool.h>
25 #include <linux/random.h>
26 #include <asm/system.h>
27 #include <linux/netfilter_bridge.h>
28 #include <linux/inetdevice.h>
29 #include <linux/list.h>
31 #include "openflow-netlink.h"
37 #include "datapath_t.h"
42 /* Number of milliseconds between runs of the maintenance thread. */
43 #define MAINT_SLEEP_MSECS 1000
45 #define BRIDGE_PORT_NO_FLOOD 0x00000001
47 #define UINT32_MAX 4294967295U
48 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
50 struct net_bridge_port {
54 struct net_device *dev;
55 struct list_head node; /* Element in datapath.ports. */
58 static struct genl_family dp_genl_family;
59 static struct genl_multicast_group mc_group;
61 int dp_dev_setup(struct net_device *dev);
63 /* It's hard to imagine wanting more than one datapath, but... */
66 /* datapaths. Protected on the read side by rcu_read_lock, on the write side
69 * It is safe to access the datapath and net_bridge_port structures with just
70 * the dp_mutex, but to access the chain you need to take the rcu_read_lock
71 * also (because dp_mutex doesn't prevent flows from being destroyed).
73 static struct datapath *dps[DP_MAX];
74 static DEFINE_MUTEX(dp_mutex);
76 static int dp_maint_func(void *data);
77 static int send_port_status(struct net_bridge_port *p, uint8_t status);
80 /* nla_unreserve - reduce amount of space reserved by nla_reserve
81 * @skb: socket buffer from which to recover room
82 * @nla: netlink attribute to adjust
83 * @len: amount by which to reduce attribute payload
85 * Reduces amount of space reserved by a call to nla_reserve.
87 * No other attributes may be added between calling nla_reserve and this
88 * function, since it will create a hole in the message.
90 void nla_unreserve(struct sk_buff *skb, struct nlattr *nla, int len)
99 alloc_openflow_skb(struct datapath *dp, size_t openflow_len,
100 uint8_t type, uint32_t xid, struct sk_buff **pskb)
105 struct ofp_header *oh;
107 genl_len = nla_total_size(sizeof(uint32_t)); /* DP_GENL_A_DP_IDX */
108 genl_len += nla_total_size(openflow_len); /* DP_GENL_A_OPENFLOW */
109 skb = *pskb = genlmsg_new(genl_len, GFP_ATOMIC);
112 printk("alloc_openflow_skb: genlmsg_new failed\n");
116 /* Assemble the Generic Netlink wrapper. */
117 if (!genlmsg_put(skb, 0, 0, &dp_genl_family, 0, DP_GENL_C_OPENFLOW))
119 if (nla_put_u32(skb, DP_GENL_A_DP_IDX, dp->dp_idx) < 0)
121 attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, openflow_len);
123 nlmsg_end(skb, (struct nlmsghdr *) skb->data);
125 /* Fill in the header. */
127 oh->version = OFP_VERSION;
129 oh->length = htons(openflow_len);
136 resize_openflow_skb(struct sk_buff *skb,
137 struct ofp_header *oh, size_t new_length)
141 BUG_ON(new_length > ntohs(oh->length));
142 attr = ((void *) oh) - NLA_HDRLEN;
143 nla_unreserve(skb, attr, ntohs(oh->length) - new_length);
144 oh->length = htons(new_length);
145 nlmsg_end(skb, (struct nlmsghdr *) skb->data);
148 /* Generates a unique datapath id. It incorporates the datapath index
149 * and a hardware address, if available. If not, it generates a random
153 uint64_t gen_datapath_id(uint16_t dp_idx)
157 struct net_device *dev;
159 /* The top 16 bits are used to identify the datapath. The lower 48 bits
160 * use an interface address. */
161 id = (uint64_t)dp_idx << 48;
162 if ((dev = dev_get_by_name(&init_net, "ctl0"))
163 || (dev = dev_get_by_name(&init_net, "eth0"))) {
164 for (i=0; i<ETH_ALEN; i++) {
165 id |= (uint64_t)dev->dev_addr[i] << (8*(ETH_ALEN-1 - i));
169 /* Randomly choose the lower 48 bits if we cannot find an
170 * address and mark the most significant bit to indicate that
171 * this was randomly generated. */
172 uint8_t rand[ETH_ALEN];
173 get_random_bytes(rand, ETH_ALEN);
174 id |= (uint64_t)1 << 63;
175 for (i=0; i<ETH_ALEN; i++) {
176 id |= (uint64_t)rand[i] << (8*(ETH_ALEN-1 - i));
183 /* Creates a new datapath numbered 'dp_idx'. Returns 0 for success or a
184 * negative error code.
186 * Not called with any locks. */
187 static int new_dp(int dp_idx)
192 if (dp_idx < 0 || dp_idx >= DP_MAX)
195 if (!try_module_get(THIS_MODULE))
198 mutex_lock(&dp_mutex);
199 dp = rcu_dereference(dps[dp_idx]);
206 dp = kzalloc(sizeof *dp, GFP_KERNEL);
211 dp->id = gen_datapath_id(dp_idx);
212 dp->chain = chain_create(dp);
213 if (dp->chain == NULL)
215 INIT_LIST_HEAD(&dp->port_list);
218 /* Setup our "of" device */
221 err = dp_dev_setup(&dp->dev);
224 printk("datapath: problem setting up 'of' device\n");
227 dp->config.flags = 0;
228 dp->config.miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
230 dp->dp_task = kthread_run(dp_maint_func, dp, "dp%d", dp_idx);
231 if (IS_ERR(dp->dp_task))
234 rcu_assign_pointer(dps[dp_idx], dp);
235 mutex_unlock(&dp_mutex);
242 mutex_unlock(&dp_mutex);
243 module_put(THIS_MODULE);
247 /* Find and return a free port number under 'dp'. Called under dp_mutex. */
248 static int find_portno(struct datapath *dp)
251 for (i = 0; i < OFPP_MAX; i++)
252 if (dp->ports[i] == NULL)
257 static struct net_bridge_port *new_nbp(struct datapath *dp,
258 struct net_device *dev)
260 struct net_bridge_port *p;
263 port_no = find_portno(dp);
265 return ERR_PTR(port_no);
267 p = kzalloc(sizeof(*p), GFP_KERNEL);
269 return ERR_PTR(-ENOMEM);
274 p->port_no = port_no;
279 /* Called with dp_mutex. */
280 int add_switch_port(struct datapath *dp, struct net_device *dev)
282 struct net_bridge_port *p;
284 if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER)
287 if (dev->br_port != NULL)
290 p = new_nbp(dp, dev);
295 rcu_assign_pointer(dev->br_port, p);
297 dev_set_promiscuity(dev, 1);
300 rcu_assign_pointer(dp->ports[p->port_no], p);
301 list_add_rcu(&p->node, &dp->port_list);
303 /* Notify the ctlpath that this port has been added */
304 send_port_status(p, OFPPR_ADD);
309 /* Delete 'p' from switch.
310 * Called with dp_mutex. */
311 static int del_switch_port(struct net_bridge_port *p)
313 /* First drop references to device. */
315 dev_set_promiscuity(p->dev, -1);
317 list_del_rcu(&p->node);
318 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
319 rcu_assign_pointer(p->dev->br_port, NULL);
321 /* Then wait until no one is still using it, and destroy it. */
324 /* Notify the ctlpath that this port no longer exists */
325 send_port_status(p, OFPPR_DELETE);
333 /* Called with dp_mutex. */
334 static void del_dp(struct datapath *dp)
336 struct net_bridge_port *p, *n;
339 /* Unregister the "of" device of this dp */
341 unregister_netdevice(&dp->dev);
345 kthread_stop(dp->dp_task);
347 /* Drop references to DP. */
348 list_for_each_entry_safe (p, n, &dp->port_list, node)
350 rcu_assign_pointer(dps[dp->dp_idx], NULL);
352 /* Wait until no longer in use, then destroy it. */
354 chain_destroy(dp->chain);
356 module_put(THIS_MODULE);
359 static int dp_maint_func(void *data)
361 struct datapath *dp = (struct datapath *) data;
363 while (!kthread_should_stop()) {
365 chain_timeout(dp->chain);
367 int count = chain_timeout(dp->chain);
368 chain_print_stats(dp->chain);
370 printk("%d flows timed out\n", count);
372 msleep_interruptible(MAINT_SLEEP_MSECS);
379 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
380 * different set of devices!) Returns 0 if *pskb should be processed further,
381 * 1 if *pskb is handled. */
382 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
383 /* Called with rcu_read_lock. */
384 static struct sk_buff *dp_frame_hook(struct net_bridge_port *p,
387 struct ethhdr *eh = eth_hdr(skb);
388 struct sk_buff *skb_local = NULL;
391 if (compare_ether_addr(eh->h_dest, skb->dev->dev_addr) == 0)
394 if (is_broadcast_ether_addr(eh->h_dest)
395 || is_multicast_ether_addr(eh->h_dest)
396 || is_local_ether_addr(eh->h_dest))
397 skb_local = skb_clone(skb, GFP_ATOMIC);
399 /* Push the Ethernet header back on. */
400 if (skb->protocol == htons(ETH_P_8021Q))
401 skb_push(skb, VLAN_ETH_HLEN);
403 skb_push(skb, ETH_HLEN);
405 fwd_port_input(p->dp->chain, skb, p->port_no);
409 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
410 static int dp_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
412 /* Push the Ethernet header back on. */
413 if ((*pskb)->protocol == htons(ETH_P_8021Q))
414 skb_push(*pskb, VLAN_ETH_HLEN);
416 skb_push(*pskb, ETH_HLEN);
418 fwd_port_input(p->dp->chain, *pskb, p->port_no);
422 /* NB: This has only been tested on 2.4.35 */
424 /* Called without any locks (?) */
425 static void dp_frame_hook(struct sk_buff *skb)
427 struct net_bridge_port *p = skb->dev->br_port;
429 /* Push the Ethernet header back on. */
430 if (skb->protocol == htons(ETH_P_8021Q))
431 skb_push(skb, VLAN_ETH_HLEN);
433 skb_push(skb, ETH_HLEN);
437 fwd_port_input(p->dp->chain, skb, p->port_no);
444 /* Forwarding output path.
445 * Based on net/bridge/br_forward.c. */
447 /* Don't forward packets to originating port or with flooding disabled */
448 static inline int should_deliver(const struct net_bridge_port *p,
449 const struct sk_buff *skb)
451 if ((skb->dev == p->dev) || (p->flags & BRIDGE_PORT_NO_FLOOD)) {
458 static inline unsigned packet_length(const struct sk_buff *skb)
460 int length = skb->len - ETH_HLEN;
461 if (skb->protocol == htons(ETH_P_8021Q))
467 flood(struct datapath *dp, struct sk_buff *skb)
469 struct net_bridge_port *p;
473 list_for_each_entry_rcu (p, &dp->port_list, node) {
474 if (!should_deliver(p, skb))
476 if (prev_port != -1) {
477 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
482 dp_output_port(dp, clone, prev_port);
484 prev_port = p->port_no;
487 dp_output_port(dp, skb, prev_port);
494 /* Marks 'skb' as having originated from 'in_port' in 'dp'.
495 FIXME: how are devices reference counted? */
496 int dp_set_origin(struct datapath *dp, uint16_t in_port,
499 if (in_port < OFPP_MAX && dp->ports[in_port]) {
500 skb->dev = dp->ports[in_port]->dev;
506 /* Takes ownership of 'skb' and transmits it to 'out_port' on 'dp'.
508 int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port)
510 struct net_bridge_port *p;
514 if (out_port == OFPP_FLOOD)
515 return flood(dp, skb);
516 else if (out_port == OFPP_CONTROLLER)
517 return dp_output_control(dp, skb, fwd_save_skb(skb), 0,
519 else if (out_port >= OFPP_MAX)
522 p = dp->ports[out_port];
527 if (packet_length(skb) > skb->dev->mtu) {
528 printk("dropped over-mtu packet: %d > %d\n",
529 packet_length(skb), skb->dev->mtu);
541 printk("can't forward to bad port %d\n", out_port);
545 /* Takes ownership of 'skb' and transmits it to 'dp''s control path. If
546 * 'buffer_id' != -1, then only the first 64 bytes of 'skb' are sent;
547 * otherwise, all of 'skb' is sent. 'reason' indicates why 'skb' is being
548 * sent. 'max_len' sets the maximum number of bytes that the caller
549 * wants to be sent; a value of 0 indicates the entire packet should be
552 dp_output_control(struct datapath *dp, struct sk_buff *skb,
553 uint32_t buffer_id, size_t max_len, int reason)
555 /* FIXME? Can we avoid creating a new skbuff in the case where we
556 * forward the whole packet? */
557 struct sk_buff *f_skb;
558 struct ofp_packet_in *opi;
559 size_t fwd_len, opi_len;
563 if ((buffer_id != (uint32_t) -1) && max_len)
564 fwd_len = min(fwd_len, max_len);
566 opi_len = offsetof(struct ofp_packet_in, data) + fwd_len;
567 opi = alloc_openflow_skb(dp, opi_len, OFPT_PACKET_IN, 0, &f_skb);
568 opi->buffer_id = htonl(buffer_id);
569 opi->total_len = htons(skb->len);
570 opi->in_port = htons(skb->dev->br_port->port_no);
571 opi->reason = reason;
573 memcpy(opi->data, skb_mac_header(skb), fwd_len);
575 err = genlmsg_multicast(f_skb, 0, mc_group.id, GFP_ATOMIC);
576 if (err && net_ratelimit())
577 printk(KERN_WARNING "dp_output_control: genlmsg_multicast failed: %d\n", err);
584 static void fill_port_desc(struct net_bridge_port *p, struct ofp_phy_port *desc)
586 desc->port_no = htons(p->port_no);
587 strncpy(desc->name, p->dev->name, OFP_MAX_PORT_NAME_LEN);
588 desc->name[OFP_MAX_PORT_NAME_LEN-1] = '\0';
589 memcpy(desc->hw_addr, p->dev->dev_addr, ETH_ALEN);
590 desc->flags = htonl(p->flags);
594 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,24)
595 if (p->dev->ethtool_ops && p->dev->ethtool_ops->get_settings) {
596 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
598 if (!p->dev->ethtool_ops->get_settings(p->dev, &ecmd)) {
599 if (ecmd.supported & SUPPORTED_10baseT_Half)
600 desc->features |= OFPPF_10MB_HD;
601 if (ecmd.supported & SUPPORTED_10baseT_Full)
602 desc->features |= OFPPF_10MB_FD;
603 if (ecmd.supported & SUPPORTED_100baseT_Half)
604 desc->features |= OFPPF_100MB_HD;
605 if (ecmd.supported & SUPPORTED_100baseT_Full)
606 desc->features |= OFPPF_100MB_FD;
607 if (ecmd.supported & SUPPORTED_1000baseT_Half)
608 desc->features |= OFPPF_1GB_HD;
609 if (ecmd.supported & SUPPORTED_1000baseT_Full)
610 desc->features |= OFPPF_1GB_FD;
611 /* 10Gbps half-duplex doesn't exist... */
612 if (ecmd.supported & SUPPORTED_10000baseT_Full)
613 desc->features |= OFPPF_10GB_FD;
615 desc->features = htonl(desc->features);
616 desc->speed = htonl(ecmd.speed);
623 fill_features_reply(struct datapath *dp, struct ofp_switch_features *ofr)
625 struct net_bridge_port *p;
628 ofr->datapath_id = cpu_to_be64(dp->id);
630 ofr->n_exact = htonl(2 * TABLE_HASH_MAX_FLOWS);
631 ofr->n_mac_only = htonl(TABLE_MAC_MAX_FLOWS);
632 ofr->n_compression = 0; /* Not supported */
633 ofr->n_general = htonl(TABLE_LINEAR_MAX_FLOWS);
634 ofr->buffer_mb = htonl(UINT32_MAX);
635 ofr->n_buffers = htonl(N_PKT_BUFFERS);
636 ofr->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
637 ofr->actions = htonl(OFP_SUPPORTED_ACTIONS);
639 list_for_each_entry_rcu (p, &dp->port_list, node) {
640 fill_port_desc(p, &ofr->ports[port_count]);
648 dp_send_features_reply(struct datapath *dp, uint32_t xid)
651 struct ofp_switch_features *ofr;
652 size_t ofr_len, port_max_len;
657 port_max_len = sizeof(struct ofp_phy_port) * OFPP_MAX;
658 ofr = alloc_openflow_skb(dp, sizeof(*ofr) + port_max_len,
659 OFPT_FEATURES_REPLY, xid, &skb);
664 port_count = fill_features_reply(dp, ofr);
667 ofr_len = sizeof(*ofr) + (sizeof(struct ofp_phy_port) * port_count);
668 resize_openflow_skb(skb, &ofr->header, ofr_len);
670 /* FIXME: send as reply. */
671 err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
672 if (err && net_ratelimit())
673 printk(KERN_WARNING "dp_send_hello: genlmsg_multicast failed: %d\n", err);
679 dp_send_config_reply(struct datapath *dp, uint32_t xid)
682 struct ofp_switch_config *osc;
685 osc = alloc_openflow_skb(dp, sizeof *osc, OFPT_PORT_STATUS, 0, &skb);
688 memcpy(((char *)osc) + sizeof osc->header,
689 ((char *)&dp->config) + sizeof dp->config.header,
690 sizeof dp->config - sizeof dp->config.header);
692 err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
693 if (err && net_ratelimit())
694 printk(KERN_WARNING "send_port_status: genlmsg_multicast failed: %d\n", err);
700 dp_update_port_flags(struct datapath *dp, const struct ofp_phy_port *opp)
702 struct net_bridge_port *p;
704 p = dp->ports[htons(opp->port_no)];
706 /* Make sure the port id hasn't changed since this was sent */
707 if (!p || memcmp(opp->hw_addr, p->dev->dev_addr, ETH_ALEN) != 0)
710 p->flags = htonl(opp->flags);
717 send_port_status(struct net_bridge_port *p, uint8_t status)
720 struct ofp_port_status *ops;
723 ops = alloc_openflow_skb(p->dp, sizeof *ops, OFPT_PORT_STATUS, 0,
727 ops->reason = status;
728 fill_port_desc(p, &ops->desc);
730 err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
731 if (err && net_ratelimit())
732 printk(KERN_WARNING "send_port_status: genlmsg_multicast failed: %d\n", err);
738 dp_send_flow_expired(struct datapath *dp, struct sw_flow *flow)
741 struct ofp_flow_expired *ofe;
742 unsigned long duration_j;
745 ofe = alloc_openflow_skb(dp, sizeof *ofe, OFPT_FLOW_EXPIRED, 0, &skb);
749 flow_fill_match(&ofe->match, &flow->key);
750 duration_j = (flow->timeout - HZ * flow->max_idle) - flow->init_time;
751 ofe->duration = htonl(duration_j / HZ);
752 ofe->packet_count = cpu_to_be64(flow->packet_count);
753 ofe->byte_count = cpu_to_be64(flow->byte_count);
755 err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
756 if (err && net_ratelimit())
757 printk(KERN_WARNING "send_flow_expired: genlmsg_multicast failed: %d\n", err);
762 /* Generic Netlink interface.
764 * See netlink(7) for an introduction to netlink. See
765 * http://linux-net.osdl.org/index.php/Netlink for more information and
766 * pointers on how to work with netlink and Generic Netlink in the kernel and
769 static struct genl_family dp_genl_family = {
770 .id = GENL_ID_GENERATE,
772 .name = DP_GENL_FAMILY_NAME,
774 .maxattr = DP_GENL_A_MAX,
777 /* Attribute policy: what each attribute may contain. */
778 static struct nla_policy dp_genl_policy[DP_GENL_A_MAX + 1] = {
779 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
780 [DP_GENL_A_MC_GROUP] = { .type = NLA_U32 },
781 [DP_GENL_A_PORTNAME] = { .type = NLA_STRING }
784 static int dp_genl_add(struct sk_buff *skb, struct genl_info *info)
786 if (!info->attrs[DP_GENL_A_DP_IDX])
789 return new_dp(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
792 static struct genl_ops dp_genl_ops_add_dp = {
793 .cmd = DP_GENL_C_ADD_DP,
794 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
795 .policy = dp_genl_policy,
800 struct datapath *dp_get(int dp_idx)
802 if (dp_idx < 0 || dp_idx > DP_MAX)
804 return rcu_dereference(dps[dp_idx]);
807 static int dp_genl_del(struct sk_buff *skb, struct genl_info *info)
812 if (!info->attrs[DP_GENL_A_DP_IDX])
815 mutex_lock(&dp_mutex);
816 dp = dp_get(nla_get_u32((info->attrs[DP_GENL_A_DP_IDX])));
823 mutex_unlock(&dp_mutex);
827 static struct genl_ops dp_genl_ops_del_dp = {
828 .cmd = DP_GENL_C_DEL_DP,
829 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
830 .policy = dp_genl_policy,
835 /* Queries a datapath for related information. Currently the only relevant
836 * information is the datapath's multicast group ID. Really we want one
837 * multicast group per datapath, but because of locking issues[*] we can't
838 * easily get one. Thus, every datapath will currently return the same
839 * global multicast group ID, but in the future it would be nice to fix that.
841 * [*] dp_genl_add, to add a new datapath, is called under the genl_lock
842 * mutex, and genl_register_mc_group, called to acquire a new multicast
843 * group ID, also acquires genl_lock, thus deadlock.
845 static int dp_genl_query(struct sk_buff *skb, struct genl_info *info)
848 struct sk_buff *ans_skb = NULL;
852 if (!info->attrs[DP_GENL_A_DP_IDX])
856 dp_idx = nla_get_u32((info->attrs[DP_GENL_A_DP_IDX]));
862 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
867 data = genlmsg_put_reply(ans_skb, info, &dp_genl_family,
868 0, DP_GENL_C_QUERY_DP);
873 NLA_PUT_U32(ans_skb, DP_GENL_A_DP_IDX, dp_idx);
874 NLA_PUT_U32(ans_skb, DP_GENL_A_MC_GROUP, mc_group.id);
876 genlmsg_end(ans_skb, data);
877 err = genlmsg_reply(ans_skb, info);
890 * Fill flow entry for nl flow query. Called with rcu_lock
895 dp_fill_flow(struct ofp_flow_mod* ofm, struct swt_iterator* iter)
897 ofm->header.version = OFP_VERSION;
898 ofm->header.type = OFPT_FLOW_MOD;
899 ofm->header.length = htons(sizeof(struct ofp_flow_mod)
900 + sizeof(ofm->actions[0]));
901 ofm->header.xid = htonl(0);
903 ofm->match.wildcards = htons(iter->flow->key.wildcards);
904 ofm->match.in_port = iter->flow->key.in_port;
905 ofm->match.dl_vlan = iter->flow->key.dl_vlan;
906 memcpy(ofm->match.dl_src, iter->flow->key.dl_src, ETH_ALEN);
907 memcpy(ofm->match.dl_dst, iter->flow->key.dl_dst, ETH_ALEN);
908 ofm->match.dl_type = iter->flow->key.dl_type;
909 ofm->match.nw_src = iter->flow->key.nw_src;
910 ofm->match.nw_dst = iter->flow->key.nw_dst;
911 ofm->match.nw_proto = iter->flow->key.nw_proto;
912 ofm->match.tp_src = iter->flow->key.tp_src;
913 ofm->match.tp_dst = iter->flow->key.tp_dst;
914 ofm->group_id = iter->flow->group_id;
915 ofm->max_idle = iter->flow->max_idle;
916 /* TODO support multiple actions */
917 ofm->actions[0] = iter->flow->actions[0];
922 /* Convenience function */
925 dp_init_nl_flow_msg(uint32_t dp_idx, uint16_t table_idx,
926 struct genl_info *info, struct sk_buff* skb)
930 data = genlmsg_put_reply(skb, info, &dp_genl_family, 0,
931 DP_GENL_C_QUERY_FLOW);
934 NLA_PUT_U32(skb, DP_GENL_A_DP_IDX, dp_idx);
935 NLA_PUT_U16(skb, DP_GENL_A_TABLEIDX, table_idx);
943 /* Iterate through the specified table and send all flow entries over
944 * netlink to userspace. Each flow message has the following format:
948 * 32bit number of flows
949 * openflow-flow-entries
951 * The full table may require multiple messages. A message with 0 flows
952 * signifies end-of message.
957 dp_dump_table(struct datapath *dp, uint16_t table_idx, struct genl_info *info, struct ofp_flow_mod* matchme)
959 struct sk_buff *skb = 0;
960 struct sw_table *table = 0;
961 struct swt_iterator iter;
962 struct sw_flow_key in_flow;
964 int count = 0, sum_count = 0;
966 uint8_t* ofm_ptr = 0;
967 struct nlattr *num_attr;
970 table = dp->chain->tables[table_idx];
971 if ( table == NULL ) {
972 dprintk("dp::dp_dump_table error, non-existant table at position %d\n", table_idx);
976 if (!table->iterator(table, &iter)) {
977 dprintk("dp::dp_dump_table couldn't initialize empty table iterator\n");
983 /* verify that we can fit all NL_FLOWS_PER_MESSAGE in a single
985 if( (sizeof(dp_genl_family) + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint32_t) +
986 (NL_FLOWS_PER_MESSAGE * sizeof(struct ofp_flow_mod))) > (8192 - 64)){
987 dprintk("dp::dp_dump_table NL_FLOWS_PER_MESSAGE may cause overrun in skbuf\n");
991 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
996 data = dp_init_nl_flow_msg(dp->dp_idx, table_idx, info, skb);
1002 /* reserve space to put the number of flows for this message, to
1003 * be filled after the loop*/
1004 num_attr = nla_reserve(skb, DP_GENL_A_NUMFLOWS, sizeof(uint32_t));
1007 goto error_free_skb;
1010 /* Only load NL_FLOWS_PER_MESSAGE flows at a time */
1011 attr = nla_reserve(skb, DP_GENL_A_FLOW,
1012 (sizeof(struct ofp_flow_mod) + sizeof(struct ofp_action)) * NL_FLOWS_PER_MESSAGE);
1015 goto error_free_skb;
1018 /* internal loop to fill NL_FLOWS_PER_MESSAGE flows */
1019 ofm_ptr = nla_data(attr);
1020 flow_extract_match(&in_flow, &matchme->match);
1021 while (iter.flow && count < NL_FLOWS_PER_MESSAGE) {
1022 if(flow_matches(&in_flow, &iter.flow->key)){
1023 if((err = dp_fill_flow((struct ofp_flow_mod*)ofm_ptr, &iter)))
1024 goto error_free_skb;
1026 /* TODO support multiple actions */
1027 ofm_ptr += sizeof(struct ofp_flow_mod) + sizeof(struct ofp_action);
1029 table->iterator_next(&iter);
1032 *((uint32_t*)nla_data(num_attr)) = count;
1033 genlmsg_end(skb, data);
1038 err = genlmsg_unicast(skb, info->snd_pid);
1042 /* send a sentinal message saying we're done */
1043 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
1047 data = dp_init_nl_flow_msg(dp->dp_idx, table_idx, info, skb);
1050 goto error_free_skb;
1053 NLA_PUT_U32(skb, DP_GENL_A_NUMFLOWS, 0);
1054 /* dummy flow so nl doesn't complain */
1055 attr = nla_reserve(skb, DP_GENL_A_FLOW, sizeof(struct ofp_flow_mod));
1058 goto error_free_skb;
1060 genlmsg_end(skb, data);
1061 err = genlmsg_reply(skb, info); skb = 0;
1070 /* Helper function to query_table which creates and sends a message packed with
1071 * table stats. Message form is:
1075 * OFP_TABLE (list of OFP_TABLES)
1081 dp_dump_table_stats(struct datapath *dp, int dp_idx, struct genl_info *info)
1083 struct sk_buff *skb = 0;
1084 struct ofp_table *ot = 0;
1085 struct nlattr *attr;
1086 struct sw_table_stats stats;
1091 int nt = dp->chain->n_tables;
1093 len = 4 + 4 + (sizeof(struct ofp_table) * nt);
1095 /* u32 IDX, u32 NUMTABLES, list-of-tables */
1096 skb = nlmsg_new(MAX(len, NLMSG_GOODSIZE), GFP_ATOMIC);
1101 data = genlmsg_put_reply(skb, info, &dp_genl_family, 0,
1102 DP_GENL_C_QUERY_TABLE);
1107 NLA_PUT_U32(skb, DP_GENL_A_DP_IDX, dp_idx);
1108 NLA_PUT_U32(skb, DP_GENL_A_NUMTABLES, nt);
1110 /* ... we assume that all tables can fit in a single message.
1111 * Probably a reasonable assumption seeing that we only have
1113 attr = nla_reserve(skb, DP_GENL_A_TABLE, (sizeof(struct ofp_table) * nt));
1116 goto error_free_skb;
1119 ot = nla_data(attr);
1121 for (i = 0; i < nt; ++i) {
1122 dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
1123 ot->header.version = OFP_VERSION;
1124 ot->header.type = OFPT_TABLE;
1125 ot->header.length = htons(sizeof(struct ofp_table));
1126 ot->header.xid = htonl(0);
1128 strncpy(ot->name, stats.name, OFP_MAX_TABLE_NAME_LEN);
1129 ot->table_id = htons(i);
1130 ot->n_flows = htonl(stats.n_flows);
1131 ot->max_flows = htonl(stats.max_flows);
1135 genlmsg_end(skb, data);
1136 err = genlmsg_reply(skb, info); skb = 0;
1146 * Queries a datapath for flow-table statistics
1150 static int dp_genl_table_query(struct sk_buff *skb, struct genl_info *info)
1152 struct datapath* dp;
1155 if (!info->attrs[DP_GENL_A_DP_IDX]) {
1156 dprintk("dp::dp_genl_table_query received message with missing attributes\n");
1161 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1167 err = dp_dump_table_stats(dp, nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]), info);
1175 * Queries a datapath for flow-table entries.
1178 static int dp_genl_flow_query(struct sk_buff *skb, struct genl_info *info)
1180 struct datapath* dp;
1181 struct ofp_flow_mod* ofm;
1185 if (!info->attrs[DP_GENL_A_DP_IDX]
1186 || !info->attrs[DP_GENL_A_TABLEIDX]
1187 || !info->attrs[DP_GENL_A_FLOW]) {
1188 dprintk("dp::dp_genl_flow_query received message with missing attributes\n");
1193 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1199 table_idx = nla_get_u16(info->attrs[DP_GENL_A_TABLEIDX]);
1201 if (dp->chain->n_tables <= table_idx){
1202 printk("table index %d invalid (dp has %d tables)\n",
1203 table_idx, dp->chain->n_tables);
1208 ofm = nla_data(info->attrs[DP_GENL_A_FLOW]);
1209 err = dp_dump_table(dp, table_idx, info, ofm);
1216 static struct nla_policy dp_genl_flow_policy[DP_GENL_A_MAX + 1] = {
1217 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1218 [DP_GENL_A_TABLEIDX] = { .type = NLA_U16 },
1219 [DP_GENL_A_NUMFLOWS] = { .type = NLA_U32 },
1222 static struct genl_ops dp_genl_ops_query_flow = {
1223 .cmd = DP_GENL_C_QUERY_FLOW,
1224 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1225 .policy = dp_genl_flow_policy,
1226 .doit = dp_genl_flow_query,
1230 static struct nla_policy dp_genl_table_policy[DP_GENL_A_MAX + 1] = {
1231 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1234 static struct genl_ops dp_genl_ops_query_table = {
1235 .cmd = DP_GENL_C_QUERY_TABLE,
1236 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1237 .policy = dp_genl_table_policy,
1238 .doit = dp_genl_table_query,
1243 static struct genl_ops dp_genl_ops_query_dp = {
1244 .cmd = DP_GENL_C_QUERY_DP,
1245 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1246 .policy = dp_genl_policy,
1247 .doit = dp_genl_query,
1251 static int dp_genl_add_del_port(struct sk_buff *skb, struct genl_info *info)
1253 struct datapath *dp;
1254 struct net_device *port;
1257 if (!info->attrs[DP_GENL_A_DP_IDX] || !info->attrs[DP_GENL_A_PORTNAME])
1261 mutex_lock(&dp_mutex);
1262 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1268 /* Get interface to add/remove. */
1269 port = dev_get_by_name(&init_net,
1270 nla_data(info->attrs[DP_GENL_A_PORTNAME]));
1276 /* Execute operation. */
1277 if (info->genlhdr->cmd == DP_GENL_C_ADD_PORT)
1278 err = add_switch_port(dp, port);
1280 if (port->br_port == NULL || port->br_port->dp != dp) {
1284 err = del_switch_port(port->br_port);
1290 mutex_unlock(&dp_mutex);
1294 static struct genl_ops dp_genl_ops_add_port = {
1295 .cmd = DP_GENL_C_ADD_PORT,
1296 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1297 .policy = dp_genl_policy,
1298 .doit = dp_genl_add_del_port,
1302 static struct genl_ops dp_genl_ops_del_port = {
1303 .cmd = DP_GENL_C_DEL_PORT,
1304 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1305 .policy = dp_genl_policy,
1306 .doit = dp_genl_add_del_port,
1310 static int dp_genl_openflow(struct sk_buff *skb, struct genl_info *info)
1312 struct nlattr *va = info->attrs[DP_GENL_A_OPENFLOW];
1313 struct datapath *dp;
1316 if (!info->attrs[DP_GENL_A_DP_IDX] || !va)
1320 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1326 va = info->attrs[DP_GENL_A_OPENFLOW];
1328 err = fwd_control_input(dp->chain, nla_data(va), nla_len(va));
1335 static struct nla_policy dp_genl_openflow_policy[DP_GENL_A_MAX + 1] = {
1336 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1339 static struct genl_ops dp_genl_ops_openflow = {
1340 .cmd = DP_GENL_C_OPENFLOW,
1341 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1342 .policy = dp_genl_openflow_policy,
1343 .doit = dp_genl_openflow,
1347 static struct nla_policy dp_genl_benchmark_policy[DP_GENL_A_MAX + 1] = {
1348 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1349 [DP_GENL_A_NPACKETS] = { .type = NLA_U32 },
1350 [DP_GENL_A_PSIZE] = { .type = NLA_U32 },
1353 static struct genl_ops dp_genl_ops_benchmark_nl = {
1354 .cmd = DP_GENL_C_BENCHMARK_NL,
1355 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1356 .policy = dp_genl_benchmark_policy,
1357 .doit = dp_genl_benchmark_nl,
1361 static struct genl_ops *dp_genl_all_ops[] = {
1362 /* Keep this operation first. Generic Netlink dispatching
1363 * looks up operations with linear search, so we want it at the
1365 &dp_genl_ops_openflow,
1367 &dp_genl_ops_query_flow,
1368 &dp_genl_ops_query_table,
1369 &dp_genl_ops_add_dp,
1370 &dp_genl_ops_del_dp,
1371 &dp_genl_ops_query_dp,
1372 &dp_genl_ops_add_port,
1373 &dp_genl_ops_del_port,
1374 &dp_genl_ops_benchmark_nl,
1377 static int dp_init_netlink(void)
1382 err = genl_register_family(&dp_genl_family);
1386 for (i = 0; i < ARRAY_SIZE(dp_genl_all_ops); i++) {
1387 err = genl_register_ops(&dp_genl_family, dp_genl_all_ops[i]);
1389 goto err_unregister;
1392 strcpy(mc_group.name, "openflow");
1393 err = genl_register_mc_group(&dp_genl_family, &mc_group);
1395 goto err_unregister;
1400 genl_unregister_family(&dp_genl_family);
1404 static void dp_uninit_netlink(void)
1406 genl_unregister_family(&dp_genl_family);
1409 #define DRV_NAME "openflow"
1410 #define DRV_VERSION VERSION
1411 #define DRV_DESCRIPTION "OpenFlow switching datapath implementation"
1412 #define DRV_COPYRIGHT "Copyright (c) 2007, 2008 The Board of Trustees of The Leland Stanford Junior University"
1415 static int __init dp_init(void)
1419 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION "\n");
1420 printk(KERN_INFO DRV_NAME ": " VERSION" built on "__DATE__" "__TIME__"\n");
1421 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
1427 err = dp_init_netlink();
1429 goto error_flow_exit;
1431 /* Hook into callback used by the bridge to intercept packets.
1432 * Parasites we are. */
1433 if (br_handle_frame_hook)
1434 printk("openflow: hijacking bridge hook\n");
1435 br_handle_frame_hook = dp_frame_hook;
1442 printk(KERN_EMERG "openflow: failed to install!");
1446 static void dp_cleanup(void)
1449 dp_uninit_netlink();
1451 br_handle_frame_hook = NULL;
1454 module_init(dp_init);
1455 module_exit(dp_cleanup);
1457 MODULE_DESCRIPTION(DRV_DESCRIPTION);
1458 MODULE_AUTHOR(DRV_COPYRIGHT);
1459 MODULE_LICENSE("GPL");