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