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/if_ether.h>
8 #include <linux/if_vlan.h>
11 #include <linux/tcp.h>
12 #include <linux/udp.h>
13 #include <linux/in6.h>
14 #include <asm/uaccess.h>
15 #include <linux/types.h>
16 #include <net/checksum.h>
22 /* FIXME: do we need to use GFP_ATOMIC everywhere here? */
24 static void execute_actions(struct datapath *, struct sk_buff *,
25 const struct sw_flow_key *,
26 const struct ofp_action *, int n_actions);
27 static int make_writable(struct sk_buff **);
29 static struct sk_buff *retrieve_skb(uint32_t id);
30 static void discard_skb(uint32_t id);
32 /* 'skb' was received on 'in_port', a physical switch port between 0 and
33 * OFPP_MAX. Process it according to 'chain'. */
34 void fwd_port_input(struct sw_chain *chain, struct sk_buff *skb, int in_port)
36 struct sw_flow_key key;
39 flow_extract(skb, in_port, &key);
40 flow = chain_lookup(chain, &key);
41 if (likely(flow != NULL)) {
43 execute_actions(chain->dp, skb, &key,
44 flow->actions, flow->n_actions);
46 dp_output_control(chain->dp, skb, fwd_save_skb(skb),
47 chain->dp->config.miss_send_len,
52 static int do_output(struct datapath *dp, struct sk_buff *skb, size_t max_len,
57 return (likely(out_port != OFPP_CONTROLLER)
58 ? dp_output_port(dp, skb, out_port)
59 : dp_output_control(dp, skb, fwd_save_skb(skb),
60 max_len, OFPR_ACTION));
63 static void execute_actions(struct datapath *dp, struct sk_buff *skb,
64 const struct sw_flow_key *key,
65 const struct ofp_action *actions, int n_actions)
67 /* Every output action needs a separate clone of 'skb', but the common
68 * case is just a single output action, so that doing a clone and
69 * then freeing the original skbuff is wasteful. So the following code
70 * is slightly obscure just to avoid that. */
72 size_t max_len=0; /* Initialze to make compiler happy */
77 eth_proto = ntohs(key->dl_type);
79 for (i = 0; i < n_actions; i++) {
80 const struct ofp_action *a = &actions[i];
82 if (prev_port != -1) {
83 do_output(dp, skb_clone(skb, GFP_ATOMIC),
88 if (likely(a->type == htons(OFPAT_OUTPUT))) {
89 prev_port = ntohs(a->arg.output.port);
90 max_len = ntohs(a->arg.output.max_len);
92 if (!make_writable(&skb)) {
93 printk("make_writable failed\n");
96 skb = execute_setter(skb, eth_proto, key, a);
100 do_output(dp, skb, max_len, prev_port);
105 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
106 * covered by the sum has been changed from 'from' to 'to'. If set,
107 * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
108 * Based on nf_proto_csum_replace4. */
109 static void update_csum(__sum16 *sum, struct sk_buff *skb,
110 __be32 from, __be32 to, int pseudohdr)
112 __be32 diff[] = { ~from, to };
113 if (skb->ip_summed != CHECKSUM_PARTIAL) {
114 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
115 ~csum_unfold(*sum)));
116 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
117 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
119 } else if (pseudohdr)
120 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
124 static void modify_nh(struct sk_buff *skb, uint16_t eth_proto,
125 uint8_t nw_proto, const struct ofp_action *a)
127 if (eth_proto == ETH_P_IP) {
128 struct iphdr *nh = ip_hdr(skb);
129 uint32_t new, *field;
131 new = a->arg.nw_addr;
133 if (a->type == htons(OFPAT_SET_NW_SRC))
138 if (nw_proto == IPPROTO_TCP) {
139 struct tcphdr *th = tcp_hdr(skb);
140 update_csum(&th->check, skb, *field, new, 1);
141 } else if (nw_proto == IPPROTO_UDP) {
142 struct udphdr *th = udp_hdr(skb);
143 update_csum(&th->check, skb, *field, new, 1);
145 update_csum(&nh->check, skb, *field, new, 0);
150 static void modify_th(struct sk_buff *skb, uint16_t eth_proto,
151 uint8_t nw_proto, const struct ofp_action *a)
153 if (eth_proto == ETH_P_IP) {
154 uint16_t new, *field;
158 if (nw_proto == IPPROTO_TCP) {
159 struct tcphdr *th = tcp_hdr(skb);
161 if (a->type == htons(OFPAT_SET_TP_SRC))
166 update_csum(&th->check, skb, *field, new, 1);
168 } else if (nw_proto == IPPROTO_UDP) {
169 struct udphdr *th = udp_hdr(skb);
171 if (a->type == htons(OFPAT_SET_TP_SRC))
176 update_csum(&th->check, skb, *field, new, 1);
182 static struct sk_buff *vlan_pull_tag(struct sk_buff *skb)
184 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
188 /* Verify we were given a vlan packet */
189 if (vh->h_vlan_proto != htons(ETH_P_8021Q))
192 memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
194 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
196 skb->protocol = eh->h_proto;
197 skb->mac_header += VLAN_HLEN;
202 static struct sk_buff *modify_vlan(struct sk_buff *skb,
203 const struct sw_flow_key *key, const struct ofp_action *a)
205 uint16_t new_id = a->arg.vlan_id;
207 if (new_id != OFP_VLAN_NONE) {
208 if (key->dl_vlan != htons(OFP_VLAN_NONE)) {
209 /* Modify vlan id, but maintain other TCI values */
210 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
211 vh->h_vlan_TCI = (vh->h_vlan_TCI
212 & ~(htons(VLAN_VID_MASK))) | htons(new_id);
214 /* Add vlan header */
215 skb = vlan_put_tag(skb, new_id);
218 /* Remove an existing vlan header if it exists */
225 struct sk_buff *execute_setter(struct sk_buff *skb, uint16_t eth_proto,
226 const struct sw_flow_key *key, const struct ofp_action *a)
228 switch (ntohs(a->type)) {
229 case OFPAT_SET_DL_VLAN:
230 skb = modify_vlan(skb, key, a);
233 case OFPAT_SET_DL_SRC: {
234 struct ethhdr *eh = eth_hdr(skb);
235 memcpy(eh->h_source, a->arg.dl_addr, sizeof eh->h_source);
238 case OFPAT_SET_DL_DST: {
239 struct ethhdr *eh = eth_hdr(skb);
240 memcpy(eh->h_dest, a->arg.dl_addr, sizeof eh->h_dest);
244 case OFPAT_SET_NW_SRC:
245 case OFPAT_SET_NW_DST:
246 modify_nh(skb, eth_proto, key->nw_proto, a);
249 case OFPAT_SET_TP_SRC:
250 case OFPAT_SET_TP_DST:
251 modify_th(skb, eth_proto, key->nw_proto, a);
256 printk("execute_setter: unknown action: %d\n", ntohs(a->type));
263 recv_features_request(struct sw_chain *chain, const void *msg)
265 const struct ofp_header *ofr = msg;
266 return dp_send_features_reply(chain->dp, ofr->xid);
270 recv_get_config_request(struct sw_chain *chain, const void *msg)
272 const struct ofp_header *ofr = msg;
273 return dp_send_config_reply(chain->dp, ofr->xid);
277 recv_set_config(struct sw_chain *chain, const void *msg)
279 const struct ofp_switch_config *osc = msg;
280 chain->dp->config = *osc;
285 recv_packet_out(struct sw_chain *chain, const void *msg)
287 const struct ofp_packet_out *opo = msg;
289 struct vlan_ethhdr *mac;
292 if (ntohl(opo->buffer_id) == (uint32_t) -1) {
293 int data_len = ntohs(opo->header.length) - sizeof *opo;
295 /* FIXME: there is likely a way to reuse the data in msg. */
296 skb = alloc_skb(data_len, GFP_ATOMIC);
300 /* FIXME? We don't reserve NET_IP_ALIGN or NET_SKB_PAD since
301 * we're just transmitting this raw without examining anything
302 * at those layers. */
303 memcpy(skb_put(skb, data_len), opo->u.data, data_len);
304 dp_set_origin(chain->dp, ntohs(opo->in_port), skb);
306 skb_set_mac_header(skb, 0);
307 mac = vlan_eth_hdr(skb);
308 if (likely(mac->h_vlan_proto != htons(ETH_P_8021Q)))
309 nh_ofs = sizeof(struct ethhdr);
311 nh_ofs = sizeof(struct vlan_ethhdr);
312 skb_set_network_header(skb, nh_ofs);
314 dp_output_port(chain->dp, skb, ntohs(opo->out_port));
316 struct sw_flow_key key;
319 skb = retrieve_skb(ntohl(opo->buffer_id));
322 dp_set_origin(chain->dp, ntohs(opo->in_port), skb);
324 n_acts = (ntohs(opo->header.length) - sizeof *opo)
325 / sizeof *opo->u.actions;
326 flow_extract(skb, ntohs(opo->in_port), &key);
327 execute_actions(chain->dp, skb, &key, opo->u.actions, n_acts);
333 recv_port_mod(struct sw_chain *chain, const void *msg)
335 const struct ofp_port_mod *opm = msg;
337 dp_update_port_flags(chain->dp, &opm->desc);
343 add_flow(struct sw_chain *chain, const struct ofp_flow_mod *ofm)
347 struct sw_flow *flow;
350 /* Check number of actions. */
351 n_acts = (ntohs(ofm->header.length) - sizeof *ofm) / sizeof *ofm->actions;
352 if (n_acts > MAX_ACTIONS) {
357 /* Allocate memory. */
358 flow = flow_alloc(n_acts, GFP_ATOMIC);
363 flow_extract_match(&flow->key, &ofm->match);
364 flow->group_id = ntohl(ofm->group_id);
365 flow->max_idle = ntohs(ofm->max_idle);
366 flow->timeout = jiffies + flow->max_idle * HZ;
367 flow->n_actions = n_acts;
368 flow->init_time = jiffies;
369 flow->byte_count = 0;
370 flow->packet_count = 0;
371 atomic_set(&flow->deleted, 0);
372 spin_lock_init(&flow->lock);
373 memcpy(flow->actions, ofm->actions, n_acts * sizeof *flow->actions);
376 error = chain_insert(chain, flow);
378 goto error_free_flow;
380 if (ntohl(ofm->buffer_id) != (uint32_t) -1) {
381 struct sk_buff *skb = retrieve_skb(ntohl(ofm->buffer_id));
383 struct sw_flow_key key;
384 flow_used(flow, skb);
385 flow_extract(skb, ntohs(ofm->match.in_port), &key);
386 execute_actions(chain->dp, skb, &key,
387 ofm->actions, n_acts);
397 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
398 discard_skb(ntohl(ofm->buffer_id));
403 recv_flow(struct sw_chain *chain, const void *msg)
405 const struct ofp_flow_mod *ofm = msg;
406 uint16_t command = ntohs(ofm->command);
408 if (command == OFPFC_ADD) {
409 return add_flow(chain, ofm);
410 } else if (command == OFPFC_DELETE) {
411 struct sw_flow_key key;
412 flow_extract_match(&key, &ofm->match);
413 return chain_delete(chain, &key, 0) ? 0 : -ESRCH;
414 } else if (command == OFPFC_DELETE_STRICT) {
415 struct sw_flow_key key;
416 flow_extract_match(&key, &ofm->match);
417 return chain_delete(chain, &key, 1) ? 0 : -ESRCH;
423 /* 'msg', which is 'length' bytes long, was received from the control path.
424 * Apply it to 'chain'. */
426 fwd_control_input(struct sw_chain *chain, const void *msg, size_t length)
429 struct openflow_packet {
431 int (*handler)(struct sw_chain *, const void *);
434 static const struct openflow_packet packets[] = {
435 [OFPT_FEATURES_REQUEST] = {
436 sizeof (struct ofp_header),
437 recv_features_request,
439 [OFPT_GET_CONFIG_REQUEST] = {
440 sizeof (struct ofp_header),
441 recv_get_config_request,
443 [OFPT_SET_CONFIG] = {
444 sizeof (struct ofp_switch_config),
447 [OFPT_PACKET_OUT] = {
448 sizeof (struct ofp_packet_out),
452 sizeof (struct ofp_flow_mod),
456 sizeof (struct ofp_port_mod),
461 const struct openflow_packet *pkt;
462 struct ofp_header *oh;
464 if (length < sizeof(struct ofp_header))
467 oh = (struct ofp_header *) msg;
468 if (oh->version != 1 || oh->type >= ARRAY_SIZE(packets)
469 || ntohs(oh->length) > length)
472 pkt = &packets[oh->type];
475 if (length < pkt->min_size)
478 return pkt->handler(chain, msg);
481 /* Packet buffering. */
483 #define OVERWRITE_SECS 1
484 #define OVERWRITE_JIFFIES (OVERWRITE_SECS * HZ)
486 struct packet_buffer {
489 unsigned long exp_jiffies;
492 static struct packet_buffer buffers[N_PKT_BUFFERS];
493 static unsigned int buffer_idx;
494 static DEFINE_SPINLOCK(buffer_lock);
496 uint32_t fwd_save_skb(struct sk_buff *skb)
498 struct packet_buffer *p;
499 unsigned long int flags;
502 spin_lock_irqsave(&buffer_lock, flags);
503 buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
504 p = &buffers[buffer_idx];
506 /* Don't buffer packet if existing entry is less than
507 * OVERWRITE_SECS old. */
508 if (time_before(jiffies, p->exp_jiffies)) {
509 spin_unlock_irqrestore(&buffer_lock, flags);
514 /* Don't use maximum cookie value since the all-bits-1 id is
516 if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
520 p->exp_jiffies = jiffies + OVERWRITE_JIFFIES;
521 id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
522 spin_unlock_irqrestore(&buffer_lock, flags);
527 static struct sk_buff *retrieve_skb(uint32_t id)
529 unsigned long int flags;
530 struct sk_buff *skb = NULL;
531 struct packet_buffer *p;
533 spin_lock_irqsave(&buffer_lock, flags);
534 p = &buffers[id & PKT_BUFFER_MASK];
535 if (p->cookie == id >> PKT_BUFFER_BITS) {
539 printk("cookie mismatch: %x != %x\n",
540 id >> PKT_BUFFER_BITS, p->cookie);
542 spin_unlock_irqrestore(&buffer_lock, flags);
547 static void discard_skb(uint32_t id)
549 unsigned long int flags;
550 struct packet_buffer *p;
552 spin_lock_irqsave(&buffer_lock, flags);
553 p = &buffers[id & PKT_BUFFER_MASK];
554 if (p->cookie == id >> PKT_BUFFER_BITS) {
558 spin_unlock_irqrestore(&buffer_lock, flags);
565 for (i = 0; i < N_PKT_BUFFERS; i++)
566 kfree_skb(buffers[i].skb);
569 /* Utility functions. */
571 /* Makes '*pskb' writable, possibly copying it and setting '*pskb' to point to
573 * Returns 1 if successful, 0 on failure. */
575 make_writable(struct sk_buff **pskb)
577 /* Based on skb_make_writable() in net/netfilter/core.c. */
578 struct sk_buff *nskb;
580 /* Not exclusive use of packet? Must copy. */
581 if (skb_shared(*pskb) || skb_cloned(*pskb))
584 return pskb_may_pull(*pskb, 64); /* FIXME? */
587 nskb = skb_copy(*pskb, GFP_ATOMIC);
590 BUG_ON(skb_is_nonlinear(nskb));
592 /* Rest of kernel will get very unhappy if we pass it a
593 suddenly-orphaned skbuff */
595 skb_set_owner_w(nskb, (*pskb)->sk);