Reset mac header in flow_extract(), because it might initially be null.
[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/netdevice.h>
8 #include <linux/etherdevice.h>
9 #include <linux/if_ether.h>
10 #include <linux/if_vlan.h>
11 #include <asm/uaccess.h>
12 #include <linux/types.h>
13 #include "forward.h"
14 #include "datapath.h"
15 #include "openflow/nicira-ext.h"
16 #include "dp_act.h"
17 #include "nx_msg.h"
18 #include "chain.h"
19 #include "flow.h"
20
21 /* FIXME: do we need to use GFP_ATOMIC everywhere here? */
22
23
24 static struct sk_buff *retrieve_skb(uint32_t id);
25 static void discard_skb(uint32_t id);
26
27 /* 'skb' was received on port 'p', which may be a physical switch port, the
28  * local port, or a null pointer.  Process it according to 'chain'.  Returns 0
29  * if successful, in which case 'skb' is destroyed, or -ESRCH if there is no
30  * matching flow, in which case 'skb' still belongs to the caller. */
31 int run_flow_through_tables(struct sw_chain *chain, struct sk_buff *skb,
32                             struct net_bridge_port *p)
33 {
34         /* Ethernet address used as the destination for STP frames. */
35         static const uint8_t stp_eth_addr[ETH_ALEN]
36                 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x01 };
37         struct sw_flow_key key;
38         struct sw_flow *flow;
39
40         if (flow_extract(skb, p ? p->port_no : OFPP_NONE, &key)
41             && (chain->dp->flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP) {
42                 /* Drop fragment. */
43                 kfree_skb(skb);
44                 return 0;
45         }
46         if (p && p->config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
47             p->config & (compare_ether_addr(key.dl_dst, stp_eth_addr)
48                         ? OFPPC_NO_RECV : OFPPC_NO_RECV_STP)) {
49                 kfree_skb(skb);
50                 return 0;
51         }
52
53         flow = chain_lookup(chain, &key);
54         if (likely(flow != NULL)) {
55                 struct sw_flow_actions *sf_acts = rcu_dereference(flow->sf_acts);
56                 flow_used(flow, skb);
57                 execute_actions(chain->dp, skb, &key,
58                                 sf_acts->actions, sf_acts->actions_len, 0);
59                 return 0;
60         } else {
61                 return -ESRCH;
62         }
63 }
64
65 /* 'skb' was received on port 'p', which may be a physical switch port, the
66  * local port, or a null pointer.  Process it according to 'chain', sending it
67  * up to the controller if no flow matches.  Takes ownership of 'skb'. */
68 void fwd_port_input(struct sw_chain *chain, struct sk_buff *skb,
69                     struct net_bridge_port *p)
70 {
71         WARN_ON_ONCE(skb_shared(skb));
72         WARN_ON_ONCE(skb->destructor);
73         if (run_flow_through_tables(chain, skb, p))
74                 dp_output_control(chain->dp, skb, fwd_save_skb(skb), 
75                                   chain->dp->miss_send_len,
76                                   OFPR_NO_MATCH);
77 }
78
79 static int
80 recv_hello(struct sw_chain *chain, const struct sender *sender,
81            const void *msg)
82 {
83         return dp_send_hello(chain->dp, sender, msg);
84 }
85
86 static int
87 recv_features_request(struct sw_chain *chain, const struct sender *sender,
88                       const void *msg) 
89 {
90         return dp_send_features_reply(chain->dp, sender);
91 }
92
93 static int
94 recv_get_config_request(struct sw_chain *chain, const struct sender *sender,
95                         const void *msg)
96 {
97         return dp_send_config_reply(chain->dp, sender);
98 }
99
100 static int
101 recv_set_config(struct sw_chain *chain, const struct sender *sender,
102                 const void *msg)
103 {
104         const struct ofp_switch_config *osc = msg;
105         int flags;
106
107         flags = ntohs(osc->flags) & (OFPC_SEND_FLOW_EXP | OFPC_FRAG_MASK);
108         if ((flags & OFPC_FRAG_MASK) != OFPC_FRAG_NORMAL
109             && (flags & OFPC_FRAG_MASK) != OFPC_FRAG_DROP) {
110                 flags = (flags & ~OFPC_FRAG_MASK) | OFPC_FRAG_DROP;
111         }
112         chain->dp->flags = flags;
113
114         chain->dp->miss_send_len = ntohs(osc->miss_send_len);
115
116         return 0;
117 }
118
119 static int
120 recv_packet_out(struct sw_chain *chain, const struct sender *sender,
121                 const void *msg)
122 {
123         const struct ofp_packet_out *opo = msg;
124         struct sk_buff *skb;
125         struct vlan_ethhdr *mac;
126         int nh_ofs;
127         uint16_t v_code;
128         struct sw_flow_key key;
129         size_t actions_len = ntohs(opo->actions_len);
130
131         if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
132                 if (net_ratelimit()) 
133                         printk("message too short for number of actions\n");
134                 return -EINVAL;
135         }
136
137         if (ntohl(opo->buffer_id) == (uint32_t) -1) {
138                 int data_len = ntohs(opo->header.length) - sizeof *opo - actions_len;
139
140                 /* FIXME: there is likely a way to reuse the data in msg. */
141                 skb = alloc_skb(data_len, GFP_ATOMIC);
142                 if (!skb)
143                         return -ENOMEM;
144
145                 /* FIXME?  We don't reserve NET_IP_ALIGN or NET_SKB_PAD since
146                  * we're just transmitting this raw without examining anything
147                  * at those layers. */
148                 memcpy(skb_put(skb, data_len), (uint8_t *)opo->actions + actions_len, 
149                                 data_len);
150
151                 skb_set_mac_header(skb, 0);
152                 mac = vlan_eth_hdr(skb);
153                 if (likely(mac->h_vlan_proto != htons(ETH_P_8021Q)))
154                         nh_ofs = sizeof(struct ethhdr);
155                 else
156                         nh_ofs = sizeof(struct vlan_ethhdr);
157                 skb_set_network_header(skb, nh_ofs);
158         } else {
159                 skb = retrieve_skb(ntohl(opo->buffer_id));
160                 if (!skb)
161                         return -ESRCH;
162         }
163
164         dp_set_origin(chain->dp, ntohs(opo->in_port), skb);
165
166         flow_extract(skb, ntohs(opo->in_port), &key);
167
168         v_code = validate_actions(chain->dp, &key, opo->actions, actions_len);
169         if (v_code != ACT_VALIDATION_OK) {
170                 dp_send_error_msg(chain->dp, sender, OFPET_BAD_ACTION, v_code,
171                                   msg, ntohs(opo->header.length));
172                 goto error;
173         }
174
175         execute_actions(chain->dp, skb, &key, opo->actions, actions_len, 1);
176
177         return 0;
178
179 error:
180         kfree_skb(skb);
181         return -EINVAL;
182 }
183
184 static int
185 recv_port_mod(struct sw_chain *chain, const struct sender *sender,
186               const void *msg)
187 {
188         const struct ofp_port_mod *opm = msg;
189
190         dp_update_port_flags(chain->dp, opm);
191
192         return 0;
193 }
194
195 static int
196 recv_echo_request(struct sw_chain *chain, const struct sender *sender,
197                   const void *msg) 
198 {
199         return dp_send_echo_reply(chain->dp, sender, msg);
200 }
201
202 static int
203 recv_echo_reply(struct sw_chain *chain, const struct sender *sender,
204                   const void *msg) 
205 {
206         return 0;
207 }
208
209 static int
210 add_flow(struct sw_chain *chain, const struct sender *sender, 
211                 const struct ofp_flow_mod *ofm)
212 {
213         int error = -ENOMEM;
214         uint16_t v_code;
215         struct sw_flow *flow;
216         size_t actions_len = ntohs(ofm->header.length) - sizeof *ofm;
217
218         /* Allocate memory. */
219         flow = flow_alloc(actions_len, GFP_ATOMIC);
220         if (flow == NULL)
221                 goto error;
222
223         flow_extract_match(&flow->key, &ofm->match);
224
225         v_code = validate_actions(chain->dp, &flow->key, ofm->actions, actions_len);
226         if (v_code != ACT_VALIDATION_OK) {
227                 dp_send_error_msg(chain->dp, sender, OFPET_BAD_ACTION, v_code,
228                                   ofm, ntohs(ofm->header.length));
229                 goto error_free_flow;
230         }
231
232         /* Fill out flow. */
233         flow->priority = flow->key.wildcards ? ntohs(ofm->priority) : -1;
234         flow->idle_timeout = ntohs(ofm->idle_timeout);
235         flow->hard_timeout = ntohs(ofm->hard_timeout);
236         flow->used = jiffies;
237         flow->init_time = jiffies;
238         flow->byte_count = 0;
239         flow->packet_count = 0;
240         spin_lock_init(&flow->lock);
241         memcpy(flow->sf_acts->actions, ofm->actions, actions_len);
242
243         /* Act. */
244         error = chain_insert(chain, flow);
245         if (error == -ENOBUFS) {
246                 dp_send_error_msg(chain->dp, sender, OFPET_FLOW_MOD_FAILED, 
247                                 OFPFMFC_ALL_TABLES_FULL, ofm, ntohs(ofm->header.length));
248                 goto error_free_flow;
249         } else if (error)
250                 goto error_free_flow;
251         error = 0;
252         if (ntohl(ofm->buffer_id) != (uint32_t) -1) {
253                 struct sk_buff *skb = retrieve_skb(ntohl(ofm->buffer_id));
254                 if (skb) {
255                         struct sw_flow_key key;
256                         flow_used(flow, skb);
257                         dp_set_origin(chain->dp, ntohs(ofm->match.in_port), skb);
258                         flow_extract(skb, ntohs(ofm->match.in_port), &key);
259                         execute_actions(chain->dp, skb, &key, ofm->actions, actions_len, 0);
260                 }
261                 else
262                         error = -ESRCH;
263         }
264         return error;
265
266 error_free_flow:
267         flow_free(flow);
268 error:
269         if (ntohl(ofm->buffer_id) != (uint32_t) -1)
270                 discard_skb(ntohl(ofm->buffer_id));
271         return error;
272 }
273
274 static int
275 mod_flow(struct sw_chain *chain, const struct sender *sender,
276                 const struct ofp_flow_mod *ofm)
277 {
278         int error = -ENOMEM;
279         uint16_t v_code;
280         size_t actions_len;
281         struct sw_flow_key key;
282         uint16_t priority;
283         int strict;
284
285         flow_extract_match(&key, &ofm->match);
286
287         actions_len = ntohs(ofm->header.length) - sizeof *ofm;
288
289         v_code = validate_actions(chain->dp, &key, ofm->actions, actions_len);
290         if (v_code != ACT_VALIDATION_OK) {
291                 dp_send_error_msg(chain->dp, sender, OFPET_BAD_ACTION, v_code,
292                                   ofm, ntohs(ofm->header.length));
293                 goto error;
294         }
295
296         priority = key.wildcards ? ntohs(ofm->priority) : -1;
297         strict = (ofm->command == htons(OFPFC_MODIFY_STRICT)) ? 1 : 0;
298         chain_modify(chain, &key, priority, strict, ofm->actions, actions_len);
299
300         if (ntohl(ofm->buffer_id) != (uint32_t) -1) {
301                 struct sk_buff *skb = retrieve_skb(ntohl(ofm->buffer_id));
302                 if (skb) {
303                         struct sw_flow_key skb_key;
304                         flow_extract(skb, ntohs(ofm->match.in_port), &skb_key);
305                         execute_actions(chain->dp, skb, &skb_key, 
306                                         ofm->actions, actions_len, 0);
307                 }
308                 else
309                         error = -ESRCH;
310         }
311         return error;
312
313 error:
314         if (ntohl(ofm->buffer_id) != (uint32_t) -1)
315                 discard_skb(ntohl(ofm->buffer_id));
316         return error;
317 }
318
319 static int
320 recv_flow(struct sw_chain *chain, const struct sender *sender, const void *msg)
321 {
322         const struct ofp_flow_mod *ofm = msg;
323         uint16_t command = ntohs(ofm->command);
324
325         if (command == OFPFC_ADD) {
326                 return add_flow(chain, sender, ofm);
327         } else if ((command == OFPFC_MODIFY) || (command == OFPFC_MODIFY_STRICT)) {
328                 return mod_flow(chain, sender, ofm);
329         }  else if (command == OFPFC_DELETE) {
330                 struct sw_flow_key key;
331                 flow_extract_match(&key, &ofm->match);
332                 return chain_delete(chain, &key, 0, 0) ? 0 : -ESRCH;
333         } else if (command == OFPFC_DELETE_STRICT) {
334                 struct sw_flow_key key;
335                 uint16_t priority;
336                 flow_extract_match(&key, &ofm->match);
337                 priority = key.wildcards ? ntohs(ofm->priority) : -1;
338                 return chain_delete(chain, &key, priority, 1) ? 0 : -ESRCH;
339         } else {
340                 return -ENOTSUPP;
341         }
342 }
343
344 static int
345 recv_vendor(struct sw_chain *chain, const struct sender *sender, 
346                 const void *msg)
347 {
348         const struct ofp_vendor_header *ovh = msg;
349
350         switch(ntohl(ovh->vendor))
351         {
352         case NX_VENDOR_ID:
353                 return nx_recv_msg(chain, sender, msg);
354         default:
355                 if (net_ratelimit())
356                         printk("Uknown vendor: %#x\n", ntohl(ovh->vendor));
357                 dp_send_error_msg(chain->dp, sender, OFPET_BAD_REQUEST,
358                                   OFPBRC_BAD_VENDOR, msg, ntohs(ovh->header.length));
359                 return -EINVAL;
360         }
361 }
362
363 /* 'msg', which is 'length' bytes long, was received across Netlink from
364  * 'sender'.  Apply it to 'chain'. */
365 int
366 fwd_control_input(struct sw_chain *chain, const struct sender *sender,
367                   const void *msg, size_t length)
368 {
369
370         struct openflow_packet {
371                 size_t min_size;
372                 int (*handler)(struct sw_chain *, const struct sender *,
373                                const void *);
374         };
375
376         static const struct openflow_packet packets[] = {
377                 [OFPT_HELLO] = {
378                         sizeof (struct ofp_header),
379                         recv_hello,
380                 },
381                 [OFPT_ECHO_REQUEST] = {
382                         sizeof (struct ofp_header),
383                         recv_echo_request,
384                 },
385                 [OFPT_ECHO_REPLY] = {
386                         sizeof (struct ofp_header),
387                         recv_echo_reply,
388                 },
389                 [OFPT_VENDOR] = {
390                         sizeof (struct ofp_vendor_header),
391                         recv_vendor,
392                 },
393                 [OFPT_FEATURES_REQUEST] = {
394                         sizeof (struct ofp_header),
395                         recv_features_request,
396                 },
397                 [OFPT_GET_CONFIG_REQUEST] = {
398                         sizeof (struct ofp_header),
399                         recv_get_config_request,
400                 },
401                 [OFPT_SET_CONFIG] = {
402                         sizeof (struct ofp_switch_config),
403                         recv_set_config,
404                 },
405                 [OFPT_PACKET_OUT] = {
406                         sizeof (struct ofp_packet_out),
407                         recv_packet_out,
408                 },
409                 [OFPT_FLOW_MOD] = {
410                         sizeof (struct ofp_flow_mod),
411                         recv_flow,
412                 },
413                 [OFPT_PORT_MOD] = {
414                         sizeof (struct ofp_port_mod),
415                         recv_port_mod,
416                 }
417         };
418
419         struct ofp_header *oh;
420
421         oh = (struct ofp_header *) msg;
422         if (oh->version != OFP_VERSION
423             && oh->type != OFPT_HELLO
424             && oh->type != OFPT_ERROR
425             && oh->type != OFPT_ECHO_REQUEST
426             && oh->type != OFPT_ECHO_REPLY
427             && oh->type != OFPT_VENDOR)
428         {
429                 dp_send_error_msg(chain->dp, sender, OFPET_BAD_REQUEST,
430                                   OFPBRC_BAD_VERSION, msg, length);
431                 return -EINVAL;
432         }
433         if (ntohs(oh->length) != length) {
434                 if (net_ratelimit())
435                         printk("received message length wrong: %d/%d\n", 
436                                 ntohs(oh->length), length);
437                 return -EINVAL;
438         }
439
440         if (oh->type < ARRAY_SIZE(packets)) {
441                 const struct openflow_packet *pkt = &packets[oh->type];
442                 if (pkt->handler) {
443                         if (length < pkt->min_size)
444                                 return -EFAULT;
445                         return pkt->handler(chain, sender, msg);
446                 }
447         }
448         dp_send_error_msg(chain->dp, sender, OFPET_BAD_REQUEST,
449                           OFPBRC_BAD_TYPE, msg, length);
450         return -EINVAL;
451 }
452
453 /* Packet buffering. */
454
455 #define OVERWRITE_SECS  1
456 #define OVERWRITE_JIFFIES (OVERWRITE_SECS * HZ)
457
458 struct packet_buffer {
459         struct sk_buff *skb;
460         uint32_t cookie;
461         unsigned long exp_jiffies;
462 };
463
464 static struct packet_buffer buffers[N_PKT_BUFFERS];
465 static unsigned int buffer_idx;
466 static DEFINE_SPINLOCK(buffer_lock);
467
468 uint32_t fwd_save_skb(struct sk_buff *skb)
469 {
470         struct sk_buff *old_skb = NULL;
471         struct packet_buffer *p;
472         unsigned long int flags;
473         uint32_t id;
474
475         spin_lock_irqsave(&buffer_lock, flags);
476         buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
477         p = &buffers[buffer_idx];
478         if (p->skb) {
479                 /* Don't buffer packet if existing entry is less than
480                  * OVERWRITE_SECS old. */
481                 if (time_before(jiffies, p->exp_jiffies)) {
482                         spin_unlock_irqrestore(&buffer_lock, flags);
483                         return -1;
484                 } else {
485                         /* Defer kfree_skb() until interrupts re-enabled. */
486                         old_skb = p->skb;
487                 }
488         }
489         /* Don't use maximum cookie value since the all-bits-1 id is
490          * special. */
491         if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
492                 p->cookie = 0;
493         skb_get(skb);
494         p->skb = skb;
495         p->exp_jiffies = jiffies + OVERWRITE_JIFFIES;
496         id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
497         spin_unlock_irqrestore(&buffer_lock, flags);
498
499         if (old_skb)
500                 kfree_skb(old_skb);
501
502         return id;
503 }
504
505 static struct sk_buff *retrieve_skb(uint32_t id)
506 {
507         unsigned long int flags;
508         struct sk_buff *skb = NULL;
509         struct packet_buffer *p;
510
511         spin_lock_irqsave(&buffer_lock, flags);
512         p = &buffers[id & PKT_BUFFER_MASK];
513         if (p->cookie == id >> PKT_BUFFER_BITS) {
514                 skb = p->skb;
515                 p->skb = NULL;
516         } else {
517                 printk("cookie mismatch: %x != %x\n",
518                                 id >> PKT_BUFFER_BITS, p->cookie);
519         }
520         spin_unlock_irqrestore(&buffer_lock, flags);
521
522         return skb;
523 }
524
525 void fwd_discard_all(void) 
526 {
527         int i;
528
529         for (i = 0; i < N_PKT_BUFFERS; i++) {
530                 struct sk_buff *skb;
531                 unsigned long int flags;
532
533                 /* Defer kfree_skb() until interrupts re-enabled. */
534                 spin_lock_irqsave(&buffer_lock, flags);
535                 skb = buffers[i].skb;
536                 buffers[i].skb = NULL;
537                 spin_unlock_irqrestore(&buffer_lock, flags);
538
539                 kfree_skb(skb);
540         }
541 }
542
543 static void discard_skb(uint32_t id)
544 {
545         struct sk_buff *old_skb = NULL;
546         unsigned long int flags;
547         struct packet_buffer *p;
548
549         spin_lock_irqsave(&buffer_lock, flags);
550         p = &buffers[id & PKT_BUFFER_MASK];
551         if (p->cookie == id >> PKT_BUFFER_BITS) {
552                 /* Defer kfree_skb() until interrupts re-enabled. */
553                 old_skb = p->skb;
554                 p->skb = NULL;
555         }
556         spin_unlock_irqrestore(&buffer_lock, flags);
557
558         if (old_skb)
559                 kfree_skb(old_skb);
560 }
561
562 void fwd_exit(void)
563 {
564         fwd_discard_all();
565 }