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
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>
15 #include "nicira-ext.h"
21 /* FIXME: do we need to use GFP_ATOMIC everywhere here? */
24 static struct sk_buff *retrieve_skb(uint32_t id);
25 static void discard_skb(uint32_t id);
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)
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;
40 if (flow_extract(skb, p ? p->port_no : OFPP_NONE, &key)
41 && (chain->dp->flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP) {
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)) {
53 flow = chain_lookup(chain, &key);
54 if (likely(flow != NULL)) {
55 struct sw_flow_actions *sf_acts = rcu_dereference(flow->sf_acts);
57 execute_actions(chain->dp, skb, &key,
58 sf_acts->actions, sf_acts->actions_len, 0);
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)
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,
78 recv_hello(struct sw_chain *chain, const struct sender *sender,
81 return dp_send_hello(chain->dp, sender, msg);
85 recv_features_request(struct sw_chain *chain, const struct sender *sender,
88 return dp_send_features_reply(chain->dp, sender);
92 recv_get_config_request(struct sw_chain *chain, const struct sender *sender,
95 return dp_send_config_reply(chain->dp, sender);
99 recv_set_config(struct sw_chain *chain, const struct sender *sender,
102 const struct ofp_switch_config *osc = msg;
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;
110 chain->dp->flags = flags;
112 chain->dp->miss_send_len = ntohs(osc->miss_send_len);
118 recv_packet_out(struct sw_chain *chain, const struct sender *sender,
121 const struct ofp_packet_out *opo = msg;
123 struct vlan_ethhdr *mac;
126 struct sw_flow_key key;
127 size_t actions_len = ntohs(opo->actions_len);
129 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
131 printk("message too short for number of actions\n");
135 if (ntohl(opo->buffer_id) == (uint32_t) -1) {
136 int data_len = ntohs(opo->header.length) - sizeof *opo - actions_len;
138 /* FIXME: there is likely a way to reuse the data in msg. */
139 skb = alloc_skb(data_len, GFP_ATOMIC);
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,
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);
154 nh_ofs = sizeof(struct vlan_ethhdr);
155 skb_set_network_header(skb, nh_ofs);
157 skb = retrieve_skb(ntohl(opo->buffer_id));
162 dp_set_origin(chain->dp, ntohs(opo->in_port), skb);
164 flow_extract(skb, ntohs(opo->in_port), &key);
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));
173 execute_actions(chain->dp, skb, &key, opo->actions, actions_len, 1);
183 recv_port_mod(struct sw_chain *chain, const struct sender *sender,
186 const struct ofp_port_mod *opm = msg;
188 dp_update_port_flags(chain->dp, opm);
194 recv_echo_request(struct sw_chain *chain, const struct sender *sender,
197 return dp_send_echo_reply(chain->dp, sender, msg);
201 recv_echo_reply(struct sw_chain *chain, const struct sender *sender,
208 add_flow(struct sw_chain *chain, const struct sender *sender,
209 const struct ofp_flow_mod *ofm)
213 struct sw_flow *flow;
214 size_t actions_len = ntohs(ofm->header.length) - sizeof *ofm;
216 /* Allocate memory. */
217 flow = flow_alloc(actions_len, GFP_ATOMIC);
221 flow_extract_match(&flow->key, &ofm->match);
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;
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);
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;
248 goto error_free_flow;
250 if (ntohl(ofm->buffer_id) != (uint32_t) -1) {
251 struct sk_buff *skb = retrieve_skb(ntohl(ofm->buffer_id));
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);
267 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
268 discard_skb(ntohl(ofm->buffer_id));
273 mod_flow(struct sw_chain *chain, const struct sender *sender,
274 const struct ofp_flow_mod *ofm)
279 struct sw_flow_key key;
283 flow_extract_match(&key, &ofm->match);
285 actions_len = ntohs(ofm->header.length) - sizeof *ofm;
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));
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);
298 if (ntohl(ofm->buffer_id) != (uint32_t) -1) {
299 struct sk_buff *skb = retrieve_skb(ntohl(ofm->buffer_id));
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);
312 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
313 discard_skb(ntohl(ofm->buffer_id));
318 recv_flow(struct sw_chain *chain, const struct sender *sender, const void *msg)
320 const struct ofp_flow_mod *ofm = msg;
321 uint16_t command = ntohs(ofm->command);
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;
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;
343 recv_vendor(struct sw_chain *chain, const struct sender *sender,
346 const struct ofp_vendor_header *ovh = msg;
348 switch(ntohl(ovh->vendor))
351 return nx_recv_msg(chain, sender, msg);
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));
361 /* 'msg', which is 'length' bytes long, was received across Netlink from
362 * 'sender'. Apply it to 'chain'. */
364 fwd_control_input(struct sw_chain *chain, const struct sender *sender,
365 const void *msg, size_t length)
368 struct openflow_packet {
370 int (*handler)(struct sw_chain *, const struct sender *,
374 static const struct openflow_packet packets[] = {
376 sizeof (struct ofp_header),
379 [OFPT_ECHO_REQUEST] = {
380 sizeof (struct ofp_header),
383 [OFPT_ECHO_REPLY] = {
384 sizeof (struct ofp_header),
388 sizeof (struct ofp_vendor_header),
391 [OFPT_FEATURES_REQUEST] = {
392 sizeof (struct ofp_header),
393 recv_features_request,
395 [OFPT_GET_CONFIG_REQUEST] = {
396 sizeof (struct ofp_header),
397 recv_get_config_request,
399 [OFPT_SET_CONFIG] = {
400 sizeof (struct ofp_switch_config),
403 [OFPT_PACKET_OUT] = {
404 sizeof (struct ofp_packet_out),
408 sizeof (struct ofp_flow_mod),
412 sizeof (struct ofp_port_mod),
417 struct ofp_header *oh;
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)
427 dp_send_error_msg(chain->dp, sender, OFPET_BAD_REQUEST,
428 OFPBRC_BAD_VERSION, msg, length);
431 if (ntohs(oh->length) != length) {
433 printk("received message length wrong: %d/%d\n",
434 ntohs(oh->length), length);
438 if (oh->type < ARRAY_SIZE(packets)) {
439 const struct openflow_packet *pkt = &packets[oh->type];
441 if (length < pkt->min_size)
443 return pkt->handler(chain, sender, msg);
446 dp_send_error_msg(chain->dp, sender, OFPET_BAD_REQUEST,
447 OFPBRC_BAD_TYPE, msg, length);
451 /* Packet buffering. */
453 #define OVERWRITE_SECS 1
454 #define OVERWRITE_JIFFIES (OVERWRITE_SECS * HZ)
456 struct packet_buffer {
459 unsigned long exp_jiffies;
462 static struct packet_buffer buffers[N_PKT_BUFFERS];
463 static unsigned int buffer_idx;
464 static DEFINE_SPINLOCK(buffer_lock);
466 uint32_t fwd_save_skb(struct sk_buff *skb)
468 struct sk_buff *old_skb = NULL;
469 struct packet_buffer *p;
470 unsigned long int flags;
473 spin_lock_irqsave(&buffer_lock, flags);
474 buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
475 p = &buffers[buffer_idx];
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);
483 /* Defer kfree_skb() until interrupts re-enabled. */
487 /* Don't use maximum cookie value since the all-bits-1 id is
489 if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
493 p->exp_jiffies = jiffies + OVERWRITE_JIFFIES;
494 id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
495 spin_unlock_irqrestore(&buffer_lock, flags);
503 static struct sk_buff *retrieve_skb(uint32_t id)
505 unsigned long int flags;
506 struct sk_buff *skb = NULL;
507 struct packet_buffer *p;
509 spin_lock_irqsave(&buffer_lock, flags);
510 p = &buffers[id & PKT_BUFFER_MASK];
511 if (p->cookie == id >> PKT_BUFFER_BITS) {
515 printk("cookie mismatch: %x != %x\n",
516 id >> PKT_BUFFER_BITS, p->cookie);
518 spin_unlock_irqrestore(&buffer_lock, flags);
523 void fwd_discard_all(void)
527 for (i = 0; i < N_PKT_BUFFERS; i++) {
529 unsigned long int flags;
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);
541 static void discard_skb(uint32_t id)
543 struct sk_buff *old_skb = NULL;
544 unsigned long int flags;
545 struct packet_buffer *p;
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. */
554 spin_unlock_irqrestore(&buffer_lock, flags);