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/init.h>
10 #include <linux/module.h>
11 #include <linux/if_arp.h>
12 #include <linux/if_bridge.h>
13 #include <linux/if_vlan.h>
15 #include <net/genetlink.h>
17 #include <linux/delay.h>
18 #include <linux/etherdevice.h>
19 #include <linux/kernel.h>
20 #include <linux/kthread.h>
21 #include <linux/mutex.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/rcupdate.h>
24 #include <linux/version.h>
25 #include <linux/ethtool.h>
26 #include <linux/random.h>
27 #include <asm/system.h>
28 #include <linux/netfilter_bridge.h>
29 #include <linux/inetdevice.h>
30 #include <linux/list.h>
32 #include "openflow-netlink.h"
38 #include "datapath_t.h"
43 /* Number of milliseconds between runs of the maintenance thread. */
44 #define MAINT_SLEEP_MSECS 1000
46 #define BRIDGE_PORT_NO_FLOOD 0x00000001
48 #define UINT32_MAX 4294967295U
49 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
51 struct net_bridge_port {
55 struct net_device *dev;
56 struct list_head node; /* Element in datapath.ports. */
59 static struct genl_family dp_genl_family;
60 static struct genl_multicast_group mc_group;
62 int dp_dev_setup(struct net_device *dev);
64 /* It's hard to imagine wanting more than one datapath, but... */
67 /* datapaths. Protected on the read side by rcu_read_lock, on the write side
70 * It is safe to access the datapath and net_bridge_port structures with just
71 * the dp_mutex, but to access the chain you need to take the rcu_read_lock
72 * also (because dp_mutex doesn't prevent flows from being destroyed).
74 static struct datapath *dps[DP_MAX];
75 static DEFINE_MUTEX(dp_mutex);
77 static int dp_maint_func(void *data);
78 static int send_port_status(struct net_bridge_port *p, uint8_t status);
81 /* nla_unreserve - reduce amount of space reserved by nla_reserve
82 * @skb: socket buffer from which to recover room
83 * @nla: netlink attribute to adjust
84 * @len: amount by which to reduce attribute payload
86 * Reduces amount of space reserved by a call to nla_reserve.
88 * No other attributes may be added between calling nla_reserve and this
89 * function, since it will create a hole in the message.
91 void nla_unreserve(struct sk_buff *skb, struct nlattr *nla, int len)
100 alloc_openflow_skb(struct datapath *dp, size_t openflow_len, uint8_t type,
101 const struct sender *sender, struct sk_buff **pskb)
106 struct ofp_header *oh;
108 genl_len = nlmsg_total_size(GENL_HDRLEN + dp_genl_family.hdrsize);
109 genl_len += nla_total_size(sizeof(uint32_t)); /* DP_GENL_A_DP_IDX */
110 genl_len += nla_total_size(openflow_len); /* DP_GENL_A_OPENFLOW */
111 skb = *pskb = genlmsg_new(genl_len, GFP_ATOMIC);
114 printk("alloc_openflow_skb: genlmsg_new failed\n");
118 /* Assemble the Generic Netlink wrapper. */
119 if (!genlmsg_put(skb,
120 sender ? sender->pid : 0,
121 sender ? sender->seq : 0,
122 &dp_genl_family, 0, DP_GENL_C_OPENFLOW))
124 if (nla_put_u32(skb, DP_GENL_A_DP_IDX, dp->dp_idx) < 0)
126 attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, openflow_len);
128 nlmsg_end(skb, (struct nlmsghdr *) skb->data);
130 /* Fill in the header. */
132 oh->version = OFP_VERSION;
134 oh->length = htons(openflow_len);
135 oh->xid = sender ? sender->xid : 0;
141 resize_openflow_skb(struct sk_buff *skb,
142 struct ofp_header *oh, size_t new_length)
146 BUG_ON(new_length > ntohs(oh->length));
147 attr = ((void *) oh) - NLA_HDRLEN;
148 nla_unreserve(skb, attr, ntohs(oh->length) - new_length);
149 oh->length = htons(new_length);
150 nlmsg_end(skb, (struct nlmsghdr *) skb->data);
154 send_openflow_skb(struct sk_buff *skb, const struct sender *sender)
157 ? genlmsg_unicast(skb, sender->pid)
158 : genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC));
159 if (err && net_ratelimit())
160 printk(KERN_WARNING "send_openflow_skb: send failed: %d\n",
165 /* Generates a unique datapath id. It incorporates the datapath index
166 * and a hardware address, if available. If not, it generates a random
170 uint64_t gen_datapath_id(uint16_t dp_idx)
174 struct net_device *dev;
176 /* The top 16 bits are used to identify the datapath. The lower 48 bits
177 * use an interface address. */
178 id = (uint64_t)dp_idx << 48;
179 if ((dev = dev_get_by_name(&init_net, "ctl0"))
180 || (dev = dev_get_by_name(&init_net, "eth0"))) {
181 for (i=0; i<ETH_ALEN; i++) {
182 id |= (uint64_t)dev->dev_addr[i] << (8*(ETH_ALEN-1 - i));
186 /* Randomly choose the lower 48 bits if we cannot find an
187 * address and mark the most significant bit to indicate that
188 * this was randomly generated. */
189 uint8_t rand[ETH_ALEN];
190 get_random_bytes(rand, ETH_ALEN);
191 id |= (uint64_t)1 << 63;
192 for (i=0; i<ETH_ALEN; i++) {
193 id |= (uint64_t)rand[i] << (8*(ETH_ALEN-1 - i));
200 /* Creates a new datapath numbered 'dp_idx'. Returns 0 for success or a
201 * negative error code.
203 * Not called with any locks. */
204 static int new_dp(int dp_idx)
209 if (dp_idx < 0 || dp_idx >= DP_MAX)
212 if (!try_module_get(THIS_MODULE))
215 mutex_lock(&dp_mutex);
216 dp = rcu_dereference(dps[dp_idx]);
223 dp = kzalloc(sizeof *dp, GFP_KERNEL);
228 dp->id = gen_datapath_id(dp_idx);
229 dp->chain = chain_create(dp);
230 if (dp->chain == NULL)
232 INIT_LIST_HEAD(&dp->port_list);
235 /* Setup our "of" device */
238 err = dp_dev_setup(&dp->dev);
241 printk("datapath: problem setting up 'of' device\n");
245 dp->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
247 dp->dp_task = kthread_run(dp_maint_func, dp, "dp%d", dp_idx);
248 if (IS_ERR(dp->dp_task))
251 rcu_assign_pointer(dps[dp_idx], dp);
252 mutex_unlock(&dp_mutex);
259 mutex_unlock(&dp_mutex);
260 module_put(THIS_MODULE);
264 /* Find and return a free port number under 'dp'. Called under dp_mutex. */
265 static int find_portno(struct datapath *dp)
268 for (i = 0; i < OFPP_MAX; i++)
269 if (dp->ports[i] == NULL)
274 static struct net_bridge_port *new_nbp(struct datapath *dp,
275 struct net_device *dev)
277 struct net_bridge_port *p;
280 port_no = find_portno(dp);
282 return ERR_PTR(port_no);
284 p = kzalloc(sizeof(*p), GFP_KERNEL);
286 return ERR_PTR(-ENOMEM);
291 p->port_no = port_no;
296 /* Called with dp_mutex. */
297 int add_switch_port(struct datapath *dp, struct net_device *dev)
299 struct net_bridge_port *p;
301 if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER)
304 if (dev->br_port != NULL)
307 p = new_nbp(dp, dev);
312 rcu_assign_pointer(dev->br_port, p);
314 dev_set_promiscuity(dev, 1);
317 rcu_assign_pointer(dp->ports[p->port_no], p);
318 list_add_rcu(&p->node, &dp->port_list);
320 /* Notify the ctlpath that this port has been added */
321 send_port_status(p, OFPPR_ADD);
326 /* Delete 'p' from switch.
327 * Called with dp_mutex. */
328 static int del_switch_port(struct net_bridge_port *p)
330 /* First drop references to device. */
332 dev_set_promiscuity(p->dev, -1);
334 list_del_rcu(&p->node);
335 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
336 rcu_assign_pointer(p->dev->br_port, NULL);
338 /* Then wait until no one is still using it, and destroy it. */
341 /* Notify the ctlpath that this port no longer exists */
342 send_port_status(p, OFPPR_DELETE);
350 /* Called with dp_mutex. */
351 static void del_dp(struct datapath *dp)
353 struct net_bridge_port *p, *n;
356 /* Unregister the "of" device of this dp */
358 unregister_netdevice(&dp->dev);
362 kthread_stop(dp->dp_task);
364 /* Drop references to DP. */
365 list_for_each_entry_safe (p, n, &dp->port_list, node)
367 rcu_assign_pointer(dps[dp->dp_idx], NULL);
369 /* Wait until no longer in use, then destroy it. */
371 chain_destroy(dp->chain);
373 module_put(THIS_MODULE);
376 static int dp_maint_func(void *data)
378 struct datapath *dp = (struct datapath *) data;
380 while (!kthread_should_stop()) {
382 chain_timeout(dp->chain);
384 int count = chain_timeout(dp->chain);
385 chain_print_stats(dp->chain);
387 printk("%d flows timed out\n", count);
389 msleep_interruptible(MAINT_SLEEP_MSECS);
396 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
397 * different set of devices!) Returns 0 if *pskb should be processed further,
398 * 1 if *pskb is handled. */
399 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
400 /* Called with rcu_read_lock. */
401 static struct sk_buff *dp_frame_hook(struct net_bridge_port *p,
404 struct ethhdr *eh = eth_hdr(skb);
405 struct sk_buff *skb_local = NULL;
408 if (compare_ether_addr(eh->h_dest, skb->dev->dev_addr) == 0)
411 if (is_broadcast_ether_addr(eh->h_dest)
412 || is_multicast_ether_addr(eh->h_dest)
413 || is_local_ether_addr(eh->h_dest))
414 skb_local = skb_clone(skb, GFP_ATOMIC);
416 /* Push the Ethernet header back on. */
417 if (skb->protocol == htons(ETH_P_8021Q))
418 skb_push(skb, VLAN_ETH_HLEN);
420 skb_push(skb, ETH_HLEN);
422 fwd_port_input(p->dp->chain, skb, p->port_no);
426 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
427 static int dp_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
429 /* Push the Ethernet header back on. */
430 if ((*pskb)->protocol == htons(ETH_P_8021Q))
431 skb_push(*pskb, VLAN_ETH_HLEN);
433 skb_push(*pskb, ETH_HLEN);
435 fwd_port_input(p->dp->chain, *pskb, p->port_no);
439 /* NB: This has only been tested on 2.4.35 */
441 /* Called without any locks (?) */
442 static void dp_frame_hook(struct sk_buff *skb)
444 struct net_bridge_port *p = skb->dev->br_port;
446 /* Push the Ethernet header back on. */
447 if (skb->protocol == htons(ETH_P_8021Q))
448 skb_push(skb, VLAN_ETH_HLEN);
450 skb_push(skb, ETH_HLEN);
454 fwd_port_input(p->dp->chain, skb, p->port_no);
461 /* Forwarding output path.
462 * Based on net/bridge/br_forward.c. */
464 /* Don't forward packets to originating port or with flooding disabled */
465 static inline int should_deliver(const struct net_bridge_port *p,
466 const struct sk_buff *skb)
468 if ((skb->dev == p->dev) || (p->flags & BRIDGE_PORT_NO_FLOOD)) {
475 static inline unsigned packet_length(const struct sk_buff *skb)
477 int length = skb->len - ETH_HLEN;
478 if (skb->protocol == htons(ETH_P_8021Q))
484 flood(struct datapath *dp, struct sk_buff *skb)
486 struct net_bridge_port *p;
490 list_for_each_entry_rcu (p, &dp->port_list, node) {
491 if (!should_deliver(p, skb))
493 if (prev_port != -1) {
494 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
499 dp_output_port(dp, clone, prev_port);
501 prev_port = p->port_no;
504 dp_output_port(dp, skb, prev_port);
511 /* Marks 'skb' as having originated from 'in_port' in 'dp'.
512 FIXME: how are devices reference counted? */
513 int dp_set_origin(struct datapath *dp, uint16_t in_port,
516 if (in_port < OFPP_MAX && dp->ports[in_port]) {
517 skb->dev = dp->ports[in_port]->dev;
523 /* Takes ownership of 'skb' and transmits it to 'out_port' on 'dp'.
525 int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port)
527 struct net_bridge_port *p;
531 if (out_port == OFPP_FLOOD)
532 return flood(dp, skb);
533 else if (out_port == OFPP_CONTROLLER)
534 return dp_output_control(dp, skb, fwd_save_skb(skb), 0,
536 else if (out_port == OFPP_TABLE) {
537 struct sw_flow_key key;
538 struct sw_flow *flow;
540 flow_extract(skb, skb->dev->br_port->port_no, &key);
541 flow = chain_lookup(dp->chain, &key);
542 if (likely(flow != NULL)) {
543 flow_used(flow, skb);
544 execute_actions(dp, skb, &key, flow->actions, flow->n_actions);
548 } else if (out_port >= OFPP_MAX)
551 p = dp->ports[out_port];
556 if (packet_length(skb) > skb->dev->mtu) {
557 printk("dropped over-mtu packet: %d > %d\n",
558 packet_length(skb), skb->dev->mtu);
570 printk("can't forward to bad port %d\n", out_port);
574 /* Takes ownership of 'skb' and transmits it to 'dp''s control path. If
575 * 'buffer_id' != -1, then only the first 64 bytes of 'skb' are sent;
576 * otherwise, all of 'skb' is sent. 'reason' indicates why 'skb' is being
577 * sent. 'max_len' sets the maximum number of bytes that the caller
578 * wants to be sent; a value of 0 indicates the entire packet should be
581 dp_output_control(struct datapath *dp, struct sk_buff *skb,
582 uint32_t buffer_id, size_t max_len, int reason)
584 /* FIXME? Can we avoid creating a new skbuff in the case where we
585 * forward the whole packet? */
586 struct sk_buff *f_skb;
587 struct ofp_packet_in *opi;
588 size_t fwd_len, opi_len;
592 if ((buffer_id != (uint32_t) -1) && max_len)
593 fwd_len = min(fwd_len, max_len);
595 opi_len = offsetof(struct ofp_packet_in, data) + fwd_len;
596 opi = alloc_openflow_skb(dp, opi_len, OFPT_PACKET_IN, NULL, &f_skb);
597 opi->buffer_id = htonl(buffer_id);
598 opi->total_len = htons(skb->len);
599 opi->in_port = htons(skb->dev->br_port->port_no);
600 opi->reason = reason;
602 memcpy(opi->data, skb_mac_header(skb), fwd_len);
603 err = send_openflow_skb(f_skb, NULL);
610 static void fill_port_desc(struct net_bridge_port *p, struct ofp_phy_port *desc)
612 desc->port_no = htons(p->port_no);
613 strncpy(desc->name, p->dev->name, OFP_MAX_PORT_NAME_LEN);
614 desc->name[OFP_MAX_PORT_NAME_LEN-1] = '\0';
615 memcpy(desc->hw_addr, p->dev->dev_addr, ETH_ALEN);
616 desc->flags = htonl(p->flags);
620 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,24)
621 if (p->dev->ethtool_ops && p->dev->ethtool_ops->get_settings) {
622 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
624 if (!p->dev->ethtool_ops->get_settings(p->dev, &ecmd)) {
625 if (ecmd.supported & SUPPORTED_10baseT_Half)
626 desc->features |= OFPPF_10MB_HD;
627 if (ecmd.supported & SUPPORTED_10baseT_Full)
628 desc->features |= OFPPF_10MB_FD;
629 if (ecmd.supported & SUPPORTED_100baseT_Half)
630 desc->features |= OFPPF_100MB_HD;
631 if (ecmd.supported & SUPPORTED_100baseT_Full)
632 desc->features |= OFPPF_100MB_FD;
633 if (ecmd.supported & SUPPORTED_1000baseT_Half)
634 desc->features |= OFPPF_1GB_HD;
635 if (ecmd.supported & SUPPORTED_1000baseT_Full)
636 desc->features |= OFPPF_1GB_FD;
637 /* 10Gbps half-duplex doesn't exist... */
638 if (ecmd.supported & SUPPORTED_10000baseT_Full)
639 desc->features |= OFPPF_10GB_FD;
641 desc->features = htonl(desc->features);
642 desc->speed = htonl(ecmd.speed);
649 fill_features_reply(struct datapath *dp, struct ofp_switch_features *ofr)
651 struct net_bridge_port *p;
654 ofr->datapath_id = cpu_to_be64(dp->id);
656 ofr->n_exact = htonl(2 * TABLE_HASH_MAX_FLOWS);
657 ofr->n_compression = 0; /* Not supported */
658 ofr->n_general = htonl(TABLE_LINEAR_MAX_FLOWS);
659 ofr->buffer_mb = htonl(UINT32_MAX);
660 ofr->n_buffers = htonl(N_PKT_BUFFERS);
661 ofr->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
662 ofr->actions = htonl(OFP_SUPPORTED_ACTIONS);
664 list_for_each_entry_rcu (p, &dp->port_list, node) {
665 fill_port_desc(p, &ofr->ports[port_count]);
673 dp_send_features_reply(struct datapath *dp, const struct sender *sender)
676 struct ofp_switch_features *ofr;
677 size_t ofr_len, port_max_len;
681 port_max_len = sizeof(struct ofp_phy_port) * OFPP_MAX;
682 ofr = alloc_openflow_skb(dp, sizeof(*ofr) + port_max_len,
683 OFPT_FEATURES_REPLY, sender, &skb);
688 port_count = fill_features_reply(dp, ofr);
691 ofr_len = sizeof(*ofr) + (sizeof(struct ofp_phy_port) * port_count);
692 resize_openflow_skb(skb, &ofr->header, ofr_len);
693 return send_openflow_skb(skb, sender);
697 dp_send_config_reply(struct datapath *dp, const struct sender *sender)
700 struct ofp_switch_config *osc;
702 osc = alloc_openflow_skb(dp, sizeof *osc, OFPT_GET_CONFIG_REPLY, sender,
707 osc->flags = htons(dp->flags);
708 osc->miss_send_len = htons(dp->miss_send_len);
710 return send_openflow_skb(skb, sender);
714 dp_update_port_flags(struct datapath *dp, const struct ofp_phy_port *opp)
716 struct net_bridge_port *p;
718 p = dp->ports[htons(opp->port_no)];
720 /* Make sure the port id hasn't changed since this was sent */
721 if (!p || memcmp(opp->hw_addr, p->dev->dev_addr, ETH_ALEN) != 0)
724 p->flags = htonl(opp->flags);
731 send_port_status(struct net_bridge_port *p, uint8_t status)
734 struct ofp_port_status *ops;
736 ops = alloc_openflow_skb(p->dp, sizeof *ops, OFPT_PORT_STATUS, NULL,
740 ops->reason = status;
741 fill_port_desc(p, &ops->desc);
743 return send_openflow_skb(skb, NULL);
747 dp_send_flow_expired(struct datapath *dp, struct sw_flow *flow)
750 struct ofp_flow_expired *ofe;
751 unsigned long duration_j;
753 ofe = alloc_openflow_skb(dp, sizeof *ofe, OFPT_FLOW_EXPIRED, 0, &skb);
757 flow_fill_match(&ofe->match, &flow->key);
758 duration_j = (flow->timeout - HZ * flow->max_idle) - flow->init_time;
759 ofe->duration = htonl(duration_j / HZ);
760 ofe->packet_count = cpu_to_be64(flow->packet_count);
761 ofe->byte_count = cpu_to_be64(flow->byte_count);
762 return send_openflow_skb(skb, NULL);
766 fill_flow_stats(struct ofp_flow_stats *ofs, struct sw_flow *flow,
769 ofs->match.wildcards = htons(flow->key.wildcards);
770 ofs->match.in_port = flow->key.in_port;
771 memcpy(ofs->match.dl_src, flow->key.dl_src, ETH_ALEN);
772 memcpy(ofs->match.dl_dst, flow->key.dl_dst, ETH_ALEN);
773 ofs->match.dl_vlan = flow->key.dl_vlan;
774 ofs->match.dl_type = flow->key.dl_type;
775 ofs->match.nw_src = flow->key.nw_src;
776 ofs->match.nw_dst = flow->key.nw_dst;
777 ofs->match.nw_proto = flow->key.nw_proto;
778 memset(ofs->match.pad, 0, sizeof ofs->match.pad);
779 ofs->match.tp_src = flow->key.tp_src;
780 ofs->match.tp_dst = flow->key.tp_dst;
781 ofs->duration = htonl((jiffies - flow->init_time) / HZ);
782 ofs->table_id = table_idx;
783 ofs->packet_count = cpu_to_be64(flow->packet_count);
784 ofs->byte_count = cpu_to_be64(flow->byte_count);
788 dp_send_flow_stats(struct datapath *dp, const struct sender *sender,
789 const struct ofp_match *match)
792 struct ofp_flow_stats_reply *fsr;
793 size_t header_size, fudge, flow_size;
794 struct sw_flow_key match_key;
795 int table_idx, n_flows, max_flows;
797 header_size = offsetof(struct ofp_flow_stats_reply, flows);
799 flow_size = sizeof fsr->flows[0];
800 max_flows = (NLMSG_GOODSIZE - header_size - fudge) / flow_size;
801 fsr = alloc_openflow_skb(dp, header_size + max_flows * flow_size,
802 OFPT_FLOW_STATS_REPLY, sender, &skb);
807 flow_extract_match(&match_key, match);
808 for (table_idx = 0; table_idx < dp->chain->n_tables; table_idx++) {
809 struct sw_table *table = dp->chain->tables[table_idx];
810 struct swt_iterator iter;
812 if (n_flows >= max_flows) {
816 if (!table->iterator(table, &iter)) {
818 printk("iterator failed for table %d\n",
823 for (; iter.flow; table->iterator_next(&iter)) {
824 if (flow_matches(&match_key, &iter.flow->key)) {
825 fill_flow_stats(&fsr->flows[n_flows],
826 iter.flow, table_idx);
827 if (++n_flows >= max_flows) {
832 table->iterator_destroy(&iter);
834 resize_openflow_skb(skb, &fsr->header,
835 header_size + flow_size * n_flows);
836 return send_openflow_skb(skb, sender);
840 fill_port_stats_reply(struct datapath *dp, struct ofp_port_stats_reply *psr)
842 struct net_bridge_port *p;
845 list_for_each_entry_rcu (p, &dp->port_list, node) {
846 struct ofp_port_stats *ps = &psr->ports[port_count++];
847 struct net_device_stats *stats = p->dev->get_stats(p->dev);
848 ps->port_no = htons(p->port_no);
849 memset(ps->pad, 0, sizeof ps->pad);
850 ps->rx_count = cpu_to_be64(stats->rx_packets);
851 ps->tx_count = cpu_to_be64(stats->tx_packets);
852 ps->drop_count = cpu_to_be64(stats->rx_dropped
853 + stats->tx_dropped);
860 dp_send_port_stats(struct datapath *dp, const struct sender *sender)
863 struct ofp_port_stats_reply *psr;
864 size_t psr_len, port_max_len;
868 port_max_len = sizeof(struct ofp_port_stats) * OFPP_MAX;
869 psr = alloc_openflow_skb(dp, sizeof *psr + port_max_len,
870 OFPT_PORT_STATS_REPLY, sender, &skb);
875 port_count = fill_port_stats_reply(dp, psr);
878 psr_len = sizeof *psr + sizeof(struct ofp_port_stats) * port_count;
879 resize_openflow_skb(skb, &psr->header, psr_len);
880 return send_openflow_skb(skb, sender);
884 dp_send_table_stats(struct datapath *dp, const struct sender *sender)
887 struct ofp_table_stats_reply *tsr;
890 n_tables = dp->chain->n_tables;
891 tsr = alloc_openflow_skb(dp, (offsetof(struct ofp_table_stats_reply,
893 + sizeof tsr->tables[0] * n_tables),
894 OFPT_TABLE_STATS_REPLY, sender, &skb);
897 for (i = 0; i < n_tables; i++) {
898 struct ofp_table_stats *ots = &tsr->tables[i];
899 struct sw_table_stats stats;
900 dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
901 strncpy(ots->name, stats.name, sizeof ots->name);
903 ots->pad[0] = ots->pad[1] = 0;
904 ots->max_entries = htonl(stats.max_flows);
905 ots->active_count = htonl(stats.n_flows);
906 ots->matched_count = cpu_to_be64(0); /* FIXME */
908 return send_openflow_skb(skb, sender);
911 /* Generic Netlink interface.
913 * See netlink(7) for an introduction to netlink. See
914 * http://linux-net.osdl.org/index.php/Netlink for more information and
915 * pointers on how to work with netlink and Generic Netlink in the kernel and
918 static struct genl_family dp_genl_family = {
919 .id = GENL_ID_GENERATE,
921 .name = DP_GENL_FAMILY_NAME,
923 .maxattr = DP_GENL_A_MAX,
926 /* Attribute policy: what each attribute may contain. */
927 static struct nla_policy dp_genl_policy[DP_GENL_A_MAX + 1] = {
928 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
929 [DP_GENL_A_MC_GROUP] = { .type = NLA_U32 },
930 [DP_GENL_A_PORTNAME] = { .type = NLA_STRING }
933 static int dp_genl_add(struct sk_buff *skb, struct genl_info *info)
935 if (!info->attrs[DP_GENL_A_DP_IDX])
938 return new_dp(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
941 static struct genl_ops dp_genl_ops_add_dp = {
942 .cmd = DP_GENL_C_ADD_DP,
943 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
944 .policy = dp_genl_policy,
949 struct datapath *dp_get(int dp_idx)
951 if (dp_idx < 0 || dp_idx > DP_MAX)
953 return rcu_dereference(dps[dp_idx]);
956 static int dp_genl_del(struct sk_buff *skb, struct genl_info *info)
961 if (!info->attrs[DP_GENL_A_DP_IDX])
964 mutex_lock(&dp_mutex);
965 dp = dp_get(nla_get_u32((info->attrs[DP_GENL_A_DP_IDX])));
972 mutex_unlock(&dp_mutex);
976 static struct genl_ops dp_genl_ops_del_dp = {
977 .cmd = DP_GENL_C_DEL_DP,
978 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
979 .policy = dp_genl_policy,
984 /* Queries a datapath for related information. Currently the only relevant
985 * information is the datapath's multicast group ID. Really we want one
986 * multicast group per datapath, but because of locking issues[*] we can't
987 * easily get one. Thus, every datapath will currently return the same
988 * global multicast group ID, but in the future it would be nice to fix that.
990 * [*] dp_genl_add, to add a new datapath, is called under the genl_lock
991 * mutex, and genl_register_mc_group, called to acquire a new multicast
992 * group ID, also acquires genl_lock, thus deadlock.
994 static int dp_genl_query(struct sk_buff *skb, struct genl_info *info)
997 struct sk_buff *ans_skb = NULL;
1001 if (!info->attrs[DP_GENL_A_DP_IDX])
1005 dp_idx = nla_get_u32((info->attrs[DP_GENL_A_DP_IDX]));
1006 dp = dp_get(dp_idx);
1011 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1016 data = genlmsg_put_reply(ans_skb, info, &dp_genl_family,
1017 0, DP_GENL_C_QUERY_DP);
1022 NLA_PUT_U32(ans_skb, DP_GENL_A_DP_IDX, dp_idx);
1023 NLA_PUT_U32(ans_skb, DP_GENL_A_MC_GROUP, mc_group.id);
1025 genlmsg_end(ans_skb, data);
1026 err = genlmsg_reply(ans_skb, info);
1038 static struct genl_ops dp_genl_ops_query_dp = {
1039 .cmd = DP_GENL_C_QUERY_DP,
1040 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1041 .policy = dp_genl_policy,
1042 .doit = dp_genl_query,
1046 static int dp_genl_add_del_port(struct sk_buff *skb, struct genl_info *info)
1048 struct datapath *dp;
1049 struct net_device *port;
1052 if (!info->attrs[DP_GENL_A_DP_IDX] || !info->attrs[DP_GENL_A_PORTNAME])
1056 mutex_lock(&dp_mutex);
1057 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1063 /* Get interface to add/remove. */
1064 port = dev_get_by_name(&init_net,
1065 nla_data(info->attrs[DP_GENL_A_PORTNAME]));
1071 /* Execute operation. */
1072 if (info->genlhdr->cmd == DP_GENL_C_ADD_PORT)
1073 err = add_switch_port(dp, port);
1075 if (port->br_port == NULL || port->br_port->dp != dp) {
1079 err = del_switch_port(port->br_port);
1085 mutex_unlock(&dp_mutex);
1089 static struct genl_ops dp_genl_ops_add_port = {
1090 .cmd = DP_GENL_C_ADD_PORT,
1091 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1092 .policy = dp_genl_policy,
1093 .doit = dp_genl_add_del_port,
1097 static struct genl_ops dp_genl_ops_del_port = {
1098 .cmd = DP_GENL_C_DEL_PORT,
1099 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1100 .policy = dp_genl_policy,
1101 .doit = dp_genl_add_del_port,
1105 static int dp_genl_openflow(struct sk_buff *skb, struct genl_info *info)
1107 struct nlattr *va = info->attrs[DP_GENL_A_OPENFLOW];
1108 struct datapath *dp;
1109 struct ofp_header *oh;
1110 struct sender sender;
1113 if (!info->attrs[DP_GENL_A_DP_IDX] || !va)
1117 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1123 if (nla_len(va) < sizeof(struct ofp_header)) {
1129 sender.xid = oh->xid;
1130 sender.pid = info->snd_pid;
1131 sender.seq = info->snd_seq;
1132 err = fwd_control_input(dp->chain, &sender, nla_data(va), nla_len(va));
1139 static struct nla_policy dp_genl_openflow_policy[DP_GENL_A_MAX + 1] = {
1140 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1143 static struct genl_ops dp_genl_ops_openflow = {
1144 .cmd = DP_GENL_C_OPENFLOW,
1145 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1146 .policy = dp_genl_openflow_policy,
1147 .doit = dp_genl_openflow,
1151 static struct nla_policy dp_genl_benchmark_policy[DP_GENL_A_MAX + 1] = {
1152 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1153 [DP_GENL_A_NPACKETS] = { .type = NLA_U32 },
1154 [DP_GENL_A_PSIZE] = { .type = NLA_U32 },
1157 static struct genl_ops dp_genl_ops_benchmark_nl = {
1158 .cmd = DP_GENL_C_BENCHMARK_NL,
1159 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1160 .policy = dp_genl_benchmark_policy,
1161 .doit = dp_genl_benchmark_nl,
1165 static struct genl_ops *dp_genl_all_ops[] = {
1166 /* Keep this operation first. Generic Netlink dispatching
1167 * looks up operations with linear search, so we want it at the
1169 &dp_genl_ops_openflow,
1171 &dp_genl_ops_add_dp,
1172 &dp_genl_ops_del_dp,
1173 &dp_genl_ops_query_dp,
1174 &dp_genl_ops_add_port,
1175 &dp_genl_ops_del_port,
1176 &dp_genl_ops_benchmark_nl,
1179 static int dp_init_netlink(void)
1184 err = genl_register_family(&dp_genl_family);
1188 for (i = 0; i < ARRAY_SIZE(dp_genl_all_ops); i++) {
1189 err = genl_register_ops(&dp_genl_family, dp_genl_all_ops[i]);
1191 goto err_unregister;
1194 strcpy(mc_group.name, "openflow");
1195 err = genl_register_mc_group(&dp_genl_family, &mc_group);
1197 goto err_unregister;
1202 genl_unregister_family(&dp_genl_family);
1206 static void dp_uninit_netlink(void)
1208 genl_unregister_family(&dp_genl_family);
1211 #define DRV_NAME "openflow"
1212 #define DRV_VERSION VERSION
1213 #define DRV_DESCRIPTION "OpenFlow switching datapath implementation"
1214 #define DRV_COPYRIGHT "Copyright (c) 2007, 2008 The Board of Trustees of The Leland Stanford Junior University"
1217 static int __init dp_init(void)
1221 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION "\n");
1222 printk(KERN_INFO DRV_NAME ": " VERSION" built on "__DATE__" "__TIME__"\n");
1223 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
1229 err = dp_init_netlink();
1231 goto error_flow_exit;
1233 /* Hook into callback used by the bridge to intercept packets.
1234 * Parasites we are. */
1235 if (br_handle_frame_hook)
1236 printk("openflow: hijacking bridge hook\n");
1237 br_handle_frame_hook = dp_frame_hook;
1244 printk(KERN_EMERG "openflow: failed to install!");
1248 static void dp_cleanup(void)
1251 dp_uninit_netlink();
1253 br_handle_frame_hook = NULL;
1256 module_init(dp_init);
1257 module_exit(dp_cleanup);
1259 MODULE_DESCRIPTION(DRV_DESCRIPTION);
1260 MODULE_AUTHOR(DRV_COPYRIGHT);
1261 MODULE_LICENSE("GPL");