Fix "waiting for of0 to become free" message on deldp.
[sliver-openvswitch.git] / datapath / forward.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 #include <linux/if_ether.h>
8 #include <linux/if_vlan.h>
9 #include <linux/in.h>
10 #include <linux/ip.h>
11 #include <linux/tcp.h>
12 #include <linux/udp.h>
13 #include <linux/in6.h>
14 #include <asm/uaccess.h>
15 #include <linux/types.h>
16 #include <net/checksum.h>
17 #include "forward.h"
18 #include "datapath.h"
19 #include "chain.h"
20 #include "flow.h"
21
22 /* FIXME: do we need to use GFP_ATOMIC everywhere here? */
23
24 static int make_writable(struct sk_buff **);
25
26 static struct sk_buff *retrieve_skb(uint32_t id);
27 static void discard_skb(uint32_t id);
28
29 /* 'skb' was received on 'in_port', a physical switch port between 0 and
30  * OFPP_MAX.  Process it according to 'chain'. */
31 void fwd_port_input(struct sw_chain *chain, struct sk_buff *skb, int in_port)
32 {
33         struct sw_flow_key key;
34         struct sw_flow *flow;
35
36         flow_extract(skb, in_port, &key);
37         flow = chain_lookup(chain, &key);
38         if (likely(flow != NULL)) {
39                 flow_used(flow, skb);
40                 execute_actions(chain->dp, skb, &key,
41                                 flow->actions, flow->n_actions);
42         } else {
43                 dp_output_control(chain->dp, skb, fwd_save_skb(skb), 
44                                   chain->dp->miss_send_len,
45                                   OFPR_NO_MATCH);
46         }
47 }
48
49 static int do_output(struct datapath *dp, struct sk_buff *skb, size_t max_len,
50                         int out_port)
51 {
52         if (!skb)
53                 return -ENOMEM;
54         return (likely(out_port != OFPP_CONTROLLER)
55                 ? dp_output_port(dp, skb, out_port)
56                 : dp_output_control(dp, skb, fwd_save_skb(skb),
57                                          max_len, OFPR_ACTION));
58 }
59
60 void execute_actions(struct datapath *dp, struct sk_buff *skb,
61                                 const struct sw_flow_key *key,
62                                 const struct ofp_action *actions, int n_actions)
63 {
64         /* Every output action needs a separate clone of 'skb', but the common
65          * case is just a single output action, so that doing a clone and
66          * then freeing the original skbuff is wasteful.  So the following code
67          * is slightly obscure just to avoid that. */
68         int prev_port;
69         size_t max_len=0;        /* Initialze to make compiler happy */
70         uint16_t eth_proto;
71         int i;
72
73         prev_port = -1;
74         eth_proto = ntohs(key->dl_type);
75
76         for (i = 0; i < n_actions; i++) {
77                 const struct ofp_action *a = &actions[i];
78
79                 if (prev_port != -1) {
80                         do_output(dp, skb_clone(skb, GFP_ATOMIC),
81                                   max_len, prev_port);
82                         prev_port = -1;
83                 }
84
85                 if (likely(a->type == htons(OFPAT_OUTPUT))) {
86                         prev_port = ntohs(a->arg.output.port);
87                         max_len = ntohs(a->arg.output.max_len);
88                 } else {
89                         if (!make_writable(&skb)) {
90                                 printk("make_writable failed\n");
91                                 break;
92                         }
93                         skb = execute_setter(skb, eth_proto, key, a);
94                 }
95         }
96         if (prev_port != -1)
97                 do_output(dp, skb, max_len, prev_port);
98         else
99                 kfree_skb(skb);
100 }
101
102 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
103  * covered by the sum has been changed from 'from' to 'to'.  If set,
104  * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
105  * Based on nf_proto_csum_replace4. */
106 static void update_csum(__sum16 *sum, struct sk_buff *skb,
107                         __be32 from, __be32 to, int pseudohdr)
108 {
109         __be32 diff[] = { ~from, to };
110         if (skb->ip_summed != CHECKSUM_PARTIAL) {
111                 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
112                                 ~csum_unfold(*sum)));
113                 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
114                         skb->csum = ~csum_partial((char *)diff, sizeof(diff),
115                                                 ~skb->csum);
116         } else if (pseudohdr)
117                 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
118                                 csum_unfold(*sum)));
119 }
120
121 static void modify_nh(struct sk_buff *skb, uint16_t eth_proto,
122                         uint8_t nw_proto, const struct ofp_action *a)
123 {
124         if (eth_proto == ETH_P_IP) {
125                 struct iphdr *nh = ip_hdr(skb);
126                 uint32_t new, *field;
127
128                 new = a->arg.nw_addr;
129
130                 if (a->type == htons(OFPAT_SET_NW_SRC))
131                         field = &nh->saddr;
132                 else
133                         field = &nh->daddr;
134
135                 if (nw_proto == IPPROTO_TCP) {
136                         struct tcphdr *th = tcp_hdr(skb);
137                         update_csum(&th->check, skb, *field, new, 1);
138                 } else if (nw_proto == IPPROTO_UDP) {
139                         struct udphdr *th = udp_hdr(skb);
140                         update_csum(&th->check, skb, *field, new, 1);
141                 }
142                 update_csum(&nh->check, skb, *field, new, 0);
143                 *field = new;
144         }
145 }
146
147 static void modify_th(struct sk_buff *skb, uint16_t eth_proto,
148                         uint8_t nw_proto, const struct ofp_action *a)
149 {
150         if (eth_proto == ETH_P_IP) {
151                 uint16_t new, *field;
152
153                 new = a->arg.tp;
154
155                 if (nw_proto == IPPROTO_TCP) {
156                         struct tcphdr *th = tcp_hdr(skb);
157
158                         if (a->type == htons(OFPAT_SET_TP_SRC))
159                                 field = &th->source;
160                         else
161                                 field = &th->dest;
162
163                         update_csum(&th->check, skb, *field, new, 1);
164                         *field = new;
165                 } else if (nw_proto == IPPROTO_UDP) {
166                         struct udphdr *th = udp_hdr(skb);
167
168                         if (a->type == htons(OFPAT_SET_TP_SRC))
169                                 field = &th->source;
170                         else
171                                 field = &th->dest;
172
173                         update_csum(&th->check, skb, *field, new, 1);
174                         *field = new;
175                 }
176         }
177 }
178
179 static struct sk_buff *vlan_pull_tag(struct sk_buff *skb)
180 {
181         struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
182         struct ethhdr *eh;
183
184
185         /* Verify we were given a vlan packet */
186         if (vh->h_vlan_proto != htons(ETH_P_8021Q))
187                 return skb;
188
189         memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
190
191         eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
192
193         skb->protocol = eh->h_proto;
194         skb->mac_header += VLAN_HLEN;
195
196         return skb;
197 }
198
199 static struct sk_buff *modify_vlan(struct sk_buff *skb, 
200                 const struct sw_flow_key *key, const struct ofp_action *a)
201 {
202         uint16_t new_id = a->arg.vlan_id;
203
204         if (new_id != OFP_VLAN_NONE) {
205                 if (key->dl_vlan != htons(OFP_VLAN_NONE)) {
206                         /* Modify vlan id, but maintain other TCI values */
207                         struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
208                         vh->h_vlan_TCI = (vh->h_vlan_TCI 
209                                         & ~(htons(VLAN_VID_MASK))) | htons(new_id);
210                 } else  {
211                         /* Add vlan header */
212                         skb = vlan_put_tag(skb, new_id);
213                 }
214         } else  {
215                 /* Remove an existing vlan header if it exists */
216                 vlan_pull_tag(skb);
217         }
218
219         return skb;
220 }
221
222 struct sk_buff *execute_setter(struct sk_buff *skb, uint16_t eth_proto,
223                         const struct sw_flow_key *key, const struct ofp_action *a)
224 {
225         switch (ntohs(a->type)) {
226         case OFPAT_SET_DL_VLAN:
227                 skb = modify_vlan(skb, key, a);
228                 break;
229
230         case OFPAT_SET_DL_SRC: {
231                 struct ethhdr *eh = eth_hdr(skb);
232                 memcpy(eh->h_source, a->arg.dl_addr, sizeof eh->h_source);
233                 break;
234         }
235         case OFPAT_SET_DL_DST: {
236                 struct ethhdr *eh = eth_hdr(skb);
237                 memcpy(eh->h_dest, a->arg.dl_addr, sizeof eh->h_dest);
238                 break;
239         }
240
241         case OFPAT_SET_NW_SRC:
242         case OFPAT_SET_NW_DST:
243                 modify_nh(skb, eth_proto, key->nw_proto, a);
244                 break;
245
246         case OFPAT_SET_TP_SRC:
247         case OFPAT_SET_TP_DST:
248                 modify_th(skb, eth_proto, key->nw_proto, a);
249                 break;
250         
251         default:
252                 if (net_ratelimit())
253                         printk("execute_setter: unknown action: %d\n", ntohs(a->type));
254         }
255
256         return skb;
257 }
258
259 static int
260 recv_features_request(struct sw_chain *chain, const struct sender *sender,
261                       const void *msg) 
262 {
263         return dp_send_features_reply(chain->dp, sender);
264 }
265
266 static int
267 recv_get_config_request(struct sw_chain *chain, const struct sender *sender,
268                         const void *msg)
269 {
270         return dp_send_config_reply(chain->dp, sender);
271 }
272
273 static int
274 recv_set_config(struct sw_chain *chain, const struct sender *sender,
275                 const void *msg)
276 {
277         const struct ofp_switch_config *osc = msg;
278
279         chain->dp->flags = ntohs(osc->flags);
280         chain->dp->miss_send_len = ntohs(osc->miss_send_len);
281
282         return 0;
283 }
284
285 static int
286 recv_packet_out(struct sw_chain *chain, const struct sender *sender,
287                 const void *msg)
288 {
289         const struct ofp_packet_out *opo = msg;
290         struct sk_buff *skb;
291         struct vlan_ethhdr *mac;
292         int nh_ofs;
293
294         if (ntohl(opo->buffer_id) == (uint32_t) -1) {
295                 int data_len = ntohs(opo->header.length) - sizeof *opo;
296
297                 /* FIXME: there is likely a way to reuse the data in msg. */
298                 skb = alloc_skb(data_len, GFP_ATOMIC);
299                 if (!skb)
300                         return -ENOMEM;
301
302                 /* FIXME?  We don't reserve NET_IP_ALIGN or NET_SKB_PAD since
303                  * we're just transmitting this raw without examining anything
304                  * at those layers. */
305                 memcpy(skb_put(skb, data_len), opo->u.data, data_len);
306                 dp_set_origin(chain->dp, ntohs(opo->in_port), skb);
307
308                 skb_set_mac_header(skb, 0);
309                 mac = vlan_eth_hdr(skb);
310                 if (likely(mac->h_vlan_proto != htons(ETH_P_8021Q)))
311                         nh_ofs = sizeof(struct ethhdr);
312                 else
313                         nh_ofs = sizeof(struct vlan_ethhdr);
314                 skb_set_network_header(skb, nh_ofs);
315
316                 dp_output_port(chain->dp, skb, ntohs(opo->out_port));
317         } else {
318                 struct sw_flow_key key;
319                 int n_acts;
320
321                 skb = retrieve_skb(ntohl(opo->buffer_id));
322                 if (!skb)
323                         return -ESRCH;
324                 dp_set_origin(chain->dp, ntohs(opo->in_port), skb);
325
326                 n_acts = (ntohs(opo->header.length) - sizeof *opo) 
327                                 / sizeof *opo->u.actions;
328                 flow_extract(skb, ntohs(opo->in_port), &key);
329                 execute_actions(chain->dp, skb, &key, opo->u.actions, n_acts);
330         }
331         return 0;
332 }
333
334 static int
335 recv_port_mod(struct sw_chain *chain, const struct sender *sender,
336               const void *msg)
337 {
338         const struct ofp_port_mod *opm = msg;
339
340         dp_update_port_flags(chain->dp, &opm->desc);
341
342         return 0;
343 }
344
345 static int
346 add_flow(struct sw_chain *chain, const struct ofp_flow_mod *ofm)
347 {
348         int error = -ENOMEM;
349         int i;
350         int n_acts;
351         struct sw_flow *flow;
352
353
354         /* To prevent loops, make sure there's no action to send to the
355          * OFP_TABLE virtual port.
356          */
357         n_acts = (ntohs(ofm->header.length) - sizeof *ofm) / sizeof *ofm->actions;
358         for (i=0; i<n_acts; i++) {
359                 const struct ofp_action *a = &ofm->actions[i];
360
361                 if (a->type == htons(OFPAT_OUTPUT) 
362                                         && a->arg.output.port == htons(OFPP_TABLE)) {
363                         /* xxx Send fancy new error message? */
364                         goto error;
365                 }
366         }
367
368         /* Allocate memory. */
369         flow = flow_alloc(n_acts, GFP_ATOMIC);
370         if (flow == NULL)
371                 goto error;
372
373         /* Fill out flow. */
374         flow_extract_match(&flow->key, &ofm->match);
375         flow->max_idle = ntohs(ofm->max_idle);
376         flow->priority = flow->key.wildcards ? ntohs(ofm->priority) : -1;
377         flow->timeout = jiffies + flow->max_idle * HZ;
378         flow->n_actions = n_acts;
379         flow->init_time = jiffies;
380         flow->byte_count = 0;
381         flow->packet_count = 0;
382         atomic_set(&flow->deleted, 0);
383         spin_lock_init(&flow->lock);
384         memcpy(flow->actions, ofm->actions, n_acts * sizeof *flow->actions);
385
386         /* Act. */
387         error = chain_insert(chain, flow);
388         if (error)
389                 goto error_free_flow;
390         error = 0;
391         if (ntohl(ofm->buffer_id) != (uint32_t) -1) {
392                 struct sk_buff *skb = retrieve_skb(ntohl(ofm->buffer_id));
393                 if (skb) {
394                         struct sw_flow_key key;
395                         flow_used(flow, skb);
396                         flow_extract(skb, ntohs(ofm->match.in_port), &key);
397                         execute_actions(chain->dp, skb, &key,
398                                         ofm->actions, n_acts);
399                 }
400                 else
401                         error = -ESRCH;
402         }
403         return error;
404
405 error_free_flow:
406         flow_free(flow);
407 error:
408         if (ntohl(ofm->buffer_id) != (uint32_t) -1)
409                 discard_skb(ntohl(ofm->buffer_id));
410         return error;
411 }
412
413 static int
414 recv_flow(struct sw_chain *chain, const struct sender *sender, const void *msg)
415 {
416         const struct ofp_flow_mod *ofm = msg;
417         uint16_t command = ntohs(ofm->command);
418
419         if (command == OFPFC_ADD) {
420                 return add_flow(chain, ofm);
421         }  else if (command == OFPFC_DELETE) {
422                 struct sw_flow_key key;
423                 flow_extract_match(&key, &ofm->match);
424                 return chain_delete(chain, &key, 0, 0) ? 0 : -ESRCH;
425         } else if (command == OFPFC_DELETE_STRICT) {
426                 struct sw_flow_key key;
427                 uint16_t priority;
428                 flow_extract_match(&key, &ofm->match);
429                 priority = key.wildcards ? ntohs(ofm->priority) : -1;
430                 return chain_delete(chain, &key, priority, 1) ? 0 : -ESRCH;
431         } else {
432                 return -ENOTSUPP;
433         }
434 }
435
436 /* 'msg', which is 'length' bytes long, was received across Netlink from
437  * 'sender'.  Apply it to 'chain'. */
438 int
439 fwd_control_input(struct sw_chain *chain, const struct sender *sender,
440                   const void *msg, size_t length)
441 {
442
443         struct openflow_packet {
444                 size_t min_size;
445                 int (*handler)(struct sw_chain *, const struct sender *,
446                                const void *);
447         };
448
449         static const struct openflow_packet packets[] = {
450                 [OFPT_FEATURES_REQUEST] = {
451                         sizeof (struct ofp_header),
452                         recv_features_request,
453                 },
454                 [OFPT_GET_CONFIG_REQUEST] = {
455                         sizeof (struct ofp_header),
456                         recv_get_config_request,
457                 },
458                 [OFPT_SET_CONFIG] = {
459                         sizeof (struct ofp_switch_config),
460                         recv_set_config,
461                 },
462                 [OFPT_PACKET_OUT] = {
463                         sizeof (struct ofp_packet_out),
464                         recv_packet_out,
465                 },
466                 [OFPT_FLOW_MOD] = {
467                         sizeof (struct ofp_flow_mod),
468                         recv_flow,
469                 },
470                 [OFPT_PORT_MOD] = {
471                         sizeof (struct ofp_port_mod),
472                         recv_port_mod,
473                 },
474         };
475
476         const struct openflow_packet *pkt;
477         struct ofp_header *oh;
478
479         oh = (struct ofp_header *) msg;
480         if (oh->version != OFP_VERSION || oh->type >= ARRAY_SIZE(packets)
481                 || ntohs(oh->length) > length)
482                 return -EINVAL;
483
484         pkt = &packets[oh->type];
485         if (!pkt->handler)
486                 return -ENOSYS;
487         if (length < pkt->min_size)
488                 return -EFAULT;
489
490         return pkt->handler(chain, sender, msg);
491 }
492
493 /* Packet buffering. */
494
495 #define OVERWRITE_SECS  1
496 #define OVERWRITE_JIFFIES (OVERWRITE_SECS * HZ)
497
498 struct packet_buffer {
499         struct sk_buff *skb;
500         uint32_t cookie;
501         unsigned long exp_jiffies;
502 };
503
504 static struct packet_buffer buffers[N_PKT_BUFFERS];
505 static unsigned int buffer_idx;
506 static DEFINE_SPINLOCK(buffer_lock);
507
508 uint32_t fwd_save_skb(struct sk_buff *skb)
509 {
510         struct packet_buffer *p;
511         unsigned long int flags;
512         uint32_t id;
513
514         spin_lock_irqsave(&buffer_lock, flags);
515         buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
516         p = &buffers[buffer_idx];
517         if (p->skb) {
518                 /* Don't buffer packet if existing entry is less than
519                  * OVERWRITE_SECS old. */
520                 if (time_before(jiffies, p->exp_jiffies)) {
521                         spin_unlock_irqrestore(&buffer_lock, flags);
522                         return -1;
523                 } else 
524                         kfree_skb(p->skb);
525         }
526         /* Don't use maximum cookie value since the all-bits-1 id is
527          * special. */
528         if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
529                 p->cookie = 0;
530         skb_get(skb);
531         p->skb = skb;
532         p->exp_jiffies = jiffies + OVERWRITE_JIFFIES;
533         id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
534         spin_unlock_irqrestore(&buffer_lock, flags);
535
536         return id;
537 }
538
539 static struct sk_buff *retrieve_skb(uint32_t id)
540 {
541         unsigned long int flags;
542         struct sk_buff *skb = NULL;
543         struct packet_buffer *p;
544
545         spin_lock_irqsave(&buffer_lock, flags);
546         p = &buffers[id & PKT_BUFFER_MASK];
547         if (p->cookie == id >> PKT_BUFFER_BITS) {
548                 skb = p->skb;
549                 p->skb = NULL;
550         } else {
551                 printk("cookie mismatch: %x != %x\n",
552                                 id >> PKT_BUFFER_BITS, p->cookie);
553         }
554         spin_unlock_irqrestore(&buffer_lock, flags);
555
556         return skb;
557 }
558
559 void fwd_discard_all(void) 
560 {
561         unsigned long int flags;
562         int i;
563
564         spin_lock_irqsave(&buffer_lock, flags);
565         for (i = 0; i < N_PKT_BUFFERS; i++) {
566                 kfree_skb(buffers[i].skb);
567                 buffers[i].skb = NULL;
568         }
569         spin_unlock_irqrestore(&buffer_lock, flags);
570 }
571
572 static void discard_skb(uint32_t id)
573 {
574         unsigned long int flags;
575         struct packet_buffer *p;
576
577         spin_lock_irqsave(&buffer_lock, flags);
578         p = &buffers[id & PKT_BUFFER_MASK];
579         if (p->cookie == id >> PKT_BUFFER_BITS) {
580                 kfree_skb(p->skb);
581                 p->skb = NULL;
582         }
583         spin_unlock_irqrestore(&buffer_lock, flags);
584 }
585
586 void fwd_exit(void)
587 {
588         fwd_discard_all();
589 }
590
591 /* Utility functions. */
592
593 /* Makes '*pskb' writable, possibly copying it and setting '*pskb' to point to
594  * the copy.
595  * Returns 1 if successful, 0 on failure. */
596 static int
597 make_writable(struct sk_buff **pskb)
598 {
599         /* Based on skb_make_writable() in net/netfilter/core.c. */
600         struct sk_buff *nskb;
601
602         /* Not exclusive use of packet?  Must copy. */
603         if (skb_shared(*pskb) || skb_cloned(*pskb))
604                 goto copy_skb;
605
606         return pskb_may_pull(*pskb, 64); /* FIXME? */
607
608 copy_skb:
609         nskb = skb_copy(*pskb, GFP_ATOMIC);
610         if (!nskb)
611                 return 0;
612         BUG_ON(skb_is_nonlinear(nskb));
613
614         /* Rest of kernel will get very unhappy if we pass it a
615            suddenly-orphaned skbuff */
616         if ((*pskb)->sk)
617                 skb_set_owner_w(nskb, (*pskb)->sk);
618         kfree_skb(*pskb);
619         *pskb = nskb;
620         return 1;
621 }