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/time.h>
19 #include <linux/etherdevice.h>
20 #include <linux/kernel.h>
21 #include <linux/kthread.h>
22 #include <linux/mutex.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/rcupdate.h>
25 #include <linux/version.h>
26 #include <linux/ethtool.h>
27 #include <linux/random.h>
28 #include <asm/system.h>
29 #include <asm/div64.h>
30 #include <linux/netfilter_bridge.h>
31 #include <linux/netfilter_ipv4.h>
32 #include <linux/inetdevice.h>
33 #include <linux/list.h>
34 #include <linux/rculist.h>
35 #include <linux/workqueue.h>
36 #include <linux/dmi.h>
38 #include "openflow/nicira-ext.h"
39 #include "openflow/openflow-netlink.h"
41 #include "nx_act_snat.h"
51 /* Strings to describe the manufacturer, hardware, and software. This data
52 * is queriable through the switch description stats message. */
53 static char mfr_desc[DESC_STR_LEN] = "Nicira Networks, Inc.";
54 static char hw_desc[DESC_STR_LEN] = "Reference Linux Kernel Module";
55 static char sw_desc[DESC_STR_LEN] = VERSION BUILDNR;
56 static char serial_num[SERIAL_NUM_LEN] = "None";
58 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
59 module_param_string(mfr_desc, mfr_desc, sizeof mfr_desc, 0444);
60 module_param_string(hw_desc, hw_desc, sizeof hw_desc, 0444);
61 module_param_string(sw_desc, sw_desc, sizeof sw_desc, 0444);
62 module_param_string(serial_num, serial_num, sizeof serial_num, 0444);
64 MODULE_PARM(mfr_desc, "s");
65 MODULE_PARM(hw_desc, "s");
66 MODULE_PARM(sw_desc, "s");
67 MODULE_PARM(serial_num, "s");
71 /* Number of milliseconds between runs of the maintenance thread. */
72 #define MAINT_SLEEP_MSECS 1000
74 #define UINT32_MAX 4294967295U
75 #define UINT16_MAX 65535
76 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
78 static struct genl_family dp_genl_family;
79 static struct genl_multicast_group mc_group;
81 /* It's hard to imagine wanting more than one datapath, but... */
84 /* Datapaths. Protected on the read side by rcu_read_lock, on the write side
85 * by dp_mutex. dp_mutex is almost completely redundant with genl_mutex
86 * maintained by the Generic Netlink code, but the timeout path needs mutual
89 * It is safe to access the datapath and net_bridge_port structures with just
92 static struct datapath *dps[DP_MAX];
93 DEFINE_MUTEX(dp_mutex);
94 EXPORT_SYMBOL(dp_mutex);
96 static int dp_maint_func(void *data);
97 static void init_port_status(struct net_bridge_port *p);
98 static int dp_genl_openflow_done(struct netlink_callback *);
99 static struct net_bridge_port *new_nbp(struct datapath *,
100 struct net_device *, int port_no);
102 /* nla_shrink - reduce amount of space reserved by nla_reserve
103 * @skb: socket buffer from which to recover room
104 * @nla: netlink attribute to adjust
105 * @len: new length of attribute payload
107 * Reduces amount of space reserved by a call to nla_reserve.
109 * No other attributes may be added between calling nla_reserve and this
110 * function, since it will create a hole in the message.
112 void nla_shrink(struct sk_buff *skb, struct nlattr *nla, int len)
114 int delta = nla_total_size(len) - nla_total_size(nla_len(nla));
118 nla->nla_len = nla_attr_size(len);
121 /* Puts a set of openflow headers for a message of the given 'type' into 'skb'.
122 * If 'sender' is nonnull, then it is used as the message's destination. 'dp'
123 * must specify the datapath to use.
125 * '*max_openflow_len' receives the maximum number of bytes that are available
126 * for the embedded OpenFlow message. The caller must call
127 * resize_openflow_skb() to set the actual size of the message to this number
130 * Returns the openflow header if successful, otherwise (if 'skb' is too small)
133 put_openflow_headers(struct datapath *dp, struct sk_buff *skb, uint8_t type,
134 const struct sender *sender, int *max_openflow_len)
136 struct ofp_header *oh;
140 /* Assemble the Generic Netlink wrapper. */
141 if (!genlmsg_put(skb,
142 sender ? sender->pid : 0,
143 sender ? sender->seq : 0,
144 &dp_genl_family, 0, DP_GENL_C_OPENFLOW))
145 return ERR_PTR(-ENOBUFS);
146 if (nla_put_u32(skb, DP_GENL_A_DP_IDX, dp->dp_idx) < 0)
147 return ERR_PTR(-ENOBUFS);
148 openflow_len = (skb_tailroom(skb) - NLA_HDRLEN) & ~(NLA_ALIGNTO - 1);
149 if (openflow_len < sizeof *oh)
150 return ERR_PTR(-ENOBUFS);
151 *max_openflow_len = openflow_len;
152 attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, openflow_len);
155 /* Fill in the header. The caller is responsible for the length. */
157 oh->version = OFP_VERSION;
159 oh->xid = sender ? sender->xid : 0;
164 /* Resizes OpenFlow header 'oh', which must be at the tail end of 'skb', to new
165 * length 'new_length' (in bytes), adjusting pointers and size values as
168 resize_openflow_skb(struct sk_buff *skb,
169 struct ofp_header *oh, size_t new_length)
171 struct nlattr *attr = ((void *) oh) - NLA_HDRLEN;
172 nla_shrink(skb, attr, new_length);
173 oh->length = htons(new_length);
174 nlmsg_end(skb, (struct nlmsghdr *) skb->data);
177 /* Allocates a new skb to contain an OpenFlow message 'openflow_len' bytes in
178 * length. Returns a null pointer if memory is unavailable, otherwise returns
179 * the OpenFlow header and stores a pointer to the skb in '*pskb'.
181 * 'type' is the OpenFlow message type. If 'sender' is nonnull, then it is
182 * used as the message's destination. 'dp' must specify the datapath to
185 alloc_openflow_skb(struct datapath *dp, size_t openflow_len, uint8_t type,
186 const struct sender *sender, struct sk_buff **pskb)
188 struct ofp_header *oh;
191 int max_openflow_len;
193 if ((openflow_len + sizeof(struct ofp_header)) > UINT16_MAX) {
195 printk("alloc_openflow_skb: openflow message too large: %zu\n",
200 genl_len = nlmsg_total_size(GENL_HDRLEN + dp_genl_family.hdrsize);
201 genl_len += nla_total_size(sizeof(uint32_t)); /* DP_GENL_A_DP_IDX */
202 genl_len += nla_total_size(openflow_len); /* DP_GENL_A_OPENFLOW */
203 skb = *pskb = genlmsg_new(genl_len, GFP_ATOMIC);
206 printk("alloc_openflow_skb: genlmsg_new failed\n");
210 oh = put_openflow_headers(dp, skb, type, sender, &max_openflow_len);
211 BUG_ON(!oh || IS_ERR(oh));
212 resize_openflow_skb(skb, oh, openflow_len);
217 /* Sends 'skb' to 'sender' if it is nonnull, otherwise multicasts 'skb' to all
220 send_openflow_skb(struct sk_buff *skb, const struct sender *sender)
223 ? genlmsg_unicast(skb, sender->pid)
224 : genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC));
227 /* Retrieves the datapath id, which is the MAC address of the "of" device. */
229 uint64_t get_datapath_id(struct net_device *dev)
234 for (i=0; i<ETH_ALEN; i++)
235 id |= (uint64_t)dev->dev_addr[i] << (8*(ETH_ALEN-1 - i));
240 /* Creates a new datapath numbered 'dp_idx'. Returns 0 for success or a
241 * negative error code. */
242 static int new_dp(int dp_idx)
247 if (dp_idx < 0 || dp_idx >= DP_MAX)
250 if (!try_module_get(THIS_MODULE))
253 /* Exit early if a datapath with that number already exists. */
260 dp = kzalloc(sizeof *dp, GFP_KERNEL);
266 /* Setup our "of" device */
267 err = dp_dev_setup(dp);
271 dp->chain = chain_create(dp);
272 if (dp->chain == NULL)
273 goto err_destroy_dp_dev;
274 INIT_LIST_HEAD(&dp->port_list);
276 dp->local_port = new_nbp(dp, dp->netdev, OFPP_LOCAL);
277 if (IS_ERR(dp->local_port)) {
278 err = PTR_ERR(dp->local_port);
279 goto err_destroy_local_port;
283 dp->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
285 dp->dp_task = kthread_run(dp_maint_func, dp, "dp%d", dp_idx);
286 if (IS_ERR(dp->dp_task))
287 goto err_destroy_chain;
293 err_destroy_local_port:
294 dp_del_switch_port(dp->local_port);
296 chain_destroy(dp->chain);
302 module_put(THIS_MODULE);
306 /* Find and return a free port number under 'dp'. */
307 static int find_portno(struct datapath *dp)
310 for (i = 0; i < DP_MAX_PORTS; i++)
311 if (dp->ports[i] == NULL)
316 static struct net_bridge_port *new_nbp(struct datapath *dp,
317 struct net_device *dev, int port_no)
319 struct net_bridge_port *p;
321 if (dev->br_port != NULL)
322 return ERR_PTR(-EBUSY);
324 p = kzalloc(sizeof(*p), GFP_KERNEL);
326 return ERR_PTR(-ENOMEM);
329 dev_set_promiscuity(dev, 1);
334 p->port_no = port_no;
335 spin_lock_init(&p->lock);
336 INIT_WORK(&p->port_task, NULL);
337 if (port_no != OFPP_LOCAL)
338 rcu_assign_pointer(dev->br_port, p);
339 if (port_no < DP_MAX_PORTS)
340 rcu_assign_pointer(dp->ports[port_no], p);
341 list_add_rcu(&p->node, &dp->port_list);
346 int add_switch_port(struct datapath *dp, struct net_device *dev)
348 struct net_bridge_port *p;
351 if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER
355 port_no = find_portno(dp);
359 p = new_nbp(dp, dev, port_no);
365 /* Notify the ctlpath that this port has been added */
366 dp_send_port_status(p, OFPPR_ADD);
371 /* Delete 'p' from switch. */
372 int dp_del_switch_port(struct net_bridge_port *p)
378 /* First drop references to device. */
379 cancel_work_sync(&p->port_task);
381 dev_set_promiscuity(p->dev, -1);
383 list_del_rcu(&p->node);
384 if (p->port_no != OFPP_LOCAL)
385 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
386 rcu_assign_pointer(p->dev->br_port, NULL);
388 /* Then wait until no one is still using it, and destroy it. */
392 /* Free any SNAT configuration on the port. */
393 spin_lock_irqsave(&p->lock, flags);
395 spin_unlock_irqrestore(&p->lock, flags);
398 /* Notify the ctlpath that this port no longer exists */
399 dp_send_port_status(p, OFPPR_DELETE);
407 static void del_dp(struct datapath *dp)
409 struct net_bridge_port *p, *n;
411 kthread_stop(dp->dp_task);
413 /* Drop references to DP. */
414 list_for_each_entry_safe (p, n, &dp->port_list, node)
415 dp_del_switch_port(p);
416 rcu_assign_pointer(dps[dp->dp_idx], NULL);
418 /* Kill off local_port dev references from buffered packets that have
419 * associated dst entries. */
423 /* Destroy dp->netdev. (Must follow deleting switch ports since
424 * dp->local_port has a reference to it.) */
427 /* Wait until no longer in use, then destroy it. */
429 chain_destroy(dp->chain);
431 module_put(THIS_MODULE);
434 static int dp_maint_func(void *data)
436 struct datapath *dp = (struct datapath *) data;
438 while (!kthread_should_stop()) {
440 struct net_bridge_port *p;
442 /* Expire old SNAT entries */
444 list_for_each_entry_rcu (p, &dp->port_list, node)
449 /* Timeout old entries */
450 chain_timeout(dp->chain);
451 msleep_interruptible(MAINT_SLEEP_MSECS);
458 do_port_input(struct net_bridge_port *p, struct sk_buff *skb)
460 /* Make our own copy of the packet. Otherwise we will mangle the
461 * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
462 * (No one comes after us, since we tell handle_bridge() that we took
464 skb = skb_share_check(skb, GFP_ATOMIC);
469 /* Check if this packet needs early SNAT processing. */
470 if (snat_pre_route(skb)) {
475 /* Push the Ethernet header back on. */
476 skb_push(skb, ETH_HLEN);
477 skb_reset_mac_header(skb);
478 fwd_port_input(p->dp->chain, skb, p);
482 * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on
483 * different set of devices!)
485 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
486 /* Called with rcu_read_lock. */
487 static struct sk_buff *dp_frame_hook(struct net_bridge_port *p,
490 do_port_input(p, skb);
493 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
494 static int dp_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
496 do_port_input(p, *pskb);
500 /* NB: This has only been tested on 2.4.35 */
501 static void dp_frame_hook(struct sk_buff *skb)
503 struct net_bridge_port *p = skb->dev->br_port;
506 do_port_input(p, skb);
513 /* Forwarding output path.
514 * Based on net/bridge/br_forward.c. */
516 static inline unsigned packet_length(const struct sk_buff *skb)
518 int length = skb->len - ETH_HLEN;
519 if (skb->protocol == htons(ETH_P_8021Q))
524 /* Send packets out all the ports except the originating one. If the
525 * "flood" argument is set, only send along the minimum spanning tree.
528 output_all(struct datapath *dp, struct sk_buff *skb, int flood)
530 u32 disable = flood ? OFPPC_NO_FLOOD : 0;
531 struct net_bridge_port *p;
534 list_for_each_entry_rcu (p, &dp->port_list, node) {
535 if (skb->dev == p->dev || p->config & disable)
537 if (prev_port != -1) {
538 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
543 dp_output_port(dp, clone, prev_port, 0);
545 prev_port = p->port_no;
548 dp_output_port(dp, skb, prev_port, 0);
555 /* Marks 'skb' as having originated from 'in_port' in 'dp'.
556 FIXME: how are devices reference counted? */
557 void dp_set_origin(struct datapath *dp, uint16_t in_port,
560 struct net_bridge_port *p;
561 p = (in_port < DP_MAX_PORTS ? dp->ports[in_port]
562 : in_port == OFPP_LOCAL ? dp->local_port
571 dp_xmit_skb(struct sk_buff *skb)
574 if (packet_length(skb) > skb->dev->mtu) {
575 printk("dropped over-mtu packet: %d > %d\n",
576 packet_length(skb), skb->dev->mtu);
586 /* Takes ownership of 'skb' and transmits it to 'out_port' on 'dp'.
588 int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port,
594 /* Send it out the port it came in on, which is already set in
598 printk("skb device not set forwarding to in_port\n");
602 return dp_xmit_skb(skb);
605 int retval = run_flow_through_tables(dp->chain, skb,
613 return output_all(dp, skb, 1);
616 return output_all(dp, skb, 0);
618 case OFPP_CONTROLLER:
619 return dp_output_control(dp, skb, fwd_save_skb(skb), 0,
623 struct net_device *dev = dp->netdev;
627 return dev ? dp_dev_recv(dev, skb) : -ESRCH;
630 case 0 ... DP_MAX_PORTS - 1: {
631 struct net_bridge_port *p = dp->ports[out_port];
634 if (p->dev == skb->dev) {
635 /* To send to the input port, must use OFPP_IN_PORT */
638 printk("can't directly forward to input port\n");
641 if (p->config & OFPPC_NO_FWD && !ignore_no_fwd) {
646 return dp_xmit_skb(skb);
656 printk("can't forward to bad port %d\n", out_port);
660 /* Takes ownership of 'skb' and transmits it to 'dp''s control path. If
661 * 'buffer_id' != -1, then only the first 64 bytes of 'skb' are sent;
662 * otherwise, all of 'skb' is sent. 'reason' indicates why 'skb' is being
663 * sent. 'max_len' sets the maximum number of bytes that the caller
664 * wants to be sent; a value of 0 indicates the entire packet should be
667 dp_output_control(struct datapath *dp, struct sk_buff *skb,
668 uint32_t buffer_id, size_t max_len, int reason)
670 /* FIXME? Can we avoid creating a new skbuff in the case where we
671 * forward the whole packet? */
672 struct sk_buff *f_skb;
673 struct ofp_packet_in *opi;
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 opi->in_port = htons(skb->dev && skb->dev->br_port
690 ? skb->dev->br_port->port_no
692 opi->reason = reason;
694 skb_copy_bits(skb, 0, opi->data, fwd_len);
695 err = send_openflow_skb(f_skb, NULL);
702 static void fill_port_desc(struct net_bridge_port *p, struct ofp_phy_port *desc)
705 desc->port_no = htons(p->port_no);
706 strncpy(desc->name, p->dev->name, OFP_MAX_PORT_NAME_LEN);
707 desc->name[OFP_MAX_PORT_NAME_LEN-1] = '\0';
708 memcpy(desc->hw_addr, p->dev->dev_addr, ETH_ALEN);
711 desc->advertised = 0;
714 spin_lock_irqsave(&p->lock, flags);
715 desc->config = htonl(p->config);
716 desc->state = htonl(p->state);
717 spin_unlock_irqrestore(&p->lock, flags);
719 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,24)
720 if (p->dev->ethtool_ops && p->dev->ethtool_ops->get_settings) {
721 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
723 if (!p->dev->ethtool_ops->get_settings(p->dev, &ecmd)) {
724 /* Set the supported features */
725 if (ecmd.supported & SUPPORTED_10baseT_Half)
726 desc->supported |= OFPPF_10MB_HD;
727 if (ecmd.supported & SUPPORTED_10baseT_Full)
728 desc->supported |= OFPPF_10MB_FD;
729 if (ecmd.supported & SUPPORTED_100baseT_Half)
730 desc->supported |= OFPPF_100MB_HD;
731 if (ecmd.supported & SUPPORTED_100baseT_Full)
732 desc->supported |= OFPPF_100MB_FD;
733 if (ecmd.supported & SUPPORTED_1000baseT_Half)
734 desc->supported |= OFPPF_1GB_HD;
735 if (ecmd.supported & SUPPORTED_1000baseT_Full)
736 desc->supported |= OFPPF_1GB_FD;
737 if (ecmd.supported & SUPPORTED_10000baseT_Full)
738 desc->supported |= OFPPF_10GB_FD;
739 if (ecmd.supported & SUPPORTED_TP)
740 desc->supported |= OFPPF_COPPER;
741 if (ecmd.supported & SUPPORTED_FIBRE)
742 desc->supported |= OFPPF_FIBER;
743 if (ecmd.supported & SUPPORTED_Autoneg)
744 desc->supported |= OFPPF_AUTONEG;
745 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
746 if (ecmd.supported & SUPPORTED_Pause)
747 desc->supported |= OFPPF_PAUSE;
748 if (ecmd.supported & SUPPORTED_Asym_Pause)
749 desc->supported |= OFPPF_PAUSE_ASYM;
750 #endif /* kernel >= 2.6.14 */
752 /* Set the advertised features */
753 if (ecmd.advertising & ADVERTISED_10baseT_Half)
754 desc->advertised |= OFPPF_10MB_HD;
755 if (ecmd.advertising & ADVERTISED_10baseT_Full)
756 desc->advertised |= OFPPF_10MB_FD;
757 if (ecmd.advertising & ADVERTISED_100baseT_Half)
758 desc->advertised |= OFPPF_100MB_HD;
759 if (ecmd.advertising & ADVERTISED_100baseT_Full)
760 desc->advertised |= OFPPF_100MB_FD;
761 if (ecmd.advertising & ADVERTISED_1000baseT_Half)
762 desc->advertised |= OFPPF_1GB_HD;
763 if (ecmd.advertising & ADVERTISED_1000baseT_Full)
764 desc->advertised |= OFPPF_1GB_FD;
765 if (ecmd.advertising & ADVERTISED_10000baseT_Full)
766 desc->advertised |= OFPPF_10GB_FD;
767 if (ecmd.advertising & ADVERTISED_TP)
768 desc->advertised |= OFPPF_COPPER;
769 if (ecmd.advertising & ADVERTISED_FIBRE)
770 desc->advertised |= OFPPF_FIBER;
771 if (ecmd.advertising & ADVERTISED_Autoneg)
772 desc->advertised |= OFPPF_AUTONEG;
773 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
774 if (ecmd.advertising & ADVERTISED_Pause)
775 desc->advertised |= OFPPF_PAUSE;
776 if (ecmd.advertising & ADVERTISED_Asym_Pause)
777 desc->advertised |= OFPPF_PAUSE_ASYM;
778 #endif /* kernel >= 2.6.14 */
780 /* Set the current features */
781 if (ecmd.speed == SPEED_10)
782 desc->curr = (ecmd.duplex) ? OFPPF_10MB_FD : OFPPF_10MB_HD;
783 else if (ecmd.speed == SPEED_100)
784 desc->curr = (ecmd.duplex) ? OFPPF_100MB_FD : OFPPF_100MB_HD;
785 else if (ecmd.speed == SPEED_1000)
786 desc->curr = (ecmd.duplex) ? OFPPF_1GB_FD : OFPPF_1GB_HD;
787 else if (ecmd.speed == SPEED_10000)
788 desc->curr = OFPPF_10GB_FD;
790 if (ecmd.port == PORT_TP)
791 desc->curr |= OFPPF_COPPER;
792 else if (ecmd.port == PORT_FIBRE)
793 desc->curr |= OFPPF_FIBER;
796 desc->curr |= OFPPF_AUTONEG;
800 desc->curr = htonl(desc->curr);
801 desc->supported = htonl(desc->supported);
802 desc->advertised = htonl(desc->advertised);
803 desc->peer = htonl(desc->peer);
807 fill_features_reply(struct datapath *dp, struct ofp_switch_features *ofr)
809 struct net_bridge_port *p;
810 uint64_t dpid = get_datapath_id(dp->netdev);
813 ofr->datapath_id = cpu_to_be64(dpid);
815 ofr->n_buffers = htonl(N_PKT_BUFFERS);
816 ofr->n_tables = dp->chain->n_tables;
817 ofr->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
818 ofr->actions = htonl(OFP_SUPPORTED_ACTIONS);
819 memset(ofr->pad, 0, sizeof ofr->pad);
821 list_for_each_entry_rcu (p, &dp->port_list, node) {
822 fill_port_desc(p, &ofr->ports[port_count]);
830 dp_send_features_reply(struct datapath *dp, const struct sender *sender)
833 struct ofp_switch_features *ofr;
834 size_t ofr_len, port_max_len;
838 port_max_len = sizeof(struct ofp_phy_port) * DP_MAX_PORTS;
839 ofr = alloc_openflow_skb(dp, sizeof(*ofr) + port_max_len,
840 OFPT_FEATURES_REPLY, sender, &skb);
845 port_count = fill_features_reply(dp, ofr);
848 ofr_len = sizeof(*ofr) + (sizeof(struct ofp_phy_port) * port_count);
849 resize_openflow_skb(skb, &ofr->header, ofr_len);
850 return send_openflow_skb(skb, sender);
854 dp_send_config_reply(struct datapath *dp, const struct sender *sender)
857 struct ofp_switch_config *osc;
859 osc = alloc_openflow_skb(dp, sizeof *osc, OFPT_GET_CONFIG_REPLY, sender,
864 osc->flags = htons(dp->flags);
865 osc->miss_send_len = htons(dp->miss_send_len);
867 return send_openflow_skb(skb, sender);
871 dp_send_hello(struct datapath *dp, const struct sender *sender,
872 const struct ofp_header *request)
874 if (request->version < OFP_VERSION) {
876 sprintf(err, "Only version 0x%02x supported", OFP_VERSION);
877 dp_send_error_msg(dp, sender, OFPET_HELLO_FAILED,
878 OFPHFC_INCOMPATIBLE, err, strlen(err));
882 struct ofp_header *reply;
884 reply = alloc_openflow_skb(dp, sizeof *reply,
885 OFPT_HELLO, sender, &skb);
889 return send_openflow_skb(skb, sender);
893 /* Callback function for a workqueue to disable an interface */
895 down_port_cb(struct work_struct *work)
897 struct net_bridge_port *p = container_of(work, struct net_bridge_port,
901 if (dev_change_flags(p->dev, p->dev->flags & ~IFF_UP) < 0)
903 printk("problem bringing up port %s\n", p->dev->name);
905 p->config |= OFPPC_PORT_DOWN;
908 /* Callback function for a workqueue to enable an interface */
910 up_port_cb(struct work_struct *work)
912 struct net_bridge_port *p = container_of(work, struct net_bridge_port,
916 if (dev_change_flags(p->dev, p->dev->flags | IFF_UP) < 0)
918 printk("problem bringing down port %s\n", p->dev->name);
920 p->config &= ~OFPPC_PORT_DOWN;
924 dp_update_port_flags(struct datapath *dp, const struct ofp_port_mod *opm)
926 unsigned long int flags;
927 int port_no = ntohs(opm->port_no);
928 struct net_bridge_port *p;
929 p = (port_no < DP_MAX_PORTS ? dp->ports[port_no]
930 : port_no == OFPP_LOCAL ? dp->local_port
933 /* Make sure the port id hasn't changed since this was sent */
934 if (!p || memcmp(opm->hw_addr, p->dev->dev_addr, ETH_ALEN))
937 spin_lock_irqsave(&p->lock, flags);
939 uint32_t config_mask = ntohl(opm->mask);
940 p->config &= ~config_mask;
941 p->config |= ntohl(opm->config) & config_mask;
944 /* Modifying the status of an interface requires taking a lock
945 * that cannot be done from here. For this reason, we use a shared
946 * workqueue, which will cause it to be executed from a safer
948 if (opm->mask & htonl(OFPPC_PORT_DOWN)) {
949 if ((opm->config & htonl(OFPPC_PORT_DOWN))
950 && (p->config & OFPPC_PORT_DOWN) == 0) {
951 PREPARE_WORK(&p->port_task, down_port_cb);
952 schedule_work(&p->port_task);
953 } else if ((opm->config & htonl(OFPPC_PORT_DOWN)) == 0
954 && (p->config & OFPPC_PORT_DOWN)) {
955 PREPARE_WORK(&p->port_task, up_port_cb);
956 schedule_work(&p->port_task);
959 spin_unlock_irqrestore(&p->lock, flags);
964 /* Initialize the port status field of the bridge port. */
966 init_port_status(struct net_bridge_port *p)
968 unsigned long int flags;
970 spin_lock_irqsave(&p->lock, flags);
972 if (p->dev->flags & IFF_UP)
973 p->config &= ~OFPPC_PORT_DOWN;
975 p->config |= OFPPC_PORT_DOWN;
977 if (netif_carrier_ok(p->dev))
978 p->state &= ~OFPPS_LINK_DOWN;
980 p->state |= OFPPS_LINK_DOWN;
982 spin_unlock_irqrestore(&p->lock, flags);
986 dp_send_port_status(struct net_bridge_port *p, uint8_t status)
989 struct ofp_port_status *ops;
991 ops = alloc_openflow_skb(p->dp, sizeof *ops, OFPT_PORT_STATUS, NULL,
995 ops->reason = status;
996 memset(ops->pad, 0, sizeof ops->pad);
997 fill_port_desc(p, &ops->desc);
999 return send_openflow_skb(skb, NULL);
1002 /* Convert jiffies_64 to milliseconds. */
1003 static u64 inline jiffies_64_to_msecs(const u64 j)
1005 #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
1006 return (MSEC_PER_SEC / HZ) * j;
1007 #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
1008 return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
1010 return (j * MSEC_PER_SEC) / HZ;
1015 dp_send_flow_end(struct datapath *dp, struct sw_flow *flow,
1016 enum nx_flow_end_reason reason)
1018 struct sk_buff *skb;
1019 struct nx_flow_end *nfe;
1021 if (!dp->send_flow_end)
1024 nfe = alloc_openflow_skb(dp, sizeof *nfe, OFPT_VENDOR, 0, &skb);
1028 nfe->header.vendor = htonl(NX_VENDOR_ID);
1029 nfe->header.subtype = htonl(NXT_FLOW_END);
1031 flow_fill_match(&nfe->match, &flow->key);
1033 nfe->priority = htons(flow->priority);
1034 nfe->reason = reason;
1036 nfe->tcp_flags = flow->tcp_flags;
1037 nfe->ip_tos = flow->ip_tos;
1039 memset(nfe->pad, 0, sizeof nfe->pad);
1041 nfe->init_time = cpu_to_be64(jiffies_64_to_msecs(flow->created));
1042 nfe->used_time = cpu_to_be64(jiffies_64_to_msecs(flow->used));
1043 nfe->end_time = cpu_to_be64(jiffies_64_to_msecs(get_jiffies_64()));
1045 nfe->packet_count = cpu_to_be64(flow->packet_count);
1046 nfe->byte_count = cpu_to_be64(flow->byte_count);
1048 return send_openflow_skb(skb, NULL);
1050 EXPORT_SYMBOL(dp_send_flow_end);
1053 dp_send_error_msg(struct datapath *dp, const struct sender *sender,
1054 uint16_t type, uint16_t code, const void *data, size_t len)
1056 struct sk_buff *skb;
1057 struct ofp_error_msg *oem;
1060 oem = alloc_openflow_skb(dp, sizeof(*oem)+len, OFPT_ERROR,
1065 oem->type = htons(type);
1066 oem->code = htons(code);
1067 memcpy(oem->data, data, len);
1069 return send_openflow_skb(skb, sender);
1073 dp_send_echo_reply(struct datapath *dp, const struct sender *sender,
1074 const struct ofp_header *rq)
1076 struct sk_buff *skb;
1077 struct ofp_header *reply;
1079 reply = alloc_openflow_skb(dp, ntohs(rq->length), OFPT_ECHO_REPLY,
1084 memcpy(reply + 1, rq + 1, ntohs(rq->length) - sizeof *rq);
1085 return send_openflow_skb(skb, sender);
1088 /* Generic Netlink interface.
1090 * See netlink(7) for an introduction to netlink. See
1091 * http://linux-net.osdl.org/index.php/Netlink for more information and
1092 * pointers on how to work with netlink and Generic Netlink in the kernel and
1095 static struct genl_family dp_genl_family = {
1096 .id = GENL_ID_GENERATE,
1098 .name = DP_GENL_FAMILY_NAME,
1100 .maxattr = DP_GENL_A_MAX,
1103 /* Attribute policy: what each attribute may contain. */
1104 static struct nla_policy dp_genl_policy[DP_GENL_A_MAX + 1] = {
1105 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1106 [DP_GENL_A_MC_GROUP] = { .type = NLA_U32 },
1107 [DP_GENL_A_PORTNAME] = { .type = NLA_STRING }
1110 static int dp_genl_add(struct sk_buff *skb, struct genl_info *info)
1112 if (!info->attrs[DP_GENL_A_DP_IDX])
1115 return new_dp(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1118 static struct genl_ops dp_genl_ops_add_dp = {
1119 .cmd = DP_GENL_C_ADD_DP,
1120 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1121 .policy = dp_genl_policy,
1122 .doit = dp_genl_add,
1126 struct datapath *dp_get(int dp_idx)
1128 if (dp_idx < 0 || dp_idx >= DP_MAX)
1130 return rcu_dereference(dps[dp_idx]);
1133 static int dp_genl_del(struct sk_buff *skb, struct genl_info *info)
1135 struct datapath *dp;
1138 if (!info->attrs[DP_GENL_A_DP_IDX])
1141 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1151 static struct genl_ops dp_genl_ops_del_dp = {
1152 .cmd = DP_GENL_C_DEL_DP,
1153 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1154 .policy = dp_genl_policy,
1155 .doit = dp_genl_del,
1159 /* Queries a datapath for related information. Currently the only relevant
1160 * information is the datapath's multicast group ID. Really we want one
1161 * multicast group per datapath, but because of locking issues[*] we can't
1162 * easily get one. Thus, every datapath will currently return the same
1163 * global multicast group ID, but in the future it would be nice to fix that.
1165 * [*] dp_genl_add, to add a new datapath, is called under the genl_lock
1166 * mutex, and genl_register_mc_group, called to acquire a new multicast
1167 * group ID, also acquires genl_lock, thus deadlock.
1169 static int dp_genl_query(struct sk_buff *skb, struct genl_info *info)
1171 struct datapath *dp;
1172 struct sk_buff *ans_skb = NULL;
1176 if (!info->attrs[DP_GENL_A_DP_IDX])
1180 dp_idx = nla_get_u32((info->attrs[DP_GENL_A_DP_IDX]));
1181 dp = dp_get(dp_idx);
1186 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1191 data = genlmsg_put_reply(ans_skb, info, &dp_genl_family,
1192 0, DP_GENL_C_QUERY_DP);
1197 NLA_PUT_U32(ans_skb, DP_GENL_A_DP_IDX, dp_idx);
1198 NLA_PUT_U32(ans_skb, DP_GENL_A_MC_GROUP, mc_group.id);
1200 genlmsg_end(ans_skb, data);
1201 err = genlmsg_reply(ans_skb, info);
1211 static struct genl_ops dp_genl_ops_query_dp = {
1212 .cmd = DP_GENL_C_QUERY_DP,
1213 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1214 .policy = dp_genl_policy,
1215 .doit = dp_genl_query,
1219 static int dp_genl_add_del_port(struct sk_buff *skb, struct genl_info *info)
1221 struct datapath *dp;
1222 struct net_device *port;
1225 if (!info->attrs[DP_GENL_A_DP_IDX] || !info->attrs[DP_GENL_A_PORTNAME])
1229 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1235 /* Get interface to add/remove. */
1236 port = dev_get_by_name(&init_net,
1237 nla_data(info->attrs[DP_GENL_A_PORTNAME]));
1243 /* Execute operation. */
1244 if (info->genlhdr->cmd == DP_GENL_C_ADD_PORT)
1245 err = add_switch_port(dp, port);
1247 if (port->br_port == NULL || port->br_port->dp != dp) {
1251 err = dp_del_switch_port(port->br_port);
1260 static struct genl_ops dp_genl_ops_add_port = {
1261 .cmd = DP_GENL_C_ADD_PORT,
1262 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1263 .policy = dp_genl_policy,
1264 .doit = dp_genl_add_del_port,
1268 static struct genl_ops dp_genl_ops_del_port = {
1269 .cmd = DP_GENL_C_DEL_PORT,
1270 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1271 .policy = dp_genl_policy,
1272 .doit = dp_genl_add_del_port,
1276 static int dp_genl_openflow(struct sk_buff *skb, struct genl_info *info)
1278 struct nlattr *va = info->attrs[DP_GENL_A_OPENFLOW];
1279 struct datapath *dp;
1280 struct ofp_header *oh;
1281 struct sender sender;
1284 if (!info->attrs[DP_GENL_A_DP_IDX] || !va)
1287 dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1291 if (nla_len(va) < sizeof(struct ofp_header))
1295 sender.xid = oh->xid;
1296 sender.pid = info->snd_pid;
1297 sender.seq = info->snd_seq;
1299 mutex_lock(&dp_mutex);
1300 err = fwd_control_input(dp->chain, &sender,
1301 nla_data(va), nla_len(va));
1302 mutex_unlock(&dp_mutex);
1306 static struct nla_policy dp_genl_openflow_policy[DP_GENL_A_MAX + 1] = {
1307 [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1310 static int desc_stats_dump(struct datapath *dp, void *state,
1311 void *body, int *body_len)
1313 struct ofp_desc_stats *ods = body;
1314 int n_bytes = sizeof *ods;
1316 if (n_bytes > *body_len) {
1319 *body_len = n_bytes;
1321 strncpy(ods->mfr_desc, mfr_desc, sizeof ods->mfr_desc);
1322 strncpy(ods->hw_desc, hw_desc, sizeof ods->hw_desc);
1323 strncpy(ods->sw_desc, sw_desc, sizeof ods->sw_desc);
1324 strncpy(ods->serial_num, serial_num, sizeof ods->serial_num);
1329 struct flow_stats_state {
1331 struct sw_table_position position;
1332 const struct ofp_flow_stats_request *rq;
1335 int bytes_used, bytes_allocated;
1338 static int flow_stats_init(struct datapath *dp, const void *body, int body_len,
1341 const struct ofp_flow_stats_request *fsr = body;
1342 struct flow_stats_state *s = kmalloc(sizeof *s, GFP_ATOMIC);
1345 s->table_idx = fsr->table_id == 0xff ? 0 : fsr->table_id;
1346 memset(&s->position, 0, sizeof s->position);
1352 static int flow_stats_dump_callback(struct sw_flow *flow, void *private)
1354 struct sw_flow_actions *sf_acts = rcu_dereference(flow->sf_acts);
1355 struct flow_stats_state *s = private;
1356 struct ofp_flow_stats *ofs;
1360 length = sizeof *ofs + sf_acts->actions_len;
1361 if (length + s->bytes_used > s->bytes_allocated)
1364 ofs = s->body + s->bytes_used;
1365 ofs->length = htons(length);
1366 ofs->table_id = s->table_idx;
1368 ofs->match.wildcards = htonl(flow->key.wildcards);
1369 ofs->match.in_port = flow->key.in_port;
1370 memcpy(ofs->match.dl_src, flow->key.dl_src, ETH_ALEN);
1371 memcpy(ofs->match.dl_dst, flow->key.dl_dst, ETH_ALEN);
1372 ofs->match.dl_vlan = flow->key.dl_vlan;
1373 ofs->match.dl_type = flow->key.dl_type;
1374 ofs->match.nw_src = flow->key.nw_src;
1375 ofs->match.nw_dst = flow->key.nw_dst;
1376 ofs->match.nw_proto = flow->key.nw_proto;
1378 ofs->match.tp_src = flow->key.tp_src;
1379 ofs->match.tp_dst = flow->key.tp_dst;
1381 /* The kernel doesn't support 64-bit division, so use the 'do_div'
1382 * macro instead. The first argument is replaced with the quotient,
1383 * while the remainder is the return value. */
1384 duration = get_jiffies_64() - flow->created;
1385 do_div(duration, HZ);
1386 ofs->duration = htonl(duration);
1388 ofs->priority = htons(flow->priority);
1389 ofs->idle_timeout = htons(flow->idle_timeout);
1390 ofs->hard_timeout = htons(flow->hard_timeout);
1391 memset(ofs->pad2, 0, sizeof ofs->pad2);
1392 ofs->packet_count = cpu_to_be64(flow->packet_count);
1393 ofs->byte_count = cpu_to_be64(flow->byte_count);
1394 memcpy(ofs->actions, sf_acts->actions, sf_acts->actions_len);
1396 s->bytes_used += length;
1400 static int flow_stats_dump(struct datapath *dp, void *state,
1401 void *body, int *body_len)
1403 struct flow_stats_state *s = state;
1404 struct sw_flow_key match_key;
1408 s->bytes_allocated = *body_len;
1411 flow_extract_match(&match_key, &s->rq->match);
1412 while (s->table_idx < dp->chain->n_tables
1413 && (s->rq->table_id == 0xff || s->rq->table_id == s->table_idx))
1415 struct sw_table *table = dp->chain->tables[s->table_idx];
1417 error = table->iterate(table, &match_key, s->rq->out_port,
1418 &s->position, flow_stats_dump_callback, s);
1423 memset(&s->position, 0, sizeof s->position);
1425 *body_len = s->bytes_used;
1427 /* If error is 0, we're done.
1428 * Otherwise, if some bytes were used, there are more flows to come.
1429 * Otherwise, we were not able to fit even a single flow in the body,
1430 * which indicates that we have a single flow with too many actions to
1431 * fit. We won't ever make any progress at that rate, so give up. */
1432 return !error ? 0 : s->bytes_used ? 1 : -ENOMEM;
1435 static void flow_stats_done(void *state)
1440 static int aggregate_stats_init(struct datapath *dp,
1441 const void *body, int body_len,
1444 *state = (void *)body;
1448 static int aggregate_stats_dump_callback(struct sw_flow *flow, void *private)
1450 struct ofp_aggregate_stats_reply *rpy = private;
1451 rpy->packet_count += flow->packet_count;
1452 rpy->byte_count += flow->byte_count;
1457 static int aggregate_stats_dump(struct datapath *dp, void *state,
1458 void *body, int *body_len)
1460 struct ofp_aggregate_stats_request *rq = state;
1461 struct ofp_aggregate_stats_reply *rpy;
1462 struct sw_table_position position;
1463 struct sw_flow_key match_key;
1466 if (*body_len < sizeof *rpy)
1469 *body_len = sizeof *rpy;
1471 memset(rpy, 0, sizeof *rpy);
1473 flow_extract_match(&match_key, &rq->match);
1474 table_idx = rq->table_id == 0xff ? 0 : rq->table_id;
1475 memset(&position, 0, sizeof position);
1476 while (table_idx < dp->chain->n_tables
1477 && (rq->table_id == 0xff || rq->table_id == table_idx))
1479 struct sw_table *table = dp->chain->tables[table_idx];
1482 error = table->iterate(table, &match_key, rq->out_port, &position,
1483 aggregate_stats_dump_callback, rpy);
1488 memset(&position, 0, sizeof position);
1491 rpy->packet_count = cpu_to_be64(rpy->packet_count);
1492 rpy->byte_count = cpu_to_be64(rpy->byte_count);
1493 rpy->flow_count = htonl(rpy->flow_count);
1497 static int table_stats_dump(struct datapath *dp, void *state,
1498 void *body, int *body_len)
1500 struct ofp_table_stats *ots;
1501 int n_bytes = dp->chain->n_tables * sizeof *ots;
1503 if (n_bytes > *body_len)
1505 *body_len = n_bytes;
1506 for (i = 0, ots = body; i < dp->chain->n_tables; i++, ots++) {
1507 struct sw_table_stats stats;
1508 dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
1509 strncpy(ots->name, stats.name, sizeof ots->name);
1511 ots->wildcards = htonl(stats.wildcards);
1512 memset(ots->pad, 0, sizeof ots->pad);
1513 ots->max_entries = htonl(stats.max_flows);
1514 ots->active_count = htonl(stats.n_flows);
1515 ots->lookup_count = cpu_to_be64(stats.n_lookup);
1516 ots->matched_count = cpu_to_be64(stats.n_matched);
1521 struct port_stats_state {
1525 static int port_stats_init(struct datapath *dp, const void *body, int body_len,
1528 struct port_stats_state *s = kmalloc(sizeof *s, GFP_ATOMIC);
1536 static int port_stats_dump(struct datapath *dp, void *state,
1537 void *body, int *body_len)
1539 struct port_stats_state *s = state;
1540 struct ofp_port_stats *ops;
1541 int n_ports, max_ports;
1544 max_ports = *body_len / sizeof *ops;
1550 for (i = s->port; i < DP_MAX_PORTS && n_ports < max_ports; i++) {
1551 struct net_bridge_port *p = dp->ports[i];
1552 struct net_device_stats *stats;
1555 stats = p->dev->get_stats(p->dev);
1556 ops->port_no = htons(p->port_no);
1557 memset(ops->pad, 0, sizeof ops->pad);
1558 ops->rx_packets = cpu_to_be64(stats->rx_packets);
1559 ops->tx_packets = cpu_to_be64(stats->tx_packets);
1560 ops->rx_bytes = cpu_to_be64(stats->rx_bytes);
1561 ops->tx_bytes = cpu_to_be64(stats->tx_bytes);
1562 ops->rx_dropped = cpu_to_be64(stats->rx_dropped);
1563 ops->tx_dropped = cpu_to_be64(stats->tx_dropped);
1564 ops->rx_errors = cpu_to_be64(stats->rx_errors);
1565 ops->tx_errors = cpu_to_be64(stats->tx_errors);
1566 ops->rx_frame_err = cpu_to_be64(stats->rx_frame_errors);
1567 ops->rx_over_err = cpu_to_be64(stats->rx_over_errors);
1568 ops->rx_crc_err = cpu_to_be64(stats->rx_crc_errors);
1569 ops->collisions = cpu_to_be64(stats->collisions);
1574 *body_len = n_ports * sizeof *ops;
1575 return n_ports >= max_ports;
1578 static void port_stats_done(void *state)
1584 /* Minimum and maximum acceptable number of bytes in body member of
1585 * struct ofp_stats_request. */
1586 size_t min_body, max_body;
1588 /* Prepares to dump some kind of statistics on 'dp'. 'body' and
1589 * 'body_len' are the 'body' member of the struct ofp_stats_request.
1590 * Returns zero if successful, otherwise a negative error code.
1591 * May initialize '*state' to state information. May be null if no
1592 * initialization is required.*/
1593 int (*init)(struct datapath *dp, const void *body, int body_len,
1596 /* Dumps statistics for 'dp' into the '*body_len' bytes at 'body', and
1597 * modifies '*body_len' to reflect the number of bytes actually used.
1598 * ('body' will be transmitted as the 'body' member of struct
1599 * ofp_stats_reply.) */
1600 int (*dump)(struct datapath *dp, void *state,
1601 void *body, int *body_len);
1603 /* Cleans any state created by the init or dump functions. May be null
1604 * if no cleanup is required. */
1605 void (*done)(void *state);
1608 static const struct stats_type stats[] = {
1617 sizeof(struct ofp_flow_stats_request),
1618 sizeof(struct ofp_flow_stats_request),
1623 [OFPST_AGGREGATE] = {
1624 sizeof(struct ofp_aggregate_stats_request),
1625 sizeof(struct ofp_aggregate_stats_request),
1626 aggregate_stats_init,
1627 aggregate_stats_dump,
1647 dp_genl_openflow_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
1649 struct datapath *dp;
1650 struct sender sender;
1651 const struct stats_type *s;
1652 struct ofp_stats_reply *osr;
1654 int max_openflow_len, body_len;
1658 /* Set up the cleanup function for this dump. Linux 2.6.20 and later
1659 * support setting up cleanup functions via the .doneit member of
1660 * struct genl_ops. This kluge supports earlier versions also. */
1661 cb->done = dp_genl_openflow_done;
1663 sender.pid = NETLINK_CB(cb->skb).pid;
1664 sender.seq = cb->nlh->nlmsg_seq;
1666 struct nlattr *attrs[DP_GENL_A_MAX + 1];
1667 struct ofp_stats_request *rq;
1669 size_t len, body_len;
1672 err = nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs, DP_GENL_A_MAX,
1673 dp_genl_openflow_policy);
1677 if (!attrs[DP_GENL_A_DP_IDX])
1679 dp_idx = nla_get_u16(attrs[DP_GENL_A_DP_IDX]);
1680 dp = dp_get(dp_idx);
1684 va = attrs[DP_GENL_A_OPENFLOW];
1686 if (!va || len < sizeof *rq)
1690 sender.xid = rq->header.xid;
1691 type = ntohs(rq->type);
1692 if (rq->header.version != OFP_VERSION) {
1693 dp_send_error_msg(dp, &sender, OFPET_BAD_REQUEST,
1694 OFPBRC_BAD_VERSION, rq, len);
1697 if (rq->header.type != OFPT_STATS_REQUEST
1698 || ntohs(rq->header.length) != len)
1701 if (type >= ARRAY_SIZE(stats) || !stats[type].dump) {
1702 dp_send_error_msg(dp, &sender, OFPET_BAD_REQUEST,
1703 OFPBRC_BAD_STAT, rq, len);
1708 body_len = len - offsetof(struct ofp_stats_request, body);
1709 if (body_len < s->min_body || body_len > s->max_body)
1713 cb->args[1] = dp_idx;
1715 cb->args[3] = rq->header.xid;
1718 err = s->init(dp, rq->body, body_len, &state);
1721 cb->args[4] = (long) state;
1723 } else if (cb->args[0] == 1) {
1724 sender.xid = cb->args[3];
1725 dp_idx = cb->args[1];
1726 s = &stats[cb->args[2]];
1728 dp = dp_get(dp_idx);
1735 osr = put_openflow_headers(dp, skb, OFPT_STATS_REPLY, &sender,
1738 return PTR_ERR(osr);
1739 osr->type = htons(s - stats);
1741 resize_openflow_skb(skb, &osr->header, max_openflow_len);
1743 body_len = max_openflow_len - offsetof(struct ofp_stats_reply, body);
1745 err = s->dump(dp, (void *) cb->args[4], body, &body_len);
1750 osr->flags = ntohs(OFPSF_REPLY_MORE);
1751 resize_openflow_skb(skb, &osr->header,
1752 (offsetof(struct ofp_stats_reply, body)
1761 dp_genl_openflow_done(struct netlink_callback *cb)
1764 const struct stats_type *s = &stats[cb->args[2]];
1766 s->done((void *) cb->args[4]);
1771 static struct genl_ops dp_genl_ops_openflow = {
1772 .cmd = DP_GENL_C_OPENFLOW,
1773 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1774 .policy = dp_genl_openflow_policy,
1775 .doit = dp_genl_openflow,
1776 .dumpit = dp_genl_openflow_dumpit,
1779 static struct genl_ops *dp_genl_all_ops[] = {
1780 /* Keep this operation first. Generic Netlink dispatching
1781 * looks up operations with linear search, so we want it at the
1783 &dp_genl_ops_openflow,
1785 &dp_genl_ops_add_dp,
1786 &dp_genl_ops_del_dp,
1787 &dp_genl_ops_query_dp,
1788 &dp_genl_ops_add_port,
1789 &dp_genl_ops_del_port,
1792 static int dp_init_netlink(void)
1797 err = genl_register_family(&dp_genl_family);
1801 for (i = 0; i < ARRAY_SIZE(dp_genl_all_ops); i++) {
1802 err = genl_register_ops(&dp_genl_family, dp_genl_all_ops[i]);
1804 goto err_unregister;
1807 strcpy(mc_group.name, "openflow");
1808 err = genl_register_mc_group(&dp_genl_family, &mc_group);
1810 goto err_unregister;
1815 genl_unregister_family(&dp_genl_family);
1819 static void dp_uninit_netlink(void)
1821 genl_unregister_family(&dp_genl_family);
1824 /* Set the description strings if appropriate values are available from
1826 static void set_desc(void)
1828 const char *uuid = dmi_get_system_info(DMI_PRODUCT_UUID);
1829 const char *uptr = uuid + 24;
1831 if (!uuid || *uuid == '\0' || strlen(uuid) != 36)
1834 /* We are only interested version 1 UUIDs, since the last six bytes
1835 * are an IEEE 802 MAC address. */
1836 if (uuid[14] != '1')
1839 /* Only set if the UUID is from Nicira. */
1840 if (strncmp(uptr, NICIRA_OUI_STR, strlen(NICIRA_OUI_STR)))
1843 strlcpy(mfr_desc, dmi_get_system_info(DMI_SYS_VENDOR), sizeof(mfr_desc));
1844 snprintf(hw_desc, sizeof(hw_desc), "%s %s",
1845 dmi_get_system_info(DMI_PRODUCT_NAME),
1846 dmi_get_system_info(DMI_PRODUCT_VERSION));
1847 strlcpy(serial_num, dmi_get_system_info(DMI_PRODUCT_SERIAL),
1848 sizeof(serial_num));
1851 static int __init dp_init(void)
1855 printk("OpenFlow %s, built "__DATE__" "__TIME__", "
1856 "protocol 0x%02x\n", VERSION BUILDNR, OFP_VERSION);
1862 err = register_netdevice_notifier(&dp_device_notifier);
1864 goto error_flow_exit;
1866 err = dp_init_netlink();
1868 goto error_unreg_notifier;
1870 /* Check if better descriptions of the switch are available than the
1874 /* Hook into callback used by the bridge to intercept packets.
1875 * Parasites we are. */
1876 if (br_handle_frame_hook)
1877 printk("openflow: hijacking bridge hook\n");
1878 br_handle_frame_hook = dp_frame_hook;
1882 error_unreg_notifier:
1883 unregister_netdevice_notifier(&dp_device_notifier);
1887 printk(KERN_EMERG "openflow: failed to install!");
1891 static void dp_cleanup(void)
1894 dp_uninit_netlink();
1895 unregister_netdevice_notifier(&dp_device_notifier);
1897 br_handle_frame_hook = NULL;
1900 module_init(dp_init);
1901 module_exit(dp_cleanup);
1903 MODULE_DESCRIPTION("OpenFlow switching datapath");
1904 MODULE_AUTHOR("Copyright (c) 2007, 2008 The Board of Trustees of The Leland Stanford Junior University");
1905 MODULE_LICENSE("GPL");