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>
19 /* FIXME: do we need to use GFP_ATOMIC everywhere here? */
22 static struct sk_buff *retrieve_skb(uint32_t id);
23 static void discard_skb(uint32_t id);
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)
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;
38 if (flow_extract(skb, p ? p->port_no : OFPP_NONE, &key)
39 && (chain->dp->flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP) {
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)) {
51 flow = chain_lookup(chain, &key);
52 if (likely(flow != NULL)) {
53 struct sw_flow_actions *sf_acts = rcu_dereference(flow->sf_acts);
55 execute_actions(chain->dp, skb, &key,
56 sf_acts->actions, sf_acts->actions_len, 0);
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)
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,
76 recv_hello(struct sw_chain *chain, const struct sender *sender,
79 return dp_send_hello(chain->dp, sender, msg);
83 recv_features_request(struct sw_chain *chain, const struct sender *sender,
86 return dp_send_features_reply(chain->dp, sender);
90 recv_get_config_request(struct sw_chain *chain, const struct sender *sender,
93 return dp_send_config_reply(chain->dp, sender);
97 recv_set_config(struct sw_chain *chain, const struct sender *sender,
100 const struct ofp_switch_config *osc = msg;
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;
108 chain->dp->flags = flags;
110 chain->dp->miss_send_len = ntohs(osc->miss_send_len);
116 recv_packet_out(struct sw_chain *chain, const struct sender *sender,
119 const struct ofp_packet_out *opo = msg;
121 struct vlan_ethhdr *mac;
124 struct sw_flow_key key;
125 size_t actions_len = ntohs(opo->actions_len);
127 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
129 printk("message too short for number of actions\n");
133 if (ntohl(opo->buffer_id) == (uint32_t) -1) {
134 int data_len = ntohs(opo->header.length) - sizeof *opo - actions_len;
136 /* FIXME: there is likely a way to reuse the data in msg. */
137 skb = alloc_skb(data_len, GFP_ATOMIC);
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,
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);
152 nh_ofs = sizeof(struct vlan_ethhdr);
153 skb_set_network_header(skb, nh_ofs);
155 skb = retrieve_skb(ntohl(opo->buffer_id));
160 dp_set_origin(chain->dp, ntohs(opo->in_port), skb);
162 flow_extract(skb, ntohs(opo->in_port), &key);
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));
171 execute_actions(chain->dp, skb, &key, opo->actions, actions_len, 1);
181 recv_port_mod(struct sw_chain *chain, const struct sender *sender,
184 const struct ofp_port_mod *opm = msg;
186 dp_update_port_flags(chain->dp, opm);
192 recv_echo_request(struct sw_chain *chain, const struct sender *sender,
195 return dp_send_echo_reply(chain->dp, sender, msg);
199 recv_echo_reply(struct sw_chain *chain, const struct sender *sender,
206 add_flow(struct sw_chain *chain, const struct sender *sender,
207 const struct ofp_flow_mod *ofm)
211 struct sw_flow *flow;
212 size_t actions_len = ntohs(ofm->header.length) - sizeof *ofm;
214 /* Allocate memory. */
215 flow = flow_alloc(actions_len, GFP_ATOMIC);
219 flow_extract_match(&flow->key, &ofm->match);
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));
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);
240 error = chain_insert(chain, flow);
242 goto error_free_flow;
244 if (ntohl(ofm->buffer_id) != (uint32_t) -1) {
245 struct sk_buff *skb = retrieve_skb(ntohl(ofm->buffer_id));
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);
260 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
261 discard_skb(ntohl(ofm->buffer_id));
266 mod_flow(struct sw_chain *chain, const struct sender *sender,
267 const struct ofp_flow_mod *ofm)
272 struct sw_flow_key key;
276 flow_extract_match(&key, &ofm->match);
278 actions_len = ntohs(ofm->header.length) - sizeof *ofm;
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));
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);
291 if (ntohl(ofm->buffer_id) != (uint32_t) -1) {
292 struct sk_buff *skb = retrieve_skb(ntohl(ofm->buffer_id));
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);
305 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
306 discard_skb(ntohl(ofm->buffer_id));
311 recv_flow(struct sw_chain *chain, const struct sender *sender, const void *msg)
313 const struct ofp_flow_mod *ofm = msg;
314 uint16_t command = ntohs(ofm->command);
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;
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;
335 /* 'msg', which is 'length' bytes long, was received across Netlink from
336 * 'sender'. Apply it to 'chain'. */
338 fwd_control_input(struct sw_chain *chain, const struct sender *sender,
339 const void *msg, size_t length)
342 struct openflow_packet {
344 int (*handler)(struct sw_chain *, const struct sender *,
348 static const struct openflow_packet packets[] = {
350 sizeof (struct ofp_header),
353 [OFPT_FEATURES_REQUEST] = {
354 sizeof (struct ofp_header),
355 recv_features_request,
357 [OFPT_GET_CONFIG_REQUEST] = {
358 sizeof (struct ofp_header),
359 recv_get_config_request,
361 [OFPT_SET_CONFIG] = {
362 sizeof (struct ofp_switch_config),
365 [OFPT_PACKET_OUT] = {
366 sizeof (struct ofp_packet_out),
370 sizeof (struct ofp_flow_mod),
374 sizeof (struct ofp_port_mod),
377 [OFPT_ECHO_REQUEST] = {
378 sizeof (struct ofp_header),
381 [OFPT_ECHO_REPLY] = {
382 sizeof (struct ofp_header),
387 struct ofp_header *oh;
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)
397 dp_send_error_msg(chain->dp, sender, OFPET_BAD_REQUEST,
398 OFPBRC_BAD_VERSION, msg, length);
401 if (ntohs(oh->length) > length)
404 if (oh->type < ARRAY_SIZE(packets)) {
405 const struct openflow_packet *pkt = &packets[oh->type];
407 if (length < pkt->min_size)
409 return pkt->handler(chain, sender, msg);
412 dp_send_error_msg(chain->dp, sender, OFPET_BAD_REQUEST,
413 OFPBRC_BAD_TYPE, msg, length);
417 /* Packet buffering. */
419 #define OVERWRITE_SECS 1
420 #define OVERWRITE_JIFFIES (OVERWRITE_SECS * HZ)
422 struct packet_buffer {
425 unsigned long exp_jiffies;
428 static struct packet_buffer buffers[N_PKT_BUFFERS];
429 static unsigned int buffer_idx;
430 static DEFINE_SPINLOCK(buffer_lock);
432 uint32_t fwd_save_skb(struct sk_buff *skb)
434 struct sk_buff *old_skb = NULL;
435 struct packet_buffer *p;
436 unsigned long int flags;
439 spin_lock_irqsave(&buffer_lock, flags);
440 buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
441 p = &buffers[buffer_idx];
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);
449 /* Defer kfree_skb() until interrupts re-enabled. */
453 /* Don't use maximum cookie value since the all-bits-1 id is
455 if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
459 p->exp_jiffies = jiffies + OVERWRITE_JIFFIES;
460 id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
461 spin_unlock_irqrestore(&buffer_lock, flags);
469 static struct sk_buff *retrieve_skb(uint32_t id)
471 unsigned long int flags;
472 struct sk_buff *skb = NULL;
473 struct packet_buffer *p;
475 spin_lock_irqsave(&buffer_lock, flags);
476 p = &buffers[id & PKT_BUFFER_MASK];
477 if (p->cookie == id >> PKT_BUFFER_BITS) {
481 printk("cookie mismatch: %x != %x\n",
482 id >> PKT_BUFFER_BITS, p->cookie);
484 spin_unlock_irqrestore(&buffer_lock, flags);
489 void fwd_discard_all(void)
493 for (i = 0; i < N_PKT_BUFFERS; i++) {
495 unsigned long int flags;
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);
507 static void discard_skb(uint32_t id)
509 struct sk_buff *old_skb = NULL;
510 unsigned long int flags;
511 struct packet_buffer *p;
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. */
520 spin_unlock_irqrestore(&buffer_lock, flags);