Free sk_buffs with kfree_skb() instead of just kfree().
[sliver-openvswitch.git] / datapath / datapath.c
1 /*
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
5  */
6
7 /* Functions for managing the dp interface/device. */
8
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>
14 #include <linux/in.h>
15 #include <net/genetlink.h>
16 #include <linux/ip.h>
17 #include <linux/delay.h>
18 #include <linux/etherdevice.h>
19 #include <linux/kernel.h>
20 #include <linux/kthread.h>
21 #include <linux/mutex.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/rcupdate.h>
24 #include <linux/version.h>
25 #include <linux/ethtool.h>
26 #include <linux/random.h>
27 #include <asm/system.h>
28 #include <linux/netfilter_bridge.h>
29 #include <linux/inetdevice.h>
30 #include <linux/list.h>
31 #include <linux/rculist.h>
32 #include <linux/workqueue.h>
33
34 #include "openflow-netlink.h"
35 #include "datapath.h"
36 #include "table.h"
37 #include "chain.h"
38 #include "dp_dev.h"
39 #include "forward.h"
40 #include "flow.h"
41
42 #include "compat.h"
43
44
45 /* Strings to describe the manufacturer, hardware, and software.  This data 
46  * is queriable through the switch description stats message. */
47 static char mfr_desc[DESC_STR_LEN] = "Nicira Networks";
48 static char hw_desc[DESC_STR_LEN] = "Reference Linux Kernel Module";
49 static char sw_desc[DESC_STR_LEN] = VERSION;
50 static char serial_num[SERIAL_NUM_LEN] = "None";
51
52 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
53 module_param_string(mfr_desc, mfr_desc, sizeof mfr_desc, 0444);
54 module_param_string(hw_desc, hw_desc, sizeof hw_desc, 0444);
55 module_param_string(sw_desc, sw_desc, sizeof sw_desc, 0444);
56 module_param_string(serial_num, serial_num, sizeof serial_num, 0444);
57 #else
58 MODULE_PARM(mfr_desc, "s");
59 MODULE_PARM(hw_desc, "s");
60 MODULE_PARM(sw_desc, "s");
61 MODULE_PARM(serial_num, "s");
62 #endif
63
64
65 /* Number of milliseconds between runs of the maintenance thread. */
66 #define MAINT_SLEEP_MSECS 1000
67
68 #define UINT32_MAX                        4294967295U
69 #define UINT16_MAX                        65535
70 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
71
72 static struct genl_family dp_genl_family;
73 static struct genl_multicast_group mc_group;
74
75 /* It's hard to imagine wanting more than one datapath, but... */
76 #define DP_MAX 32
77
78 /* Datapaths.  Protected on the read side by rcu_read_lock, on the write side
79  * by dp_mutex.  dp_mutex is almost completely redundant with genl_mutex
80  * maintained by the Generic Netlink code, but the timeout path needs mutual
81  * exclusion too.
82  *
83  * It is safe to access the datapath and net_bridge_port structures with just
84  * dp_mutex.
85  */
86 static struct datapath *dps[DP_MAX];
87 DEFINE_MUTEX(dp_mutex);
88 EXPORT_SYMBOL(dp_mutex);
89
90 static int dp_maint_func(void *data);
91 static void init_port_status(struct net_bridge_port *p);
92 static int dp_genl_openflow_done(struct netlink_callback *);
93 static struct net_bridge_port *new_nbp(struct datapath *,
94                                        struct net_device *, int port_no);
95
96 /* nla_shrink - reduce amount of space reserved by nla_reserve
97  * @skb: socket buffer from which to recover room
98  * @nla: netlink attribute to adjust
99  * @len: new length of attribute payload
100  *
101  * Reduces amount of space reserved by a call to nla_reserve.
102  *
103  * No other attributes may be added between calling nla_reserve and this
104  * function, since it will create a hole in the message.
105  */
106 void nla_shrink(struct sk_buff *skb, struct nlattr *nla, int len)
107 {
108         int delta = nla_total_size(len) - nla_total_size(nla_len(nla));
109         BUG_ON(delta > 0);
110         skb->tail += delta;
111         skb->len  += delta;
112         nla->nla_len = nla_attr_size(len);
113 }
114
115 /* Puts a set of openflow headers for a message of the given 'type' into 'skb'.
116  * If 'sender' is nonnull, then it is used as the message's destination.  'dp'
117  * must specify the datapath to use.
118  *
119  * '*max_openflow_len' receives the maximum number of bytes that are available
120  * for the embedded OpenFlow message.  The caller must call
121  * resize_openflow_skb() to set the actual size of the message to this number
122  * of bytes or less.
123  *
124  * Returns the openflow header if successful, otherwise (if 'skb' is too small)
125  * an error code. */
126 static void *
127 put_openflow_headers(struct datapath *dp, struct sk_buff *skb, uint8_t type,
128                      const struct sender *sender, int *max_openflow_len)
129 {
130         struct ofp_header *oh;
131         struct nlattr *attr;
132         int openflow_len;
133
134         /* Assemble the Generic Netlink wrapper. */
135         if (!genlmsg_put(skb,
136                          sender ? sender->pid : 0,
137                          sender ? sender->seq : 0,
138                          &dp_genl_family, 0, DP_GENL_C_OPENFLOW))
139                 return ERR_PTR(-ENOBUFS);
140         if (nla_put_u32(skb, DP_GENL_A_DP_IDX, dp->dp_idx) < 0)
141                 return ERR_PTR(-ENOBUFS);
142         openflow_len = (skb_tailroom(skb) - NLA_HDRLEN) & ~(NLA_ALIGNTO - 1);
143         if (openflow_len < sizeof *oh)
144                 return ERR_PTR(-ENOBUFS);
145         *max_openflow_len = openflow_len;
146         attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, openflow_len);
147         BUG_ON(!attr);
148
149         /* Fill in the header.  The caller is responsible for the length. */
150         oh = nla_data(attr);
151         oh->version = OFP_VERSION;
152         oh->type = type;
153         oh->xid = sender ? sender->xid : 0;
154
155         return oh;
156 }
157
158 /* Resizes OpenFlow header 'oh', which must be at the tail end of 'skb', to new
159  * length 'new_length' (in bytes), adjusting pointers and size values as
160  * necessary. */
161 static void
162 resize_openflow_skb(struct sk_buff *skb,
163                     struct ofp_header *oh, size_t new_length)
164 {
165         struct nlattr *attr = ((void *) oh) - NLA_HDRLEN;
166         nla_shrink(skb, attr, new_length);
167         oh->length = htons(new_length);
168         nlmsg_end(skb, (struct nlmsghdr *) skb->data);
169 }
170
171 /* Allocates a new skb to contain an OpenFlow message 'openflow_len' bytes in
172  * length.  Returns a null pointer if memory is unavailable, otherwise returns
173  * the OpenFlow header and stores a pointer to the skb in '*pskb'. 
174  *
175  * 'type' is the OpenFlow message type.  If 'sender' is nonnull, then it is
176  * used as the message's destination.  'dp' must specify the datapath to
177  * use.  */
178 static void *
179 alloc_openflow_skb(struct datapath *dp, size_t openflow_len, uint8_t type,
180                    const struct sender *sender, struct sk_buff **pskb) 
181 {
182         struct ofp_header *oh;
183         size_t genl_len;
184         struct sk_buff *skb;
185         int max_openflow_len;
186
187         if ((openflow_len + sizeof(struct ofp_header)) > UINT16_MAX) {
188                 if (net_ratelimit())
189                         printk("alloc_openflow_skb: openflow message too large: %zu\n", 
190                                         openflow_len);
191                 return NULL;
192         }
193
194         genl_len = nlmsg_total_size(GENL_HDRLEN + dp_genl_family.hdrsize);
195         genl_len += nla_total_size(sizeof(uint32_t)); /* DP_GENL_A_DP_IDX */
196         genl_len += nla_total_size(openflow_len);    /* DP_GENL_A_OPENFLOW */
197         skb = *pskb = genlmsg_new(genl_len, GFP_ATOMIC);
198         if (!skb) {
199                 if (net_ratelimit())
200                         printk("alloc_openflow_skb: genlmsg_new failed\n");
201                 return NULL;
202         }
203
204         oh = put_openflow_headers(dp, skb, type, sender, &max_openflow_len);
205         BUG_ON(!oh || IS_ERR(oh));
206         resize_openflow_skb(skb, oh, openflow_len);
207
208         return oh;
209 }
210
211 /* Sends 'skb' to 'sender' if it is nonnull, otherwise multicasts 'skb' to all
212  * listeners. */
213 static int
214 send_openflow_skb(struct sk_buff *skb, const struct sender *sender) 
215 {
216         return (sender
217                 ? genlmsg_unicast(skb, sender->pid)
218                 : genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC));
219 }
220
221 /* Generates a unique datapath id.  It incorporates the datapath index
222  * and a hardware address, if available.  If not, it generates a random
223  * one.
224  */
225 static 
226 uint64_t gen_datapath_id(uint16_t dp_idx)
227 {
228         uint64_t id;
229         int i;
230         struct net_device *dev;
231
232         /* The top 16 bits are used to identify the datapath.  The lower 48 bits
233          * use an interface address.  */
234         id = (uint64_t)dp_idx << 48;
235         if ((dev = dev_get_by_name(&init_net, "ctl0")) 
236                         || (dev = dev_get_by_name(&init_net, "eth0"))) {
237                 for (i=0; i<ETH_ALEN; i++) {
238                         id |= (uint64_t)dev->dev_addr[i] << (8*(ETH_ALEN-1 - i));
239                 }
240                 dev_put(dev);
241         } else {
242                 /* Randomly choose the lower 48 bits if we cannot find an
243                  * address and mark the most significant bit to indicate that
244                  * this was randomly generated. */
245                 uint8_t rand[ETH_ALEN];
246                 get_random_bytes(rand, ETH_ALEN);
247                 id |= (uint64_t)1 << 63;
248                 for (i=0; i<ETH_ALEN; i++) {
249                         id |= (uint64_t)rand[i] << (8*(ETH_ALEN-1 - i));
250                 }
251         }
252
253         return id;
254 }
255
256 /* Creates a new datapath numbered 'dp_idx'.  Returns 0 for success or a
257  * negative error code. */
258 static int new_dp(int dp_idx)
259 {
260         struct datapath *dp;
261         int err;
262
263         if (dp_idx < 0 || dp_idx >= DP_MAX)
264                 return -EINVAL;
265
266         if (!try_module_get(THIS_MODULE))
267                 return -ENODEV;
268
269         /* Exit early if a datapath with that number already exists. */
270         if (dps[dp_idx]) {
271                 err = -EEXIST;
272                 goto err_unlock;
273         }
274
275         err = -ENOMEM;
276         dp = kzalloc(sizeof *dp, GFP_KERNEL);
277         if (dp == NULL)
278                 goto err_unlock;
279
280         /* Setup our "of" device */
281         err = dp_dev_setup(dp);
282         if (err)
283                 goto err_free_dp;
284
285         dp->dp_idx = dp_idx;
286         dp->id = gen_datapath_id(dp_idx);
287         dp->chain = chain_create(dp);
288         if (dp->chain == NULL)
289                 goto err_destroy_dp_dev;
290         INIT_LIST_HEAD(&dp->port_list);
291
292         dp->local_port = new_nbp(dp, dp->netdev, OFPP_LOCAL);
293         if (IS_ERR(dp->local_port)) {
294                 err = PTR_ERR(dp->local_port);
295                 goto err_destroy_local_port;
296         }
297
298         dp->flags = 0;
299         dp->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
300
301         dp->dp_task = kthread_run(dp_maint_func, dp, "dp%d", dp_idx);
302         if (IS_ERR(dp->dp_task))
303                 goto err_destroy_chain;
304
305         dps[dp_idx] = dp;
306
307         return 0;
308
309 err_destroy_local_port:
310         dp_del_switch_port(dp->local_port);
311 err_destroy_chain:
312         chain_destroy(dp->chain);
313 err_destroy_dp_dev:
314         dp_dev_destroy(dp);
315 err_free_dp:
316         kfree(dp);
317 err_unlock:
318         module_put(THIS_MODULE);
319                 return err;
320 }
321
322 /* Find and return a free port number under 'dp'. */
323 static int find_portno(struct datapath *dp)
324 {
325         int i;
326         for (i = 0; i < OFPP_MAX; i++)
327                 if (dp->ports[i] == NULL)
328                         return i;
329         return -EXFULL;
330 }
331
332 static struct net_bridge_port *new_nbp(struct datapath *dp,
333                                        struct net_device *dev, int port_no)
334 {
335         struct net_bridge_port *p;
336
337         if (dev->br_port != NULL)
338                 return ERR_PTR(-EBUSY);
339
340         p = kzalloc(sizeof(*p), GFP_KERNEL);
341         if (p == NULL)
342                 return ERR_PTR(-ENOMEM);
343
344         rtnl_lock();
345         dev_set_promiscuity(dev, 1);
346         rtnl_unlock();
347         dev_hold(dev);
348         p->dp = dp;
349         p->dev = dev;
350         p->port_no = port_no;
351         spin_lock_init(&p->lock);
352         INIT_WORK(&p->port_task, NULL);
353         if (port_no != OFPP_LOCAL)
354                 rcu_assign_pointer(dev->br_port, p);
355         if (port_no < OFPP_MAX)
356                 rcu_assign_pointer(dp->ports[port_no], p); 
357         list_add_rcu(&p->node, &dp->port_list);
358
359         return p;
360 }
361
362 int add_switch_port(struct datapath *dp, struct net_device *dev)
363 {
364         struct net_bridge_port *p;
365         int port_no;
366
367         if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER
368             || is_dp_dev(dev))
369                 return -EINVAL;
370
371         port_no = find_portno(dp);
372         if (port_no < 0)
373                 return port_no;
374
375         p = new_nbp(dp, dev, port_no);
376         if (IS_ERR(p))
377                 return PTR_ERR(p);
378
379         init_port_status(p);
380
381         /* Notify the ctlpath that this port has been added */
382         dp_send_port_status(p, OFPPR_ADD);
383
384         return 0;
385 }
386
387 /* Delete 'p' from switch. */
388 int dp_del_switch_port(struct net_bridge_port *p)
389 {
390         /* First drop references to device. */
391         cancel_work_sync(&p->port_task);
392         rtnl_lock();
393         dev_set_promiscuity(p->dev, -1);
394         rtnl_unlock();
395         list_del_rcu(&p->node);
396         if (p->port_no != OFPP_LOCAL)
397                 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
398         rcu_assign_pointer(p->dev->br_port, NULL);
399
400         /* Then wait until no one is still using it, and destroy it. */
401         synchronize_rcu();
402
403         /* Notify the ctlpath that this port no longer exists */
404         dp_send_port_status(p, OFPPR_DELETE);
405
406         dev_put(p->dev);
407         kfree(p);
408
409         return 0;
410 }
411
412 static void del_dp(struct datapath *dp)
413 {
414         struct net_bridge_port *p, *n;
415
416         kthread_stop(dp->dp_task);
417
418         /* Drop references to DP. */
419         list_for_each_entry_safe (p, n, &dp->port_list, node)
420                 dp_del_switch_port(p);
421         rcu_assign_pointer(dps[dp->dp_idx], NULL);
422
423         /* Kill off local_port dev references from buffered packets that have
424          * associated dst entries. */
425         synchronize_rcu();
426         fwd_discard_all();
427
428         /* Destroy dp->netdev.  (Must follow deleting switch ports since
429          * dp->local_port has a reference to it.) */
430         dp_dev_destroy(dp);
431
432         /* Wait until no longer in use, then destroy it. */
433         synchronize_rcu();
434         chain_destroy(dp->chain);
435         kfree(dp);
436         module_put(THIS_MODULE);
437 }
438
439 static int dp_maint_func(void *data)
440 {
441         struct datapath *dp = (struct datapath *) data;
442
443         while (!kthread_should_stop()) {
444                 /* Timeout old entries */
445                 chain_timeout(dp->chain);
446                 msleep_interruptible(MAINT_SLEEP_MSECS);
447         }
448                 
449         return 0;
450 }
451
452 static void
453 do_port_input(struct net_bridge_port *p, struct sk_buff *skb) 
454 {
455         /* Push the Ethernet header back on. */
456         skb_push(skb, ETH_HLEN);
457         fwd_port_input(p->dp->chain, skb, p);
458 }
459
460 /*
461  * Used as br_handle_frame_hook.  (Cannot run bridge at the same time, even on
462  * different set of devices!)
463  */
464 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
465 /* Called with rcu_read_lock. */
466 static struct sk_buff *dp_frame_hook(struct net_bridge_port *p,
467                                          struct sk_buff *skb)
468 {
469         do_port_input(p, skb);
470         return NULL;
471 }
472 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
473 static int dp_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
474 {
475         do_port_input(p, *pskb);
476         return 1;
477 }
478 #else
479 /* NB: This has only been tested on 2.4.35 */
480 static void dp_frame_hook(struct sk_buff *skb)
481 {
482         struct net_bridge_port *p = skb->dev->br_port;
483         if (p) {
484                 rcu_read_lock();
485                 do_port_input(p, skb);
486                 rcu_read_unlock();
487         } else
488                 kfree_skb(skb);
489 }
490 #endif
491
492 /* Forwarding output path.
493  * Based on net/bridge/br_forward.c. */
494
495 static inline unsigned packet_length(const struct sk_buff *skb)
496 {
497         int length = skb->len - ETH_HLEN;
498         if (skb->protocol == htons(ETH_P_8021Q))
499                 length -= VLAN_HLEN;
500         return length;
501 }
502
503 /* Send packets out all the ports except the originating one.  If the
504  * "flood" argument is set, only send along the minimum spanning tree.
505  */
506 static int
507 output_all(struct datapath *dp, struct sk_buff *skb, int flood)
508 {
509         u32 disable = flood ? OFPPC_NO_FLOOD : 0;
510         struct net_bridge_port *p;
511         int prev_port = -1;
512
513         list_for_each_entry_rcu (p, &dp->port_list, node) {
514                 if (skb->dev == p->dev || p->config & disable)
515                         continue;
516                 if (prev_port != -1) {
517                         struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
518                         if (!clone) {
519                                 kfree_skb(skb);
520                                 return -ENOMEM;
521                         }
522                         dp_output_port(dp, clone, prev_port, 0); 
523                 }
524                 prev_port = p->port_no;
525         }
526         if (prev_port != -1)
527                 dp_output_port(dp, skb, prev_port, 0);
528         else
529                 kfree_skb(skb);
530
531         return 0;
532 }
533
534 /* Marks 'skb' as having originated from 'in_port' in 'dp'.
535    FIXME: how are devices reference counted? */
536 void dp_set_origin(struct datapath *dp, uint16_t in_port,
537                            struct sk_buff *skb)
538 {
539         struct net_bridge_port *p = (in_port < OFPP_MAX ? dp->ports[in_port]
540                                      : in_port == OFPP_LOCAL ? dp->local_port
541                                      : NULL);
542         if (p) 
543                 skb->dev = p->dev;
544          else 
545                 skb->dev = NULL;
546 }
547
548 static int xmit_skb(struct sk_buff *skb)
549 {
550         int len = skb->len;
551         if (packet_length(skb) > skb->dev->mtu) {
552                 printk("dropped over-mtu packet: %d > %d\n",
553                            packet_length(skb), skb->dev->mtu);
554                 kfree_skb(skb);
555                 return -E2BIG;
556         }
557
558         dev_queue_xmit(skb);
559
560         return len;
561 }
562
563 /* Takes ownership of 'skb' and transmits it to 'out_port' on 'dp'.
564  */
565 int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port,
566                    int ignore_no_fwd)
567 {
568         BUG_ON(!skb);
569         switch (out_port){
570         case OFPP_IN_PORT:
571                 /* Send it out the port it came in on, which is already set in
572                  * the skb. */
573                 if (!skb->dev) {
574                         if (net_ratelimit())
575                                 printk("skb device not set forwarding to in_port\n");
576                         kfree_skb(skb);
577                         return -ESRCH;
578                 }
579                 return xmit_skb(skb);
580                 
581         case OFPP_TABLE: {
582                 int retval = run_flow_through_tables(dp->chain, skb,
583                                                      skb->dev->br_port);
584                 if (retval)
585                         kfree_skb(skb);
586                 return retval;
587         }
588
589         case OFPP_FLOOD:
590                 return output_all(dp, skb, 1);
591
592         case OFPP_ALL:
593                 return output_all(dp, skb, 0);
594
595         case OFPP_CONTROLLER:
596                 return dp_output_control(dp, skb, fwd_save_skb(skb), 0,
597                                                   OFPR_ACTION);
598
599         case OFPP_LOCAL: {
600                 struct net_device *dev = dp->netdev;
601                 return dev ? dp_dev_recv(dev, skb) : -ESRCH;
602         }
603
604         case 0 ... OFPP_MAX-1: {
605                 struct net_bridge_port *p = dp->ports[out_port];
606                 if (p == NULL)
607                         goto bad_port;
608                 if (p->dev == skb->dev) {
609                         /* To send to the input port, must use OFPP_IN_PORT */
610                         kfree_skb(skb);
611                         if (net_ratelimit())
612                                 printk("can't directly forward to input port\n");
613                         return -EINVAL;
614                 }
615                 if (p->config & OFPPC_NO_FWD && !ignore_no_fwd) {
616                         kfree_skb(skb);
617                         return 0;
618                 }
619                 skb->dev = p->dev; 
620                 return xmit_skb(skb);
621         }
622
623         default:
624                 goto bad_port;
625         }
626
627 bad_port:
628         kfree_skb(skb);
629         if (net_ratelimit())
630                 printk("can't forward to bad port %d\n", out_port);
631         return -ENOENT;
632 }
633
634 /* Takes ownership of 'skb' and transmits it to 'dp''s control path.  If
635  * 'buffer_id' != -1, then only the first 64 bytes of 'skb' are sent;
636  * otherwise, all of 'skb' is sent.  'reason' indicates why 'skb' is being
637  * sent. 'max_len' sets the maximum number of bytes that the caller
638  * wants to be sent; a value of 0 indicates the entire packet should be
639  * sent. */
640 int
641 dp_output_control(struct datapath *dp, struct sk_buff *skb,
642                            uint32_t buffer_id, size_t max_len, int reason)
643 {
644         /* FIXME?  Can we avoid creating a new skbuff in the case where we
645          * forward the whole packet? */
646         struct sk_buff *f_skb;
647         struct ofp_packet_in *opi;
648         struct net_bridge_port *p;
649         size_t fwd_len, opi_len;
650         int err;
651
652         fwd_len = skb->len;
653         if ((buffer_id != (uint32_t) -1) && max_len)
654                 fwd_len = min(fwd_len, max_len);
655
656         opi_len = offsetof(struct ofp_packet_in, data) + fwd_len;
657         opi = alloc_openflow_skb(dp, opi_len, OFPT_PACKET_IN, NULL, &f_skb);
658         if (!opi) {
659                 err = -ENOMEM;
660                 goto out;
661         }
662         opi->buffer_id      = htonl(buffer_id);
663         opi->total_len      = htons(skb->len);
664         p = skb->dev->br_port;
665         opi->in_port        = htons(p ? p->port_no : OFPP_LOCAL);
666         opi->reason         = reason;
667         opi->pad            = 0;
668         memcpy(opi->data, skb_mac_header(skb), fwd_len);
669         err = send_openflow_skb(f_skb, NULL);
670
671 out:
672         kfree_skb(skb);
673         return err;
674 }
675
676 static void fill_port_desc(struct net_bridge_port *p, struct ofp_phy_port *desc)
677 {
678         unsigned long flags;
679         desc->port_no = htons(p->port_no);
680         strncpy(desc->name, p->dev->name, OFP_MAX_PORT_NAME_LEN);
681         desc->name[OFP_MAX_PORT_NAME_LEN-1] = '\0';
682         memcpy(desc->hw_addr, p->dev->dev_addr, ETH_ALEN);
683         desc->curr = 0;
684         desc->supported = 0;
685         desc->advertised = 0;
686         desc->peer = 0;
687
688         spin_lock_irqsave(&p->lock, flags);
689         desc->config = htonl(p->config);
690         desc->state = htonl(p->state);
691         spin_unlock_irqrestore(&p->lock, flags);
692
693 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,24)
694         if (p->dev->ethtool_ops && p->dev->ethtool_ops->get_settings) {
695                 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
696
697                 if (!p->dev->ethtool_ops->get_settings(p->dev, &ecmd)) {
698                         /* Set the supported features */
699                         if (ecmd.supported & SUPPORTED_10baseT_Half) 
700                                 desc->supported |= OFPPF_10MB_HD;
701                         if (ecmd.supported & SUPPORTED_10baseT_Full)
702                                 desc->supported |= OFPPF_10MB_FD;
703                         if (ecmd.supported & SUPPORTED_100baseT_Half) 
704                                 desc->supported |= OFPPF_100MB_HD;
705                         if (ecmd.supported & SUPPORTED_100baseT_Full)
706                                 desc->supported |= OFPPF_100MB_FD;
707                         if (ecmd.supported & SUPPORTED_1000baseT_Half)
708                                 desc->supported |= OFPPF_1GB_HD;
709                         if (ecmd.supported & SUPPORTED_1000baseT_Full)
710                                 desc->supported |= OFPPF_1GB_FD;
711                         if (ecmd.supported & SUPPORTED_10000baseT_Full)
712                                 desc->supported |= OFPPF_10GB_FD;
713                         if (ecmd.supported & SUPPORTED_TP)
714                                 desc->supported |= OFPPF_COPPER;
715                         if (ecmd.supported & SUPPORTED_FIBRE)
716                                 desc->supported |= OFPPF_FIBER;
717                         if (ecmd.supported & SUPPORTED_Autoneg)
718                                 desc->supported |= OFPPF_AUTONEG;
719 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
720                         if (ecmd.supported & SUPPORTED_Pause)
721                                 desc->supported |= OFPPF_PAUSE;
722                         if (ecmd.supported & SUPPORTED_Asym_Pause)
723                                 desc->supported |= OFPPF_PAUSE_ASYM;
724 #endif /* kernel >= 2.6.14 */
725
726                         /* Set the advertised features */
727                         if (ecmd.advertising & ADVERTISED_10baseT_Half) 
728                                 desc->advertised |= OFPPF_10MB_HD;
729                         if (ecmd.advertising & ADVERTISED_10baseT_Full)
730                                 desc->advertised |= OFPPF_10MB_FD;
731                         if (ecmd.advertising & ADVERTISED_100baseT_Half) 
732                                 desc->advertised |= OFPPF_100MB_HD;
733                         if (ecmd.advertising & ADVERTISED_100baseT_Full)
734                                 desc->advertised |= OFPPF_100MB_FD;
735                         if (ecmd.advertising & ADVERTISED_1000baseT_Half)
736                                 desc->advertised |= OFPPF_1GB_HD;
737                         if (ecmd.advertising & ADVERTISED_1000baseT_Full)
738                                 desc->advertised |= OFPPF_1GB_FD;
739                         if (ecmd.advertising & ADVERTISED_10000baseT_Full)
740                                 desc->advertised |= OFPPF_10GB_FD;
741                         if (ecmd.advertising & ADVERTISED_TP)
742                                 desc->advertised |= OFPPF_COPPER;
743                         if (ecmd.advertising & ADVERTISED_FIBRE)
744                                 desc->advertised |= OFPPF_FIBER;
745                         if (ecmd.advertising & ADVERTISED_Autoneg)
746                                 desc->advertised |= OFPPF_AUTONEG;
747 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
748                         if (ecmd.advertising & ADVERTISED_Pause)
749                                 desc->advertised |= OFPPF_PAUSE;
750                         if (ecmd.advertising & ADVERTISED_Asym_Pause)
751                                 desc->advertised |= OFPPF_PAUSE_ASYM;
752 #endif /* kernel >= 2.6.14 */
753
754                         /* Set the current features */
755                         if (ecmd.speed == SPEED_10)
756                                 desc->curr = (ecmd.duplex) ? OFPPF_10MB_FD : OFPPF_10MB_HD;
757                         else if (ecmd.speed == SPEED_100)
758                                 desc->curr = (ecmd.duplex) ? OFPPF_100MB_FD : OFPPF_100MB_HD;
759                         else if (ecmd.speed == SPEED_1000)
760                                 desc->curr = (ecmd.duplex) ? OFPPF_1GB_FD : OFPPF_1GB_HD;
761                         else if (ecmd.speed == SPEED_10000)
762                                 desc->curr = OFPPF_10GB_FD;
763
764                         if (ecmd.port == PORT_TP) 
765                                 desc->curr |= OFPPF_COPPER;
766                         else if (ecmd.port == PORT_FIBRE) 
767                                 desc->curr |= OFPPF_FIBER;
768
769                         if (ecmd.autoneg)
770                                 desc->curr |= OFPPF_AUTONEG;
771                 }
772         }
773 #endif
774         desc->curr = htonl(desc->curr);
775         desc->supported = htonl(desc->supported);
776         desc->advertised = htonl(desc->advertised);
777         desc->peer = htonl(desc->peer);
778 }
779
780 static int 
781 fill_features_reply(struct datapath *dp, struct ofp_switch_features *ofr)
782 {
783         struct net_bridge_port *p;
784         int port_count = 0;
785
786         ofr->datapath_id  = cpu_to_be64(dp->id); 
787
788         ofr->n_buffers    = htonl(N_PKT_BUFFERS);
789         ofr->n_tables     = dp->chain->n_tables;
790         ofr->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
791         ofr->actions      = htonl(OFP_SUPPORTED_ACTIONS);
792         memset(ofr->pad, 0, sizeof ofr->pad);
793
794         list_for_each_entry_rcu (p, &dp->port_list, node) {
795                 fill_port_desc(p, &ofr->ports[port_count]);
796                 port_count++;
797         }
798
799         return port_count;
800 }
801
802 int
803 dp_send_features_reply(struct datapath *dp, const struct sender *sender)
804 {
805         struct sk_buff *skb;
806         struct ofp_switch_features *ofr;
807         size_t ofr_len, port_max_len;
808         int port_count;
809
810         /* Overallocate. */
811         port_max_len = sizeof(struct ofp_phy_port) * OFPP_MAX;
812         ofr = alloc_openflow_skb(dp, sizeof(*ofr) + port_max_len,
813                                  OFPT_FEATURES_REPLY, sender, &skb);
814         if (!ofr)
815                 return -ENOMEM;
816
817         /* Fill. */
818         port_count = fill_features_reply(dp, ofr);
819
820         /* Shrink to fit. */
821         ofr_len = sizeof(*ofr) + (sizeof(struct ofp_phy_port) * port_count);
822         resize_openflow_skb(skb, &ofr->header, ofr_len);
823         return send_openflow_skb(skb, sender);
824 }
825
826 int
827 dp_send_config_reply(struct datapath *dp, const struct sender *sender)
828 {
829         struct sk_buff *skb;
830         struct ofp_switch_config *osc;
831
832         osc = alloc_openflow_skb(dp, sizeof *osc, OFPT_GET_CONFIG_REPLY, sender,
833                                  &skb);
834         if (!osc)
835                 return -ENOMEM;
836
837         osc->flags = htons(dp->flags);
838         osc->miss_send_len = htons(dp->miss_send_len);
839
840         return send_openflow_skb(skb, sender);
841 }
842
843 int
844 dp_send_hello(struct datapath *dp, const struct sender *sender,
845               const struct ofp_header *request)
846 {
847         if (request->version < OFP_VERSION) {
848                 char err[64];
849                 sprintf(err, "Only version 0x%02x supported", OFP_VERSION);
850                 dp_send_error_msg(dp, sender, OFPET_HELLO_FAILED,
851                                   OFPHFC_INCOMPATIBLE, err, strlen(err));
852                 return -EINVAL;
853         } else {
854                 struct sk_buff *skb;
855                 struct ofp_header *reply;
856
857                 reply = alloc_openflow_skb(dp, sizeof *reply,
858                                            OFPT_HELLO, sender, &skb);
859                 if (!reply)
860                         return -ENOMEM;
861
862                 return send_openflow_skb(skb, sender);
863         }
864 }
865
866 /* Callback function for a workqueue to disable an interface */
867 static void
868 down_port_cb(struct work_struct *work)
869 {
870         struct net_bridge_port *p = container_of(work, struct net_bridge_port, 
871                         port_task);
872
873         rtnl_lock();
874         if (dev_change_flags(p->dev, p->dev->flags & ~IFF_UP) < 0)
875                 if (net_ratelimit())
876                         printk("problem bringing up port %s\n", p->dev->name);
877         rtnl_unlock();
878         p->config |= OFPPC_PORT_DOWN;
879 }
880
881 /* Callback function for a workqueue to enable an interface */
882 static void
883 up_port_cb(struct work_struct *work)
884 {
885         struct net_bridge_port *p = container_of(work, struct net_bridge_port, 
886                         port_task);
887
888         rtnl_lock();
889         if (dev_change_flags(p->dev, p->dev->flags | IFF_UP) < 0)
890                 if (net_ratelimit())
891                         printk("problem bringing down port %s\n", p->dev->name);
892         rtnl_unlock();
893         p->config &= ~OFPPC_PORT_DOWN;
894 }
895
896 int
897 dp_update_port_flags(struct datapath *dp, const struct ofp_port_mod *opm)
898 {
899         unsigned long int flags;
900         int port_no = ntohs(opm->port_no);
901         struct net_bridge_port *p = (port_no < OFPP_MAX ? dp->ports[port_no]
902                                      : port_no == OFPP_LOCAL ? dp->local_port
903                                      : NULL);
904
905         /* Make sure the port id hasn't changed since this was sent */
906         if (!p || memcmp(opm->hw_addr, p->dev->dev_addr, ETH_ALEN))
907                 return -1;
908
909         spin_lock_irqsave(&p->lock, flags);
910         if (opm->mask) {
911                 uint32_t config_mask = ntohl(opm->mask);
912                 p->config &= ~config_mask;
913                 p->config |= ntohl(opm->config) & config_mask;
914         }
915
916         /* Modifying the status of an interface requires taking a lock
917          * that cannot be done from here.  For this reason, we use a shared 
918          * workqueue, which will cause it to be executed from a safer 
919          * context. */
920         if (opm->mask & htonl(OFPPC_PORT_DOWN)) {
921                 if ((opm->config & htonl(OFPPC_PORT_DOWN))
922                     && (p->config & OFPPC_PORT_DOWN) == 0) {
923                         PREPARE_WORK(&p->port_task, down_port_cb);
924                         schedule_work(&p->port_task);
925                 } else if ((opm->config & htonl(OFPPC_PORT_DOWN)) == 0
926                            && (p->config & OFPPC_PORT_DOWN)) {
927                         PREPARE_WORK(&p->port_task, up_port_cb);
928                         schedule_work(&p->port_task);
929                 }
930         }
931         spin_unlock_irqrestore(&p->lock, flags);
932
933         return 0;
934 }
935
936 /* Initialize the port status field of the bridge port. */
937 static void
938 init_port_status(struct net_bridge_port *p)
939 {
940         unsigned long int flags;
941
942         spin_lock_irqsave(&p->lock, flags);
943
944         if (p->dev->flags & IFF_UP) 
945                 p->config &= ~OFPPC_PORT_DOWN;
946         else
947                 p->config |= OFPPC_PORT_DOWN;
948
949         if (netif_carrier_ok(p->dev))
950                 p->state &= ~OFPPS_LINK_DOWN;
951         else
952                 p->state |= OFPPS_LINK_DOWN;
953
954         spin_unlock_irqrestore(&p->lock, flags);
955 }
956
957 int
958 dp_send_port_status(struct net_bridge_port *p, uint8_t status)
959 {
960         struct sk_buff *skb;
961         struct ofp_port_status *ops;
962
963         ops = alloc_openflow_skb(p->dp, sizeof *ops, OFPT_PORT_STATUS, NULL,
964                                  &skb);
965         if (!ops)
966                 return -ENOMEM;
967         ops->reason = status;
968         memset(ops->pad, 0, sizeof ops->pad);
969         fill_port_desc(p, &ops->desc);
970
971         return send_openflow_skb(skb, NULL);
972 }
973
974 int 
975 dp_send_flow_expired(struct datapath *dp, struct sw_flow *flow,
976                      enum ofp_flow_expired_reason reason)
977 {
978         struct sk_buff *skb;
979         struct ofp_flow_expired *ofe;
980
981         if (!(dp->flags & OFPC_SEND_FLOW_EXP))
982                 return 0;
983
984         ofe = alloc_openflow_skb(dp, sizeof *ofe, OFPT_FLOW_EXPIRED, 0, &skb);
985         if (!ofe)
986                 return -ENOMEM;
987
988         flow_fill_match(&ofe->match, &flow->key);
989
990         ofe->priority = htons(flow->priority);
991         ofe->reason = reason;
992         memset(ofe->pad, 0, sizeof ofe->pad);
993
994         ofe->duration     = htonl((jiffies - flow->init_time) / HZ);
995         memset(ofe->pad2, 0, sizeof ofe->pad2);
996         ofe->packet_count = cpu_to_be64(flow->packet_count);
997         ofe->byte_count   = cpu_to_be64(flow->byte_count);
998
999         return send_openflow_skb(skb, NULL);
1000 }
1001 EXPORT_SYMBOL(dp_send_flow_expired);
1002
1003 int
1004 dp_send_error_msg(struct datapath *dp, const struct sender *sender, 
1005                 uint16_t type, uint16_t code, const void *data, size_t len)
1006 {
1007         struct sk_buff *skb;
1008         struct ofp_error_msg *oem;
1009
1010
1011         oem = alloc_openflow_skb(dp, sizeof(*oem)+len, OFPT_ERROR, 
1012                         sender, &skb);
1013         if (!oem)
1014                 return -ENOMEM;
1015
1016         oem->type = htons(type);
1017         oem->code = htons(code);
1018         memcpy(oem->data, data, len);
1019
1020         return send_openflow_skb(skb, sender);
1021 }
1022
1023 int
1024 dp_send_echo_reply(struct datapath *dp, const struct sender *sender,
1025                    const struct ofp_header *rq)
1026 {
1027         struct sk_buff *skb;
1028         struct ofp_header *reply;
1029
1030         reply = alloc_openflow_skb(dp, ntohs(rq->length), OFPT_ECHO_REPLY,
1031                                    sender, &skb);
1032         if (!reply)
1033                 return -ENOMEM;
1034
1035         memcpy(reply + 1, rq + 1, ntohs(rq->length) - sizeof *rq);
1036         return send_openflow_skb(skb, sender);
1037 }
1038
1039 /* Generic Netlink interface.
1040  *
1041  * See netlink(7) for an introduction to netlink.  See
1042  * http://linux-net.osdl.org/index.php/Netlink for more information and
1043  * pointers on how to work with netlink and Generic Netlink in the kernel and
1044  * in userspace. */
1045
1046 static struct genl_family dp_genl_family = {
1047         .id = GENL_ID_GENERATE,
1048         .hdrsize = 0,
1049         .name = DP_GENL_FAMILY_NAME,
1050         .version = 1,
1051         .maxattr = DP_GENL_A_MAX,
1052 };
1053
1054 /* Attribute policy: what each attribute may contain.  */
1055 static struct nla_policy dp_genl_policy[DP_GENL_A_MAX + 1] = {
1056         [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1057         [DP_GENL_A_MC_GROUP] = { .type = NLA_U32 },
1058         [DP_GENL_A_PORTNAME] = { .type = NLA_STRING }
1059 };
1060
1061 static int dp_genl_add(struct sk_buff *skb, struct genl_info *info)
1062 {
1063         if (!info->attrs[DP_GENL_A_DP_IDX])
1064                 return -EINVAL;
1065
1066         return new_dp(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1067 }
1068
1069 static struct genl_ops dp_genl_ops_add_dp = {
1070         .cmd = DP_GENL_C_ADD_DP,
1071         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1072         .policy = dp_genl_policy,
1073         .doit = dp_genl_add,
1074         .dumpit = NULL,
1075 };
1076
1077 struct datapath *dp_get(int dp_idx)
1078 {
1079         if (dp_idx < 0 || dp_idx > DP_MAX)
1080                 return NULL;
1081         return rcu_dereference(dps[dp_idx]);
1082 }
1083
1084 static int dp_genl_del(struct sk_buff *skb, struct genl_info *info)
1085 {
1086         struct datapath *dp;
1087         int err;
1088
1089         if (!info->attrs[DP_GENL_A_DP_IDX])
1090                 return -EINVAL;
1091
1092         dp = dp_get(nla_get_u32((info->attrs[DP_GENL_A_DP_IDX])));
1093         if (!dp)
1094                 err = -ENOENT;
1095         else {
1096                 del_dp(dp);
1097                 err = 0;
1098         }
1099         return err;
1100 }
1101
1102 static struct genl_ops dp_genl_ops_del_dp = {
1103         .cmd = DP_GENL_C_DEL_DP,
1104         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1105         .policy = dp_genl_policy,
1106         .doit = dp_genl_del,
1107         .dumpit = NULL,
1108 };
1109
1110 /* Queries a datapath for related information.  Currently the only relevant
1111  * information is the datapath's multicast group ID.  Really we want one
1112  * multicast group per datapath, but because of locking issues[*] we can't
1113  * easily get one.  Thus, every datapath will currently return the same
1114  * global multicast group ID, but in the future it would be nice to fix that.
1115  *
1116  * [*] dp_genl_add, to add a new datapath, is called under the genl_lock
1117  *       mutex, and genl_register_mc_group, called to acquire a new multicast
1118  *       group ID, also acquires genl_lock, thus deadlock.
1119  */
1120 static int dp_genl_query(struct sk_buff *skb, struct genl_info *info)
1121 {
1122         struct datapath *dp;
1123         struct sk_buff *ans_skb = NULL;
1124         int dp_idx;
1125         int err = -ENOMEM;
1126
1127         if (!info->attrs[DP_GENL_A_DP_IDX])
1128                 return -EINVAL;
1129
1130         rcu_read_lock();
1131         dp_idx = nla_get_u32((info->attrs[DP_GENL_A_DP_IDX]));
1132         dp = dp_get(dp_idx);
1133         if (!dp)
1134                 err = -ENOENT;
1135         else {
1136                 void *data;
1137                 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1138                 if (!ans_skb) {
1139                         err = -ENOMEM;
1140                         goto err;
1141                 }
1142                 data = genlmsg_put_reply(ans_skb, info, &dp_genl_family,
1143                                          0, DP_GENL_C_QUERY_DP);
1144                 if (data == NULL) {
1145                         err = -ENOMEM;
1146                         goto err;
1147                 }
1148                 NLA_PUT_U32(ans_skb, DP_GENL_A_DP_IDX, dp_idx);
1149                 NLA_PUT_U32(ans_skb, DP_GENL_A_MC_GROUP, mc_group.id);
1150
1151                 genlmsg_end(ans_skb, data);
1152                 err = genlmsg_reply(ans_skb, info);
1153                 if (!err)
1154                         ans_skb = NULL;
1155         }
1156 err:
1157 nla_put_failure:
1158         if (ans_skb)
1159                 kfree_skb(ans_skb);
1160         rcu_read_unlock();
1161         return err;
1162 }
1163
1164 static struct genl_ops dp_genl_ops_query_dp = {
1165         .cmd = DP_GENL_C_QUERY_DP,
1166         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1167         .policy = dp_genl_policy,
1168         .doit = dp_genl_query,
1169         .dumpit = NULL,
1170 };
1171
1172 static int dp_genl_add_del_port(struct sk_buff *skb, struct genl_info *info)
1173 {
1174         struct datapath *dp;
1175         struct net_device *port;
1176         int err;
1177
1178         if (!info->attrs[DP_GENL_A_DP_IDX] || !info->attrs[DP_GENL_A_PORTNAME])
1179                 return -EINVAL;
1180
1181         /* Get datapath. */
1182         dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1183         if (!dp) {
1184                 err = -ENOENT;
1185                 goto out;
1186         }
1187
1188         /* Get interface to add/remove. */
1189         port = dev_get_by_name(&init_net, 
1190                         nla_data(info->attrs[DP_GENL_A_PORTNAME]));
1191         if (!port) {
1192                 err = -ENOENT;
1193                 goto out;
1194         }
1195
1196         /* Execute operation. */
1197         if (info->genlhdr->cmd == DP_GENL_C_ADD_PORT)
1198                 err = add_switch_port(dp, port);
1199         else {
1200                 if (port->br_port == NULL || port->br_port->dp != dp) {
1201                         err = -ENOENT;
1202                         goto out_put;
1203                 }
1204                 err = dp_del_switch_port(port->br_port);
1205         }
1206
1207 out_put:
1208         dev_put(port);
1209 out:
1210         return err;
1211 }
1212
1213 static struct genl_ops dp_genl_ops_add_port = {
1214         .cmd = DP_GENL_C_ADD_PORT,
1215         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1216         .policy = dp_genl_policy,
1217         .doit = dp_genl_add_del_port,
1218         .dumpit = NULL,
1219 };
1220
1221 static struct genl_ops dp_genl_ops_del_port = {
1222         .cmd = DP_GENL_C_DEL_PORT,
1223         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1224         .policy = dp_genl_policy,
1225         .doit = dp_genl_add_del_port,
1226         .dumpit = NULL,
1227 };
1228
1229 static int dp_genl_openflow(struct sk_buff *skb, struct genl_info *info)
1230 {
1231         struct nlattr *va = info->attrs[DP_GENL_A_OPENFLOW];
1232         struct datapath *dp;
1233         struct ofp_header *oh;
1234         struct sender sender;
1235         int err;
1236
1237         if (!info->attrs[DP_GENL_A_DP_IDX] || !va)
1238                 return -EINVAL;
1239
1240         dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1241         if (!dp)
1242                 return -ENOENT;
1243
1244         if (nla_len(va) < sizeof(struct ofp_header))
1245                 return -EINVAL;
1246         oh = nla_data(va);
1247
1248         sender.xid = oh->xid;
1249         sender.pid = info->snd_pid;
1250         sender.seq = info->snd_seq;
1251
1252         mutex_lock(&dp_mutex);
1253         err = fwd_control_input(dp->chain, &sender,
1254                                 nla_data(va), nla_len(va));
1255         mutex_unlock(&dp_mutex);
1256         return err;
1257 }
1258
1259 static struct nla_policy dp_genl_openflow_policy[DP_GENL_A_MAX + 1] = {
1260         [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1261 };
1262
1263 static int desc_stats_dump(struct datapath *dp, void *state,
1264                             void *body, int *body_len)
1265 {
1266         struct ofp_desc_stats *ods = body;
1267         int n_bytes = sizeof *ods;
1268
1269         if (n_bytes > *body_len) {
1270                 return -ENOBUFS;
1271         }
1272         *body_len = n_bytes;
1273
1274         strncpy(ods->mfr_desc, mfr_desc, sizeof ods->mfr_desc);
1275         strncpy(ods->hw_desc, hw_desc, sizeof ods->hw_desc);
1276         strncpy(ods->sw_desc, sw_desc, sizeof ods->sw_desc);
1277         strncpy(ods->serial_num, serial_num, sizeof ods->serial_num);
1278
1279         return 0;
1280 }
1281
1282 struct flow_stats_state {
1283         int table_idx;
1284         struct sw_table_position position;
1285         const struct ofp_flow_stats_request *rq;
1286
1287         void *body;
1288         int bytes_used, bytes_allocated;
1289 };
1290
1291 static int flow_stats_init(struct datapath *dp, const void *body, int body_len,
1292                            void **state)
1293 {
1294         const struct ofp_flow_stats_request *fsr = body;
1295         struct flow_stats_state *s = kmalloc(sizeof *s, GFP_ATOMIC);
1296         if (!s)
1297                 return -ENOMEM;
1298         s->table_idx = fsr->table_id == 0xff ? 0 : fsr->table_id;
1299         memset(&s->position, 0, sizeof s->position);
1300         s->rq = fsr;
1301         *state = s;
1302         return 0;
1303 }
1304
1305 static int flow_stats_dump_callback(struct sw_flow *flow, void *private)
1306 {
1307         struct sw_flow_actions *sf_acts = rcu_dereference(flow->sf_acts);
1308         struct flow_stats_state *s = private;
1309         struct ofp_flow_stats *ofs;
1310         int length;
1311
1312         length = sizeof *ofs + sf_acts->actions_len;
1313         if (length + s->bytes_used > s->bytes_allocated)
1314                 return 1;
1315
1316         ofs = s->body + s->bytes_used;
1317         ofs->length          = htons(length);
1318         ofs->table_id        = s->table_idx;
1319         ofs->pad             = 0;
1320         ofs->match.wildcards = htonl(flow->key.wildcards);
1321         ofs->match.in_port   = flow->key.in_port;
1322         memcpy(ofs->match.dl_src, flow->key.dl_src, ETH_ALEN);
1323         memcpy(ofs->match.dl_dst, flow->key.dl_dst, ETH_ALEN);
1324         ofs->match.dl_vlan   = flow->key.dl_vlan;
1325         ofs->match.dl_type   = flow->key.dl_type;
1326         ofs->match.nw_src    = flow->key.nw_src;
1327         ofs->match.nw_dst    = flow->key.nw_dst;
1328         ofs->match.nw_proto  = flow->key.nw_proto;
1329         ofs->match.pad       = 0;
1330         ofs->match.tp_src    = flow->key.tp_src;
1331         ofs->match.tp_dst    = flow->key.tp_dst;
1332         ofs->duration        = htonl((jiffies - flow->init_time) / HZ);
1333         ofs->priority        = htons(flow->priority);
1334         ofs->idle_timeout    = htons(flow->idle_timeout);
1335         ofs->hard_timeout    = htons(flow->hard_timeout);
1336         memset(ofs->pad2, 0, sizeof ofs->pad2);
1337         ofs->packet_count    = cpu_to_be64(flow->packet_count);
1338         ofs->byte_count      = cpu_to_be64(flow->byte_count);
1339         memcpy(ofs->actions, sf_acts->actions, sf_acts->actions_len);
1340
1341         s->bytes_used += length;
1342         return 0;
1343 }
1344
1345 static int flow_stats_dump(struct datapath *dp, void *state,
1346                            void *body, int *body_len)
1347 {
1348         struct flow_stats_state *s = state;
1349         struct sw_flow_key match_key;
1350         int error = 0;
1351
1352         s->bytes_used = 0;
1353         s->bytes_allocated = *body_len;
1354         s->body = body;
1355
1356         flow_extract_match(&match_key, &s->rq->match);
1357         while (s->table_idx < dp->chain->n_tables
1358                && (s->rq->table_id == 0xff || s->rq->table_id == s->table_idx))
1359         {
1360                 struct sw_table *table = dp->chain->tables[s->table_idx];
1361
1362                 error = table->iterate(table, &match_key, &s->position,
1363                                        flow_stats_dump_callback, s);
1364                 if (error)
1365                         break;
1366
1367                 s->table_idx++;
1368                 memset(&s->position, 0, sizeof s->position);
1369         }
1370         *body_len = s->bytes_used;
1371
1372         /* If error is 0, we're done.
1373          * Otherwise, if some bytes were used, there are more flows to come.
1374          * Otherwise, we were not able to fit even a single flow in the body,
1375          * which indicates that we have a single flow with too many actions to
1376          * fit.  We won't ever make any progress at that rate, so give up. */
1377         return !error ? 0 : s->bytes_used ? 1 : -ENOMEM;
1378 }
1379
1380 static void flow_stats_done(void *state)
1381 {
1382         kfree(state);
1383 }
1384
1385 static int aggregate_stats_init(struct datapath *dp,
1386                                 const void *body, int body_len,
1387                                 void **state)
1388 {
1389         *state = (void *)body;
1390         return 0;
1391 }
1392
1393 static int aggregate_stats_dump_callback(struct sw_flow *flow, void *private)
1394 {
1395         struct ofp_aggregate_stats_reply *rpy = private;
1396         rpy->packet_count += flow->packet_count;
1397         rpy->byte_count += flow->byte_count;
1398         rpy->flow_count++;
1399         return 0;
1400 }
1401
1402 static int aggregate_stats_dump(struct datapath *dp, void *state,
1403                                 void *body, int *body_len)
1404 {
1405         struct ofp_aggregate_stats_request *rq = state;
1406         struct ofp_aggregate_stats_reply *rpy;
1407         struct sw_table_position position;
1408         struct sw_flow_key match_key;
1409         int table_idx;
1410
1411         if (*body_len < sizeof *rpy)
1412                 return -ENOBUFS;
1413         rpy = body;
1414         *body_len = sizeof *rpy;
1415
1416         memset(rpy, 0, sizeof *rpy);
1417
1418         flow_extract_match(&match_key, &rq->match);
1419         table_idx = rq->table_id == 0xff ? 0 : rq->table_id;
1420         memset(&position, 0, sizeof position);
1421         while (table_idx < dp->chain->n_tables
1422                && (rq->table_id == 0xff || rq->table_id == table_idx))
1423         {
1424                 struct sw_table *table = dp->chain->tables[table_idx];
1425                 int error;
1426
1427                 error = table->iterate(table, &match_key, &position,
1428                                        aggregate_stats_dump_callback, rpy);
1429                 if (error)
1430                         return error;
1431
1432                 table_idx++;
1433                 memset(&position, 0, sizeof position);
1434         }
1435
1436         rpy->packet_count = cpu_to_be64(rpy->packet_count);
1437         rpy->byte_count = cpu_to_be64(rpy->byte_count);
1438         rpy->flow_count = htonl(rpy->flow_count);
1439         return 0;
1440 }
1441
1442 static int table_stats_dump(struct datapath *dp, void *state,
1443                             void *body, int *body_len)
1444 {
1445         struct ofp_table_stats *ots;
1446         int n_bytes = dp->chain->n_tables * sizeof *ots;
1447         int i;
1448         if (n_bytes > *body_len)
1449                 return -ENOBUFS;
1450         *body_len = n_bytes;
1451         for (i = 0, ots = body; i < dp->chain->n_tables; i++, ots++) {
1452                 struct sw_table_stats stats;
1453                 dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
1454                 strncpy(ots->name, stats.name, sizeof ots->name);
1455                 ots->table_id = i;
1456                 ots->wildcards = htonl(stats.wildcards);
1457                 memset(ots->pad, 0, sizeof ots->pad);
1458                 ots->max_entries = htonl(stats.max_flows);
1459                 ots->active_count = htonl(stats.n_flows);
1460                 ots->lookup_count = cpu_to_be64(stats.n_lookup);
1461                 ots->matched_count = cpu_to_be64(stats.n_matched);
1462         }
1463         return 0;
1464 }
1465
1466 struct port_stats_state {
1467         int port;
1468 };
1469
1470 static int port_stats_init(struct datapath *dp, const void *body, int body_len,
1471                            void **state)
1472 {
1473         struct port_stats_state *s = kmalloc(sizeof *s, GFP_ATOMIC);
1474         if (!s)
1475                 return -ENOMEM;
1476         s->port = 0;
1477         *state = s;
1478         return 0;
1479 }
1480
1481 static int port_stats_dump(struct datapath *dp, void *state,
1482                            void *body, int *body_len)
1483 {
1484         struct port_stats_state *s = state;
1485         struct ofp_port_stats *ops;
1486         int n_ports, max_ports;
1487         int i;
1488
1489         max_ports = *body_len / sizeof *ops;
1490         if (!max_ports)
1491                 return -ENOMEM;
1492         ops = body;
1493
1494         n_ports = 0;
1495         for (i = s->port; i < OFPP_MAX && n_ports < max_ports; i++) {
1496                 struct net_bridge_port *p = dp->ports[i];
1497                 struct net_device_stats *stats;
1498                 if (!p)
1499                         continue;
1500                 stats = p->dev->get_stats(p->dev);
1501                 ops->port_no = htons(p->port_no);
1502                 memset(ops->pad, 0, sizeof ops->pad);
1503                 ops->rx_packets   = cpu_to_be64(stats->rx_packets);
1504                 ops->tx_packets   = cpu_to_be64(stats->tx_packets);
1505                 ops->rx_bytes     = cpu_to_be64(stats->rx_bytes);
1506                 ops->tx_bytes     = cpu_to_be64(stats->tx_bytes);
1507                 ops->rx_dropped   = cpu_to_be64(stats->rx_dropped);
1508                 ops->tx_dropped   = cpu_to_be64(stats->tx_dropped);
1509                 ops->rx_errors    = cpu_to_be64(stats->rx_errors);
1510                 ops->tx_errors    = cpu_to_be64(stats->tx_errors);
1511                 ops->rx_frame_err = cpu_to_be64(stats->rx_frame_errors);
1512                 ops->rx_over_err  = cpu_to_be64(stats->rx_over_errors);
1513                 ops->rx_crc_err   = cpu_to_be64(stats->rx_crc_errors);
1514                 ops->collisions   = cpu_to_be64(stats->collisions);
1515                 n_ports++;
1516                 ops++;
1517         }
1518         s->port = i;
1519         *body_len = n_ports * sizeof *ops;
1520         return n_ports >= max_ports;
1521 }
1522
1523 static void port_stats_done(void *state)
1524 {
1525         kfree(state);
1526 }
1527
1528 struct stats_type {
1529         /* Minimum and maximum acceptable number of bytes in body member of
1530          * struct ofp_stats_request. */
1531         size_t min_body, max_body;
1532
1533         /* Prepares to dump some kind of statistics on 'dp'.  'body' and
1534          * 'body_len' are the 'body' member of the struct ofp_stats_request.
1535          * Returns zero if successful, otherwise a negative error code.
1536          * May initialize '*state' to state information.  May be null if no
1537          * initialization is required.*/
1538         int (*init)(struct datapath *dp, const void *body, int body_len,
1539                     void **state);
1540
1541         /* Dumps statistics for 'dp' into the '*body_len' bytes at 'body', and
1542          * modifies '*body_len' to reflect the number of bytes actually used.
1543          * ('body' will be transmitted as the 'body' member of struct
1544          * ofp_stats_reply.) */
1545         int (*dump)(struct datapath *dp, void *state,
1546                     void *body, int *body_len);
1547
1548         /* Cleans any state created by the init or dump functions.  May be null
1549          * if no cleanup is required. */
1550         void (*done)(void *state);
1551 };
1552
1553 static const struct stats_type stats[] = {
1554         [OFPST_DESC] = {
1555                 0,
1556                 0,
1557                 NULL,
1558                 desc_stats_dump,
1559                 NULL
1560         },
1561         [OFPST_FLOW] = {
1562                 sizeof(struct ofp_flow_stats_request),
1563                 sizeof(struct ofp_flow_stats_request),
1564                 flow_stats_init,
1565                 flow_stats_dump,
1566                 flow_stats_done
1567         },
1568         [OFPST_AGGREGATE] = {
1569                 sizeof(struct ofp_aggregate_stats_request),
1570                 sizeof(struct ofp_aggregate_stats_request),
1571                 aggregate_stats_init,
1572                 aggregate_stats_dump,
1573                 NULL
1574         },
1575         [OFPST_TABLE] = {
1576                 0,
1577                 0,
1578                 NULL,
1579                 table_stats_dump,
1580                 NULL
1581         },
1582         [OFPST_PORT] = {
1583                 0,
1584                 0,
1585                 port_stats_init,
1586                 port_stats_dump,
1587                 port_stats_done
1588         },
1589 };
1590
1591 static int
1592 dp_genl_openflow_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
1593 {
1594         struct datapath *dp;
1595         struct sender sender;
1596         const struct stats_type *s;
1597         struct ofp_stats_reply *osr;
1598         int dp_idx;
1599         int max_openflow_len, body_len;
1600         void *body;
1601         int err;
1602
1603         /* Set up the cleanup function for this dump.  Linux 2.6.20 and later
1604          * support setting up cleanup functions via the .doneit member of
1605          * struct genl_ops.  This kluge supports earlier versions also. */
1606         cb->done = dp_genl_openflow_done;
1607
1608         sender.pid = NETLINK_CB(cb->skb).pid;
1609         sender.seq = cb->nlh->nlmsg_seq;
1610         if (!cb->args[0]) {
1611                 struct nlattr *attrs[DP_GENL_A_MAX + 1];
1612                 struct ofp_stats_request *rq;
1613                 struct nlattr *va;
1614                 size_t len, body_len;
1615                 int type;
1616
1617                 err = nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs, DP_GENL_A_MAX,
1618                                   dp_genl_openflow_policy);
1619                 if (err < 0)
1620                         return err;
1621
1622                 if (!attrs[DP_GENL_A_DP_IDX])
1623                         return -EINVAL;
1624                 dp_idx = nla_get_u16(attrs[DP_GENL_A_DP_IDX]);
1625                 dp = dp_get(dp_idx);
1626                 if (!dp)
1627                         return -ENOENT;
1628
1629                 va = attrs[DP_GENL_A_OPENFLOW];
1630                 len = nla_len(va);
1631                 if (!va || len < sizeof *rq)
1632                         return -EINVAL;
1633
1634                 rq = nla_data(va);
1635                 sender.xid = rq->header.xid;
1636                 type = ntohs(rq->type);
1637                 if (rq->header.version != OFP_VERSION) {
1638                         dp_send_error_msg(dp, &sender, OFPET_BAD_REQUEST,
1639                                           OFPBRC_BAD_VERSION, rq, len);
1640                         return -EINVAL;
1641                 }
1642                 if (rq->header.type != OFPT_STATS_REQUEST
1643                     || ntohs(rq->header.length) != len)
1644                         return -EINVAL;
1645
1646                 if (type >= ARRAY_SIZE(stats) || !stats[type].dump) {
1647                         dp_send_error_msg(dp, &sender, OFPET_BAD_REQUEST,
1648                                           OFPBRC_BAD_STAT, rq, len);
1649                         return -EINVAL;
1650                 }
1651
1652                 s = &stats[type];
1653                 body_len = len - offsetof(struct ofp_stats_request, body);
1654                 if (body_len < s->min_body || body_len > s->max_body)
1655                         return -EINVAL;
1656
1657                 cb->args[0] = 1;
1658                 cb->args[1] = dp_idx;
1659                 cb->args[2] = type;
1660                 cb->args[3] = rq->header.xid;
1661                 if (s->init) {
1662                         void *state;
1663                         err = s->init(dp, rq->body, body_len, &state);
1664                         if (err)
1665                                 return err;
1666                         cb->args[4] = (long) state;
1667                 }
1668         } else if (cb->args[0] == 1) {
1669                 sender.xid = cb->args[3];
1670                 dp_idx = cb->args[1];
1671                 s = &stats[cb->args[2]];
1672
1673                 dp = dp_get(dp_idx);
1674                 if (!dp)
1675                         return -ENOENT;
1676         } else {
1677                 return 0;
1678         }
1679
1680         osr = put_openflow_headers(dp, skb, OFPT_STATS_REPLY, &sender,
1681                                    &max_openflow_len);
1682         if (IS_ERR(osr))
1683                 return PTR_ERR(osr);
1684         osr->type = htons(s - stats);
1685         osr->flags = 0;
1686         resize_openflow_skb(skb, &osr->header, max_openflow_len);
1687         body = osr->body;
1688         body_len = max_openflow_len - offsetof(struct ofp_stats_reply, body);
1689
1690         err = s->dump(dp, (void *) cb->args[4], body, &body_len);
1691         if (err >= 0) {
1692                 if (!err)
1693                         cb->args[0] = 2;
1694                 else
1695                         osr->flags = ntohs(OFPSF_REPLY_MORE);
1696                 resize_openflow_skb(skb, &osr->header,
1697                                     (offsetof(struct ofp_stats_reply, body)
1698                                      + body_len));
1699                 err = skb->len;
1700         }
1701
1702         return err;
1703 }
1704
1705 static int
1706 dp_genl_openflow_done(struct netlink_callback *cb)
1707 {
1708         if (cb->args[0]) {
1709                 const struct stats_type *s = &stats[cb->args[2]];
1710                 if (s->done)
1711                         s->done((void *) cb->args[4]);
1712         }
1713         return 0;
1714 }
1715
1716 static struct genl_ops dp_genl_ops_openflow = {
1717         .cmd = DP_GENL_C_OPENFLOW,
1718         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1719         .policy = dp_genl_openflow_policy,
1720         .doit = dp_genl_openflow,
1721         .dumpit = dp_genl_openflow_dumpit,
1722 };
1723
1724 static struct genl_ops *dp_genl_all_ops[] = {
1725         /* Keep this operation first.  Generic Netlink dispatching
1726          * looks up operations with linear search, so we want it at the
1727          * front. */
1728         &dp_genl_ops_openflow,
1729
1730         &dp_genl_ops_add_dp,
1731         &dp_genl_ops_del_dp,
1732         &dp_genl_ops_query_dp,
1733         &dp_genl_ops_add_port,
1734         &dp_genl_ops_del_port,
1735 };
1736
1737 static int dp_init_netlink(void)
1738 {
1739         int err;
1740         int i;
1741
1742         err = genl_register_family(&dp_genl_family);
1743         if (err)
1744                 return err;
1745
1746         for (i = 0; i < ARRAY_SIZE(dp_genl_all_ops); i++) {
1747                 err = genl_register_ops(&dp_genl_family, dp_genl_all_ops[i]);
1748                 if (err)
1749                         goto err_unregister;
1750         }
1751
1752         strcpy(mc_group.name, "openflow");
1753         err = genl_register_mc_group(&dp_genl_family, &mc_group);
1754         if (err < 0)
1755                 goto err_unregister;
1756
1757         return 0;
1758
1759 err_unregister:
1760         genl_unregister_family(&dp_genl_family);
1761                 return err;
1762 }
1763
1764 static void dp_uninit_netlink(void)
1765 {
1766         genl_unregister_family(&dp_genl_family);
1767 }
1768
1769 static int __init dp_init(void)
1770 {
1771         int err;
1772
1773         printk("OpenFlow "VERSION", built "__DATE__" "__TIME__", "
1774                "protocol 0x%02x\n", OFP_VERSION);
1775
1776         err = flow_init();
1777         if (err)
1778                 goto error;
1779
1780         err = register_netdevice_notifier(&dp_device_notifier);
1781         if (err)
1782                 goto error_flow_exit;
1783
1784         err = dp_init_netlink();
1785         if (err)
1786                 goto error_unreg_notifier;
1787
1788         /* Hook into callback used by the bridge to intercept packets.
1789          * Parasites we are. */
1790         if (br_handle_frame_hook)
1791                 printk("openflow: hijacking bridge hook\n");
1792         br_handle_frame_hook = dp_frame_hook;
1793
1794         return 0;
1795
1796 error_unreg_notifier:
1797         unregister_netdevice_notifier(&dp_device_notifier);
1798 error_flow_exit:
1799         flow_exit();
1800 error:
1801         printk(KERN_EMERG "openflow: failed to install!");
1802         return err;
1803 }
1804
1805 static void dp_cleanup(void)
1806 {
1807         fwd_exit();
1808         dp_uninit_netlink();
1809         unregister_netdevice_notifier(&dp_device_notifier);
1810         flow_exit();
1811         br_handle_frame_hook = NULL;
1812 }
1813
1814 module_init(dp_init);
1815 module_exit(dp_cleanup);
1816
1817 MODULE_DESCRIPTION("OpenFlow switching datapath");
1818 MODULE_AUTHOR("Copyright (c) 2007, 2008 The Board of Trustees of The Leland Stanford Junior University");
1819 MODULE_LICENSE("GPL");