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>
35 #include "openflow-netlink.h"
37 #include "nx_act_snat.h"
47 /* Strings to describe the manufacturer, hardware, and software. This data
48 * is queriable through the switch description stats message. */
49 static char mfr_desc[DESC_STR_LEN] = "Nicira Networks";
50 static char hw_desc[DESC_STR_LEN] = "Reference Linux Kernel Module";
51 static char sw_desc[DESC_STR_LEN] = VERSION;
52 static char serial_num[SERIAL_NUM_LEN] = "None";
54 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
55 module_param_string(mfr_desc, mfr_desc, sizeof mfr_desc, 0444);
56 module_param_string(hw_desc, hw_desc, sizeof hw_desc, 0444);
57 module_param_string(sw_desc, sw_desc, sizeof sw_desc, 0444);
58 module_param_string(serial_num, serial_num, sizeof serial_num, 0444);
60 MODULE_PARM(mfr_desc, "s");
61 MODULE_PARM(hw_desc, "s");
62 MODULE_PARM(sw_desc, "s");
63 MODULE_PARM(serial_num, "s");
67 /* Number of milliseconds between runs of the maintenance thread. */
68 #define MAINT_SLEEP_MSECS 1000
70 #define UINT32_MAX 4294967295U
71 #define UINT16_MAX 65535
72 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
74 static struct genl_family dp_genl_family;
75 static struct genl_multicast_group mc_group;
77 /* It's hard to imagine wanting more than one datapath, but... */
80 /* Datapaths. Protected on the read side by rcu_read_lock, on the write side
81 * by dp_mutex. dp_mutex is almost completely redundant with genl_mutex
82 * maintained by the Generic Netlink code, but the timeout path needs mutual
85 * It is safe to access the datapath and net_bridge_port structures with just
88 static struct datapath *dps[DP_MAX];
89 DEFINE_MUTEX(dp_mutex);
90 EXPORT_SYMBOL(dp_mutex);
92 static int dp_maint_func(void *data);
93 static void init_port_status(struct net_bridge_port *p);
94 static int dp_genl_openflow_done(struct netlink_callback *);
95 static struct net_bridge_port *new_nbp(struct datapath *,
96 struct net_device *, int port_no);
98 /* nla_shrink - reduce amount of space reserved by nla_reserve
99 * @skb: socket buffer from which to recover room
100 * @nla: netlink attribute to adjust
101 * @len: new length of attribute payload
103 * Reduces amount of space reserved by a call to nla_reserve.
105 * No other attributes may be added between calling nla_reserve and this
106 * function, since it will create a hole in the message.
108 void nla_shrink(struct sk_buff *skb, struct nlattr *nla, int len)
110 int delta = nla_total_size(len) - nla_total_size(nla_len(nla));
114 nla->nla_len = nla_attr_size(len);
117 /* Puts a set of openflow headers for a message of the given 'type' into 'skb'.
118 * If 'sender' is nonnull, then it is used as the message's destination. 'dp'
119 * must specify the datapath to use.
121 * '*max_openflow_len' receives the maximum number of bytes that are available
122 * for the embedded OpenFlow message. The caller must call
123 * resize_openflow_skb() to set the actual size of the message to this number
126 * Returns the openflow header if successful, otherwise (if 'skb' is too small)
129 put_openflow_headers(struct datapath *dp, struct sk_buff *skb, uint8_t type,
130 const struct sender *sender, int *max_openflow_len)
132 struct ofp_header *oh;
136 /* Assemble the Generic Netlink wrapper. */
137 if (!genlmsg_put(skb,
138 sender ? sender->pid : 0,
139 sender ? sender->seq : 0,
140 &dp_genl_family, 0, DP_GENL_C_OPENFLOW))
141 return ERR_PTR(-ENOBUFS);
142 if (nla_put_u32(skb, DP_GENL_A_DP_IDX, dp->dp_idx) < 0)
143 return ERR_PTR(-ENOBUFS);
144 openflow_len = (skb_tailroom(skb) - NLA_HDRLEN) & ~(NLA_ALIGNTO - 1);
145 if (openflow_len < sizeof *oh)
146 return ERR_PTR(-ENOBUFS);
147 *max_openflow_len = openflow_len;
148 attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, openflow_len);
151 /* Fill in the header. The caller is responsible for the length. */
153 oh->version = OFP_VERSION;
155 oh->xid = sender ? sender->xid : 0;
160 /* Resizes OpenFlow header 'oh', which must be at the tail end of 'skb', to new
161 * length 'new_length' (in bytes), adjusting pointers and size values as
164 resize_openflow_skb(struct sk_buff *skb,
165 struct ofp_header *oh, size_t new_length)
167 struct nlattr *attr = ((void *) oh) - NLA_HDRLEN;
168 nla_shrink(skb, attr, new_length);
169 oh->length = htons(new_length);
170 nlmsg_end(skb, (struct nlmsghdr *) skb->data);
173 /* Allocates a new skb to contain an OpenFlow message 'openflow_len' bytes in
174 * length. Returns a null pointer if memory is unavailable, otherwise returns
175 * the OpenFlow header and stores a pointer to the skb in '*pskb'.
177 * 'type' is the OpenFlow message type. If 'sender' is nonnull, then it is
178 * used as the message's destination. 'dp' must specify the datapath to
181 alloc_openflow_skb(struct datapath *dp, size_t openflow_len, uint8_t type,
182 const struct sender *sender, struct sk_buff **pskb)
184 struct ofp_header *oh;
187 int max_openflow_len;
189 if ((openflow_len + sizeof(struct ofp_header)) > UINT16_MAX) {
191 printk("alloc_openflow_skb: openflow message too large: %zu\n",
196 genl_len = nlmsg_total_size(GENL_HDRLEN + dp_genl_family.hdrsize);
197 genl_len += nla_total_size(sizeof(uint32_t)); /* DP_GENL_A_DP_IDX */
198 genl_len += nla_total_size(openflow_len); /* DP_GENL_A_OPENFLOW */
199 skb = *pskb = genlmsg_new(genl_len, GFP_ATOMIC);
202 printk("alloc_openflow_skb: genlmsg_new failed\n");
206 oh = put_openflow_headers(dp, skb, type, sender, &max_openflow_len);
207 BUG_ON(!oh || IS_ERR(oh));
208 resize_openflow_skb(skb, oh, openflow_len);
213 /* Sends 'skb' to 'sender' if it is nonnull, otherwise multicasts 'skb' to all
216 send_openflow_skb(struct sk_buff *skb, const struct sender *sender)
219 ? genlmsg_unicast(skb, sender->pid)
220 : genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC));
223 /* Generates a unique datapath id. It incorporates the datapath index
224 * and a hardware address, if available. If not, it generates a random
228 uint64_t gen_datapath_id(uint16_t dp_idx)
232 struct net_device *dev;
234 /* The top 16 bits are used to identify the datapath. The lower 48 bits
235 * use an interface address. */
236 id = (uint64_t)dp_idx << 48;
237 if ((dev = dev_get_by_name(&init_net, "ctl0"))
238 || (dev = dev_get_by_name(&init_net, "eth0"))) {
239 for (i=0; i<ETH_ALEN; i++) {
240 id |= (uint64_t)dev->dev_addr[i] << (8*(ETH_ALEN-1 - i));
244 /* Randomly choose the lower 48 bits if we cannot find an
245 * address and mark the most significant bit to indicate that
246 * this was randomly generated. */
247 uint8_t rand[ETH_ALEN];
248 get_random_bytes(rand, ETH_ALEN);
249 id |= (uint64_t)1 << 63;
250 for (i=0; i<ETH_ALEN; i++) {
251 id |= (uint64_t)rand[i] << (8*(ETH_ALEN-1 - i));
258 /* Creates a new datapath numbered 'dp_idx'. Returns 0 for success or a
259 * negative error code. */
260 static int new_dp(int dp_idx)
265 if (dp_idx < 0 || dp_idx >= DP_MAX)
268 if (!try_module_get(THIS_MODULE))
271 /* Exit early if a datapath with that number already exists. */
278 dp = kzalloc(sizeof *dp, GFP_KERNEL);
282 /* Setup our "of" device */
283 err = dp_dev_setup(dp);
288 dp->id = gen_datapath_id(dp_idx);
289 dp->chain = chain_create(dp);
290 if (dp->chain == NULL)
291 goto err_destroy_dp_dev;
292 INIT_LIST_HEAD(&dp->port_list);
294 dp->local_port = new_nbp(dp, dp->netdev, OFPP_LOCAL);
295 if (IS_ERR(dp->local_port)) {
296 err = PTR_ERR(dp->local_port);
297 goto err_destroy_local_port;
301 dp->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
303 dp->dp_task = kthread_run(dp_maint_func, dp, "dp%d", dp_idx);
304 if (IS_ERR(dp->dp_task))
305 goto err_destroy_chain;
311 err_destroy_local_port:
312 dp_del_switch_port(dp->local_port);
314 chain_destroy(dp->chain);
320 module_put(THIS_MODULE);
324 /* Find and return a free port number under 'dp'. */
325 static int find_portno(struct datapath *dp)
328 for (i = 0; i < DP_MAX_PORTS; i++)
329 if (dp->ports[i] == NULL)
334 static struct net_bridge_port *new_nbp(struct datapath *dp,
335 struct net_device *dev, int port_no)
337 struct net_bridge_port *p;
339 if (dev->br_port != NULL)
340 return ERR_PTR(-EBUSY);
342 p = kzalloc(sizeof(*p), GFP_KERNEL);
344 return ERR_PTR(-ENOMEM);
347 dev_set_promiscuity(dev, 1);
352 p->port_no = port_no;
353 spin_lock_init(&p->lock);
354 INIT_WORK(&p->port_task, NULL);
355 if (port_no != OFPP_LOCAL)
356 rcu_assign_pointer(dev->br_port, p);
357 if (port_no < DP_MAX_PORTS)
358 rcu_assign_pointer(dp->ports[port_no], p);
359 list_add_rcu(&p->node, &dp->port_list);
364 int add_switch_port(struct datapath *dp, struct net_device *dev)
366 struct net_bridge_port *p;
369 if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER
373 port_no = find_portno(dp);
377 p = new_nbp(dp, dev, port_no);
383 /* Notify the ctlpath that this port has been added */
384 dp_send_port_status(p, OFPPR_ADD);
389 /* Delete 'p' from switch. */
390 int dp_del_switch_port(struct net_bridge_port *p)
396 /* First drop references to device. */
397 cancel_work_sync(&p->port_task);
399 dev_set_promiscuity(p->dev, -1);
401 list_del_rcu(&p->node);
402 if (p->port_no != OFPP_LOCAL)
403 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
404 rcu_assign_pointer(p->dev->br_port, NULL);
406 /* Then wait until no one is still using it, and destroy it. */
410 /* Free any SNAT configuration on the port. */
411 spin_lock_irqsave(&p->lock, flags);
413 spin_unlock_irqrestore(&p->lock, flags);
416 /* Notify the ctlpath that this port no longer exists */
417 dp_send_port_status(p, OFPPR_DELETE);
425 static void del_dp(struct datapath *dp)
427 struct net_bridge_port *p, *n;
429 kthread_stop(dp->dp_task);
431 /* Drop references to DP. */
432 list_for_each_entry_safe (p, n, &dp->port_list, node)
433 dp_del_switch_port(p);
434 rcu_assign_pointer(dps[dp->dp_idx], NULL);
436 /* Kill off local_port dev references from buffered packets that have
437 * associated dst entries. */
441 /* Destroy dp->netdev. (Must follow deleting switch ports since
442 * dp->local_port has a reference to it.) */
445 /* Wait until no longer in use, then destroy it. */
447 chain_destroy(dp->chain);
449 module_put(THIS_MODULE);
452 static int dp_maint_func(void *data)
454 struct datapath *dp = (struct datapath *) data;
456 while (!kthread_should_stop()) {
458 struct net_bridge_port *p;
460 /* Expire old SNAT entries */
462 list_for_each_entry_rcu (p, &dp->port_list, node)
467 /* Timeout old entries */
468 chain_timeout(dp->chain);
469 msleep_interruptible(MAINT_SLEEP_MSECS);
476 do_port_input(struct net_bridge_port *p, struct sk_buff *skb)
479 /* Check if this packet needs early SNAT processing. */
480 if (snat_pre_route(skb)) {
486 /* Push the Ethernet header back on. */
487 skb_push(skb, ETH_HLEN);
488 fwd_port_input(p->dp->chain, skb, p);
492 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
493 * different set of devices!)
495 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
496 /* Called with rcu_read_lock. */
497 static struct sk_buff *dp_frame_hook(struct net_bridge_port *p,
500 do_port_input(p, skb);
503 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
504 static int dp_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
506 do_port_input(p, *pskb);
510 /* NB: This has only been tested on 2.4.35 */
511 static void dp_frame_hook(struct sk_buff *skb)
513 struct net_bridge_port *p = skb->dev->br_port;
516 do_port_input(p, skb);
523 /* Forwarding output path.
524 * Based on net/bridge/br_forward.c. */
526 static inline unsigned packet_length(const struct sk_buff *skb)
528 int length = skb->len - ETH_HLEN;
529 if (skb->protocol == htons(ETH_P_8021Q))
534 /* Send packets out all the ports except the originating one. If the
535 * "flood" argument is set, only send along the minimum spanning tree.
538 output_all(struct datapath *dp, struct sk_buff *skb, int flood)
540 u32 disable = flood ? OFPPC_NO_FLOOD : 0;
541 struct net_bridge_port *p;
544 list_for_each_entry_rcu (p, &dp->port_list, node) {
545 if (skb->dev == p->dev || p->config & disable)
547 if (prev_port != -1) {
548 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
553 dp_output_port(dp, clone, prev_port, 0);
555 prev_port = p->port_no;
558 dp_output_port(dp, skb, prev_port, 0);
565 /* Marks 'skb' as having originated from 'in_port' in 'dp'.
566 FIXME: how are devices reference counted? */
567 void dp_set_origin(struct datapath *dp, uint16_t in_port,
570 struct net_bridge_port *p;
571 p = (in_port < DP_MAX_PORTS ? dp->ports[in_port]
572 : in_port == OFPP_LOCAL ? dp->local_port
581 dp_xmit_skb(struct sk_buff *skb)
584 if (packet_length(skb) > skb->dev->mtu) {
585 printk("dropped over-mtu packet: %d > %d\n",
586 packet_length(skb), skb->dev->mtu);
596 /* Takes ownership of 'skb' and transmits it to 'out_port' on 'dp'.
598 int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port,
604 /* Send it out the port it came in on, which is already set in
608 printk("skb device not set forwarding to in_port\n");
612 return dp_xmit_skb(skb);
615 int retval = run_flow_through_tables(dp->chain, skb,
623 return output_all(dp, skb, 1);
626 return output_all(dp, skb, 0);
628 case OFPP_CONTROLLER:
629 return dp_output_control(dp, skb, fwd_save_skb(skb), 0,
633 struct net_device *dev = dp->netdev;
637 return dev ? dp_dev_recv(dev, skb) : -ESRCH;
640 case 0 ... DP_MAX_PORTS - 1: {
641 struct net_bridge_port *p = dp->ports[out_port];
644 if (p->dev == skb->dev) {
645 /* To send to the input port, must use OFPP_IN_PORT */
648 printk("can't directly forward to input port\n");
651 if (p->config & OFPPC_NO_FWD && !ignore_no_fwd) {
656 return dp_xmit_skb(skb);
666 printk("can't forward to bad port %d\n", out_port);
670 /* Takes ownership of 'skb' and transmits it to 'dp''s control path. If
671 * 'buffer_id' != -1, then only the first 64 bytes of 'skb' are sent;
672 * otherwise, all of 'skb' is sent. 'reason' indicates why 'skb' is being
673 * sent. 'max_len' sets the maximum number of bytes that the caller
674 * wants to be sent; a value of 0 indicates the entire packet should be
677 dp_output_control(struct datapath *dp, struct sk_buff *skb,
678 uint32_t buffer_id, size_t max_len, int reason)
680 /* FIXME? Can we avoid creating a new skbuff in the case where we
681 * forward the whole packet? */
682 struct sk_buff *f_skb;
683 struct ofp_packet_in *opi;
684 struct net_bridge_port *p;
685 size_t fwd_len, opi_len;
689 if ((buffer_id != (uint32_t) -1) && max_len)
690 fwd_len = min(fwd_len, max_len);
692 opi_len = offsetof(struct ofp_packet_in, data) + fwd_len;
693 opi = alloc_openflow_skb(dp, opi_len, OFPT_PACKET_IN, NULL, &f_skb);
698 opi->buffer_id = htonl(buffer_id);
699 opi->total_len = htons(skb->len);
700 p = skb->dev->br_port;
701 opi->in_port = htons(p ? p->port_no : OFPP_LOCAL);
702 opi->reason = reason;
704 memcpy(opi->data, skb_mac_header(skb), fwd_len);
705 err = send_openflow_skb(f_skb, NULL);
712 static void fill_port_desc(struct net_bridge_port *p, struct ofp_phy_port *desc)
715 desc->port_no = htons(p->port_no);
716 strncpy(desc->name, p->dev->name, OFP_MAX_PORT_NAME_LEN);
717 desc->name[OFP_MAX_PORT_NAME_LEN-1] = '\0';
718 memcpy(desc->hw_addr, p->dev->dev_addr, ETH_ALEN);
721 desc->advertised = 0;
724 spin_lock_irqsave(&p->lock, flags);
725 desc->config = htonl(p->config);
726 desc->state = htonl(p->state);
727 spin_unlock_irqrestore(&p->lock, flags);
729 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,24)
730 if (p->dev->ethtool_ops && p->dev->ethtool_ops->get_settings) {
731 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
733 if (!p->dev->ethtool_ops->get_settings(p->dev, &ecmd)) {
734 /* Set the supported features */
735 if (ecmd.supported & SUPPORTED_10baseT_Half)
736 desc->supported |= OFPPF_10MB_HD;
737 if (ecmd.supported & SUPPORTED_10baseT_Full)
738 desc->supported |= OFPPF_10MB_FD;
739 if (ecmd.supported & SUPPORTED_100baseT_Half)
740 desc->supported |= OFPPF_100MB_HD;
741 if (ecmd.supported & SUPPORTED_100baseT_Full)
742 desc->supported |= OFPPF_100MB_FD;
743 if (ecmd.supported & SUPPORTED_1000baseT_Half)
744 desc->supported |= OFPPF_1GB_HD;
745 if (ecmd.supported & SUPPORTED_1000baseT_Full)
746 desc->supported |= OFPPF_1GB_FD;
747 if (ecmd.supported & SUPPORTED_10000baseT_Full)
748 desc->supported |= OFPPF_10GB_FD;
749 if (ecmd.supported & SUPPORTED_TP)
750 desc->supported |= OFPPF_COPPER;
751 if (ecmd.supported & SUPPORTED_FIBRE)
752 desc->supported |= OFPPF_FIBER;
753 if (ecmd.supported & SUPPORTED_Autoneg)
754 desc->supported |= OFPPF_AUTONEG;
755 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
756 if (ecmd.supported & SUPPORTED_Pause)
757 desc->supported |= OFPPF_PAUSE;
758 if (ecmd.supported & SUPPORTED_Asym_Pause)
759 desc->supported |= OFPPF_PAUSE_ASYM;
760 #endif /* kernel >= 2.6.14 */
762 /* Set the advertised features */
763 if (ecmd.advertising & ADVERTISED_10baseT_Half)
764 desc->advertised |= OFPPF_10MB_HD;
765 if (ecmd.advertising & ADVERTISED_10baseT_Full)
766 desc->advertised |= OFPPF_10MB_FD;
767 if (ecmd.advertising & ADVERTISED_100baseT_Half)
768 desc->advertised |= OFPPF_100MB_HD;
769 if (ecmd.advertising & ADVERTISED_100baseT_Full)
770 desc->advertised |= OFPPF_100MB_FD;
771 if (ecmd.advertising & ADVERTISED_1000baseT_Half)
772 desc->advertised |= OFPPF_1GB_HD;
773 if (ecmd.advertising & ADVERTISED_1000baseT_Full)
774 desc->advertised |= OFPPF_1GB_FD;
775 if (ecmd.advertising & ADVERTISED_10000baseT_Full)
776 desc->advertised |= OFPPF_10GB_FD;
777 if (ecmd.advertising & ADVERTISED_TP)
778 desc->advertised |= OFPPF_COPPER;
779 if (ecmd.advertising & ADVERTISED_FIBRE)
780 desc->advertised |= OFPPF_FIBER;
781 if (ecmd.advertising & ADVERTISED_Autoneg)
782 desc->advertised |= OFPPF_AUTONEG;
783 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
784 if (ecmd.advertising & ADVERTISED_Pause)
785 desc->advertised |= OFPPF_PAUSE;
786 if (ecmd.advertising & ADVERTISED_Asym_Pause)
787 desc->advertised |= OFPPF_PAUSE_ASYM;
788 #endif /* kernel >= 2.6.14 */
790 /* Set the current features */
791 if (ecmd.speed == SPEED_10)
792 desc->curr = (ecmd.duplex) ? OFPPF_10MB_FD : OFPPF_10MB_HD;
793 else if (ecmd.speed == SPEED_100)
794 desc->curr = (ecmd.duplex) ? OFPPF_100MB_FD : OFPPF_100MB_HD;
795 else if (ecmd.speed == SPEED_1000)
796 desc->curr = (ecmd.duplex) ? OFPPF_1GB_FD : OFPPF_1GB_HD;
797 else if (ecmd.speed == SPEED_10000)
798 desc->curr = OFPPF_10GB_FD;
800 if (ecmd.port == PORT_TP)
801 desc->curr |= OFPPF_COPPER;
802 else if (ecmd.port == PORT_FIBRE)
803 desc->curr |= OFPPF_FIBER;
806 desc->curr |= OFPPF_AUTONEG;
810 desc->curr = htonl(desc->curr);
811 desc->supported = htonl(desc->supported);
812 desc->advertised = htonl(desc->advertised);
813 desc->peer = htonl(desc->peer);
817 fill_features_reply(struct datapath *dp, struct ofp_switch_features *ofr)
819 struct net_bridge_port *p;
822 ofr->datapath_id = cpu_to_be64(dp->id);
824 ofr->n_buffers = htonl(N_PKT_BUFFERS);
825 ofr->n_tables = dp->chain->n_tables;
826 ofr->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
827 ofr->actions = htonl(OFP_SUPPORTED_ACTIONS);
828 memset(ofr->pad, 0, sizeof ofr->pad);
830 list_for_each_entry_rcu (p, &dp->port_list, node) {
831 fill_port_desc(p, &ofr->ports[port_count]);
839 dp_send_features_reply(struct datapath *dp, const struct sender *sender)
842 struct ofp_switch_features *ofr;
843 size_t ofr_len, port_max_len;
847 port_max_len = sizeof(struct ofp_phy_port) * DP_MAX_PORTS;
848 ofr = alloc_openflow_skb(dp, sizeof(*ofr) + port_max_len,
849 OFPT_FEATURES_REPLY, sender, &skb);
854 port_count = fill_features_reply(dp, ofr);
857 ofr_len = sizeof(*ofr) + (sizeof(struct ofp_phy_port) * port_count);
858 resize_openflow_skb(skb, &ofr->header, ofr_len);
859 return send_openflow_skb(skb, sender);
863 dp_send_config_reply(struct datapath *dp, const struct sender *sender)
866 struct ofp_switch_config *osc;
868 osc = alloc_openflow_skb(dp, sizeof *osc, OFPT_GET_CONFIG_REPLY, sender,
873 osc->flags = htons(dp->flags);
874 osc->miss_send_len = htons(dp->miss_send_len);
876 return send_openflow_skb(skb, sender);
880 dp_send_hello(struct datapath *dp, const struct sender *sender,
881 const struct ofp_header *request)
883 if (request->version < OFP_VERSION) {
885 sprintf(err, "Only version 0x%02x supported", OFP_VERSION);
886 dp_send_error_msg(dp, sender, OFPET_HELLO_FAILED,
887 OFPHFC_INCOMPATIBLE, err, strlen(err));
891 struct ofp_header *reply;
893 reply = alloc_openflow_skb(dp, sizeof *reply,
894 OFPT_HELLO, sender, &skb);
898 return send_openflow_skb(skb, sender);
902 /* Callback function for a workqueue to disable an interface */
904 down_port_cb(struct work_struct *work)
906 struct net_bridge_port *p = container_of(work, struct net_bridge_port,
910 if (dev_change_flags(p->dev, p->dev->flags & ~IFF_UP) < 0)
912 printk("problem bringing up port %s\n", p->dev->name);
914 p->config |= OFPPC_PORT_DOWN;
917 /* Callback function for a workqueue to enable an interface */
919 up_port_cb(struct work_struct *work)
921 struct net_bridge_port *p = container_of(work, struct net_bridge_port,
925 if (dev_change_flags(p->dev, p->dev->flags | IFF_UP) < 0)
927 printk("problem bringing down port %s\n", p->dev->name);
929 p->config &= ~OFPPC_PORT_DOWN;
933 dp_update_port_flags(struct datapath *dp, const struct ofp_port_mod *opm)
935 unsigned long int flags;
936 int port_no = ntohs(opm->port_no);
937 struct net_bridge_port *p;
938 p = (port_no < DP_MAX_PORTS ? dp->ports[port_no]
939 : port_no == OFPP_LOCAL ? dp->local_port
942 /* Make sure the port id hasn't changed since this was sent */
943 if (!p || memcmp(opm->hw_addr, p->dev->dev_addr, ETH_ALEN))
946 spin_lock_irqsave(&p->lock, flags);
948 uint32_t config_mask = ntohl(opm->mask);
949 p->config &= ~config_mask;
950 p->config |= ntohl(opm->config) & config_mask;
953 /* Modifying the status of an interface requires taking a lock
954 * that cannot be done from here. For this reason, we use a shared
955 * workqueue, which will cause it to be executed from a safer
957 if (opm->mask & htonl(OFPPC_PORT_DOWN)) {
958 if ((opm->config & htonl(OFPPC_PORT_DOWN))
959 && (p->config & OFPPC_PORT_DOWN) == 0) {
960 PREPARE_WORK(&p->port_task, down_port_cb);
961 schedule_work(&p->port_task);
962 } else if ((opm->config & htonl(OFPPC_PORT_DOWN)) == 0
963 && (p->config & OFPPC_PORT_DOWN)) {
964 PREPARE_WORK(&p->port_task, up_port_cb);
965 schedule_work(&p->port_task);
968 spin_unlock_irqrestore(&p->lock, flags);
973 /* Initialize the port status field of the bridge port. */
975 init_port_status(struct net_bridge_port *p)
977 unsigned long int flags;
979 spin_lock_irqsave(&p->lock, flags);
981 if (p->dev->flags & IFF_UP)
982 p->config &= ~OFPPC_PORT_DOWN;
984 p->config |= OFPPC_PORT_DOWN;
986 if (netif_carrier_ok(p->dev))
987 p->state &= ~OFPPS_LINK_DOWN;
989 p->state |= OFPPS_LINK_DOWN;
991 spin_unlock_irqrestore(&p->lock, flags);
995 dp_send_port_status(struct net_bridge_port *p, uint8_t status)
998 struct ofp_port_status *ops;
1000 ops = alloc_openflow_skb(p->dp, sizeof *ops, OFPT_PORT_STATUS, NULL,
1004 ops->reason = status;
1005 memset(ops->pad, 0, sizeof ops->pad);
1006 fill_port_desc(p, &ops->desc);
1008 return send_openflow_skb(skb, NULL);
1012 dp_send_flow_expired(struct datapath *dp, struct sw_flow *flow,
1013 enum ofp_flow_expired_reason reason)
1015 struct sk_buff *skb;
1016 struct ofp_flow_expired *ofe;
1018 if (!(dp->flags & OFPC_SEND_FLOW_EXP))
1021 ofe = alloc_openflow_skb(dp, sizeof *ofe, OFPT_FLOW_EXPIRED, 0, &skb);
1025 flow_fill_match(&ofe->match, &flow->key);
1027 ofe->priority = htons(flow->priority);
1028 ofe->reason = reason;
1029 memset(ofe->pad, 0, sizeof ofe->pad);
1031 ofe->duration = htonl((jiffies - flow->init_time) / HZ);
1032 memset(ofe->pad2, 0, sizeof ofe->pad2);
1033 ofe->packet_count = cpu_to_be64(flow->packet_count);
1034 ofe->byte_count = cpu_to_be64(flow->byte_count);
1036 return send_openflow_skb(skb, NULL);
1038 EXPORT_SYMBOL(dp_send_flow_expired);
1041 dp_send_error_msg(struct datapath *dp, const struct sender *sender,
1042 uint16_t type, uint16_t code, const void *data, size_t len)
1044 struct sk_buff *skb;
1045 struct ofp_error_msg *oem;
1048 oem = alloc_openflow_skb(dp, sizeof(*oem)+len, OFPT_ERROR,
1053 oem->type = htons(type);
1054 oem->code = htons(code);
1055 memcpy(oem->data, data, len);
1057 return send_openflow_skb(skb, sender);
1061 dp_send_echo_reply(struct datapath *dp, const struct sender *sender,
1062 const struct ofp_header *rq)
1064 struct sk_buff *skb;
1065 struct ofp_header *reply;
1067 reply = alloc_openflow_skb(dp, ntohs(rq->length), OFPT_ECHO_REPLY,
1072 memcpy(reply + 1, rq + 1, ntohs(rq->length) - sizeof *rq);
1073 return send_openflow_skb(skb, sender);
1076 /* Generic Netlink interface.
1078 * See netlink(7) for an introduction to netlink. See
1079 * http://linux-net.osdl.org/index.php/Netlink for more information and
1080 * pointers on how to work with netlink and Generic Netlink in the kernel and
1083 static struct genl_family dp_genl_family = {
1084 .id = GENL_ID_GENERATE,
1086 .name = DP_GENL_FAMILY_NAME,
1088 .maxattr = DP_GENL_A_MAX,
1091 /* Attribute policy: what each attribute may contain. */
1092 static struct nla_policy dp_genl_policy[DP_GENL_A_MAX + 1] = {
1093 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1094 [DP_GENL_A_MC_GROUP] = { .type = NLA_U32 },
1095 [DP_GENL_A_PORTNAME] = { .type = NLA_STRING }
1098 static int dp_genl_add(struct sk_buff *skb, struct genl_info *info)
1100 if (!info->attrs[DP_GENL_A_DP_IDX])
1103 return new_dp(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1106 static struct genl_ops dp_genl_ops_add_dp = {
1107 .cmd = DP_GENL_C_ADD_DP,
1108 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1109 .policy = dp_genl_policy,
1110 .doit = dp_genl_add,
1114 struct datapath *dp_get(int dp_idx)
1116 if (dp_idx < 0 || dp_idx > DP_MAX)
1118 return rcu_dereference(dps[dp_idx]);
1121 static int dp_genl_del(struct sk_buff *skb, struct genl_info *info)
1123 struct datapath *dp;
1126 if (!info->attrs[DP_GENL_A_DP_IDX])
1129 dp = dp_get(nla_get_u32((info->attrs[DP_GENL_A_DP_IDX])));
1139 static struct genl_ops dp_genl_ops_del_dp = {
1140 .cmd = DP_GENL_C_DEL_DP,
1141 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1142 .policy = dp_genl_policy,
1143 .doit = dp_genl_del,
1147 /* Queries a datapath for related information. Currently the only relevant
1148 * information is the datapath's multicast group ID. Really we want one
1149 * multicast group per datapath, but because of locking issues[*] we can't
1150 * easily get one. Thus, every datapath will currently return the same
1151 * global multicast group ID, but in the future it would be nice to fix that.
1153 * [*] dp_genl_add, to add a new datapath, is called under the genl_lock
1154 * mutex, and genl_register_mc_group, called to acquire a new multicast
1155 * group ID, also acquires genl_lock, thus deadlock.
1157 static int dp_genl_query(struct sk_buff *skb, struct genl_info *info)
1159 struct datapath *dp;
1160 struct sk_buff *ans_skb = NULL;
1164 if (!info->attrs[DP_GENL_A_DP_IDX])
1168 dp_idx = nla_get_u32((info->attrs[DP_GENL_A_DP_IDX]));
1169 dp = dp_get(dp_idx);
1174 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1179 data = genlmsg_put_reply(ans_skb, info, &dp_genl_family,
1180 0, DP_GENL_C_QUERY_DP);
1185 NLA_PUT_U32(ans_skb, DP_GENL_A_DP_IDX, dp_idx);
1186 NLA_PUT_U32(ans_skb, DP_GENL_A_MC_GROUP, mc_group.id);
1188 genlmsg_end(ans_skb, data);
1189 err = genlmsg_reply(ans_skb, info);
1201 static struct genl_ops dp_genl_ops_query_dp = {
1202 .cmd = DP_GENL_C_QUERY_DP,
1203 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1204 .policy = dp_genl_policy,
1205 .doit = dp_genl_query,
1209 static int dp_genl_add_del_port(struct sk_buff *skb, struct genl_info *info)
1211 struct datapath *dp;
1212 struct net_device *port;
1215 if (!info->attrs[DP_GENL_A_DP_IDX] || !info->attrs[DP_GENL_A_PORTNAME])
1219 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1225 /* Get interface to add/remove. */
1226 port = dev_get_by_name(&init_net,
1227 nla_data(info->attrs[DP_GENL_A_PORTNAME]));
1233 /* Execute operation. */
1234 if (info->genlhdr->cmd == DP_GENL_C_ADD_PORT)
1235 err = add_switch_port(dp, port);
1237 if (port->br_port == NULL || port->br_port->dp != dp) {
1241 err = dp_del_switch_port(port->br_port);
1250 static struct genl_ops dp_genl_ops_add_port = {
1251 .cmd = DP_GENL_C_ADD_PORT,
1252 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1253 .policy = dp_genl_policy,
1254 .doit = dp_genl_add_del_port,
1258 static struct genl_ops dp_genl_ops_del_port = {
1259 .cmd = DP_GENL_C_DEL_PORT,
1260 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1261 .policy = dp_genl_policy,
1262 .doit = dp_genl_add_del_port,
1266 static int dp_genl_openflow(struct sk_buff *skb, struct genl_info *info)
1268 struct nlattr *va = info->attrs[DP_GENL_A_OPENFLOW];
1269 struct datapath *dp;
1270 struct ofp_header *oh;
1271 struct sender sender;
1274 if (!info->attrs[DP_GENL_A_DP_IDX] || !va)
1277 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1281 if (nla_len(va) < sizeof(struct ofp_header))
1285 sender.xid = oh->xid;
1286 sender.pid = info->snd_pid;
1287 sender.seq = info->snd_seq;
1289 mutex_lock(&dp_mutex);
1290 err = fwd_control_input(dp->chain, &sender,
1291 nla_data(va), nla_len(va));
1292 mutex_unlock(&dp_mutex);
1296 static struct nla_policy dp_genl_openflow_policy[DP_GENL_A_MAX + 1] = {
1297 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1300 static int desc_stats_dump(struct datapath *dp, void *state,
1301 void *body, int *body_len)
1303 struct ofp_desc_stats *ods = body;
1304 int n_bytes = sizeof *ods;
1306 if (n_bytes > *body_len) {
1309 *body_len = n_bytes;
1311 strncpy(ods->mfr_desc, mfr_desc, sizeof ods->mfr_desc);
1312 strncpy(ods->hw_desc, hw_desc, sizeof ods->hw_desc);
1313 strncpy(ods->sw_desc, sw_desc, sizeof ods->sw_desc);
1314 strncpy(ods->serial_num, serial_num, sizeof ods->serial_num);
1319 struct flow_stats_state {
1321 struct sw_table_position position;
1322 const struct ofp_flow_stats_request *rq;
1325 int bytes_used, bytes_allocated;
1328 static int flow_stats_init(struct datapath *dp, const void *body, int body_len,
1331 const struct ofp_flow_stats_request *fsr = body;
1332 struct flow_stats_state *s = kmalloc(sizeof *s, GFP_ATOMIC);
1335 s->table_idx = fsr->table_id == 0xff ? 0 : fsr->table_id;
1336 memset(&s->position, 0, sizeof s->position);
1342 static int flow_stats_dump_callback(struct sw_flow *flow, void *private)
1344 struct sw_flow_actions *sf_acts = rcu_dereference(flow->sf_acts);
1345 struct flow_stats_state *s = private;
1346 struct ofp_flow_stats *ofs;
1349 length = sizeof *ofs + sf_acts->actions_len;
1350 if (length + s->bytes_used > s->bytes_allocated)
1353 ofs = s->body + s->bytes_used;
1354 ofs->length = htons(length);
1355 ofs->table_id = s->table_idx;
1357 ofs->match.wildcards = htonl(flow->key.wildcards);
1358 ofs->match.in_port = flow->key.in_port;
1359 memcpy(ofs->match.dl_src, flow->key.dl_src, ETH_ALEN);
1360 memcpy(ofs->match.dl_dst, flow->key.dl_dst, ETH_ALEN);
1361 ofs->match.dl_vlan = flow->key.dl_vlan;
1362 ofs->match.dl_type = flow->key.dl_type;
1363 ofs->match.nw_src = flow->key.nw_src;
1364 ofs->match.nw_dst = flow->key.nw_dst;
1365 ofs->match.nw_proto = flow->key.nw_proto;
1367 ofs->match.tp_src = flow->key.tp_src;
1368 ofs->match.tp_dst = flow->key.tp_dst;
1369 ofs->duration = htonl((jiffies - flow->init_time) / HZ);
1370 ofs->priority = htons(flow->priority);
1371 ofs->idle_timeout = htons(flow->idle_timeout);
1372 ofs->hard_timeout = htons(flow->hard_timeout);
1373 memset(ofs->pad2, 0, sizeof ofs->pad2);
1374 ofs->packet_count = cpu_to_be64(flow->packet_count);
1375 ofs->byte_count = cpu_to_be64(flow->byte_count);
1376 memcpy(ofs->actions, sf_acts->actions, sf_acts->actions_len);
1378 s->bytes_used += length;
1382 static int flow_stats_dump(struct datapath *dp, void *state,
1383 void *body, int *body_len)
1385 struct flow_stats_state *s = state;
1386 struct sw_flow_key match_key;
1390 s->bytes_allocated = *body_len;
1393 flow_extract_match(&match_key, &s->rq->match);
1394 while (s->table_idx < dp->chain->n_tables
1395 && (s->rq->table_id == 0xff || s->rq->table_id == s->table_idx))
1397 struct sw_table *table = dp->chain->tables[s->table_idx];
1399 error = table->iterate(table, &match_key, &s->position,
1400 flow_stats_dump_callback, s);
1405 memset(&s->position, 0, sizeof s->position);
1407 *body_len = s->bytes_used;
1409 /* If error is 0, we're done.
1410 * Otherwise, if some bytes were used, there are more flows to come.
1411 * Otherwise, we were not able to fit even a single flow in the body,
1412 * which indicates that we have a single flow with too many actions to
1413 * fit. We won't ever make any progress at that rate, so give up. */
1414 return !error ? 0 : s->bytes_used ? 1 : -ENOMEM;
1417 static void flow_stats_done(void *state)
1422 static int aggregate_stats_init(struct datapath *dp,
1423 const void *body, int body_len,
1426 *state = (void *)body;
1430 static int aggregate_stats_dump_callback(struct sw_flow *flow, void *private)
1432 struct ofp_aggregate_stats_reply *rpy = private;
1433 rpy->packet_count += flow->packet_count;
1434 rpy->byte_count += flow->byte_count;
1439 static int aggregate_stats_dump(struct datapath *dp, void *state,
1440 void *body, int *body_len)
1442 struct ofp_aggregate_stats_request *rq = state;
1443 struct ofp_aggregate_stats_reply *rpy;
1444 struct sw_table_position position;
1445 struct sw_flow_key match_key;
1448 if (*body_len < sizeof *rpy)
1451 *body_len = sizeof *rpy;
1453 memset(rpy, 0, sizeof *rpy);
1455 flow_extract_match(&match_key, &rq->match);
1456 table_idx = rq->table_id == 0xff ? 0 : rq->table_id;
1457 memset(&position, 0, sizeof position);
1458 while (table_idx < dp->chain->n_tables
1459 && (rq->table_id == 0xff || rq->table_id == table_idx))
1461 struct sw_table *table = dp->chain->tables[table_idx];
1464 error = table->iterate(table, &match_key, &position,
1465 aggregate_stats_dump_callback, rpy);
1470 memset(&position, 0, sizeof position);
1473 rpy->packet_count = cpu_to_be64(rpy->packet_count);
1474 rpy->byte_count = cpu_to_be64(rpy->byte_count);
1475 rpy->flow_count = htonl(rpy->flow_count);
1479 static int table_stats_dump(struct datapath *dp, void *state,
1480 void *body, int *body_len)
1482 struct ofp_table_stats *ots;
1483 int n_bytes = dp->chain->n_tables * sizeof *ots;
1485 if (n_bytes > *body_len)
1487 *body_len = n_bytes;
1488 for (i = 0, ots = body; i < dp->chain->n_tables; i++, ots++) {
1489 struct sw_table_stats stats;
1490 dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
1491 strncpy(ots->name, stats.name, sizeof ots->name);
1493 ots->wildcards = htonl(stats.wildcards);
1494 memset(ots->pad, 0, sizeof ots->pad);
1495 ots->max_entries = htonl(stats.max_flows);
1496 ots->active_count = htonl(stats.n_flows);
1497 ots->lookup_count = cpu_to_be64(stats.n_lookup);
1498 ots->matched_count = cpu_to_be64(stats.n_matched);
1503 struct port_stats_state {
1507 static int port_stats_init(struct datapath *dp, const void *body, int body_len,
1510 struct port_stats_state *s = kmalloc(sizeof *s, GFP_ATOMIC);
1518 static int port_stats_dump(struct datapath *dp, void *state,
1519 void *body, int *body_len)
1521 struct port_stats_state *s = state;
1522 struct ofp_port_stats *ops;
1523 int n_ports, max_ports;
1526 max_ports = *body_len / sizeof *ops;
1532 for (i = s->port; i < DP_MAX_PORTS && n_ports < max_ports; i++) {
1533 struct net_bridge_port *p = dp->ports[i];
1534 struct net_device_stats *stats;
1537 stats = p->dev->get_stats(p->dev);
1538 ops->port_no = htons(p->port_no);
1539 memset(ops->pad, 0, sizeof ops->pad);
1540 ops->rx_packets = cpu_to_be64(stats->rx_packets);
1541 ops->tx_packets = cpu_to_be64(stats->tx_packets);
1542 ops->rx_bytes = cpu_to_be64(stats->rx_bytes);
1543 ops->tx_bytes = cpu_to_be64(stats->tx_bytes);
1544 ops->rx_dropped = cpu_to_be64(stats->rx_dropped);
1545 ops->tx_dropped = cpu_to_be64(stats->tx_dropped);
1546 ops->rx_errors = cpu_to_be64(stats->rx_errors);
1547 ops->tx_errors = cpu_to_be64(stats->tx_errors);
1548 ops->rx_frame_err = cpu_to_be64(stats->rx_frame_errors);
1549 ops->rx_over_err = cpu_to_be64(stats->rx_over_errors);
1550 ops->rx_crc_err = cpu_to_be64(stats->rx_crc_errors);
1551 ops->collisions = cpu_to_be64(stats->collisions);
1556 *body_len = n_ports * sizeof *ops;
1557 return n_ports >= max_ports;
1560 static void port_stats_done(void *state)
1566 /* Minimum and maximum acceptable number of bytes in body member of
1567 * struct ofp_stats_request. */
1568 size_t min_body, max_body;
1570 /* Prepares to dump some kind of statistics on 'dp'. 'body' and
1571 * 'body_len' are the 'body' member of the struct ofp_stats_request.
1572 * Returns zero if successful, otherwise a negative error code.
1573 * May initialize '*state' to state information. May be null if no
1574 * initialization is required.*/
1575 int (*init)(struct datapath *dp, const void *body, int body_len,
1578 /* Dumps statistics for 'dp' into the '*body_len' bytes at 'body', and
1579 * modifies '*body_len' to reflect the number of bytes actually used.
1580 * ('body' will be transmitted as the 'body' member of struct
1581 * ofp_stats_reply.) */
1582 int (*dump)(struct datapath *dp, void *state,
1583 void *body, int *body_len);
1585 /* Cleans any state created by the init or dump functions. May be null
1586 * if no cleanup is required. */
1587 void (*done)(void *state);
1590 static const struct stats_type stats[] = {
1599 sizeof(struct ofp_flow_stats_request),
1600 sizeof(struct ofp_flow_stats_request),
1605 [OFPST_AGGREGATE] = {
1606 sizeof(struct ofp_aggregate_stats_request),
1607 sizeof(struct ofp_aggregate_stats_request),
1608 aggregate_stats_init,
1609 aggregate_stats_dump,
1629 dp_genl_openflow_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
1631 struct datapath *dp;
1632 struct sender sender;
1633 const struct stats_type *s;
1634 struct ofp_stats_reply *osr;
1636 int max_openflow_len, body_len;
1640 /* Set up the cleanup function for this dump. Linux 2.6.20 and later
1641 * support setting up cleanup functions via the .doneit member of
1642 * struct genl_ops. This kluge supports earlier versions also. */
1643 cb->done = dp_genl_openflow_done;
1645 sender.pid = NETLINK_CB(cb->skb).pid;
1646 sender.seq = cb->nlh->nlmsg_seq;
1648 struct nlattr *attrs[DP_GENL_A_MAX + 1];
1649 struct ofp_stats_request *rq;
1651 size_t len, body_len;
1654 err = nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs, DP_GENL_A_MAX,
1655 dp_genl_openflow_policy);
1659 if (!attrs[DP_GENL_A_DP_IDX])
1661 dp_idx = nla_get_u16(attrs[DP_GENL_A_DP_IDX]);
1662 dp = dp_get(dp_idx);
1666 va = attrs[DP_GENL_A_OPENFLOW];
1668 if (!va || len < sizeof *rq)
1672 sender.xid = rq->header.xid;
1673 type = ntohs(rq->type);
1674 if (rq->header.version != OFP_VERSION) {
1675 dp_send_error_msg(dp, &sender, OFPET_BAD_REQUEST,
1676 OFPBRC_BAD_VERSION, rq, len);
1679 if (rq->header.type != OFPT_STATS_REQUEST
1680 || ntohs(rq->header.length) != len)
1683 if (type >= ARRAY_SIZE(stats) || !stats[type].dump) {
1684 dp_send_error_msg(dp, &sender, OFPET_BAD_REQUEST,
1685 OFPBRC_BAD_STAT, rq, len);
1690 body_len = len - offsetof(struct ofp_stats_request, body);
1691 if (body_len < s->min_body || body_len > s->max_body)
1695 cb->args[1] = dp_idx;
1697 cb->args[3] = rq->header.xid;
1700 err = s->init(dp, rq->body, body_len, &state);
1703 cb->args[4] = (long) state;
1705 } else if (cb->args[0] == 1) {
1706 sender.xid = cb->args[3];
1707 dp_idx = cb->args[1];
1708 s = &stats[cb->args[2]];
1710 dp = dp_get(dp_idx);
1717 osr = put_openflow_headers(dp, skb, OFPT_STATS_REPLY, &sender,
1720 return PTR_ERR(osr);
1721 osr->type = htons(s - stats);
1723 resize_openflow_skb(skb, &osr->header, max_openflow_len);
1725 body_len = max_openflow_len - offsetof(struct ofp_stats_reply, body);
1727 err = s->dump(dp, (void *) cb->args[4], body, &body_len);
1732 osr->flags = ntohs(OFPSF_REPLY_MORE);
1733 resize_openflow_skb(skb, &osr->header,
1734 (offsetof(struct ofp_stats_reply, body)
1743 dp_genl_openflow_done(struct netlink_callback *cb)
1746 const struct stats_type *s = &stats[cb->args[2]];
1748 s->done((void *) cb->args[4]);
1753 static struct genl_ops dp_genl_ops_openflow = {
1754 .cmd = DP_GENL_C_OPENFLOW,
1755 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1756 .policy = dp_genl_openflow_policy,
1757 .doit = dp_genl_openflow,
1758 .dumpit = dp_genl_openflow_dumpit,
1761 static struct genl_ops *dp_genl_all_ops[] = {
1762 /* Keep this operation first. Generic Netlink dispatching
1763 * looks up operations with linear search, so we want it at the
1765 &dp_genl_ops_openflow,
1767 &dp_genl_ops_add_dp,
1768 &dp_genl_ops_del_dp,
1769 &dp_genl_ops_query_dp,
1770 &dp_genl_ops_add_port,
1771 &dp_genl_ops_del_port,
1774 static int dp_init_netlink(void)
1779 err = genl_register_family(&dp_genl_family);
1783 for (i = 0; i < ARRAY_SIZE(dp_genl_all_ops); i++) {
1784 err = genl_register_ops(&dp_genl_family, dp_genl_all_ops[i]);
1786 goto err_unregister;
1789 strcpy(mc_group.name, "openflow");
1790 err = genl_register_mc_group(&dp_genl_family, &mc_group);
1792 goto err_unregister;
1797 genl_unregister_family(&dp_genl_family);
1801 static void dp_uninit_netlink(void)
1803 genl_unregister_family(&dp_genl_family);
1806 static int __init dp_init(void)
1810 printk("OpenFlow "VERSION", built "__DATE__" "__TIME__", "
1811 "protocol 0x%02x\n", OFP_VERSION);
1817 err = register_netdevice_notifier(&dp_device_notifier);
1819 goto error_flow_exit;
1821 err = dp_init_netlink();
1823 goto error_unreg_notifier;
1825 /* Hook into callback used by the bridge to intercept packets.
1826 * Parasites we are. */
1827 if (br_handle_frame_hook)
1828 printk("openflow: hijacking bridge hook\n");
1829 br_handle_frame_hook = dp_frame_hook;
1833 error_unreg_notifier:
1834 unregister_netdevice_notifier(&dp_device_notifier);
1838 printk(KERN_EMERG "openflow: failed to install!");
1842 static void dp_cleanup(void)
1845 dp_uninit_netlink();
1846 unregister_netdevice_notifier(&dp_device_notifier);
1848 br_handle_frame_hook = NULL;
1851 module_init(dp_init);
1852 module_exit(dp_cleanup);
1854 MODULE_DESCRIPTION("OpenFlow switching datapath");
1855 MODULE_AUTHOR("Copyright (c) 2007, 2008 The Board of Trustees of The Leland Stanford Junior University");
1856 MODULE_LICENSE("GPL");