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/netfilter_ipv4.h>
30 #include <linux/inetdevice.h>
31 #include <linux/list.h>
32 #include <linux/rculist.h>
33 #include <linux/workqueue.h>
34 #include <linux/dmi.h>
36 #include "openflow/nicira-ext.h"
37 #include "openflow/openflow-netlink.h"
39 #include "nx_act_snat.h"
49 /* Strings to describe the manufacturer, hardware, and software. This data
50 * is queriable through the switch description stats message. */
51 static char mfr_desc[DESC_STR_LEN] = "Nicira Networks, Inc.";
52 static char hw_desc[DESC_STR_LEN] = "Reference Linux Kernel Module";
53 static char sw_desc[DESC_STR_LEN] = VERSION BUILDNR;
54 static char serial_num[SERIAL_NUM_LEN] = "None";
56 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
57 module_param_string(mfr_desc, mfr_desc, sizeof mfr_desc, 0444);
58 module_param_string(hw_desc, hw_desc, sizeof hw_desc, 0444);
59 module_param_string(sw_desc, sw_desc, sizeof sw_desc, 0444);
60 module_param_string(serial_num, serial_num, sizeof serial_num, 0444);
62 MODULE_PARM(mfr_desc, "s");
63 MODULE_PARM(hw_desc, "s");
64 MODULE_PARM(sw_desc, "s");
65 MODULE_PARM(serial_num, "s");
69 /* Number of milliseconds between runs of the maintenance thread. */
70 #define MAINT_SLEEP_MSECS 1000
72 #define UINT32_MAX 4294967295U
73 #define UINT16_MAX 65535
74 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
76 static struct genl_family dp_genl_family;
77 static struct genl_multicast_group mc_group;
79 /* It's hard to imagine wanting more than one datapath, but... */
82 /* Datapaths. Protected on the read side by rcu_read_lock, on the write side
83 * by dp_mutex. dp_mutex is almost completely redundant with genl_mutex
84 * maintained by the Generic Netlink code, but the timeout path needs mutual
87 * It is safe to access the datapath and net_bridge_port structures with just
90 static struct datapath *dps[DP_MAX];
91 DEFINE_MUTEX(dp_mutex);
92 EXPORT_SYMBOL(dp_mutex);
94 static int dp_maint_func(void *data);
95 static void init_port_status(struct net_bridge_port *p);
96 static int dp_genl_openflow_done(struct netlink_callback *);
97 static struct net_bridge_port *new_nbp(struct datapath *,
98 struct net_device *, int port_no);
100 /* nla_shrink - reduce amount of space reserved by nla_reserve
101 * @skb: socket buffer from which to recover room
102 * @nla: netlink attribute to adjust
103 * @len: new length of attribute payload
105 * Reduces amount of space reserved by a call to nla_reserve.
107 * No other attributes may be added between calling nla_reserve and this
108 * function, since it will create a hole in the message.
110 void nla_shrink(struct sk_buff *skb, struct nlattr *nla, int len)
112 int delta = nla_total_size(len) - nla_total_size(nla_len(nla));
116 nla->nla_len = nla_attr_size(len);
119 /* Puts a set of openflow headers for a message of the given 'type' into 'skb'.
120 * If 'sender' is nonnull, then it is used as the message's destination. 'dp'
121 * must specify the datapath to use.
123 * '*max_openflow_len' receives the maximum number of bytes that are available
124 * for the embedded OpenFlow message. The caller must call
125 * resize_openflow_skb() to set the actual size of the message to this number
128 * Returns the openflow header if successful, otherwise (if 'skb' is too small)
131 put_openflow_headers(struct datapath *dp, struct sk_buff *skb, uint8_t type,
132 const struct sender *sender, int *max_openflow_len)
134 struct ofp_header *oh;
138 /* Assemble the Generic Netlink wrapper. */
139 if (!genlmsg_put(skb,
140 sender ? sender->pid : 0,
141 sender ? sender->seq : 0,
142 &dp_genl_family, 0, DP_GENL_C_OPENFLOW))
143 return ERR_PTR(-ENOBUFS);
144 if (nla_put_u32(skb, DP_GENL_A_DP_IDX, dp->dp_idx) < 0)
145 return ERR_PTR(-ENOBUFS);
146 openflow_len = (skb_tailroom(skb) - NLA_HDRLEN) & ~(NLA_ALIGNTO - 1);
147 if (openflow_len < sizeof *oh)
148 return ERR_PTR(-ENOBUFS);
149 *max_openflow_len = openflow_len;
150 attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, openflow_len);
153 /* Fill in the header. The caller is responsible for the length. */
155 oh->version = OFP_VERSION;
157 oh->xid = sender ? sender->xid : 0;
162 /* Resizes OpenFlow header 'oh', which must be at the tail end of 'skb', to new
163 * length 'new_length' (in bytes), adjusting pointers and size values as
166 resize_openflow_skb(struct sk_buff *skb,
167 struct ofp_header *oh, size_t new_length)
169 struct nlattr *attr = ((void *) oh) - NLA_HDRLEN;
170 nla_shrink(skb, attr, new_length);
171 oh->length = htons(new_length);
172 nlmsg_end(skb, (struct nlmsghdr *) skb->data);
175 /* Allocates a new skb to contain an OpenFlow message 'openflow_len' bytes in
176 * length. Returns a null pointer if memory is unavailable, otherwise returns
177 * the OpenFlow header and stores a pointer to the skb in '*pskb'.
179 * 'type' is the OpenFlow message type. If 'sender' is nonnull, then it is
180 * used as the message's destination. 'dp' must specify the datapath to
183 alloc_openflow_skb(struct datapath *dp, size_t openflow_len, uint8_t type,
184 const struct sender *sender, struct sk_buff **pskb)
186 struct ofp_header *oh;
189 int max_openflow_len;
191 if ((openflow_len + sizeof(struct ofp_header)) > UINT16_MAX) {
193 printk("alloc_openflow_skb: openflow message too large: %zu\n",
198 genl_len = nlmsg_total_size(GENL_HDRLEN + dp_genl_family.hdrsize);
199 genl_len += nla_total_size(sizeof(uint32_t)); /* DP_GENL_A_DP_IDX */
200 genl_len += nla_total_size(openflow_len); /* DP_GENL_A_OPENFLOW */
201 skb = *pskb = genlmsg_new(genl_len, GFP_ATOMIC);
204 printk("alloc_openflow_skb: genlmsg_new failed\n");
208 oh = put_openflow_headers(dp, skb, type, sender, &max_openflow_len);
209 BUG_ON(!oh || IS_ERR(oh));
210 resize_openflow_skb(skb, oh, openflow_len);
215 /* Sends 'skb' to 'sender' if it is nonnull, otherwise multicasts 'skb' to all
218 send_openflow_skb(struct sk_buff *skb, const struct sender *sender)
221 ? genlmsg_unicast(skb, sender->pid)
222 : genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC));
225 /* Retrieves the datapath id, which is the MAC address of the "of" device. */
227 uint64_t get_datapath_id(struct net_device *dev)
232 for (i=0; i<ETH_ALEN; i++)
233 id |= (uint64_t)dev->dev_addr[i] << (8*(ETH_ALEN-1 - i));
238 /* Creates a new datapath numbered 'dp_idx'. Returns 0 for success or a
239 * negative error code. */
240 static int new_dp(int dp_idx)
245 if (dp_idx < 0 || dp_idx >= DP_MAX)
248 if (!try_module_get(THIS_MODULE))
251 /* Exit early if a datapath with that number already exists. */
258 dp = kzalloc(sizeof *dp, GFP_KERNEL);
262 /* Setup our "of" device */
263 err = dp_dev_setup(dp);
268 dp->id = get_datapath_id(dp->netdev);
269 dp->chain = chain_create(dp);
270 if (dp->chain == NULL)
271 goto err_destroy_dp_dev;
272 INIT_LIST_HEAD(&dp->port_list);
274 dp->local_port = new_nbp(dp, dp->netdev, OFPP_LOCAL);
275 if (IS_ERR(dp->local_port)) {
276 err = PTR_ERR(dp->local_port);
277 goto err_destroy_local_port;
281 dp->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
283 dp->dp_task = kthread_run(dp_maint_func, dp, "dp%d", dp_idx);
284 if (IS_ERR(dp->dp_task))
285 goto err_destroy_chain;
291 err_destroy_local_port:
292 dp_del_switch_port(dp->local_port);
294 chain_destroy(dp->chain);
300 module_put(THIS_MODULE);
304 /* Find and return a free port number under 'dp'. */
305 static int find_portno(struct datapath *dp)
308 for (i = 0; i < DP_MAX_PORTS; i++)
309 if (dp->ports[i] == NULL)
314 static struct net_bridge_port *new_nbp(struct datapath *dp,
315 struct net_device *dev, int port_no)
317 struct net_bridge_port *p;
319 if (dev->br_port != NULL)
320 return ERR_PTR(-EBUSY);
322 p = kzalloc(sizeof(*p), GFP_KERNEL);
324 return ERR_PTR(-ENOMEM);
327 dev_set_promiscuity(dev, 1);
332 p->port_no = port_no;
333 spin_lock_init(&p->lock);
334 INIT_WORK(&p->port_task, NULL);
335 if (port_no != OFPP_LOCAL)
336 rcu_assign_pointer(dev->br_port, p);
337 if (port_no < DP_MAX_PORTS)
338 rcu_assign_pointer(dp->ports[port_no], p);
339 list_add_rcu(&p->node, &dp->port_list);
344 int add_switch_port(struct datapath *dp, struct net_device *dev)
346 struct net_bridge_port *p;
349 if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER
353 port_no = find_portno(dp);
357 p = new_nbp(dp, dev, port_no);
363 /* Notify the ctlpath that this port has been added */
364 dp_send_port_status(p, OFPPR_ADD);
369 /* Delete 'p' from switch. */
370 int dp_del_switch_port(struct net_bridge_port *p)
376 /* First drop references to device. */
377 cancel_work_sync(&p->port_task);
379 dev_set_promiscuity(p->dev, -1);
381 list_del_rcu(&p->node);
382 if (p->port_no != OFPP_LOCAL)
383 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
384 rcu_assign_pointer(p->dev->br_port, NULL);
386 /* Then wait until no one is still using it, and destroy it. */
390 /* Free any SNAT configuration on the port. */
391 spin_lock_irqsave(&p->lock, flags);
393 spin_unlock_irqrestore(&p->lock, flags);
396 /* Notify the ctlpath that this port no longer exists */
397 dp_send_port_status(p, OFPPR_DELETE);
405 static void del_dp(struct datapath *dp)
407 struct net_bridge_port *p, *n;
409 kthread_stop(dp->dp_task);
411 /* Drop references to DP. */
412 list_for_each_entry_safe (p, n, &dp->port_list, node)
413 dp_del_switch_port(p);
414 rcu_assign_pointer(dps[dp->dp_idx], NULL);
416 /* Kill off local_port dev references from buffered packets that have
417 * associated dst entries. */
421 /* Destroy dp->netdev. (Must follow deleting switch ports since
422 * dp->local_port has a reference to it.) */
425 /* Wait until no longer in use, then destroy it. */
427 chain_destroy(dp->chain);
429 module_put(THIS_MODULE);
432 static int dp_maint_func(void *data)
434 struct datapath *dp = (struct datapath *) data;
436 while (!kthread_should_stop()) {
438 struct net_bridge_port *p;
440 /* Expire old SNAT entries */
442 list_for_each_entry_rcu (p, &dp->port_list, node)
447 /* Timeout old entries */
448 chain_timeout(dp->chain);
449 msleep_interruptible(MAINT_SLEEP_MSECS);
456 do_port_input(struct net_bridge_port *p, struct sk_buff *skb)
458 /* Make our own copy of the packet. Otherwise we will mangle the
459 * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
460 * (No one comes after us, since we tell handle_bridge() that we took
462 skb = skb_share_check(skb, GFP_ATOMIC);
467 /* Check if this packet needs early SNAT processing. */
468 if (snat_pre_route(skb)) {
474 /* Push the Ethernet header back on. */
475 skb_push(skb, ETH_HLEN);
476 skb_reset_mac_header(skb);
477 fwd_port_input(p->dp->chain, skb, p);
481 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
482 * different set of devices!)
484 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
485 /* Called with rcu_read_lock. */
486 static struct sk_buff *dp_frame_hook(struct net_bridge_port *p,
489 do_port_input(p, skb);
492 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
493 static int dp_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
495 do_port_input(p, *pskb);
499 /* NB: This has only been tested on 2.4.35 */
500 static void dp_frame_hook(struct sk_buff *skb)
502 struct net_bridge_port *p = skb->dev->br_port;
505 do_port_input(p, skb);
512 /* Forwarding output path.
513 * Based on net/bridge/br_forward.c. */
515 static inline unsigned packet_length(const struct sk_buff *skb)
517 int length = skb->len - ETH_HLEN;
518 if (skb->protocol == htons(ETH_P_8021Q))
523 /* Send packets out all the ports except the originating one. If the
524 * "flood" argument is set, only send along the minimum spanning tree.
527 output_all(struct datapath *dp, struct sk_buff *skb, int flood)
529 u32 disable = flood ? OFPPC_NO_FLOOD : 0;
530 struct net_bridge_port *p;
533 list_for_each_entry_rcu (p, &dp->port_list, node) {
534 if (skb->dev == p->dev || p->config & disable)
536 if (prev_port != -1) {
537 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
542 dp_output_port(dp, clone, prev_port, 0);
544 prev_port = p->port_no;
547 dp_output_port(dp, skb, prev_port, 0);
554 /* Marks 'skb' as having originated from 'in_port' in 'dp'.
555 FIXME: how are devices reference counted? */
556 void dp_set_origin(struct datapath *dp, uint16_t in_port,
559 struct net_bridge_port *p;
560 p = (in_port < DP_MAX_PORTS ? dp->ports[in_port]
561 : in_port == OFPP_LOCAL ? dp->local_port
570 dp_xmit_skb(struct sk_buff *skb)
573 if (packet_length(skb) > skb->dev->mtu) {
574 printk("dropped over-mtu packet: %d > %d\n",
575 packet_length(skb), skb->dev->mtu);
585 /* Takes ownership of 'skb' and transmits it to 'out_port' on 'dp'.
587 int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port,
593 /* Send it out the port it came in on, which is already set in
597 printk("skb device not set forwarding to in_port\n");
601 return dp_xmit_skb(skb);
604 int retval = run_flow_through_tables(dp->chain, skb,
612 return output_all(dp, skb, 1);
615 return output_all(dp, skb, 0);
617 case OFPP_CONTROLLER:
618 return dp_output_control(dp, skb, fwd_save_skb(skb), 0,
622 struct net_device *dev = dp->netdev;
626 return dev ? dp_dev_recv(dev, skb) : -ESRCH;
629 case 0 ... DP_MAX_PORTS - 1: {
630 struct net_bridge_port *p = dp->ports[out_port];
633 if (p->dev == skb->dev) {
634 /* To send to the input port, must use OFPP_IN_PORT */
637 printk("can't directly forward to input port\n");
640 if (p->config & OFPPC_NO_FWD && !ignore_no_fwd) {
645 return dp_xmit_skb(skb);
655 printk("can't forward to bad port %d\n", out_port);
659 /* Takes ownership of 'skb' and transmits it to 'dp''s control path. If
660 * 'buffer_id' != -1, then only the first 64 bytes of 'skb' are sent;
661 * otherwise, all of 'skb' is sent. 'reason' indicates why 'skb' is being
662 * sent. 'max_len' sets the maximum number of bytes that the caller
663 * wants to be sent; a value of 0 indicates the entire packet should be
666 dp_output_control(struct datapath *dp, struct sk_buff *skb,
667 uint32_t buffer_id, size_t max_len, int reason)
669 /* FIXME? Can we avoid creating a new skbuff in the case where we
670 * forward the whole packet? */
671 struct sk_buff *f_skb;
672 struct ofp_packet_in *opi;
673 struct net_bridge_port *p;
674 size_t fwd_len, opi_len;
678 if ((buffer_id != (uint32_t) -1) && max_len)
679 fwd_len = min(fwd_len, max_len);
681 opi_len = offsetof(struct ofp_packet_in, data) + fwd_len;
682 opi = alloc_openflow_skb(dp, opi_len, OFPT_PACKET_IN, NULL, &f_skb);
687 opi->buffer_id = htonl(buffer_id);
688 opi->total_len = htons(skb->len);
689 p = skb->dev->br_port;
690 opi->in_port = htons(p ? p->port_no : OFPP_LOCAL);
691 opi->reason = reason;
693 memcpy(opi->data, skb_mac_header(skb), fwd_len);
694 err = send_openflow_skb(f_skb, NULL);
701 static void fill_port_desc(struct net_bridge_port *p, struct ofp_phy_port *desc)
704 desc->port_no = htons(p->port_no);
705 strncpy(desc->name, p->dev->name, OFP_MAX_PORT_NAME_LEN);
706 desc->name[OFP_MAX_PORT_NAME_LEN-1] = '\0';
707 memcpy(desc->hw_addr, p->dev->dev_addr, ETH_ALEN);
710 desc->advertised = 0;
713 spin_lock_irqsave(&p->lock, flags);
714 desc->config = htonl(p->config);
715 desc->state = htonl(p->state);
716 spin_unlock_irqrestore(&p->lock, flags);
718 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,24)
719 if (p->dev->ethtool_ops && p->dev->ethtool_ops->get_settings) {
720 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
722 if (!p->dev->ethtool_ops->get_settings(p->dev, &ecmd)) {
723 /* Set the supported features */
724 if (ecmd.supported & SUPPORTED_10baseT_Half)
725 desc->supported |= OFPPF_10MB_HD;
726 if (ecmd.supported & SUPPORTED_10baseT_Full)
727 desc->supported |= OFPPF_10MB_FD;
728 if (ecmd.supported & SUPPORTED_100baseT_Half)
729 desc->supported |= OFPPF_100MB_HD;
730 if (ecmd.supported & SUPPORTED_100baseT_Full)
731 desc->supported |= OFPPF_100MB_FD;
732 if (ecmd.supported & SUPPORTED_1000baseT_Half)
733 desc->supported |= OFPPF_1GB_HD;
734 if (ecmd.supported & SUPPORTED_1000baseT_Full)
735 desc->supported |= OFPPF_1GB_FD;
736 if (ecmd.supported & SUPPORTED_10000baseT_Full)
737 desc->supported |= OFPPF_10GB_FD;
738 if (ecmd.supported & SUPPORTED_TP)
739 desc->supported |= OFPPF_COPPER;
740 if (ecmd.supported & SUPPORTED_FIBRE)
741 desc->supported |= OFPPF_FIBER;
742 if (ecmd.supported & SUPPORTED_Autoneg)
743 desc->supported |= OFPPF_AUTONEG;
744 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
745 if (ecmd.supported & SUPPORTED_Pause)
746 desc->supported |= OFPPF_PAUSE;
747 if (ecmd.supported & SUPPORTED_Asym_Pause)
748 desc->supported |= OFPPF_PAUSE_ASYM;
749 #endif /* kernel >= 2.6.14 */
751 /* Set the advertised features */
752 if (ecmd.advertising & ADVERTISED_10baseT_Half)
753 desc->advertised |= OFPPF_10MB_HD;
754 if (ecmd.advertising & ADVERTISED_10baseT_Full)
755 desc->advertised |= OFPPF_10MB_FD;
756 if (ecmd.advertising & ADVERTISED_100baseT_Half)
757 desc->advertised |= OFPPF_100MB_HD;
758 if (ecmd.advertising & ADVERTISED_100baseT_Full)
759 desc->advertised |= OFPPF_100MB_FD;
760 if (ecmd.advertising & ADVERTISED_1000baseT_Half)
761 desc->advertised |= OFPPF_1GB_HD;
762 if (ecmd.advertising & ADVERTISED_1000baseT_Full)
763 desc->advertised |= OFPPF_1GB_FD;
764 if (ecmd.advertising & ADVERTISED_10000baseT_Full)
765 desc->advertised |= OFPPF_10GB_FD;
766 if (ecmd.advertising & ADVERTISED_TP)
767 desc->advertised |= OFPPF_COPPER;
768 if (ecmd.advertising & ADVERTISED_FIBRE)
769 desc->advertised |= OFPPF_FIBER;
770 if (ecmd.advertising & ADVERTISED_Autoneg)
771 desc->advertised |= OFPPF_AUTONEG;
772 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
773 if (ecmd.advertising & ADVERTISED_Pause)
774 desc->advertised |= OFPPF_PAUSE;
775 if (ecmd.advertising & ADVERTISED_Asym_Pause)
776 desc->advertised |= OFPPF_PAUSE_ASYM;
777 #endif /* kernel >= 2.6.14 */
779 /* Set the current features */
780 if (ecmd.speed == SPEED_10)
781 desc->curr = (ecmd.duplex) ? OFPPF_10MB_FD : OFPPF_10MB_HD;
782 else if (ecmd.speed == SPEED_100)
783 desc->curr = (ecmd.duplex) ? OFPPF_100MB_FD : OFPPF_100MB_HD;
784 else if (ecmd.speed == SPEED_1000)
785 desc->curr = (ecmd.duplex) ? OFPPF_1GB_FD : OFPPF_1GB_HD;
786 else if (ecmd.speed == SPEED_10000)
787 desc->curr = OFPPF_10GB_FD;
789 if (ecmd.port == PORT_TP)
790 desc->curr |= OFPPF_COPPER;
791 else if (ecmd.port == PORT_FIBRE)
792 desc->curr |= OFPPF_FIBER;
795 desc->curr |= OFPPF_AUTONEG;
799 desc->curr = htonl(desc->curr);
800 desc->supported = htonl(desc->supported);
801 desc->advertised = htonl(desc->advertised);
802 desc->peer = htonl(desc->peer);
806 fill_features_reply(struct datapath *dp, struct ofp_switch_features *ofr)
808 struct net_bridge_port *p;
811 ofr->datapath_id = cpu_to_be64(dp->id);
813 ofr->n_buffers = htonl(N_PKT_BUFFERS);
814 ofr->n_tables = dp->chain->n_tables;
815 ofr->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
816 ofr->actions = htonl(OFP_SUPPORTED_ACTIONS);
817 memset(ofr->pad, 0, sizeof ofr->pad);
819 list_for_each_entry_rcu (p, &dp->port_list, node) {
820 fill_port_desc(p, &ofr->ports[port_count]);
828 dp_send_features_reply(struct datapath *dp, const struct sender *sender)
831 struct ofp_switch_features *ofr;
832 size_t ofr_len, port_max_len;
836 port_max_len = sizeof(struct ofp_phy_port) * DP_MAX_PORTS;
837 ofr = alloc_openflow_skb(dp, sizeof(*ofr) + port_max_len,
838 OFPT_FEATURES_REPLY, sender, &skb);
843 port_count = fill_features_reply(dp, ofr);
846 ofr_len = sizeof(*ofr) + (sizeof(struct ofp_phy_port) * port_count);
847 resize_openflow_skb(skb, &ofr->header, ofr_len);
848 return send_openflow_skb(skb, sender);
852 dp_send_config_reply(struct datapath *dp, const struct sender *sender)
855 struct ofp_switch_config *osc;
857 osc = alloc_openflow_skb(dp, sizeof *osc, OFPT_GET_CONFIG_REPLY, sender,
862 osc->flags = htons(dp->flags);
863 osc->miss_send_len = htons(dp->miss_send_len);
865 return send_openflow_skb(skb, sender);
869 dp_send_hello(struct datapath *dp, const struct sender *sender,
870 const struct ofp_header *request)
872 if (request->version < OFP_VERSION) {
874 sprintf(err, "Only version 0x%02x supported", OFP_VERSION);
875 dp_send_error_msg(dp, sender, OFPET_HELLO_FAILED,
876 OFPHFC_INCOMPATIBLE, err, strlen(err));
880 struct ofp_header *reply;
882 reply = alloc_openflow_skb(dp, sizeof *reply,
883 OFPT_HELLO, sender, &skb);
887 return send_openflow_skb(skb, sender);
891 /* Callback function for a workqueue to disable an interface */
893 down_port_cb(struct work_struct *work)
895 struct net_bridge_port *p = container_of(work, struct net_bridge_port,
899 if (dev_change_flags(p->dev, p->dev->flags & ~IFF_UP) < 0)
901 printk("problem bringing up port %s\n", p->dev->name);
903 p->config |= OFPPC_PORT_DOWN;
906 /* Callback function for a workqueue to enable an interface */
908 up_port_cb(struct work_struct *work)
910 struct net_bridge_port *p = container_of(work, struct net_bridge_port,
914 if (dev_change_flags(p->dev, p->dev->flags | IFF_UP) < 0)
916 printk("problem bringing down port %s\n", p->dev->name);
918 p->config &= ~OFPPC_PORT_DOWN;
922 dp_update_port_flags(struct datapath *dp, const struct ofp_port_mod *opm)
924 unsigned long int flags;
925 int port_no = ntohs(opm->port_no);
926 struct net_bridge_port *p;
927 p = (port_no < DP_MAX_PORTS ? dp->ports[port_no]
928 : port_no == OFPP_LOCAL ? dp->local_port
931 /* Make sure the port id hasn't changed since this was sent */
932 if (!p || memcmp(opm->hw_addr, p->dev->dev_addr, ETH_ALEN))
935 spin_lock_irqsave(&p->lock, flags);
937 uint32_t config_mask = ntohl(opm->mask);
938 p->config &= ~config_mask;
939 p->config |= ntohl(opm->config) & config_mask;
942 /* Modifying the status of an interface requires taking a lock
943 * that cannot be done from here. For this reason, we use a shared
944 * workqueue, which will cause it to be executed from a safer
946 if (opm->mask & htonl(OFPPC_PORT_DOWN)) {
947 if ((opm->config & htonl(OFPPC_PORT_DOWN))
948 && (p->config & OFPPC_PORT_DOWN) == 0) {
949 PREPARE_WORK(&p->port_task, down_port_cb);
950 schedule_work(&p->port_task);
951 } else if ((opm->config & htonl(OFPPC_PORT_DOWN)) == 0
952 && (p->config & OFPPC_PORT_DOWN)) {
953 PREPARE_WORK(&p->port_task, up_port_cb);
954 schedule_work(&p->port_task);
957 spin_unlock_irqrestore(&p->lock, flags);
962 /* Initialize the port status field of the bridge port. */
964 init_port_status(struct net_bridge_port *p)
966 unsigned long int flags;
968 spin_lock_irqsave(&p->lock, flags);
970 if (p->dev->flags & IFF_UP)
971 p->config &= ~OFPPC_PORT_DOWN;
973 p->config |= OFPPC_PORT_DOWN;
975 if (netif_carrier_ok(p->dev))
976 p->state &= ~OFPPS_LINK_DOWN;
978 p->state |= OFPPS_LINK_DOWN;
980 spin_unlock_irqrestore(&p->lock, flags);
984 dp_send_port_status(struct net_bridge_port *p, uint8_t status)
987 struct ofp_port_status *ops;
989 ops = alloc_openflow_skb(p->dp, sizeof *ops, OFPT_PORT_STATUS, NULL,
993 ops->reason = status;
994 memset(ops->pad, 0, sizeof ops->pad);
995 fill_port_desc(p, &ops->desc);
997 return send_openflow_skb(skb, NULL);
1001 dp_send_flow_expired(struct datapath *dp, struct sw_flow *flow,
1002 enum ofp_flow_expired_reason reason)
1004 struct sk_buff *skb;
1005 struct ofp_flow_expired *ofe;
1007 if (!(dp->flags & OFPC_SEND_FLOW_EXP))
1010 ofe = alloc_openflow_skb(dp, sizeof *ofe, OFPT_FLOW_EXPIRED, 0, &skb);
1014 flow_fill_match(&ofe->match, &flow->key);
1016 ofe->priority = htons(flow->priority);
1017 ofe->reason = reason;
1018 memset(ofe->pad, 0, sizeof ofe->pad);
1020 ofe->duration = htonl((jiffies - flow->init_time) / HZ);
1021 memset(ofe->pad2, 0, sizeof ofe->pad2);
1022 ofe->packet_count = cpu_to_be64(flow->packet_count);
1023 ofe->byte_count = cpu_to_be64(flow->byte_count);
1025 return send_openflow_skb(skb, NULL);
1027 EXPORT_SYMBOL(dp_send_flow_expired);
1030 dp_send_error_msg(struct datapath *dp, const struct sender *sender,
1031 uint16_t type, uint16_t code, const void *data, size_t len)
1033 struct sk_buff *skb;
1034 struct ofp_error_msg *oem;
1037 oem = alloc_openflow_skb(dp, sizeof(*oem)+len, OFPT_ERROR,
1042 oem->type = htons(type);
1043 oem->code = htons(code);
1044 memcpy(oem->data, data, len);
1046 return send_openflow_skb(skb, sender);
1050 dp_send_echo_reply(struct datapath *dp, const struct sender *sender,
1051 const struct ofp_header *rq)
1053 struct sk_buff *skb;
1054 struct ofp_header *reply;
1056 reply = alloc_openflow_skb(dp, ntohs(rq->length), OFPT_ECHO_REPLY,
1061 memcpy(reply + 1, rq + 1, ntohs(rq->length) - sizeof *rq);
1062 return send_openflow_skb(skb, sender);
1065 /* Generic Netlink interface.
1067 * See netlink(7) for an introduction to netlink. See
1068 * http://linux-net.osdl.org/index.php/Netlink for more information and
1069 * pointers on how to work with netlink and Generic Netlink in the kernel and
1072 static struct genl_family dp_genl_family = {
1073 .id = GENL_ID_GENERATE,
1075 .name = DP_GENL_FAMILY_NAME,
1077 .maxattr = DP_GENL_A_MAX,
1080 /* Attribute policy: what each attribute may contain. */
1081 static struct nla_policy dp_genl_policy[DP_GENL_A_MAX + 1] = {
1082 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1083 [DP_GENL_A_MC_GROUP] = { .type = NLA_U32 },
1084 [DP_GENL_A_PORTNAME] = { .type = NLA_STRING }
1087 static int dp_genl_add(struct sk_buff *skb, struct genl_info *info)
1089 if (!info->attrs[DP_GENL_A_DP_IDX])
1092 return new_dp(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1095 static struct genl_ops dp_genl_ops_add_dp = {
1096 .cmd = DP_GENL_C_ADD_DP,
1097 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1098 .policy = dp_genl_policy,
1099 .doit = dp_genl_add,
1103 struct datapath *dp_get(int dp_idx)
1105 if (dp_idx < 0 || dp_idx > DP_MAX)
1107 return rcu_dereference(dps[dp_idx]);
1110 static int dp_genl_del(struct sk_buff *skb, struct genl_info *info)
1112 struct datapath *dp;
1115 if (!info->attrs[DP_GENL_A_DP_IDX])
1118 dp = dp_get(nla_get_u32((info->attrs[DP_GENL_A_DP_IDX])));
1128 static struct genl_ops dp_genl_ops_del_dp = {
1129 .cmd = DP_GENL_C_DEL_DP,
1130 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1131 .policy = dp_genl_policy,
1132 .doit = dp_genl_del,
1136 /* Queries a datapath for related information. Currently the only relevant
1137 * information is the datapath's multicast group ID. Really we want one
1138 * multicast group per datapath, but because of locking issues[*] we can't
1139 * easily get one. Thus, every datapath will currently return the same
1140 * global multicast group ID, but in the future it would be nice to fix that.
1142 * [*] dp_genl_add, to add a new datapath, is called under the genl_lock
1143 * mutex, and genl_register_mc_group, called to acquire a new multicast
1144 * group ID, also acquires genl_lock, thus deadlock.
1146 static int dp_genl_query(struct sk_buff *skb, struct genl_info *info)
1148 struct datapath *dp;
1149 struct sk_buff *ans_skb = NULL;
1153 if (!info->attrs[DP_GENL_A_DP_IDX])
1157 dp_idx = nla_get_u32((info->attrs[DP_GENL_A_DP_IDX]));
1158 dp = dp_get(dp_idx);
1163 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1168 data = genlmsg_put_reply(ans_skb, info, &dp_genl_family,
1169 0, DP_GENL_C_QUERY_DP);
1174 NLA_PUT_U32(ans_skb, DP_GENL_A_DP_IDX, dp_idx);
1175 NLA_PUT_U32(ans_skb, DP_GENL_A_MC_GROUP, mc_group.id);
1177 genlmsg_end(ans_skb, data);
1178 err = genlmsg_reply(ans_skb, info);
1190 static struct genl_ops dp_genl_ops_query_dp = {
1191 .cmd = DP_GENL_C_QUERY_DP,
1192 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1193 .policy = dp_genl_policy,
1194 .doit = dp_genl_query,
1198 static int dp_genl_add_del_port(struct sk_buff *skb, struct genl_info *info)
1200 struct datapath *dp;
1201 struct net_device *port;
1204 if (!info->attrs[DP_GENL_A_DP_IDX] || !info->attrs[DP_GENL_A_PORTNAME])
1208 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1214 /* Get interface to add/remove. */
1215 port = dev_get_by_name(&init_net,
1216 nla_data(info->attrs[DP_GENL_A_PORTNAME]));
1222 /* Execute operation. */
1223 if (info->genlhdr->cmd == DP_GENL_C_ADD_PORT)
1224 err = add_switch_port(dp, port);
1226 if (port->br_port == NULL || port->br_port->dp != dp) {
1230 err = dp_del_switch_port(port->br_port);
1239 static struct genl_ops dp_genl_ops_add_port = {
1240 .cmd = DP_GENL_C_ADD_PORT,
1241 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1242 .policy = dp_genl_policy,
1243 .doit = dp_genl_add_del_port,
1247 static struct genl_ops dp_genl_ops_del_port = {
1248 .cmd = DP_GENL_C_DEL_PORT,
1249 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1250 .policy = dp_genl_policy,
1251 .doit = dp_genl_add_del_port,
1255 static int dp_genl_openflow(struct sk_buff *skb, struct genl_info *info)
1257 struct nlattr *va = info->attrs[DP_GENL_A_OPENFLOW];
1258 struct datapath *dp;
1259 struct ofp_header *oh;
1260 struct sender sender;
1263 if (!info->attrs[DP_GENL_A_DP_IDX] || !va)
1266 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1270 if (nla_len(va) < sizeof(struct ofp_header))
1274 sender.xid = oh->xid;
1275 sender.pid = info->snd_pid;
1276 sender.seq = info->snd_seq;
1278 mutex_lock(&dp_mutex);
1279 err = fwd_control_input(dp->chain, &sender,
1280 nla_data(va), nla_len(va));
1281 mutex_unlock(&dp_mutex);
1285 static struct nla_policy dp_genl_openflow_policy[DP_GENL_A_MAX + 1] = {
1286 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1289 static int desc_stats_dump(struct datapath *dp, void *state,
1290 void *body, int *body_len)
1292 struct ofp_desc_stats *ods = body;
1293 int n_bytes = sizeof *ods;
1295 if (n_bytes > *body_len) {
1298 *body_len = n_bytes;
1300 strncpy(ods->mfr_desc, mfr_desc, sizeof ods->mfr_desc);
1301 strncpy(ods->hw_desc, hw_desc, sizeof ods->hw_desc);
1302 strncpy(ods->sw_desc, sw_desc, sizeof ods->sw_desc);
1303 strncpy(ods->serial_num, serial_num, sizeof ods->serial_num);
1308 struct flow_stats_state {
1310 struct sw_table_position position;
1311 const struct ofp_flow_stats_request *rq;
1314 int bytes_used, bytes_allocated;
1317 static int flow_stats_init(struct datapath *dp, const void *body, int body_len,
1320 const struct ofp_flow_stats_request *fsr = body;
1321 struct flow_stats_state *s = kmalloc(sizeof *s, GFP_ATOMIC);
1324 s->table_idx = fsr->table_id == 0xff ? 0 : fsr->table_id;
1325 memset(&s->position, 0, sizeof s->position);
1331 static int flow_stats_dump_callback(struct sw_flow *flow, void *private)
1333 struct sw_flow_actions *sf_acts = rcu_dereference(flow->sf_acts);
1334 struct flow_stats_state *s = private;
1335 struct ofp_flow_stats *ofs;
1338 length = sizeof *ofs + sf_acts->actions_len;
1339 if (length + s->bytes_used > s->bytes_allocated)
1342 ofs = s->body + s->bytes_used;
1343 ofs->length = htons(length);
1344 ofs->table_id = s->table_idx;
1346 ofs->match.wildcards = htonl(flow->key.wildcards);
1347 ofs->match.in_port = flow->key.in_port;
1348 memcpy(ofs->match.dl_src, flow->key.dl_src, ETH_ALEN);
1349 memcpy(ofs->match.dl_dst, flow->key.dl_dst, ETH_ALEN);
1350 ofs->match.dl_vlan = flow->key.dl_vlan;
1351 ofs->match.dl_type = flow->key.dl_type;
1352 ofs->match.nw_src = flow->key.nw_src;
1353 ofs->match.nw_dst = flow->key.nw_dst;
1354 ofs->match.nw_proto = flow->key.nw_proto;
1356 ofs->match.tp_src = flow->key.tp_src;
1357 ofs->match.tp_dst = flow->key.tp_dst;
1358 ofs->duration = htonl((jiffies - flow->init_time) / HZ);
1359 ofs->priority = htons(flow->priority);
1360 ofs->idle_timeout = htons(flow->idle_timeout);
1361 ofs->hard_timeout = htons(flow->hard_timeout);
1362 memset(ofs->pad2, 0, sizeof ofs->pad2);
1363 ofs->packet_count = cpu_to_be64(flow->packet_count);
1364 ofs->byte_count = cpu_to_be64(flow->byte_count);
1365 memcpy(ofs->actions, sf_acts->actions, sf_acts->actions_len);
1367 s->bytes_used += length;
1371 static int flow_stats_dump(struct datapath *dp, void *state,
1372 void *body, int *body_len)
1374 struct flow_stats_state *s = state;
1375 struct sw_flow_key match_key;
1379 s->bytes_allocated = *body_len;
1382 flow_extract_match(&match_key, &s->rq->match);
1383 while (s->table_idx < dp->chain->n_tables
1384 && (s->rq->table_id == 0xff || s->rq->table_id == s->table_idx))
1386 struct sw_table *table = dp->chain->tables[s->table_idx];
1388 error = table->iterate(table, &match_key, &s->position,
1389 flow_stats_dump_callback, s);
1394 memset(&s->position, 0, sizeof s->position);
1396 *body_len = s->bytes_used;
1398 /* If error is 0, we're done.
1399 * Otherwise, if some bytes were used, there are more flows to come.
1400 * Otherwise, we were not able to fit even a single flow in the body,
1401 * which indicates that we have a single flow with too many actions to
1402 * fit. We won't ever make any progress at that rate, so give up. */
1403 return !error ? 0 : s->bytes_used ? 1 : -ENOMEM;
1406 static void flow_stats_done(void *state)
1411 static int aggregate_stats_init(struct datapath *dp,
1412 const void *body, int body_len,
1415 *state = (void *)body;
1419 static int aggregate_stats_dump_callback(struct sw_flow *flow, void *private)
1421 struct ofp_aggregate_stats_reply *rpy = private;
1422 rpy->packet_count += flow->packet_count;
1423 rpy->byte_count += flow->byte_count;
1428 static int aggregate_stats_dump(struct datapath *dp, void *state,
1429 void *body, int *body_len)
1431 struct ofp_aggregate_stats_request *rq = state;
1432 struct ofp_aggregate_stats_reply *rpy;
1433 struct sw_table_position position;
1434 struct sw_flow_key match_key;
1437 if (*body_len < sizeof *rpy)
1440 *body_len = sizeof *rpy;
1442 memset(rpy, 0, sizeof *rpy);
1444 flow_extract_match(&match_key, &rq->match);
1445 table_idx = rq->table_id == 0xff ? 0 : rq->table_id;
1446 memset(&position, 0, sizeof position);
1447 while (table_idx < dp->chain->n_tables
1448 && (rq->table_id == 0xff || rq->table_id == table_idx))
1450 struct sw_table *table = dp->chain->tables[table_idx];
1453 error = table->iterate(table, &match_key, &position,
1454 aggregate_stats_dump_callback, rpy);
1459 memset(&position, 0, sizeof position);
1462 rpy->packet_count = cpu_to_be64(rpy->packet_count);
1463 rpy->byte_count = cpu_to_be64(rpy->byte_count);
1464 rpy->flow_count = htonl(rpy->flow_count);
1468 static int table_stats_dump(struct datapath *dp, void *state,
1469 void *body, int *body_len)
1471 struct ofp_table_stats *ots;
1472 int n_bytes = dp->chain->n_tables * sizeof *ots;
1474 if (n_bytes > *body_len)
1476 *body_len = n_bytes;
1477 for (i = 0, ots = body; i < dp->chain->n_tables; i++, ots++) {
1478 struct sw_table_stats stats;
1479 dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
1480 strncpy(ots->name, stats.name, sizeof ots->name);
1482 ots->wildcards = htonl(stats.wildcards);
1483 memset(ots->pad, 0, sizeof ots->pad);
1484 ots->max_entries = htonl(stats.max_flows);
1485 ots->active_count = htonl(stats.n_flows);
1486 ots->lookup_count = cpu_to_be64(stats.n_lookup);
1487 ots->matched_count = cpu_to_be64(stats.n_matched);
1492 struct port_stats_state {
1496 static int port_stats_init(struct datapath *dp, const void *body, int body_len,
1499 struct port_stats_state *s = kmalloc(sizeof *s, GFP_ATOMIC);
1507 static int port_stats_dump(struct datapath *dp, void *state,
1508 void *body, int *body_len)
1510 struct port_stats_state *s = state;
1511 struct ofp_port_stats *ops;
1512 int n_ports, max_ports;
1515 max_ports = *body_len / sizeof *ops;
1521 for (i = s->port; i < DP_MAX_PORTS && n_ports < max_ports; i++) {
1522 struct net_bridge_port *p = dp->ports[i];
1523 struct net_device_stats *stats;
1526 stats = p->dev->get_stats(p->dev);
1527 ops->port_no = htons(p->port_no);
1528 memset(ops->pad, 0, sizeof ops->pad);
1529 ops->rx_packets = cpu_to_be64(stats->rx_packets);
1530 ops->tx_packets = cpu_to_be64(stats->tx_packets);
1531 ops->rx_bytes = cpu_to_be64(stats->rx_bytes);
1532 ops->tx_bytes = cpu_to_be64(stats->tx_bytes);
1533 ops->rx_dropped = cpu_to_be64(stats->rx_dropped);
1534 ops->tx_dropped = cpu_to_be64(stats->tx_dropped);
1535 ops->rx_errors = cpu_to_be64(stats->rx_errors);
1536 ops->tx_errors = cpu_to_be64(stats->tx_errors);
1537 ops->rx_frame_err = cpu_to_be64(stats->rx_frame_errors);
1538 ops->rx_over_err = cpu_to_be64(stats->rx_over_errors);
1539 ops->rx_crc_err = cpu_to_be64(stats->rx_crc_errors);
1540 ops->collisions = cpu_to_be64(stats->collisions);
1545 *body_len = n_ports * sizeof *ops;
1546 return n_ports >= max_ports;
1549 static void port_stats_done(void *state)
1555 /* Minimum and maximum acceptable number of bytes in body member of
1556 * struct ofp_stats_request. */
1557 size_t min_body, max_body;
1559 /* Prepares to dump some kind of statistics on 'dp'. 'body' and
1560 * 'body_len' are the 'body' member of the struct ofp_stats_request.
1561 * Returns zero if successful, otherwise a negative error code.
1562 * May initialize '*state' to state information. May be null if no
1563 * initialization is required.*/
1564 int (*init)(struct datapath *dp, const void *body, int body_len,
1567 /* Dumps statistics for 'dp' into the '*body_len' bytes at 'body', and
1568 * modifies '*body_len' to reflect the number of bytes actually used.
1569 * ('body' will be transmitted as the 'body' member of struct
1570 * ofp_stats_reply.) */
1571 int (*dump)(struct datapath *dp, void *state,
1572 void *body, int *body_len);
1574 /* Cleans any state created by the init or dump functions. May be null
1575 * if no cleanup is required. */
1576 void (*done)(void *state);
1579 static const struct stats_type stats[] = {
1588 sizeof(struct ofp_flow_stats_request),
1589 sizeof(struct ofp_flow_stats_request),
1594 [OFPST_AGGREGATE] = {
1595 sizeof(struct ofp_aggregate_stats_request),
1596 sizeof(struct ofp_aggregate_stats_request),
1597 aggregate_stats_init,
1598 aggregate_stats_dump,
1618 dp_genl_openflow_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
1620 struct datapath *dp;
1621 struct sender sender;
1622 const struct stats_type *s;
1623 struct ofp_stats_reply *osr;
1625 int max_openflow_len, body_len;
1629 /* Set up the cleanup function for this dump. Linux 2.6.20 and later
1630 * support setting up cleanup functions via the .doneit member of
1631 * struct genl_ops. This kluge supports earlier versions also. */
1632 cb->done = dp_genl_openflow_done;
1634 sender.pid = NETLINK_CB(cb->skb).pid;
1635 sender.seq = cb->nlh->nlmsg_seq;
1637 struct nlattr *attrs[DP_GENL_A_MAX + 1];
1638 struct ofp_stats_request *rq;
1640 size_t len, body_len;
1643 err = nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs, DP_GENL_A_MAX,
1644 dp_genl_openflow_policy);
1648 if (!attrs[DP_GENL_A_DP_IDX])
1650 dp_idx = nla_get_u16(attrs[DP_GENL_A_DP_IDX]);
1651 dp = dp_get(dp_idx);
1655 va = attrs[DP_GENL_A_OPENFLOW];
1657 if (!va || len < sizeof *rq)
1661 sender.xid = rq->header.xid;
1662 type = ntohs(rq->type);
1663 if (rq->header.version != OFP_VERSION) {
1664 dp_send_error_msg(dp, &sender, OFPET_BAD_REQUEST,
1665 OFPBRC_BAD_VERSION, rq, len);
1668 if (rq->header.type != OFPT_STATS_REQUEST
1669 || ntohs(rq->header.length) != len)
1672 if (type >= ARRAY_SIZE(stats) || !stats[type].dump) {
1673 dp_send_error_msg(dp, &sender, OFPET_BAD_REQUEST,
1674 OFPBRC_BAD_STAT, rq, len);
1679 body_len = len - offsetof(struct ofp_stats_request, body);
1680 if (body_len < s->min_body || body_len > s->max_body)
1684 cb->args[1] = dp_idx;
1686 cb->args[3] = rq->header.xid;
1689 err = s->init(dp, rq->body, body_len, &state);
1692 cb->args[4] = (long) state;
1694 } else if (cb->args[0] == 1) {
1695 sender.xid = cb->args[3];
1696 dp_idx = cb->args[1];
1697 s = &stats[cb->args[2]];
1699 dp = dp_get(dp_idx);
1706 osr = put_openflow_headers(dp, skb, OFPT_STATS_REPLY, &sender,
1709 return PTR_ERR(osr);
1710 osr->type = htons(s - stats);
1712 resize_openflow_skb(skb, &osr->header, max_openflow_len);
1714 body_len = max_openflow_len - offsetof(struct ofp_stats_reply, body);
1716 err = s->dump(dp, (void *) cb->args[4], body, &body_len);
1721 osr->flags = ntohs(OFPSF_REPLY_MORE);
1722 resize_openflow_skb(skb, &osr->header,
1723 (offsetof(struct ofp_stats_reply, body)
1732 dp_genl_openflow_done(struct netlink_callback *cb)
1735 const struct stats_type *s = &stats[cb->args[2]];
1737 s->done((void *) cb->args[4]);
1742 static struct genl_ops dp_genl_ops_openflow = {
1743 .cmd = DP_GENL_C_OPENFLOW,
1744 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1745 .policy = dp_genl_openflow_policy,
1746 .doit = dp_genl_openflow,
1747 .dumpit = dp_genl_openflow_dumpit,
1750 static struct genl_ops *dp_genl_all_ops[] = {
1751 /* Keep this operation first. Generic Netlink dispatching
1752 * looks up operations with linear search, so we want it at the
1754 &dp_genl_ops_openflow,
1756 &dp_genl_ops_add_dp,
1757 &dp_genl_ops_del_dp,
1758 &dp_genl_ops_query_dp,
1759 &dp_genl_ops_add_port,
1760 &dp_genl_ops_del_port,
1763 static int dp_init_netlink(void)
1768 err = genl_register_family(&dp_genl_family);
1772 for (i = 0; i < ARRAY_SIZE(dp_genl_all_ops); i++) {
1773 err = genl_register_ops(&dp_genl_family, dp_genl_all_ops[i]);
1775 goto err_unregister;
1778 strcpy(mc_group.name, "openflow");
1779 err = genl_register_mc_group(&dp_genl_family, &mc_group);
1781 goto err_unregister;
1786 genl_unregister_family(&dp_genl_family);
1790 static void dp_uninit_netlink(void)
1792 genl_unregister_family(&dp_genl_family);
1795 /* Set the description strings if appropriate values are available from
1797 static void set_desc(void)
1799 const char *uuid = dmi_get_system_info(DMI_PRODUCT_UUID);
1800 const char *uptr = uuid + 24;
1802 if (!uuid || *uuid == '\0' || strlen(uuid) != 36)
1805 /* We are only interested version 1 UUIDs, since the last six bytes
1806 * are an IEEE 802 MAC address. */
1807 if (uuid[14] != '1')
1810 /* Only set if the UUID is from Nicira. */
1811 if (strncmp(uptr, NICIRA_OUI_STR, strlen(NICIRA_OUI_STR)))
1814 strlcpy(mfr_desc, dmi_get_system_info(DMI_SYS_VENDOR), sizeof(mfr_desc));
1815 snprintf(hw_desc, sizeof(hw_desc), "%s %s",
1816 dmi_get_system_info(DMI_PRODUCT_NAME),
1817 dmi_get_system_info(DMI_PRODUCT_VERSION));
1818 strlcpy(serial_num, dmi_get_system_info(DMI_PRODUCT_SERIAL),
1819 sizeof(serial_num));
1822 static int __init dp_init(void)
1826 printk("OpenFlow %s, built "__DATE__" "__TIME__", "
1827 "protocol 0x%02x\n", VERSION BUILDNR, OFP_VERSION);
1833 err = register_netdevice_notifier(&dp_device_notifier);
1835 goto error_flow_exit;
1837 err = dp_init_netlink();
1839 goto error_unreg_notifier;
1841 /* Check if better descriptions of the switch are available than the
1845 /* Hook into callback used by the bridge to intercept packets.
1846 * Parasites we are. */
1847 if (br_handle_frame_hook)
1848 printk("openflow: hijacking bridge hook\n");
1849 br_handle_frame_hook = dp_frame_hook;
1853 error_unreg_notifier:
1854 unregister_netdevice_notifier(&dp_device_notifier);
1858 printk(KERN_EMERG "openflow: failed to install!");
1862 static void dp_cleanup(void)
1865 dp_uninit_netlink();
1866 unregister_netdevice_notifier(&dp_device_notifier);
1868 br_handle_frame_hook = NULL;
1871 module_init(dp_init);
1872 module_exit(dp_cleanup);
1874 MODULE_DESCRIPTION("OpenFlow switching datapath");
1875 MODULE_AUTHOR("Copyright (c) 2007, 2008 The Board of Trustees of The Leland Stanford Junior University");
1876 MODULE_LICENSE("GPL");