Break data_hello and control_hello messages into multiple messages.
[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/module.h>
10 #include <linux/if_arp.h>
11 #include <linux/if_bridge.h>
12 #include <linux/if_vlan.h>
13 #include <linux/in.h>
14 #include <net/genetlink.h>
15 #include <linux/ip.h>
16 #include <linux/delay.h>
17 #include <linux/etherdevice.h>
18 #include <linux/kernel.h>
19 #include <linux/kthread.h>
20 #include <linux/mutex.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/rcupdate.h>
23 #include <linux/version.h>
24 #include <linux/ethtool.h>
25 #include <linux/random.h>
26 #include <asm/system.h>
27 #include <linux/netfilter_bridge.h>
28 #include <linux/inetdevice.h>
29 #include <linux/list.h>
30
31 #include "openflow-netlink.h"
32 #include "datapath.h"
33 #include "table.h"
34 #include "chain.h"
35 #include "forward.h"
36 #include "flow.h"
37 #include "datapath_t.h"
38
39 #include "compat.h"
40
41
42 /* Number of milliseconds between runs of the maintenance thread. */
43 #define MAINT_SLEEP_MSECS 1000
44
45 #define BRIDGE_PORT_NO_FLOOD    0x00000001 
46
47 #define UINT32_MAX                        4294967295U
48 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
49
50 struct net_bridge_port {
51         u16     port_no;
52         u32 flags;
53         struct datapath *dp;
54         struct net_device *dev;
55         struct list_head node; /* Element in datapath.ports. */
56 };
57
58 static struct genl_family dp_genl_family;
59 static struct genl_multicast_group mc_group;
60
61 int dp_dev_setup(struct net_device *dev);  
62
63 /* It's hard to imagine wanting more than one datapath, but... */
64 #define DP_MAX 32
65
66 /* datapaths.  Protected on the read side by rcu_read_lock, on the write side
67  * by dp_mutex.
68  *
69  * It is safe to access the datapath and net_bridge_port structures with just
70  * the dp_mutex, but to access the chain you need to take the rcu_read_lock
71  * also (because dp_mutex doesn't prevent flows from being destroyed).
72  */
73 static struct datapath *dps[DP_MAX];
74 static DEFINE_MUTEX(dp_mutex);
75
76 static int dp_maint_func(void *data);
77 static int send_port_status(struct net_bridge_port *p, uint8_t status);
78
79
80 /* nla_unreserve - reduce amount of space reserved by nla_reserve  
81  * @skb: socket buffer from which to recover room
82  * @nla: netlink attribute to adjust
83  * @len: amount by which to reduce attribute payload
84  *
85  * Reduces amount of space reserved by a call to nla_reserve.
86  *
87  * No other attributes may be added between calling nla_reserve and this
88  * function, since it will create a hole in the message.
89  */
90 void nla_unreserve(struct sk_buff *skb, struct nlattr *nla, int len)
91 {
92         skb->tail -= len;
93         skb->len  -= len;
94
95         nla->nla_len -= len;
96 }
97
98 static void *
99 alloc_openflow_skb(struct datapath *dp, size_t openflow_len,
100                    uint8_t type, uint32_t xid, struct sk_buff **pskb) 
101 {
102         size_t genl_len;
103         struct sk_buff *skb;
104         struct nlattr *attr;
105         struct ofp_header *oh;
106
107         genl_len = nla_total_size(sizeof(uint32_t)); /* DP_GENL_A_DP_IDX */
108         genl_len += nla_total_size(openflow_len);    /* DP_GENL_A_OPENFLOW */
109         skb = *pskb = genlmsg_new(genl_len, GFP_ATOMIC);
110         if (!skb) {
111                 if (net_ratelimit())
112                         printk("alloc_openflow_skb: genlmsg_new failed\n");
113                 return NULL;
114         }
115
116         /* Assemble the Generic Netlink wrapper. */
117         if (!genlmsg_put(skb, 0, 0, &dp_genl_family, 0, DP_GENL_C_OPENFLOW))
118                 BUG();
119         if (nla_put_u32(skb, DP_GENL_A_DP_IDX, dp->dp_idx) < 0)
120                 BUG();
121         attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, openflow_len);
122         BUG_ON(!attr);
123         nlmsg_end(skb, (struct nlmsghdr *) skb->data);
124
125         /* Fill in the header. */
126         oh = nla_data(attr);
127         oh->version = OFP_VERSION;
128         oh->type = type;
129         oh->length = htons(openflow_len);
130         oh->xid = xid;
131
132         return oh;
133 }
134
135 static void
136 resize_openflow_skb(struct sk_buff *skb,
137                     struct ofp_header *oh, size_t new_length)
138 {
139         struct nlattr *attr;
140
141         BUG_ON(new_length > ntohs(oh->length));
142         attr = ((void *) oh) - NLA_HDRLEN;
143         nla_unreserve(skb, attr, ntohs(oh->length) - new_length);
144         oh->length = htons(new_length);
145         nlmsg_end(skb, (struct nlmsghdr *) skb->data);
146 }
147
148 /* Generates a unique datapath id.  It incorporates the datapath index
149  * and a hardware address, if available.  If not, it generates a random
150  * one.
151  */
152 static 
153 uint64_t gen_datapath_id(uint16_t dp_idx)
154 {
155         uint64_t id;
156         int i;
157         struct net_device *dev;
158
159         /* The top 16 bits are used to identify the datapath.  The lower 48 bits
160          * use an interface address.  */
161         id = (uint64_t)dp_idx << 48;
162         if ((dev = dev_get_by_name(&init_net, "ctl0")) 
163                         || (dev = dev_get_by_name(&init_net, "eth0"))) {
164                 for (i=0; i<ETH_ALEN; i++) {
165                         id |= (uint64_t)dev->dev_addr[i] << (8*(ETH_ALEN-1 - i));
166                 }
167                 dev_put(dev);
168         } else {
169                 /* Randomly choose the lower 48 bits if we cannot find an
170                  * address and mark the most significant bit to indicate that
171                  * this was randomly generated. */
172                 uint8_t rand[ETH_ALEN];
173                 get_random_bytes(rand, ETH_ALEN);
174                 id |= (uint64_t)1 << 63;
175                 for (i=0; i<ETH_ALEN; i++) {
176                         id |= (uint64_t)rand[i] << (8*(ETH_ALEN-1 - i));
177                 }
178         }
179
180         return id;
181 }
182
183 /* Creates a new datapath numbered 'dp_idx'.  Returns 0 for success or a
184  * negative error code.
185  *
186  * Not called with any locks. */
187 static int new_dp(int dp_idx)
188 {
189         struct datapath *dp;
190         int err;
191
192         if (dp_idx < 0 || dp_idx >= DP_MAX)
193                 return -EINVAL;
194
195         if (!try_module_get(THIS_MODULE))
196                 return -ENODEV;
197
198         mutex_lock(&dp_mutex);
199         dp = rcu_dereference(dps[dp_idx]);
200         if (dp != NULL) {
201                 err = -EEXIST;
202                 goto err_unlock;
203         }
204
205         err = -ENOMEM;
206         dp = kzalloc(sizeof *dp, GFP_KERNEL);
207         if (dp == NULL)
208                 goto err_unlock;
209
210         dp->dp_idx = dp_idx;
211         dp->id = gen_datapath_id(dp_idx);
212         dp->chain = chain_create(dp);
213         if (dp->chain == NULL)
214                 goto err_free_dp;
215         INIT_LIST_HEAD(&dp->port_list);
216
217 #if 0
218         /* Setup our "of" device */
219         dp->dev.priv = dp;
220         rtnl_lock();
221         err = dp_dev_setup(&dp->dev);
222         rtnl_unlock();
223         if (err != 0) 
224                 printk("datapath: problem setting up 'of' device\n");
225 #endif
226
227         dp->config.flags = 0;
228         dp->config.miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
229
230         dp->dp_task = kthread_run(dp_maint_func, dp, "dp%d", dp_idx);
231         if (IS_ERR(dp->dp_task))
232                 goto err_free_dp;
233
234         rcu_assign_pointer(dps[dp_idx], dp);
235         mutex_unlock(&dp_mutex);
236
237         return 0;
238
239 err_free_dp:
240         kfree(dp);
241 err_unlock:
242         mutex_unlock(&dp_mutex);
243         module_put(THIS_MODULE);
244                 return err;
245 }
246
247 /* Find and return a free port number under 'dp'.  Called under dp_mutex. */
248 static int find_portno(struct datapath *dp)
249 {
250         int i;
251         for (i = 0; i < OFPP_MAX; i++)
252                 if (dp->ports[i] == NULL)
253                         return i;
254         return -EXFULL;
255 }
256
257 static struct net_bridge_port *new_nbp(struct datapath *dp,
258                                                                            struct net_device *dev)
259 {
260         struct net_bridge_port *p;
261         int port_no;
262
263         port_no = find_portno(dp);
264         if (port_no < 0)
265                 return ERR_PTR(port_no);
266
267         p = kzalloc(sizeof(*p), GFP_KERNEL);
268         if (p == NULL)
269                 return ERR_PTR(-ENOMEM);
270
271         p->dp = dp;
272         dev_hold(dev);
273         p->dev = dev;
274         p->port_no = port_no;
275
276         return p;
277 }
278
279 /* Called with dp_mutex. */
280 int add_switch_port(struct datapath *dp, struct net_device *dev)
281 {
282         struct net_bridge_port *p;
283
284         if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER)
285                 return -EINVAL;
286
287         if (dev->br_port != NULL)
288                 return -EBUSY;
289
290         p = new_nbp(dp, dev);
291         if (IS_ERR(p))
292                 return PTR_ERR(p);
293
294         dev_hold(dev);
295         rcu_assign_pointer(dev->br_port, p);
296         rtnl_lock();
297         dev_set_promiscuity(dev, 1);
298         rtnl_unlock();
299
300         rcu_assign_pointer(dp->ports[p->port_no], p);
301         list_add_rcu(&p->node, &dp->port_list);
302
303         /* Notify the ctlpath that this port has been added */
304         send_port_status(p, OFPPR_ADD);
305
306         return 0;
307 }
308
309 /* Delete 'p' from switch.
310  * Called with dp_mutex. */
311 static int del_switch_port(struct net_bridge_port *p)
312 {
313         /* First drop references to device. */
314         rtnl_lock();
315         dev_set_promiscuity(p->dev, -1);
316         rtnl_unlock();
317         list_del_rcu(&p->node);
318         rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
319         rcu_assign_pointer(p->dev->br_port, NULL);
320
321         /* Then wait until no one is still using it, and destroy it. */
322         synchronize_rcu();
323
324         /* Notify the ctlpath that this port no longer exists */
325         send_port_status(p, OFPPR_DELETE);
326
327         dev_put(p->dev);
328         kfree(p);
329
330         return 0;
331 }
332
333 /* Called with dp_mutex. */
334 static void del_dp(struct datapath *dp)
335 {
336         struct net_bridge_port *p, *n;
337
338 #if 0
339         /* Unregister the "of" device of this dp */
340         rtnl_lock();
341         unregister_netdevice(&dp->dev);
342         rtnl_unlock();
343 #endif
344
345         kthread_stop(dp->dp_task);
346
347         /* Drop references to DP. */
348         list_for_each_entry_safe (p, n, &dp->port_list, node)
349                 del_switch_port(p);
350         rcu_assign_pointer(dps[dp->dp_idx], NULL);
351
352         /* Wait until no longer in use, then destroy it. */
353         synchronize_rcu();
354         chain_destroy(dp->chain);
355         kfree(dp);
356         module_put(THIS_MODULE);
357 }
358
359 static int dp_maint_func(void *data)
360 {
361         struct datapath *dp = (struct datapath *) data;
362
363         while (!kthread_should_stop()) {
364 #if 1
365                 chain_timeout(dp->chain);
366 #else
367                 int count = chain_timeout(dp->chain);
368                 chain_print_stats(dp->chain);
369                 if (count)
370                         printk("%d flows timed out\n", count);
371 #endif
372                 msleep_interruptible(MAINT_SLEEP_MSECS);
373         }
374                 
375         return 0;
376 }
377
378 /*
379  * Used as br_handle_frame_hook.  (Cannot run bridge at the same time, even on
380  * different set of devices!)  Returns 0 if *pskb should be processed further,
381  * 1 if *pskb is handled. */
382 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
383 /* Called with rcu_read_lock. */
384 static struct sk_buff *dp_frame_hook(struct net_bridge_port *p,
385                                          struct sk_buff *skb)
386 {
387         struct ethhdr *eh = eth_hdr(skb);
388         struct sk_buff *skb_local = NULL;
389
390
391         if (compare_ether_addr(eh->h_dest, skb->dev->dev_addr) == 0) 
392                 return skb;
393
394         if (is_broadcast_ether_addr(eh->h_dest)
395                                 || is_multicast_ether_addr(eh->h_dest)
396                                 || is_local_ether_addr(eh->h_dest)) 
397                 skb_local = skb_clone(skb, GFP_ATOMIC);
398
399         /* Push the Ethernet header back on. */
400         if (skb->protocol == htons(ETH_P_8021Q))
401                 skb_push(skb, VLAN_ETH_HLEN);
402         else
403                 skb_push(skb, ETH_HLEN);
404
405         fwd_port_input(p->dp->chain, skb, p->port_no);
406
407         return skb_local;
408 }
409 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
410 static int dp_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
411 {
412         /* Push the Ethernet header back on. */
413         if ((*pskb)->protocol == htons(ETH_P_8021Q))
414                 skb_push(*pskb, VLAN_ETH_HLEN);
415         else
416                 skb_push(*pskb, ETH_HLEN);
417
418         fwd_port_input(p->dp->chain, *pskb, p->port_no);
419         return 1;
420 }
421 #else 
422 /* NB: This has only been tested on 2.4.35 */
423
424 /* Called without any locks (?) */
425 static void dp_frame_hook(struct sk_buff *skb)
426 {
427         struct net_bridge_port *p = skb->dev->br_port;
428
429         /* Push the Ethernet header back on. */
430         if (skb->protocol == htons(ETH_P_8021Q))
431                 skb_push(skb, VLAN_ETH_HLEN);
432         else
433                 skb_push(skb, ETH_HLEN);
434
435         if (p) {
436                 rcu_read_lock();
437                 fwd_port_input(p->dp->chain, skb, p->port_no);
438                 rcu_read_unlock();
439         } else
440                 kfree_skb(skb);
441 }
442 #endif
443
444 /* Forwarding output path.
445  * Based on net/bridge/br_forward.c. */
446
447 /* Don't forward packets to originating port or with flooding disabled */
448 static inline int should_deliver(const struct net_bridge_port *p,
449                         const struct sk_buff *skb)
450 {
451         if ((skb->dev == p->dev) || (p->flags & BRIDGE_PORT_NO_FLOOD)) {
452                 return 0;
453         } 
454
455         return 1;
456 }
457
458 static inline unsigned packet_length(const struct sk_buff *skb)
459 {
460         int length = skb->len - ETH_HLEN;
461         if (skb->protocol == htons(ETH_P_8021Q))
462                 length -= VLAN_HLEN;
463         return length;
464 }
465
466 static int
467 flood(struct datapath *dp, struct sk_buff *skb)
468 {
469         struct net_bridge_port *p;
470         int prev_port;
471
472         prev_port = -1;
473         list_for_each_entry_rcu (p, &dp->port_list, node) {
474                 if (!should_deliver(p, skb))
475                         continue;
476                 if (prev_port != -1) {
477                         struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
478                         if (!clone) {
479                                 kfree_skb(skb);
480                                 return -ENOMEM;
481                         }
482                         dp_output_port(dp, clone, prev_port); 
483                 }
484                 prev_port = p->port_no;
485         }
486         if (prev_port != -1)
487                 dp_output_port(dp, skb, prev_port);
488         else
489                 kfree_skb(skb);
490
491         return 0;
492 }
493
494 /* Marks 'skb' as having originated from 'in_port' in 'dp'.
495    FIXME: how are devices reference counted? */
496 int dp_set_origin(struct datapath *dp, uint16_t in_port,
497                            struct sk_buff *skb)
498 {
499         if (in_port < OFPP_MAX && dp->ports[in_port]) {
500                 skb->dev = dp->ports[in_port]->dev;
501                 return 0;
502         }
503         return -ENOENT;
504 }
505
506 /* Takes ownership of 'skb' and transmits it to 'out_port' on 'dp'.
507  */
508 int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port)
509 {
510         struct net_bridge_port *p;
511         int len = skb->len;
512
513         BUG_ON(!skb);
514         if (out_port == OFPP_FLOOD)
515                 return flood(dp, skb);
516         else if (out_port == OFPP_CONTROLLER)
517                 return dp_output_control(dp, skb, fwd_save_skb(skb), 0,
518                                                   OFPR_ACTION);
519         else if (out_port >= OFPP_MAX)
520                 goto bad_port;
521
522         p = dp->ports[out_port];
523         if (p == NULL)
524                 goto bad_port;
525
526         skb->dev = p->dev;
527         if (packet_length(skb) > skb->dev->mtu) {
528                 printk("dropped over-mtu packet: %d > %d\n",
529                                         packet_length(skb), skb->dev->mtu);
530                 kfree_skb(skb);
531                 return -E2BIG;
532         }
533
534         dev_queue_xmit(skb);
535
536         return len;
537
538 bad_port:
539         kfree_skb(skb);
540         if (net_ratelimit())
541                 printk("can't forward to bad port %d\n", out_port);
542         return -ENOENT;
543 }
544
545 /* Takes ownership of 'skb' and transmits it to 'dp''s control path.  If
546  * 'buffer_id' != -1, then only the first 64 bytes of 'skb' are sent;
547  * otherwise, all of 'skb' is sent.  'reason' indicates why 'skb' is being
548  * sent. 'max_len' sets the maximum number of bytes that the caller
549  * wants to be sent; a value of 0 indicates the entire packet should be
550  * sent. */
551 int
552 dp_output_control(struct datapath *dp, struct sk_buff *skb,
553                            uint32_t buffer_id, size_t max_len, int reason)
554 {
555         /* FIXME?  Can we avoid creating a new skbuff in the case where we
556          * forward the whole packet? */
557         struct sk_buff *f_skb;
558         struct ofp_packet_in *opi;
559         size_t fwd_len, opi_len;
560         int err;
561
562         fwd_len = skb->len;
563         if ((buffer_id != (uint32_t) -1) && max_len)
564                 fwd_len = min(fwd_len, max_len);
565
566         opi_len = offsetof(struct ofp_packet_in, data) + fwd_len;
567         opi = alloc_openflow_skb(dp, opi_len, OFPT_PACKET_IN, 0, &f_skb);
568         opi->buffer_id      = htonl(buffer_id);
569         opi->total_len      = htons(skb->len);
570         opi->in_port        = htons(skb->dev->br_port->port_no);
571         opi->reason         = reason;
572         opi->pad            = 0;
573         memcpy(opi->data, skb_mac_header(skb), fwd_len);
574
575         err = genlmsg_multicast(f_skb, 0, mc_group.id, GFP_ATOMIC);
576         if (err && net_ratelimit())
577                 printk(KERN_WARNING "dp_output_control: genlmsg_multicast failed: %d\n", err);
578
579         kfree_skb(skb);  
580
581         return err;
582 }
583
584 static void fill_port_desc(struct net_bridge_port *p, struct ofp_phy_port *desc)
585 {
586         desc->port_no = htons(p->port_no);
587         strncpy(desc->name, p->dev->name, OFP_MAX_PORT_NAME_LEN);
588         desc->name[OFP_MAX_PORT_NAME_LEN-1] = '\0';
589         memcpy(desc->hw_addr, p->dev->dev_addr, ETH_ALEN);
590         desc->flags = htonl(p->flags);
591         desc->features = 0;
592         desc->speed = 0;
593
594 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,24)
595         if (p->dev->ethtool_ops && p->dev->ethtool_ops->get_settings) {
596                 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
597
598                 if (!p->dev->ethtool_ops->get_settings(p->dev, &ecmd)) {
599                         if (ecmd.supported & SUPPORTED_10baseT_Half) 
600                                 desc->features |= OFPPF_10MB_HD;
601                         if (ecmd.supported & SUPPORTED_10baseT_Full)
602                                 desc->features |= OFPPF_10MB_FD;
603                         if (ecmd.supported & SUPPORTED_100baseT_Half) 
604                                 desc->features |= OFPPF_100MB_HD;
605                         if (ecmd.supported & SUPPORTED_100baseT_Full)
606                                 desc->features |= OFPPF_100MB_FD;
607                         if (ecmd.supported & SUPPORTED_1000baseT_Half)
608                                 desc->features |= OFPPF_1GB_HD;
609                         if (ecmd.supported & SUPPORTED_1000baseT_Full)
610                                 desc->features |= OFPPF_1GB_FD;
611                         /* 10Gbps half-duplex doesn't exist... */
612                         if (ecmd.supported & SUPPORTED_10000baseT_Full)
613                                 desc->features |= OFPPF_10GB_FD;
614
615                         desc->features = htonl(desc->features);
616                         desc->speed = htonl(ecmd.speed);
617                 }
618         }
619 #endif
620 }
621
622 static int 
623 fill_features_reply(struct datapath *dp, struct ofp_switch_features *ofr)
624 {
625         struct net_bridge_port *p;
626         int port_count = 0;
627
628         ofr->datapath_id    = cpu_to_be64(dp->id); 
629
630         ofr->n_exact        = htonl(2 * TABLE_HASH_MAX_FLOWS);
631         ofr->n_mac_only     = htonl(TABLE_MAC_MAX_FLOWS);
632         ofr->n_compression  = 0;                                           /* Not supported */
633         ofr->n_general      = htonl(TABLE_LINEAR_MAX_FLOWS);
634         ofr->buffer_mb      = htonl(UINT32_MAX);
635         ofr->n_buffers      = htonl(N_PKT_BUFFERS);
636         ofr->capabilities   = htonl(OFP_SUPPORTED_CAPABILITIES);
637         ofr->actions        = htonl(OFP_SUPPORTED_ACTIONS);
638
639         list_for_each_entry_rcu (p, &dp->port_list, node) {
640                 fill_port_desc(p, &ofr->ports[port_count]);
641                 port_count++;
642         }
643
644         return port_count;
645 }
646
647 int
648 dp_send_features_reply(struct datapath *dp, uint32_t xid)
649 {
650         struct sk_buff *skb;
651         struct ofp_switch_features *ofr;
652         size_t ofr_len, port_max_len;
653         int err;
654         int port_count;
655
656         /* Overallocate. */
657         port_max_len = sizeof(struct ofp_phy_port) * OFPP_MAX;
658         ofr = alloc_openflow_skb(dp, sizeof(*ofr) + port_max_len,
659                                  OFPT_FEATURES_REPLY, xid, &skb);
660         if (!ofr)
661                 return -ENOMEM;
662
663         /* Fill. */
664         port_count = fill_features_reply(dp, ofr);
665
666         /* Shrink to fit. */
667         ofr_len = sizeof(*ofr) + (sizeof(struct ofp_phy_port) * port_count);
668         resize_openflow_skb(skb, &ofr->header, ofr_len);
669
670         /* FIXME: send as reply. */
671         err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
672         if (err && net_ratelimit())
673                 printk(KERN_WARNING "dp_send_hello: genlmsg_multicast failed: %d\n", err);
674
675         return err;
676 }
677
678 int
679 dp_send_config_reply(struct datapath *dp, uint32_t xid)
680 {
681         struct sk_buff *skb;
682         struct ofp_switch_config *osc;
683         int err;
684
685         osc = alloc_openflow_skb(dp, sizeof *osc, OFPT_PORT_STATUS, 0, &skb);
686         if (!osc)
687                 return -ENOMEM;
688         memcpy(((char *)osc) + sizeof osc->header,
689                ((char *)&dp->config) + sizeof dp->config.header,
690                sizeof dp->config - sizeof dp->config.header);
691
692         err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
693         if (err && net_ratelimit())
694                 printk(KERN_WARNING "send_port_status: genlmsg_multicast failed: %d\n", err);
695
696         return err;
697 }
698
699 int
700 dp_update_port_flags(struct datapath *dp, const struct ofp_phy_port *opp)
701 {
702         struct net_bridge_port *p;
703
704         p = dp->ports[htons(opp->port_no)];
705
706         /* Make sure the port id hasn't changed since this was sent */
707         if (!p || memcmp(opp->hw_addr, p->dev->dev_addr, ETH_ALEN) != 0) 
708                 return -1;
709         
710         p->flags = htonl(opp->flags);
711
712         return 0;
713 }
714
715
716 static int
717 send_port_status(struct net_bridge_port *p, uint8_t status)
718 {
719         struct sk_buff *skb;
720         struct ofp_port_status *ops;
721         int err;
722
723         ops = alloc_openflow_skb(p->dp, sizeof *ops, OFPT_PORT_STATUS, 0,
724                                  &skb);
725         if (!ops)
726                 return -ENOMEM;
727         ops->reason = status;
728         fill_port_desc(p, &ops->desc);
729
730         err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
731         if (err && net_ratelimit())
732                 printk(KERN_WARNING "send_port_status: genlmsg_multicast failed: %d\n", err);
733
734         return err;
735 }
736
737 int 
738 dp_send_flow_expired(struct datapath *dp, struct sw_flow *flow)
739 {
740         struct sk_buff *skb;
741         struct ofp_flow_expired *ofe;
742         unsigned long duration_j;
743         int err;
744
745         ofe = alloc_openflow_skb(dp, sizeof *ofe, OFPT_FLOW_EXPIRED, 0, &skb);
746         if (!ofe)
747                 return -ENOMEM;
748
749         flow_fill_match(&ofe->match, &flow->key);
750         duration_j = (flow->timeout - HZ * flow->max_idle) - flow->init_time;
751         ofe->duration   = htonl(duration_j / HZ);
752         ofe->packet_count   = cpu_to_be64(flow->packet_count);
753         ofe->byte_count     = cpu_to_be64(flow->byte_count);
754
755         err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
756         if (err && net_ratelimit())
757                 printk(KERN_WARNING "send_flow_expired: genlmsg_multicast failed: %d\n", err);
758
759         return err;
760 }
761
762 /* Generic Netlink interface.
763  *
764  * See netlink(7) for an introduction to netlink.  See
765  * http://linux-net.osdl.org/index.php/Netlink for more information and
766  * pointers on how to work with netlink and Generic Netlink in the kernel and
767  * in userspace. */
768
769 static struct genl_family dp_genl_family = {
770         .id = GENL_ID_GENERATE,
771         .hdrsize = 0,
772         .name = DP_GENL_FAMILY_NAME,
773         .version = 1,
774         .maxattr = DP_GENL_A_MAX,
775 };
776
777 /* Attribute policy: what each attribute may contain.  */
778 static struct nla_policy dp_genl_policy[DP_GENL_A_MAX + 1] = {
779         [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
780         [DP_GENL_A_MC_GROUP] = { .type = NLA_U32 },
781         [DP_GENL_A_PORTNAME] = { .type = NLA_STRING }
782 };
783
784 static int dp_genl_add(struct sk_buff *skb, struct genl_info *info)
785 {
786         if (!info->attrs[DP_GENL_A_DP_IDX])
787                 return -EINVAL;
788
789         return new_dp(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
790 }
791
792 static struct genl_ops dp_genl_ops_add_dp = {
793         .cmd = DP_GENL_C_ADD_DP,
794         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
795         .policy = dp_genl_policy,
796         .doit = dp_genl_add,
797         .dumpit = NULL,
798 };
799
800 struct datapath *dp_get(int dp_idx)
801 {
802         if (dp_idx < 0 || dp_idx > DP_MAX)
803                 return NULL;
804         return rcu_dereference(dps[dp_idx]);
805 }
806
807 static int dp_genl_del(struct sk_buff *skb, struct genl_info *info)
808 {
809         struct datapath *dp;
810         int err;
811
812         if (!info->attrs[DP_GENL_A_DP_IDX])
813                 return -EINVAL;
814
815         mutex_lock(&dp_mutex);
816         dp = dp_get(nla_get_u32((info->attrs[DP_GENL_A_DP_IDX])));
817         if (!dp)
818                 err = -ENOENT;
819         else {
820                 del_dp(dp);
821                 err = 0;
822         }
823         mutex_unlock(&dp_mutex);
824         return err;
825 }
826
827 static struct genl_ops dp_genl_ops_del_dp = {
828         .cmd = DP_GENL_C_DEL_DP,
829         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
830         .policy = dp_genl_policy,
831         .doit = dp_genl_del,
832         .dumpit = NULL,
833 };
834
835 /* Queries a datapath for related information.  Currently the only relevant
836  * information is the datapath's multicast group ID.  Really we want one
837  * multicast group per datapath, but because of locking issues[*] we can't
838  * easily get one.  Thus, every datapath will currently return the same
839  * global multicast group ID, but in the future it would be nice to fix that.
840  *
841  * [*] dp_genl_add, to add a new datapath, is called under the genl_lock
842  *       mutex, and genl_register_mc_group, called to acquire a new multicast
843  *       group ID, also acquires genl_lock, thus deadlock.
844  */
845 static int dp_genl_query(struct sk_buff *skb, struct genl_info *info)
846 {
847         struct datapath *dp;
848         struct sk_buff *ans_skb = NULL;
849         int dp_idx;
850         int err = -ENOMEM;
851
852         if (!info->attrs[DP_GENL_A_DP_IDX])
853                 return -EINVAL;
854
855         rcu_read_lock();
856         dp_idx = nla_get_u32((info->attrs[DP_GENL_A_DP_IDX]));
857         dp = dp_get(dp_idx);
858         if (!dp)
859                 err = -ENOENT;
860         else {
861                 void *data;
862                 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
863                 if (!ans_skb) {
864                         err = -ENOMEM;
865                         goto err;
866                 }
867                 data = genlmsg_put_reply(ans_skb, info, &dp_genl_family,
868                                          0, DP_GENL_C_QUERY_DP);
869                 if (data == NULL) {
870                         err = -ENOMEM;
871                         goto err;
872                 }
873                 NLA_PUT_U32(ans_skb, DP_GENL_A_DP_IDX, dp_idx);
874                 NLA_PUT_U32(ans_skb, DP_GENL_A_MC_GROUP, mc_group.id);
875
876                 genlmsg_end(ans_skb, data);
877                 err = genlmsg_reply(ans_skb, info);
878                 if (!err)
879                         ans_skb = NULL;
880         }
881 err:
882 nla_put_failure:
883         if (ans_skb)
884                 kfree_skb(ans_skb);
885         rcu_read_unlock();
886         return err;
887 }
888
889 /*
890  * Fill flow entry for nl flow query.  Called with rcu_lock  
891  *
892  */
893 static
894 int
895 dp_fill_flow(struct ofp_flow_mod* ofm, struct swt_iterator* iter)
896 {
897         ofm->header.version  = OFP_VERSION;
898         ofm->header.type     = OFPT_FLOW_MOD;
899         ofm->header.length   = htons(sizeof(struct ofp_flow_mod) 
900                                 + sizeof(ofm->actions[0]));
901         ofm->header.xid      = htonl(0);
902
903         ofm->match.wildcards = htons(iter->flow->key.wildcards);
904         ofm->match.in_port   = iter->flow->key.in_port;
905         ofm->match.dl_vlan   = iter->flow->key.dl_vlan;
906         memcpy(ofm->match.dl_src, iter->flow->key.dl_src, ETH_ALEN);
907         memcpy(ofm->match.dl_dst, iter->flow->key.dl_dst, ETH_ALEN);
908         ofm->match.dl_type   = iter->flow->key.dl_type;
909         ofm->match.nw_src    = iter->flow->key.nw_src;
910         ofm->match.nw_dst    = iter->flow->key.nw_dst;
911         ofm->match.nw_proto  = iter->flow->key.nw_proto;
912         ofm->match.tp_src    = iter->flow->key.tp_src;
913         ofm->match.tp_dst    = iter->flow->key.tp_dst;
914         ofm->group_id        = iter->flow->group_id;
915         ofm->max_idle        = iter->flow->max_idle;
916         /* TODO support multiple actions  */
917         ofm->actions[0]      = iter->flow->actions[0];
918
919         return 0;
920 }
921
922 /* Convenience function */
923 static
924 void* 
925 dp_init_nl_flow_msg(uint32_t dp_idx, uint16_t table_idx, 
926                 struct genl_info *info, struct sk_buff* skb)
927 {
928         void* data;
929
930         data = genlmsg_put_reply(skb, info, &dp_genl_family, 0, 
931                                 DP_GENL_C_QUERY_FLOW);
932         if (data == NULL)
933                 return NULL;
934         NLA_PUT_U32(skb, DP_GENL_A_DP_IDX,   dp_idx);
935         NLA_PUT_U16(skb, DP_GENL_A_TABLEIDX, table_idx);
936
937         return data;
938
939 nla_put_failure:
940         return NULL;
941 }
942
943 /*  Iterate through the specified table and send all flow entries over
944  *  netlink to userspace.  Each flow message has the following format:
945  *
946  *  32bit dpix
947  *  16bit tabletype
948  *  32bit number of flows
949  *  openflow-flow-entries
950  *
951  *  The full table may require multiple messages.  A message with 0 flows
952  *  signifies end-of message.
953  */
954
955 static 
956 int 
957 dp_dump_table(struct datapath *dp, uint16_t table_idx, struct genl_info *info, struct ofp_flow_mod* matchme) 
958
959         struct sk_buff  *skb = 0; 
960         struct sw_table *table = 0;
961         struct swt_iterator iter;
962         struct sw_flow_key in_flow; 
963         struct nlattr   *attr;
964         int count = 0, sum_count = 0;
965         void *data; 
966         uint8_t* ofm_ptr = 0;
967         struct nlattr   *num_attr; 
968         int err = -ENOMEM;
969
970         table = dp->chain->tables[table_idx]; 
971         if ( table == NULL ) {
972                 dprintk("dp::dp_dump_table error, non-existant table at position %d\n", table_idx);
973                 return -EINVAL;
974         }
975
976         if (!table->iterator(table, &iter)) {
977                 dprintk("dp::dp_dump_table couldn't initialize empty table iterator\n");
978                 return -ENOMEM;
979         }
980
981         while (iter.flow) {
982
983                 /* verify that we can fit all NL_FLOWS_PER_MESSAGE in a single
984                  * sk_buf */
985                 if( (sizeof(dp_genl_family) + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint32_t) + 
986                                         (NL_FLOWS_PER_MESSAGE * sizeof(struct ofp_flow_mod))) > (8192 - 64)){
987                         dprintk("dp::dp_dump_table NL_FLOWS_PER_MESSAGE may cause overrun in skbuf\n");
988                         return -ENOMEM;
989                 }
990
991                 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
992                 if (skb == NULL) {
993                         return -ENOMEM;
994                 }
995
996                 data = dp_init_nl_flow_msg(dp->dp_idx, table_idx, info, skb);
997                 if (data == NULL){
998                         err= -ENOMEM;   
999                         goto error_free_skb;
1000                 } 
1001
1002                 /* reserve space to put the number of flows for this message, to
1003                  * be filled after the loop*/
1004                 num_attr = nla_reserve(skb, DP_GENL_A_NUMFLOWS, sizeof(uint32_t));
1005                 if(!num_attr){
1006                         err = -ENOMEM;
1007                         goto error_free_skb;
1008                 }
1009
1010                 /* Only load NL_FLOWS_PER_MESSAGE flows at a time */
1011                 attr = nla_reserve(skb, DP_GENL_A_FLOW, 
1012                                 (sizeof(struct ofp_flow_mod) + sizeof(struct ofp_action)) * NL_FLOWS_PER_MESSAGE);
1013                 if (!attr){
1014                         err = -ENOMEM;
1015                         goto error_free_skb;
1016                 }
1017
1018                 /* internal loop to fill NL_FLOWS_PER_MESSAGE flows */
1019                 ofm_ptr = nla_data(attr);
1020                 flow_extract_match(&in_flow, &matchme->match);
1021                 while (iter.flow && count < NL_FLOWS_PER_MESSAGE) {
1022                         if(flow_matches(&in_flow, &iter.flow->key)){
1023                                 if((err = dp_fill_flow((struct ofp_flow_mod*)ofm_ptr, &iter))) 
1024                                         goto error_free_skb;
1025                                 count++; 
1026                                 /* TODO support multiple actions  */
1027                                 ofm_ptr += sizeof(struct ofp_flow_mod) + sizeof(struct ofp_action);
1028                         }
1029                         table->iterator_next(&iter);
1030                 }
1031
1032                 *((uint32_t*)nla_data(num_attr)) = count;
1033                 genlmsg_end(skb, data); 
1034
1035                 sum_count += count; 
1036                 count = 0;
1037
1038                 err = genlmsg_unicast(skb, info->snd_pid); 
1039                 skb = 0;
1040         }
1041
1042         /* send a sentinal message saying we're done */
1043         skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
1044         if (skb == NULL) {
1045                 return -ENOMEM;
1046         }
1047         data = dp_init_nl_flow_msg(dp->dp_idx, table_idx, info, skb);
1048         if (data == NULL){
1049                 err= -ENOMEM;   
1050                 goto error_free_skb;
1051         } 
1052
1053         NLA_PUT_U32(skb, DP_GENL_A_NUMFLOWS,   0);
1054         /* dummy flow so nl doesn't complain */
1055         attr = nla_reserve(skb, DP_GENL_A_FLOW, sizeof(struct ofp_flow_mod));
1056         if (!attr){
1057                 err = -ENOMEM;
1058                 goto error_free_skb;
1059         }
1060         genlmsg_end(skb, data); 
1061         err = genlmsg_reply(skb, info); skb = 0;
1062
1063 nla_put_failure:
1064 error_free_skb:
1065         if(skb)
1066                 kfree_skb(skb);
1067         return err;
1068 }
1069
1070 /* Helper function to query_table which creates and sends a message packed with
1071  * table stats.  Message form is:
1072  *
1073  * u32 DP_IDX
1074  * u32 NUM_TABLES
1075  * OFP_TABLE (list of OFP_TABLES)
1076  *
1077  */
1078
1079 static 
1080 int 
1081 dp_dump_table_stats(struct datapath *dp, int dp_idx, struct genl_info *info) 
1082
1083         struct sk_buff   *skb = 0; 
1084         struct ofp_table *ot = 0;
1085         struct nlattr   *attr;
1086         struct sw_table_stats stats; 
1087         size_t len;
1088         void *data; 
1089         int err = -ENOMEM;
1090         int i = 0;
1091         int nt = dp->chain->n_tables;
1092
1093         len = 4 + 4 + (sizeof(struct ofp_table) * nt);
1094
1095         /* u32 IDX, u32 NUMTABLES, list-of-tables */
1096         skb = nlmsg_new(MAX(len, NLMSG_GOODSIZE), GFP_ATOMIC);
1097         if (skb == NULL) {
1098                 return -ENOMEM;
1099         }
1100         
1101         data = genlmsg_put_reply(skb, info, &dp_genl_family, 0, 
1102                                 DP_GENL_C_QUERY_TABLE);
1103         if (data == NULL){
1104                 return -ENOMEM;
1105         } 
1106
1107         NLA_PUT_U32(skb, DP_GENL_A_DP_IDX,      dp_idx);
1108         NLA_PUT_U32(skb, DP_GENL_A_NUMTABLES, nt);
1109
1110         /* ... we assume that all tables can fit in a single message.
1111          * Probably a reasonable assumption seeing that we only have
1112          * 3 atm */
1113         attr = nla_reserve(skb, DP_GENL_A_TABLE, (sizeof(struct ofp_table) * nt));
1114         if (!attr){
1115                 err = -ENOMEM;
1116                 goto error_free_skb;
1117         }
1118
1119         ot = nla_data(attr);
1120
1121         for (i = 0; i < nt; ++i) {
1122                 dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
1123                 ot->header.version = OFP_VERSION;
1124                 ot->header.type    = OFPT_TABLE;
1125                 ot->header.length  = htons(sizeof(struct ofp_table));
1126                 ot->header.xid     = htonl(0);
1127
1128                 strncpy(ot->name, stats.name, OFP_MAX_TABLE_NAME_LEN); 
1129                 ot->table_id  = htons(i);
1130                 ot->n_flows   = htonl(stats.n_flows);
1131                 ot->max_flows = htonl(stats.max_flows);
1132                 ot++;
1133         }
1134
1135         genlmsg_end(skb, data); 
1136         err = genlmsg_reply(skb, info); skb = 0;
1137
1138 nla_put_failure:
1139 error_free_skb:
1140         if(skb)
1141                 kfree_skb(skb);
1142         return err;
1143 }
1144
1145 /* 
1146  * Queries a datapath for flow-table statistics 
1147  */
1148
1149
1150 static int dp_genl_table_query(struct sk_buff *skb, struct genl_info *info)
1151 {
1152         struct   datapath* dp;
1153         int       err = 0;
1154
1155         if (!info->attrs[DP_GENL_A_DP_IDX]) {
1156                 dprintk("dp::dp_genl_table_query received message with missing attributes\n");
1157                 return -EINVAL;
1158         }
1159
1160         rcu_read_lock();
1161         dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1162         if (!dp) {
1163                 err = -ENOENT;
1164                 goto err_out;
1165         }
1166
1167         err = dp_dump_table_stats(dp, nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]), info); 
1168
1169 err_out:
1170         rcu_read_unlock();
1171         return err;
1172 }
1173
1174 /* 
1175  * Queries a datapath for flow-table entries.
1176  */
1177
1178 static int dp_genl_flow_query(struct sk_buff *skb, struct genl_info *info)
1179 {
1180         struct datapath* dp;
1181         struct ofp_flow_mod*  ofm;
1182         u16     table_idx;
1183         int     err = 0;
1184
1185         if (!info->attrs[DP_GENL_A_DP_IDX]
1186                                 || !info->attrs[DP_GENL_A_TABLEIDX]
1187                                 || !info->attrs[DP_GENL_A_FLOW]) {
1188                 dprintk("dp::dp_genl_flow_query received message with missing attributes\n");
1189                 return -EINVAL;
1190         }
1191
1192         rcu_read_lock();
1193         dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1194         if (!dp) {
1195                 err = -ENOENT;
1196                 goto err_out;
1197         }
1198
1199         table_idx = nla_get_u16(info->attrs[DP_GENL_A_TABLEIDX]);
1200
1201         if (dp->chain->n_tables <= table_idx){
1202                 printk("table index %d invalid (dp has %d tables)\n",
1203                                 table_idx, dp->chain->n_tables);
1204         err = -EINVAL;
1205                 goto err_out;
1206         }
1207
1208         ofm = nla_data(info->attrs[DP_GENL_A_FLOW]);
1209         err = dp_dump_table(dp, table_idx, info, ofm); 
1210
1211 err_out:
1212         rcu_read_unlock();
1213         return err;
1214 }
1215
1216 static struct nla_policy dp_genl_flow_policy[DP_GENL_A_MAX + 1] = {
1217         [DP_GENL_A_DP_IDX]      = { .type = NLA_U32 },
1218         [DP_GENL_A_TABLEIDX] = { .type = NLA_U16 },
1219         [DP_GENL_A_NUMFLOWS]  = { .type = NLA_U32 },
1220 };
1221
1222 static struct genl_ops dp_genl_ops_query_flow = {
1223         .cmd    = DP_GENL_C_QUERY_FLOW,
1224         .flags  = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1225         .policy = dp_genl_flow_policy,
1226         .doit   = dp_genl_flow_query,
1227         .dumpit = NULL,
1228 };
1229
1230 static struct nla_policy dp_genl_table_policy[DP_GENL_A_MAX + 1] = {
1231         [DP_GENL_A_DP_IDX]      = { .type = NLA_U32 },
1232 };
1233
1234 static struct genl_ops dp_genl_ops_query_table = {
1235         .cmd    = DP_GENL_C_QUERY_TABLE,
1236         .flags  = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1237         .policy = dp_genl_table_policy,
1238         .doit   = dp_genl_table_query,
1239         .dumpit = NULL,
1240 };
1241
1242
1243 static struct genl_ops dp_genl_ops_query_dp = {
1244         .cmd = DP_GENL_C_QUERY_DP,
1245         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1246         .policy = dp_genl_policy,
1247         .doit = dp_genl_query,
1248         .dumpit = NULL,
1249 };
1250
1251 static int dp_genl_add_del_port(struct sk_buff *skb, struct genl_info *info)
1252 {
1253         struct datapath *dp;
1254         struct net_device *port;
1255         int err;
1256
1257         if (!info->attrs[DP_GENL_A_DP_IDX] || !info->attrs[DP_GENL_A_PORTNAME])
1258                 return -EINVAL;
1259
1260         /* Get datapath. */
1261         mutex_lock(&dp_mutex);
1262         dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1263         if (!dp) {
1264                 err = -ENOENT;
1265                 goto out;
1266         }
1267
1268         /* Get interface to add/remove. */
1269         port = dev_get_by_name(&init_net, 
1270                         nla_data(info->attrs[DP_GENL_A_PORTNAME]));
1271         if (!port) {
1272                 err = -ENOENT;
1273                 goto out;
1274         }
1275
1276         /* Execute operation. */
1277         if (info->genlhdr->cmd == DP_GENL_C_ADD_PORT)
1278                 err = add_switch_port(dp, port);
1279         else {
1280                 if (port->br_port == NULL || port->br_port->dp != dp) {
1281                         err = -ENOENT;
1282                         goto out_put;
1283                 }
1284                 err = del_switch_port(port->br_port);
1285         }
1286
1287 out_put:
1288         dev_put(port);
1289 out:
1290         mutex_unlock(&dp_mutex);
1291         return err;
1292 }
1293
1294 static struct genl_ops dp_genl_ops_add_port = {
1295         .cmd = DP_GENL_C_ADD_PORT,
1296         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1297         .policy = dp_genl_policy,
1298         .doit = dp_genl_add_del_port,
1299         .dumpit = NULL,
1300 };
1301
1302 static struct genl_ops dp_genl_ops_del_port = {
1303         .cmd = DP_GENL_C_DEL_PORT,
1304         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1305         .policy = dp_genl_policy,
1306         .doit = dp_genl_add_del_port,
1307         .dumpit = NULL,
1308 };
1309
1310 static int dp_genl_openflow(struct sk_buff *skb, struct genl_info *info)
1311 {
1312         struct nlattr *va = info->attrs[DP_GENL_A_OPENFLOW];
1313         struct datapath *dp;
1314         int err;
1315
1316         if (!info->attrs[DP_GENL_A_DP_IDX] || !va)
1317                 return -EINVAL;
1318
1319         rcu_read_lock();
1320         dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
1321         if (!dp) {
1322                 err = -ENOENT;
1323                 goto out;
1324         }
1325
1326         va = info->attrs[DP_GENL_A_OPENFLOW];
1327
1328         err = fwd_control_input(dp->chain, nla_data(va), nla_len(va));
1329
1330 out:
1331         rcu_read_unlock();
1332         return err;
1333 }
1334
1335 static struct nla_policy dp_genl_openflow_policy[DP_GENL_A_MAX + 1] = {
1336         [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1337 };
1338
1339 static struct genl_ops dp_genl_ops_openflow = {
1340         .cmd = DP_GENL_C_OPENFLOW,
1341         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1342         .policy = dp_genl_openflow_policy,
1343         .doit = dp_genl_openflow,
1344         .dumpit = NULL,
1345 };
1346
1347 static struct nla_policy dp_genl_benchmark_policy[DP_GENL_A_MAX + 1] = {
1348         [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
1349         [DP_GENL_A_NPACKETS] = { .type = NLA_U32 },
1350         [DP_GENL_A_PSIZE] = { .type = NLA_U32 },
1351 };
1352
1353 static struct genl_ops dp_genl_ops_benchmark_nl = {
1354         .cmd = DP_GENL_C_BENCHMARK_NL,
1355         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1356         .policy = dp_genl_benchmark_policy,
1357         .doit = dp_genl_benchmark_nl,
1358         .dumpit = NULL,
1359 };
1360
1361 static struct genl_ops *dp_genl_all_ops[] = {
1362         /* Keep this operation first.  Generic Netlink dispatching
1363          * looks up operations with linear search, so we want it at the
1364          * front. */
1365         &dp_genl_ops_openflow,
1366
1367         &dp_genl_ops_query_flow,
1368         &dp_genl_ops_query_table,
1369         &dp_genl_ops_add_dp,
1370         &dp_genl_ops_del_dp,
1371         &dp_genl_ops_query_dp,
1372         &dp_genl_ops_add_port,
1373         &dp_genl_ops_del_port,
1374         &dp_genl_ops_benchmark_nl,
1375 };
1376
1377 static int dp_init_netlink(void)
1378 {
1379         int err;
1380         int i;
1381
1382         err = genl_register_family(&dp_genl_family);
1383         if (err)
1384                 return err;
1385
1386         for (i = 0; i < ARRAY_SIZE(dp_genl_all_ops); i++) {
1387                 err = genl_register_ops(&dp_genl_family, dp_genl_all_ops[i]);
1388                 if (err)
1389                         goto err_unregister;
1390         }
1391
1392         strcpy(mc_group.name, "openflow");
1393         err = genl_register_mc_group(&dp_genl_family, &mc_group);
1394         if (err < 0)
1395                 goto err_unregister;
1396
1397         return 0;
1398
1399 err_unregister:
1400         genl_unregister_family(&dp_genl_family);
1401                 return err;
1402 }
1403
1404 static void dp_uninit_netlink(void)
1405 {
1406         genl_unregister_family(&dp_genl_family);
1407 }
1408
1409 #define DRV_NAME                "openflow"
1410 #define DRV_VERSION      VERSION
1411 #define DRV_DESCRIPTION "OpenFlow switching datapath implementation"
1412 #define DRV_COPYRIGHT   "Copyright (c) 2007, 2008 The Board of Trustees of The Leland Stanford Junior University"
1413
1414
1415 static int __init dp_init(void)
1416 {
1417         int err;
1418
1419         printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION "\n");
1420         printk(KERN_INFO DRV_NAME ": " VERSION" built on "__DATE__" "__TIME__"\n");
1421         printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
1422
1423         err = flow_init();
1424         if (err)
1425                 goto error;
1426
1427         err = dp_init_netlink();
1428         if (err)
1429                 goto error_flow_exit;
1430
1431         /* Hook into callback used by the bridge to intercept packets.
1432          * Parasites we are. */
1433         if (br_handle_frame_hook)
1434                 printk("openflow: hijacking bridge hook\n");
1435         br_handle_frame_hook = dp_frame_hook;
1436
1437         return 0;
1438
1439 error_flow_exit:
1440         flow_exit();
1441 error:
1442         printk(KERN_EMERG "openflow: failed to install!");
1443         return err;
1444 }
1445
1446 static void dp_cleanup(void)
1447 {
1448         fwd_exit();
1449         dp_uninit_netlink();
1450         flow_exit();
1451         br_handle_frame_hook = NULL;
1452 }
1453
1454 module_init(dp_init);
1455 module_exit(dp_cleanup);
1456
1457 MODULE_DESCRIPTION(DRV_DESCRIPTION);
1458 MODULE_AUTHOR(DRV_COPYRIGHT);
1459 MODULE_LICENSE("GPL");