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