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 = nla_total_size(sizeof(uint32_t)); /* DP_GENL_A_DP_IDX */
109 genl_len += nla_total_size(openflow_len); /* DP_GENL_A_OPENFLOW */
110 skb = *pskb = genlmsg_new(genl_len, GFP_ATOMIC);
113 printk("alloc_openflow_skb: genlmsg_new failed\n");
117 /* Assemble the Generic Netlink wrapper. */
118 if (!genlmsg_put(skb,
119 sender ? sender->pid : 0,
120 sender ? sender->seq : 0,
121 &dp_genl_family, 0, DP_GENL_C_OPENFLOW))
123 if (nla_put_u32(skb, DP_GENL_A_DP_IDX, dp->dp_idx) < 0)
125 attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, openflow_len);
127 nlmsg_end(skb, (struct nlmsghdr *) skb->data);
129 /* Fill in the header. */
131 oh->version = OFP_VERSION;
133 oh->length = htons(openflow_len);
134 oh->xid = sender ? sender->xid : 0;
140 resize_openflow_skb(struct sk_buff *skb,
141 struct ofp_header *oh, size_t new_length)
145 BUG_ON(new_length > ntohs(oh->length));
146 attr = ((void *) oh) - NLA_HDRLEN;
147 nla_unreserve(skb, attr, ntohs(oh->length) - new_length);
148 oh->length = htons(new_length);
149 nlmsg_end(skb, (struct nlmsghdr *) skb->data);
153 send_openflow_skb(struct sk_buff *skb, const struct sender *sender)
156 ? genlmsg_unicast(skb, sender->pid)
157 : genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC));
158 if (err && net_ratelimit())
159 printk(KERN_WARNING "send_openflow_skb: send failed: %d\n",
164 /* Generates a unique datapath id. It incorporates the datapath index
165 * and a hardware address, if available. If not, it generates a random
169 uint64_t gen_datapath_id(uint16_t dp_idx)
173 struct net_device *dev;
175 /* The top 16 bits are used to identify the datapath. The lower 48 bits
176 * use an interface address. */
177 id = (uint64_t)dp_idx << 48;
178 if ((dev = dev_get_by_name(&init_net, "ctl0"))
179 || (dev = dev_get_by_name(&init_net, "eth0"))) {
180 for (i=0; i<ETH_ALEN; i++) {
181 id |= (uint64_t)dev->dev_addr[i] << (8*(ETH_ALEN-1 - i));
185 /* Randomly choose the lower 48 bits if we cannot find an
186 * address and mark the most significant bit to indicate that
187 * this was randomly generated. */
188 uint8_t rand[ETH_ALEN];
189 get_random_bytes(rand, ETH_ALEN);
190 id |= (uint64_t)1 << 63;
191 for (i=0; i<ETH_ALEN; i++) {
192 id |= (uint64_t)rand[i] << (8*(ETH_ALEN-1 - i));
199 /* Creates a new datapath numbered 'dp_idx'. Returns 0 for success or a
200 * negative error code.
202 * Not called with any locks. */
203 static int new_dp(int dp_idx)
208 if (dp_idx < 0 || dp_idx >= DP_MAX)
211 if (!try_module_get(THIS_MODULE))
214 mutex_lock(&dp_mutex);
215 dp = rcu_dereference(dps[dp_idx]);
222 dp = kzalloc(sizeof *dp, GFP_KERNEL);
227 dp->id = gen_datapath_id(dp_idx);
228 dp->chain = chain_create(dp);
229 if (dp->chain == NULL)
231 INIT_LIST_HEAD(&dp->port_list);
234 /* Setup our "of" device */
237 err = dp_dev_setup(&dp->dev);
240 printk("datapath: problem setting up 'of' device\n");
243 dp->config.flags = 0;
244 dp->config.miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
246 dp->dp_task = kthread_run(dp_maint_func, dp, "dp%d", dp_idx);
247 if (IS_ERR(dp->dp_task))
250 rcu_assign_pointer(dps[dp_idx], dp);
251 mutex_unlock(&dp_mutex);
258 mutex_unlock(&dp_mutex);
259 module_put(THIS_MODULE);
263 /* Find and return a free port number under 'dp'. Called under dp_mutex. */
264 static int find_portno(struct datapath *dp)
267 for (i = 0; i < OFPP_MAX; i++)
268 if (dp->ports[i] == NULL)
273 static struct net_bridge_port *new_nbp(struct datapath *dp,
274 struct net_device *dev)
276 struct net_bridge_port *p;
279 port_no = find_portno(dp);
281 return ERR_PTR(port_no);
283 p = kzalloc(sizeof(*p), GFP_KERNEL);
285 return ERR_PTR(-ENOMEM);
290 p->port_no = port_no;
295 /* Called with dp_mutex. */
296 int add_switch_port(struct datapath *dp, struct net_device *dev)
298 struct net_bridge_port *p;
300 if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER)
303 if (dev->br_port != NULL)
306 p = new_nbp(dp, dev);
311 rcu_assign_pointer(dev->br_port, p);
313 dev_set_promiscuity(dev, 1);
316 rcu_assign_pointer(dp->ports[p->port_no], p);
317 list_add_rcu(&p->node, &dp->port_list);
319 /* Notify the ctlpath that this port has been added */
320 send_port_status(p, OFPPR_ADD);
325 /* Delete 'p' from switch.
326 * Called with dp_mutex. */
327 static int del_switch_port(struct net_bridge_port *p)
329 /* First drop references to device. */
331 dev_set_promiscuity(p->dev, -1);
333 list_del_rcu(&p->node);
334 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
335 rcu_assign_pointer(p->dev->br_port, NULL);
337 /* Then wait until no one is still using it, and destroy it. */
340 /* Notify the ctlpath that this port no longer exists */
341 send_port_status(p, OFPPR_DELETE);
349 /* Called with dp_mutex. */
350 static void del_dp(struct datapath *dp)
352 struct net_bridge_port *p, *n;
355 /* Unregister the "of" device of this dp */
357 unregister_netdevice(&dp->dev);
361 kthread_stop(dp->dp_task);
363 /* Drop references to DP. */
364 list_for_each_entry_safe (p, n, &dp->port_list, node)
366 rcu_assign_pointer(dps[dp->dp_idx], NULL);
368 /* Wait until no longer in use, then destroy it. */
370 chain_destroy(dp->chain);
372 module_put(THIS_MODULE);
375 static int dp_maint_func(void *data)
377 struct datapath *dp = (struct datapath *) data;
379 while (!kthread_should_stop()) {
381 chain_timeout(dp->chain);
383 int count = chain_timeout(dp->chain);
384 chain_print_stats(dp->chain);
386 printk("%d flows timed out\n", count);
388 msleep_interruptible(MAINT_SLEEP_MSECS);
395 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
396 * different set of devices!) Returns 0 if *pskb should be processed further,
397 * 1 if *pskb is handled. */
398 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
399 /* Called with rcu_read_lock. */
400 static struct sk_buff *dp_frame_hook(struct net_bridge_port *p,
403 struct ethhdr *eh = eth_hdr(skb);
404 struct sk_buff *skb_local = NULL;
407 if (compare_ether_addr(eh->h_dest, skb->dev->dev_addr) == 0)
410 if (is_broadcast_ether_addr(eh->h_dest)
411 || is_multicast_ether_addr(eh->h_dest)
412 || is_local_ether_addr(eh->h_dest))
413 skb_local = skb_clone(skb, GFP_ATOMIC);
415 /* Push the Ethernet header back on. */
416 if (skb->protocol == htons(ETH_P_8021Q))
417 skb_push(skb, VLAN_ETH_HLEN);
419 skb_push(skb, ETH_HLEN);
421 fwd_port_input(p->dp->chain, skb, p->port_no);
425 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
426 static int dp_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
428 /* Push the Ethernet header back on. */
429 if ((*pskb)->protocol == htons(ETH_P_8021Q))
430 skb_push(*pskb, VLAN_ETH_HLEN);
432 skb_push(*pskb, ETH_HLEN);
434 fwd_port_input(p->dp->chain, *pskb, p->port_no);
438 /* NB: This has only been tested on 2.4.35 */
440 /* Called without any locks (?) */
441 static void dp_frame_hook(struct sk_buff *skb)
443 struct net_bridge_port *p = skb->dev->br_port;
445 /* Push the Ethernet header back on. */
446 if (skb->protocol == htons(ETH_P_8021Q))
447 skb_push(skb, VLAN_ETH_HLEN);
449 skb_push(skb, ETH_HLEN);
453 fwd_port_input(p->dp->chain, skb, p->port_no);
460 /* Forwarding output path.
461 * Based on net/bridge/br_forward.c. */
463 /* Don't forward packets to originating port or with flooding disabled */
464 static inline int should_deliver(const struct net_bridge_port *p,
465 const struct sk_buff *skb)
467 if ((skb->dev == p->dev) || (p->flags & BRIDGE_PORT_NO_FLOOD)) {
474 static inline unsigned packet_length(const struct sk_buff *skb)
476 int length = skb->len - ETH_HLEN;
477 if (skb->protocol == htons(ETH_P_8021Q))
483 flood(struct datapath *dp, struct sk_buff *skb)
485 struct net_bridge_port *p;
489 list_for_each_entry_rcu (p, &dp->port_list, node) {
490 if (!should_deliver(p, skb))
492 if (prev_port != -1) {
493 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
498 dp_output_port(dp, clone, prev_port);
500 prev_port = p->port_no;
503 dp_output_port(dp, skb, prev_port);
510 /* Marks 'skb' as having originated from 'in_port' in 'dp'.
511 FIXME: how are devices reference counted? */
512 int dp_set_origin(struct datapath *dp, uint16_t in_port,
515 if (in_port < OFPP_MAX && dp->ports[in_port]) {
516 skb->dev = dp->ports[in_port]->dev;
522 /* Takes ownership of 'skb' and transmits it to 'out_port' on 'dp'.
524 int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port)
526 struct net_bridge_port *p;
530 if (out_port == OFPP_FLOOD)
531 return flood(dp, skb);
532 else if (out_port == OFPP_CONTROLLER)
533 return dp_output_control(dp, skb, fwd_save_skb(skb), 0,
535 else if (out_port == OFPP_TABLE) {
536 struct sw_flow_key key;
537 struct sw_flow *flow;
539 flow_extract(skb, skb->dev->br_port->port_no, &key);
540 flow = chain_lookup(dp->chain, &key);
541 if (likely(flow != NULL)) {
542 flow_used(flow, skb);
543 execute_actions(dp, skb, &key, flow->actions, flow->n_actions);
547 } else if (out_port >= OFPP_MAX)
550 p = dp->ports[out_port];
555 if (packet_length(skb) > skb->dev->mtu) {
556 printk("dropped over-mtu packet: %d > %d\n",
557 packet_length(skb), skb->dev->mtu);
569 printk("can't forward to bad port %d\n", out_port);
573 /* Takes ownership of 'skb' and transmits it to 'dp''s control path. If
574 * 'buffer_id' != -1, then only the first 64 bytes of 'skb' are sent;
575 * otherwise, all of 'skb' is sent. 'reason' indicates why 'skb' is being
576 * sent. 'max_len' sets the maximum number of bytes that the caller
577 * wants to be sent; a value of 0 indicates the entire packet should be
580 dp_output_control(struct datapath *dp, struct sk_buff *skb,
581 uint32_t buffer_id, size_t max_len, int reason)
583 /* FIXME? Can we avoid creating a new skbuff in the case where we
584 * forward the whole packet? */
585 struct sk_buff *f_skb;
586 struct ofp_packet_in *opi;
587 size_t fwd_len, opi_len;
591 if ((buffer_id != (uint32_t) -1) && max_len)
592 fwd_len = min(fwd_len, max_len);
594 opi_len = offsetof(struct ofp_packet_in, data) + fwd_len;
595 opi = alloc_openflow_skb(dp, opi_len, OFPT_PACKET_IN, NULL, &f_skb);
596 opi->buffer_id = htonl(buffer_id);
597 opi->total_len = htons(skb->len);
598 opi->in_port = htons(skb->dev->br_port->port_no);
599 opi->reason = reason;
601 memcpy(opi->data, skb_mac_header(skb), fwd_len);
602 err = send_openflow_skb(f_skb, NULL);
609 static void fill_port_desc(struct net_bridge_port *p, struct ofp_phy_port *desc)
611 desc->port_no = htons(p->port_no);
612 strncpy(desc->name, p->dev->name, OFP_MAX_PORT_NAME_LEN);
613 desc->name[OFP_MAX_PORT_NAME_LEN-1] = '\0';
614 memcpy(desc->hw_addr, p->dev->dev_addr, ETH_ALEN);
615 desc->flags = htonl(p->flags);
619 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,24)
620 if (p->dev->ethtool_ops && p->dev->ethtool_ops->get_settings) {
621 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
623 if (!p->dev->ethtool_ops->get_settings(p->dev, &ecmd)) {
624 if (ecmd.supported & SUPPORTED_10baseT_Half)
625 desc->features |= OFPPF_10MB_HD;
626 if (ecmd.supported & SUPPORTED_10baseT_Full)
627 desc->features |= OFPPF_10MB_FD;
628 if (ecmd.supported & SUPPORTED_100baseT_Half)
629 desc->features |= OFPPF_100MB_HD;
630 if (ecmd.supported & SUPPORTED_100baseT_Full)
631 desc->features |= OFPPF_100MB_FD;
632 if (ecmd.supported & SUPPORTED_1000baseT_Half)
633 desc->features |= OFPPF_1GB_HD;
634 if (ecmd.supported & SUPPORTED_1000baseT_Full)
635 desc->features |= OFPPF_1GB_FD;
636 /* 10Gbps half-duplex doesn't exist... */
637 if (ecmd.supported & SUPPORTED_10000baseT_Full)
638 desc->features |= OFPPF_10GB_FD;
640 desc->features = htonl(desc->features);
641 desc->speed = htonl(ecmd.speed);
648 fill_features_reply(struct datapath *dp, struct ofp_switch_features *ofr)
650 struct net_bridge_port *p;
653 ofr->datapath_id = cpu_to_be64(dp->id);
655 ofr->n_exact = htonl(2 * TABLE_HASH_MAX_FLOWS);
656 ofr->n_compression = 0; /* Not supported */
657 ofr->n_general = htonl(TABLE_LINEAR_MAX_FLOWS);
658 ofr->buffer_mb = htonl(UINT32_MAX);
659 ofr->n_buffers = htonl(N_PKT_BUFFERS);
660 ofr->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
661 ofr->actions = htonl(OFP_SUPPORTED_ACTIONS);
663 list_for_each_entry_rcu (p, &dp->port_list, node) {
664 fill_port_desc(p, &ofr->ports[port_count]);
672 dp_send_features_reply(struct datapath *dp, const struct sender *sender)
675 struct ofp_switch_features *ofr;
676 size_t ofr_len, port_max_len;
680 port_max_len = sizeof(struct ofp_phy_port) * OFPP_MAX;
681 ofr = alloc_openflow_skb(dp, sizeof(*ofr) + port_max_len,
682 OFPT_FEATURES_REPLY, sender, &skb);
687 port_count = fill_features_reply(dp, ofr);
690 ofr_len = sizeof(*ofr) + (sizeof(struct ofp_phy_port) * port_count);
691 resize_openflow_skb(skb, &ofr->header, ofr_len);
692 return send_openflow_skb(skb, sender);
696 dp_send_config_reply(struct datapath *dp, const struct sender *sender)
699 struct ofp_switch_config *osc;
701 osc = alloc_openflow_skb(dp, sizeof *osc, OFPT_PORT_STATUS, sender,
705 memcpy(((char *)osc) + sizeof osc->header,
706 ((char *)&dp->config) + sizeof dp->config.header,
707 sizeof dp->config - sizeof dp->config.header);
708 return send_openflow_skb(skb, sender);
712 dp_update_port_flags(struct datapath *dp, const struct ofp_phy_port *opp)
714 struct net_bridge_port *p;
716 p = dp->ports[htons(opp->port_no)];
718 /* Make sure the port id hasn't changed since this was sent */
719 if (!p || memcmp(opp->hw_addr, p->dev->dev_addr, ETH_ALEN) != 0)
722 p->flags = htonl(opp->flags);
729 send_port_status(struct net_bridge_port *p, uint8_t status)
732 struct ofp_port_status *ops;
734 ops = alloc_openflow_skb(p->dp, sizeof *ops, OFPT_PORT_STATUS, NULL,
738 ops->reason = status;
739 fill_port_desc(p, &ops->desc);
741 return send_openflow_skb(skb, NULL);
745 dp_send_flow_expired(struct datapath *dp, struct sw_flow *flow)
748 struct ofp_flow_expired *ofe;
749 unsigned long duration_j;
751 ofe = alloc_openflow_skb(dp, sizeof *ofe, OFPT_FLOW_EXPIRED, 0, &skb);
755 flow_fill_match(&ofe->match, &flow->key);
756 duration_j = (flow->timeout - HZ * flow->max_idle) - flow->init_time;
757 ofe->duration = htonl(duration_j / HZ);
758 ofe->packet_count = cpu_to_be64(flow->packet_count);
759 ofe->byte_count = cpu_to_be64(flow->byte_count);
760 return send_openflow_skb(skb, NULL);
764 fill_flow_stats(struct ofp_flow_stats *ofs, struct sw_flow *flow,
767 ofs->match.wildcards = htons(flow->key.wildcards);
768 ofs->match.in_port = flow->key.in_port;
769 memcpy(ofs->match.dl_src, flow->key.dl_src, ETH_ALEN);
770 memcpy(ofs->match.dl_dst, flow->key.dl_dst, ETH_ALEN);
771 ofs->match.dl_vlan = flow->key.dl_vlan;
772 ofs->match.dl_type = flow->key.dl_type;
773 ofs->match.nw_src = flow->key.nw_src;
774 ofs->match.nw_dst = flow->key.nw_dst;
775 ofs->match.nw_proto = flow->key.nw_proto;
776 memset(ofs->match.pad, 0, sizeof ofs->match.pad);
777 ofs->match.tp_src = flow->key.tp_src;
778 ofs->match.tp_dst = flow->key.tp_dst;
779 ofs->duration = htonl((jiffies - flow->init_time) / HZ);
780 ofs->table_id = table_idx;
781 ofs->packet_count = cpu_to_be64(flow->packet_count);
782 ofs->byte_count = cpu_to_be64(flow->byte_count);
786 dp_send_flow_stats(struct datapath *dp, const struct sender *sender,
787 const struct ofp_match *match)
790 struct ofp_flow_stat_reply *fsr;
791 size_t header_size, fudge, flow_size;
792 struct sw_flow_key match_key;
793 int table_idx, n_flows, max_flows;
795 header_size = offsetof(struct ofp_flow_stat_reply, flows);
797 flow_size = sizeof fsr->flows[0];
798 max_flows = (NLMSG_GOODSIZE - header_size - fudge) / flow_size;
799 fsr = alloc_openflow_skb(dp, header_size + max_flows * flow_size,
800 OFPT_FLOW_STAT_REPLY, sender, &skb);
805 flow_extract_match(&match_key, match);
806 for (table_idx = 0; table_idx < dp->chain->n_tables; table_idx++) {
807 struct sw_table *table = dp->chain->tables[table_idx];
808 struct swt_iterator iter;
810 if (n_flows >= max_flows) {
814 if (!table->iterator(table, &iter)) {
816 printk("iterator failed for table %d\n",
821 for (; iter.flow; table->iterator_next(&iter)) {
822 if (flow_matches(&match_key, &iter.flow->key)) {
823 fill_flow_stats(&fsr->flows[n_flows],
824 iter.flow, table_idx);
825 if (++n_flows >= max_flows) {
830 table->iterator_destroy(&iter);
832 resize_openflow_skb(skb, &fsr->header,
833 header_size + flow_size * n_flows);
834 return send_openflow_skb(skb, sender);
838 fill_port_stat_reply(struct datapath *dp, struct ofp_port_stat_reply *psr)
840 struct net_bridge_port *p;
843 list_for_each_entry_rcu (p, &dp->port_list, node) {
844 struct ofp_port_stats *ps = &psr->ports[port_count++];
845 struct net_device_stats *stats = p->dev->get_stats(p->dev);
846 ps->port_no = htons(p->port_no);
847 memset(ps->pad, 0, sizeof ps->pad);
848 ps->rx_count = cpu_to_be64(stats->rx_packets);
849 ps->tx_count = cpu_to_be64(stats->tx_packets);
850 ps->drop_count = cpu_to_be64(stats->rx_dropped
851 + stats->tx_dropped);
858 dp_send_port_stats(struct datapath *dp, const struct sender *sender)
861 struct ofp_port_stat_reply *psr;
862 size_t psr_len, port_max_len;
866 port_max_len = sizeof(struct ofp_port_stats) * OFPP_MAX;
867 psr = alloc_openflow_skb(dp, sizeof *psr + port_max_len,
868 OFPT_PORT_STAT_REPLY, sender, &skb);
873 port_count = fill_port_stat_reply(dp, psr);
876 psr_len = sizeof *psr + sizeof(struct ofp_port_stats) * port_count;
877 resize_openflow_skb(skb, &psr->header, psr_len);
878 return send_openflow_skb(skb, sender);
882 dp_send_table_stats(struct datapath *dp, const struct sender *sender)
885 struct ofp_table_stat_reply *tsr;
888 n_tables = dp->chain->n_tables;
889 tsr = alloc_openflow_skb(dp, (offsetof(struct ofp_table_stat_reply,
891 + sizeof tsr->tables[0] * n_tables),
892 OFPT_TABLE_STAT_REPLY, sender, &skb);
895 for (i = 0; i < n_tables; i++) {
896 struct ofp_table_stats *ots = &tsr->tables[i];
897 struct sw_table_stats stats;
898 dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
899 strncpy(ots->name, stats.name, sizeof ots->name);
901 ots->pad[0] = ots->pad[1] = 0;
902 ots->max_entries = htonl(stats.max_flows);
903 ots->active_count = htonl(stats.n_flows);
904 ots->matched_count = cpu_to_be64(0); /* FIXME */
906 return send_openflow_skb(skb, sender);
909 /* Generic Netlink interface.
911 * See netlink(7) for an introduction to netlink. See
912 * http://linux-net.osdl.org/index.php/Netlink for more information and
913 * pointers on how to work with netlink and Generic Netlink in the kernel and
916 static struct genl_family dp_genl_family = {
917 .id = GENL_ID_GENERATE,
919 .name = DP_GENL_FAMILY_NAME,
921 .maxattr = DP_GENL_A_MAX,
924 /* Attribute policy: what each attribute may contain. */
925 static struct nla_policy dp_genl_policy[DP_GENL_A_MAX + 1] = {
926 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
927 [DP_GENL_A_MC_GROUP] = { .type = NLA_U32 },
928 [DP_GENL_A_PORTNAME] = { .type = NLA_STRING }
931 static int dp_genl_add(struct sk_buff *skb, struct genl_info *info)
933 if (!info->attrs[DP_GENL_A_DP_IDX])
936 return new_dp(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
939 static struct genl_ops dp_genl_ops_add_dp = {
940 .cmd = DP_GENL_C_ADD_DP,
941 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
942 .policy = dp_genl_policy,
947 struct datapath *dp_get(int dp_idx)
949 if (dp_idx < 0 || dp_idx > DP_MAX)
951 return rcu_dereference(dps[dp_idx]);
954 static int dp_genl_del(struct sk_buff *skb, struct genl_info *info)
959 if (!info->attrs[DP_GENL_A_DP_IDX])
962 mutex_lock(&dp_mutex);
963 dp = dp_get(nla_get_u32((info->attrs[DP_GENL_A_DP_IDX])));
970 mutex_unlock(&dp_mutex);
974 static struct genl_ops dp_genl_ops_del_dp = {
975 .cmd = DP_GENL_C_DEL_DP,
976 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
977 .policy = dp_genl_policy,
982 /* Queries a datapath for related information. Currently the only relevant
983 * information is the datapath's multicast group ID. Really we want one
984 * multicast group per datapath, but because of locking issues[*] we can't
985 * easily get one. Thus, every datapath will currently return the same
986 * global multicast group ID, but in the future it would be nice to fix that.
988 * [*] dp_genl_add, to add a new datapath, is called under the genl_lock
989 * mutex, and genl_register_mc_group, called to acquire a new multicast
990 * group ID, also acquires genl_lock, thus deadlock.
992 static int dp_genl_query(struct sk_buff *skb, struct genl_info *info)
995 struct sk_buff *ans_skb = NULL;
999 if (!info->attrs[DP_GENL_A_DP_IDX])
1003 dp_idx = nla_get_u32((info->attrs[DP_GENL_A_DP_IDX]));
1004 dp = dp_get(dp_idx);
1009 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1014 data = genlmsg_put_reply(ans_skb, info, &dp_genl_family,
1015 0, DP_GENL_C_QUERY_DP);
1020 NLA_PUT_U32(ans_skb, DP_GENL_A_DP_IDX, dp_idx);
1021 NLA_PUT_U32(ans_skb, DP_GENL_A_MC_GROUP, mc_group.id);
1023 genlmsg_end(ans_skb, data);
1024 err = genlmsg_reply(ans_skb, info);
1036 static struct genl_ops dp_genl_ops_query_dp = {
1037 .cmd = DP_GENL_C_QUERY_DP,
1038 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1039 .policy = dp_genl_policy,
1040 .doit = dp_genl_query,
1044 static int dp_genl_add_del_port(struct sk_buff *skb, struct genl_info *info)
1046 struct datapath *dp;
1047 struct net_device *port;
1050 if (!info->attrs[DP_GENL_A_DP_IDX] || !info->attrs[DP_GENL_A_PORTNAME])
1054 mutex_lock(&dp_mutex);
1055 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1061 /* Get interface to add/remove. */
1062 port = dev_get_by_name(&init_net,
1063 nla_data(info->attrs[DP_GENL_A_PORTNAME]));
1069 /* Execute operation. */
1070 if (info->genlhdr->cmd == DP_GENL_C_ADD_PORT)
1071 err = add_switch_port(dp, port);
1073 if (port->br_port == NULL || port->br_port->dp != dp) {
1077 err = del_switch_port(port->br_port);
1083 mutex_unlock(&dp_mutex);
1087 static struct genl_ops dp_genl_ops_add_port = {
1088 .cmd = DP_GENL_C_ADD_PORT,
1089 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1090 .policy = dp_genl_policy,
1091 .doit = dp_genl_add_del_port,
1095 static struct genl_ops dp_genl_ops_del_port = {
1096 .cmd = DP_GENL_C_DEL_PORT,
1097 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1098 .policy = dp_genl_policy,
1099 .doit = dp_genl_add_del_port,
1103 static int dp_genl_openflow(struct sk_buff *skb, struct genl_info *info)
1105 struct nlattr *va = info->attrs[DP_GENL_A_OPENFLOW];
1106 struct datapath *dp;
1107 struct ofp_header *oh;
1108 struct sender sender;
1111 if (!info->attrs[DP_GENL_A_DP_IDX] || !va)
1115 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1121 if (nla_len(va) < sizeof(struct ofp_header)) {
1127 sender.xid = oh->xid;
1128 sender.pid = info->snd_pid;
1129 sender.seq = info->snd_seq;
1130 err = fwd_control_input(dp->chain, &sender, nla_data(va), nla_len(va));
1137 static struct nla_policy dp_genl_openflow_policy[DP_GENL_A_MAX + 1] = {
1138 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1141 static struct genl_ops dp_genl_ops_openflow = {
1142 .cmd = DP_GENL_C_OPENFLOW,
1143 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1144 .policy = dp_genl_openflow_policy,
1145 .doit = dp_genl_openflow,
1149 static struct nla_policy dp_genl_benchmark_policy[DP_GENL_A_MAX + 1] = {
1150 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1151 [DP_GENL_A_NPACKETS] = { .type = NLA_U32 },
1152 [DP_GENL_A_PSIZE] = { .type = NLA_U32 },
1155 static struct genl_ops dp_genl_ops_benchmark_nl = {
1156 .cmd = DP_GENL_C_BENCHMARK_NL,
1157 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1158 .policy = dp_genl_benchmark_policy,
1159 .doit = dp_genl_benchmark_nl,
1163 static struct genl_ops *dp_genl_all_ops[] = {
1164 /* Keep this operation first. Generic Netlink dispatching
1165 * looks up operations with linear search, so we want it at the
1167 &dp_genl_ops_openflow,
1169 &dp_genl_ops_add_dp,
1170 &dp_genl_ops_del_dp,
1171 &dp_genl_ops_query_dp,
1172 &dp_genl_ops_add_port,
1173 &dp_genl_ops_del_port,
1174 &dp_genl_ops_benchmark_nl,
1177 static int dp_init_netlink(void)
1182 err = genl_register_family(&dp_genl_family);
1186 for (i = 0; i < ARRAY_SIZE(dp_genl_all_ops); i++) {
1187 err = genl_register_ops(&dp_genl_family, dp_genl_all_ops[i]);
1189 goto err_unregister;
1192 strcpy(mc_group.name, "openflow");
1193 err = genl_register_mc_group(&dp_genl_family, &mc_group);
1195 goto err_unregister;
1200 genl_unregister_family(&dp_genl_family);
1204 static void dp_uninit_netlink(void)
1206 genl_unregister_family(&dp_genl_family);
1209 #define DRV_NAME "openflow"
1210 #define DRV_VERSION VERSION
1211 #define DRV_DESCRIPTION "OpenFlow switching datapath implementation"
1212 #define DRV_COPYRIGHT "Copyright (c) 2007, 2008 The Board of Trustees of The Leland Stanford Junior University"
1215 static int __init dp_init(void)
1219 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION "\n");
1220 printk(KERN_INFO DRV_NAME ": " VERSION" built on "__DATE__" "__TIME__"\n");
1221 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
1227 err = dp_init_netlink();
1229 goto error_flow_exit;
1231 /* Hook into callback used by the bridge to intercept packets.
1232 * Parasites we are. */
1233 if (br_handle_frame_hook)
1234 printk("openflow: hijacking bridge hook\n");
1235 br_handle_frame_hook = dp_frame_hook;
1242 printk(KERN_EMERG "openflow: failed to install!");
1246 static void dp_cleanup(void)
1249 dp_uninit_netlink();
1251 br_handle_frame_hook = NULL;
1254 module_init(dp_init);
1255 module_exit(dp_cleanup);
1257 MODULE_DESCRIPTION(DRV_DESCRIPTION);
1258 MODULE_AUTHOR(DRV_COPYRIGHT);
1259 MODULE_LICENSE("GPL");