Properly set in_port in skb for Flow Mod messages.
[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                         dp_set_origin(chain->dp, ntohs(ofm->match.in_port), skb);
250                         flow_extract(skb, ntohs(ofm->match.in_port), &key);
251                         execute_actions(chain->dp, skb, &key, ofm->actions, actions_len, 0);
252                 }
253                 else
254                         error = -ESRCH;
255         }
256         return error;
257
258 error_free_flow:
259         flow_free(flow);
260 error:
261         if (ntohl(ofm->buffer_id) != (uint32_t) -1)
262                 discard_skb(ntohl(ofm->buffer_id));
263         return error;
264 }
265
266 static int
267 mod_flow(struct sw_chain *chain, const struct sender *sender,
268                 const struct ofp_flow_mod *ofm)
269 {
270         int error = -ENOMEM;
271         uint16_t v_code;
272         size_t actions_len;
273         struct sw_flow_key key;
274         uint16_t priority;
275         int strict;
276
277         flow_extract_match(&key, &ofm->match);
278
279         actions_len = ntohs(ofm->header.length) - sizeof *ofm;
280
281         v_code = validate_actions(chain->dp, &key, ofm->actions, actions_len);
282         if (v_code != ACT_VALIDATION_OK) {
283                 dp_send_error_msg(chain->dp, sender, OFPET_BAD_ACTION, v_code,
284                                   ofm, ntohs(ofm->header.length));
285                 goto error;
286         }
287
288         priority = key.wildcards ? ntohs(ofm->priority) : -1;
289         strict = (ofm->command == htons(OFPFC_MODIFY_STRICT)) ? 1 : 0;
290         chain_modify(chain, &key, priority, strict, ofm->actions, actions_len);
291
292         if (ntohl(ofm->buffer_id) != (uint32_t) -1) {
293                 struct sk_buff *skb = retrieve_skb(ntohl(ofm->buffer_id));
294                 if (skb) {
295                         struct sw_flow_key skb_key;
296                         flow_extract(skb, ntohs(ofm->match.in_port), &skb_key);
297                         execute_actions(chain->dp, skb, &skb_key, 
298                                         ofm->actions, actions_len, 0);
299                 }
300                 else
301                         error = -ESRCH;
302         }
303         return error;
304
305 error:
306         if (ntohl(ofm->buffer_id) != (uint32_t) -1)
307                 discard_skb(ntohl(ofm->buffer_id));
308         return error;
309 }
310
311 static int
312 recv_flow(struct sw_chain *chain, const struct sender *sender, const void *msg)
313 {
314         const struct ofp_flow_mod *ofm = msg;
315         uint16_t command = ntohs(ofm->command);
316
317         if (command == OFPFC_ADD) {
318                 return add_flow(chain, sender, ofm);
319         } else if ((command == OFPFC_MODIFY) || (command == OFPFC_MODIFY_STRICT)) {
320                 return mod_flow(chain, sender, ofm);
321         }  else if (command == OFPFC_DELETE) {
322                 struct sw_flow_key key;
323                 flow_extract_match(&key, &ofm->match);
324                 return chain_delete(chain, &key, 0, 0) ? 0 : -ESRCH;
325         } else if (command == OFPFC_DELETE_STRICT) {
326                 struct sw_flow_key key;
327                 uint16_t priority;
328                 flow_extract_match(&key, &ofm->match);
329                 priority = key.wildcards ? ntohs(ofm->priority) : -1;
330                 return chain_delete(chain, &key, priority, 1) ? 0 : -ESRCH;
331         } else {
332                 return -ENOTSUPP;
333         }
334 }
335
336 /* 'msg', which is 'length' bytes long, was received across Netlink from
337  * 'sender'.  Apply it to 'chain'. */
338 int
339 fwd_control_input(struct sw_chain *chain, const struct sender *sender,
340                   const void *msg, size_t length)
341 {
342
343         struct openflow_packet {
344                 size_t min_size;
345                 int (*handler)(struct sw_chain *, const struct sender *,
346                                const void *);
347         };
348
349         static const struct openflow_packet packets[] = {
350                 [OFPT_HELLO] = {
351                         sizeof (struct ofp_header),
352                         recv_hello,
353                 },
354                 [OFPT_FEATURES_REQUEST] = {
355                         sizeof (struct ofp_header),
356                         recv_features_request,
357                 },
358                 [OFPT_GET_CONFIG_REQUEST] = {
359                         sizeof (struct ofp_header),
360                         recv_get_config_request,
361                 },
362                 [OFPT_SET_CONFIG] = {
363                         sizeof (struct ofp_switch_config),
364                         recv_set_config,
365                 },
366                 [OFPT_PACKET_OUT] = {
367                         sizeof (struct ofp_packet_out),
368                         recv_packet_out,
369                 },
370                 [OFPT_FLOW_MOD] = {
371                         sizeof (struct ofp_flow_mod),
372                         recv_flow,
373                 },
374                 [OFPT_PORT_MOD] = {
375                         sizeof (struct ofp_port_mod),
376                         recv_port_mod,
377                 },
378                 [OFPT_ECHO_REQUEST] = {
379                         sizeof (struct ofp_header),
380                         recv_echo_request,
381                 },
382                 [OFPT_ECHO_REPLY] = {
383                         sizeof (struct ofp_header),
384                         recv_echo_reply,
385                 },
386         };
387
388         struct ofp_header *oh;
389
390         oh = (struct ofp_header *) msg;
391         if (oh->version != OFP_VERSION
392             && oh->type != OFPT_HELLO
393             && oh->type != OFPT_ERROR
394             && oh->type != OFPT_ECHO_REQUEST
395             && oh->type != OFPT_ECHO_REPLY
396             && oh->type != OFPT_VENDOR)
397         {
398                 dp_send_error_msg(chain->dp, sender, OFPET_BAD_REQUEST,
399                                   OFPBRC_BAD_VERSION, msg, length);
400                 return -EINVAL;
401         }
402         if (ntohs(oh->length) > length)
403                 return -EINVAL;
404
405         if (oh->type < ARRAY_SIZE(packets)) {
406                 const struct openflow_packet *pkt = &packets[oh->type];
407                 if (pkt->handler) {
408                         if (length < pkt->min_size)
409                                 return -EFAULT;
410                         return pkt->handler(chain, sender, msg);
411                 }
412         }
413         dp_send_error_msg(chain->dp, sender, OFPET_BAD_REQUEST,
414                           OFPBRC_BAD_TYPE, msg, length);
415         return -EINVAL;
416 }
417
418 /* Packet buffering. */
419
420 #define OVERWRITE_SECS  1
421 #define OVERWRITE_JIFFIES (OVERWRITE_SECS * HZ)
422
423 struct packet_buffer {
424         struct sk_buff *skb;
425         uint32_t cookie;
426         unsigned long exp_jiffies;
427 };
428
429 static struct packet_buffer buffers[N_PKT_BUFFERS];
430 static unsigned int buffer_idx;
431 static DEFINE_SPINLOCK(buffer_lock);
432
433 uint32_t fwd_save_skb(struct sk_buff *skb)
434 {
435         struct sk_buff *old_skb = NULL;
436         struct packet_buffer *p;
437         unsigned long int flags;
438         uint32_t id;
439
440         spin_lock_irqsave(&buffer_lock, flags);
441         buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
442         p = &buffers[buffer_idx];
443         if (p->skb) {
444                 /* Don't buffer packet if existing entry is less than
445                  * OVERWRITE_SECS old. */
446                 if (time_before(jiffies, p->exp_jiffies)) {
447                         spin_unlock_irqrestore(&buffer_lock, flags);
448                         return -1;
449                 } else {
450                         /* Defer kfree_skb() until interrupts re-enabled. */
451                         old_skb = p->skb;
452                 }
453         }
454         /* Don't use maximum cookie value since the all-bits-1 id is
455          * special. */
456         if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
457                 p->cookie = 0;
458         skb_get(skb);
459         p->skb = skb;
460         p->exp_jiffies = jiffies + OVERWRITE_JIFFIES;
461         id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
462         spin_unlock_irqrestore(&buffer_lock, flags);
463
464         if (old_skb)
465                 kfree_skb(old_skb);
466
467         return id;
468 }
469
470 static struct sk_buff *retrieve_skb(uint32_t id)
471 {
472         unsigned long int flags;
473         struct sk_buff *skb = NULL;
474         struct packet_buffer *p;
475
476         spin_lock_irqsave(&buffer_lock, flags);
477         p = &buffers[id & PKT_BUFFER_MASK];
478         if (p->cookie == id >> PKT_BUFFER_BITS) {
479                 skb = p->skb;
480                 p->skb = NULL;
481         } else {
482                 printk("cookie mismatch: %x != %x\n",
483                                 id >> PKT_BUFFER_BITS, p->cookie);
484         }
485         spin_unlock_irqrestore(&buffer_lock, flags);
486
487         return skb;
488 }
489
490 void fwd_discard_all(void) 
491 {
492         int i;
493
494         for (i = 0; i < N_PKT_BUFFERS; i++) {
495                 struct sk_buff *skb;
496                 unsigned long int flags;
497
498                 /* Defer kfree_skb() until interrupts re-enabled. */
499                 spin_lock_irqsave(&buffer_lock, flags);
500                 skb = buffers[i].skb;
501                 buffers[i].skb = NULL;
502                 spin_unlock_irqrestore(&buffer_lock, flags);
503
504                 kfree_skb(skb);
505         }
506 }
507
508 static void discard_skb(uint32_t id)
509 {
510         struct sk_buff *old_skb = NULL;
511         unsigned long int flags;
512         struct packet_buffer *p;
513
514         spin_lock_irqsave(&buffer_lock, flags);
515         p = &buffers[id & PKT_BUFFER_MASK];
516         if (p->cookie == id >> PKT_BUFFER_BITS) {
517                 /* Defer kfree_skb() until interrupts re-enabled. */
518                 old_skb = p->skb;
519                 p->skb = NULL;
520         }
521         spin_unlock_irqrestore(&buffer_lock, flags);
522
523         if (old_skb)
524                 kfree_skb(old_skb);
525 }
526
527 void fwd_exit(void)
528 {
529         fwd_discard_all();
530 }
531