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