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 int make_writable(struct sk_buff **);
26 static struct sk_buff *retrieve_skb(uint32_t id);
27 static void discard_skb(uint32_t id);
29 /* 'skb' was received on 'in_port', a physical switch port between 0 and
30 * OFPP_MAX. Process it according to 'chain'. */
31 void fwd_port_input(struct sw_chain *chain, struct sk_buff *skb, int in_port)
33 struct sw_flow_key key;
36 flow_extract(skb, in_port, &key);
37 flow = chain_lookup(chain, &key);
38 if (likely(flow != NULL)) {
40 execute_actions(chain->dp, skb, &key,
41 flow->actions, flow->n_actions);
43 dp_output_control(chain->dp, skb, fwd_save_skb(skb),
44 chain->dp->miss_send_len,
49 static int do_output(struct datapath *dp, struct sk_buff *skb, size_t max_len,
54 return (likely(out_port != OFPP_CONTROLLER)
55 ? dp_output_port(dp, skb, out_port)
56 : dp_output_control(dp, skb, fwd_save_skb(skb),
57 max_len, OFPR_ACTION));
60 void execute_actions(struct datapath *dp, struct sk_buff *skb,
61 const struct sw_flow_key *key,
62 const struct ofp_action *actions, int n_actions)
64 /* Every output action needs a separate clone of 'skb', but the common
65 * case is just a single output action, so that doing a clone and
66 * then freeing the original skbuff is wasteful. So the following code
67 * is slightly obscure just to avoid that. */
69 size_t max_len=0; /* Initialze to make compiler happy */
74 eth_proto = ntohs(key->dl_type);
76 for (i = 0; i < n_actions; i++) {
77 const struct ofp_action *a = &actions[i];
79 if (prev_port != -1) {
80 do_output(dp, skb_clone(skb, GFP_ATOMIC),
85 if (likely(a->type == htons(OFPAT_OUTPUT))) {
86 prev_port = ntohs(a->arg.output.port);
87 max_len = ntohs(a->arg.output.max_len);
89 if (!make_writable(&skb)) {
91 printk("make_writable failed\n");
94 skb = execute_setter(skb, eth_proto, key, a);
97 printk("execute_setter lost skb\n");
103 do_output(dp, skb, max_len, prev_port);
108 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
109 * covered by the sum has been changed from 'from' to 'to'. If set,
110 * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
111 * Based on nf_proto_csum_replace4. */
112 static void update_csum(__sum16 *sum, struct sk_buff *skb,
113 __be32 from, __be32 to, int pseudohdr)
115 __be32 diff[] = { ~from, to };
116 if (skb->ip_summed != CHECKSUM_PARTIAL) {
117 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
118 ~csum_unfold(*sum)));
119 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
120 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
122 } else if (pseudohdr)
123 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
127 static void modify_nh(struct sk_buff *skb, uint16_t eth_proto,
128 uint8_t nw_proto, const struct ofp_action *a)
130 if (eth_proto == ETH_P_IP) {
131 struct iphdr *nh = ip_hdr(skb);
132 uint32_t new, *field;
134 new = a->arg.nw_addr;
136 if (a->type == htons(OFPAT_SET_NW_SRC))
141 if (nw_proto == IPPROTO_TCP) {
142 struct tcphdr *th = tcp_hdr(skb);
143 update_csum(&th->check, skb, *field, new, 1);
144 } else if (nw_proto == IPPROTO_UDP) {
145 struct udphdr *th = udp_hdr(skb);
146 update_csum(&th->check, skb, *field, new, 1);
148 update_csum(&nh->check, skb, *field, new, 0);
153 static void modify_th(struct sk_buff *skb, uint16_t eth_proto,
154 uint8_t nw_proto, const struct ofp_action *a)
156 if (eth_proto == ETH_P_IP) {
157 uint16_t new, *field;
161 if (nw_proto == IPPROTO_TCP) {
162 struct tcphdr *th = tcp_hdr(skb);
164 if (a->type == htons(OFPAT_SET_TP_SRC))
169 update_csum(&th->check, skb, *field, new, 1);
171 } else if (nw_proto == IPPROTO_UDP) {
172 struct udphdr *th = udp_hdr(skb);
174 if (a->type == htons(OFPAT_SET_TP_SRC))
179 update_csum(&th->check, skb, *field, new, 1);
185 static struct sk_buff *vlan_pull_tag(struct sk_buff *skb)
187 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
191 /* Verify we were given a vlan packet */
192 if (vh->h_vlan_proto != htons(ETH_P_8021Q))
195 memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
197 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
199 skb->protocol = eh->h_proto;
200 skb->mac_header += VLAN_HLEN;
205 static struct sk_buff *modify_vlan(struct sk_buff *skb,
206 const struct sw_flow_key *key, const struct ofp_action *a)
208 uint16_t new_id = ntohs(a->arg.vlan_id);
210 if (new_id != OFP_VLAN_NONE) {
211 if (key->dl_vlan != htons(OFP_VLAN_NONE)) {
212 /* Modify vlan id, but maintain other TCI values */
213 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
214 vh->h_vlan_TCI = (vh->h_vlan_TCI
215 & ~(htons(VLAN_VID_MASK))) | a->arg.vlan_id;
217 /* Add vlan header */
219 /* xxx The vlan_put_tag function, doesn't seem to work
220 * xxx reliably when it attempts to use the hardware-accelerated
221 * xxx version. We'll directly use the software version
222 * xxx until the problem can be diagnosed.
224 skb = __vlan_put_tag(skb, new_id);
227 /* Remove an existing vlan header if it exists */
234 struct sk_buff *execute_setter(struct sk_buff *skb, uint16_t eth_proto,
235 const struct sw_flow_key *key, const struct ofp_action *a)
237 switch (ntohs(a->type)) {
238 case OFPAT_SET_DL_VLAN:
239 skb = modify_vlan(skb, key, a);
242 case OFPAT_SET_DL_SRC: {
243 struct ethhdr *eh = eth_hdr(skb);
244 memcpy(eh->h_source, a->arg.dl_addr, sizeof eh->h_source);
247 case OFPAT_SET_DL_DST: {
248 struct ethhdr *eh = eth_hdr(skb);
249 memcpy(eh->h_dest, a->arg.dl_addr, sizeof eh->h_dest);
253 case OFPAT_SET_NW_SRC:
254 case OFPAT_SET_NW_DST:
255 modify_nh(skb, eth_proto, key->nw_proto, a);
258 case OFPAT_SET_TP_SRC:
259 case OFPAT_SET_TP_DST:
260 modify_th(skb, eth_proto, key->nw_proto, a);
265 printk("execute_setter: unknown action: %d\n", ntohs(a->type));
272 recv_features_request(struct sw_chain *chain, const struct sender *sender,
275 return dp_send_features_reply(chain->dp, sender);
279 recv_get_config_request(struct sw_chain *chain, const struct sender *sender,
282 return dp_send_config_reply(chain->dp, sender);
286 recv_set_config(struct sw_chain *chain, const struct sender *sender,
289 const struct ofp_switch_config *osc = msg;
291 chain->dp->flags = ntohs(osc->flags);
292 chain->dp->miss_send_len = ntohs(osc->miss_send_len);
298 recv_packet_out(struct sw_chain *chain, const struct sender *sender,
301 const struct ofp_packet_out *opo = msg;
303 struct vlan_ethhdr *mac;
306 if (ntohl(opo->buffer_id) == (uint32_t) -1) {
307 int data_len = ntohs(opo->header.length) - sizeof *opo;
309 /* FIXME: there is likely a way to reuse the data in msg. */
310 skb = alloc_skb(data_len, GFP_ATOMIC);
314 /* FIXME? We don't reserve NET_IP_ALIGN or NET_SKB_PAD since
315 * we're just transmitting this raw without examining anything
316 * at those layers. */
317 memcpy(skb_put(skb, data_len), opo->u.data, data_len);
318 dp_set_origin(chain->dp, ntohs(opo->in_port), skb);
320 skb_set_mac_header(skb, 0);
321 mac = vlan_eth_hdr(skb);
322 if (likely(mac->h_vlan_proto != htons(ETH_P_8021Q)))
323 nh_ofs = sizeof(struct ethhdr);
325 nh_ofs = sizeof(struct vlan_ethhdr);
326 skb_set_network_header(skb, nh_ofs);
328 dp_output_port(chain->dp, skb, ntohs(opo->out_port));
330 struct sw_flow_key key;
333 skb = retrieve_skb(ntohl(opo->buffer_id));
336 dp_set_origin(chain->dp, ntohs(opo->in_port), skb);
338 n_acts = (ntohs(opo->header.length) - sizeof *opo)
339 / sizeof *opo->u.actions;
340 flow_extract(skb, ntohs(opo->in_port), &key);
341 execute_actions(chain->dp, skb, &key, opo->u.actions, n_acts);
347 recv_port_mod(struct sw_chain *chain, const struct sender *sender,
350 const struct ofp_port_mod *opm = msg;
352 dp_update_port_flags(chain->dp, &opm->desc);
358 recv_echo_request(struct sw_chain *chain, const struct sender *sender,
361 return dp_send_echo_reply(chain->dp, sender, msg);
365 recv_echo_reply(struct sw_chain *chain, const struct sender *sender,
372 add_flow(struct sw_chain *chain, const struct ofp_flow_mod *ofm)
377 struct sw_flow *flow;
380 /* To prevent loops, make sure there's no action to send to the
381 * OFP_TABLE virtual port.
383 n_acts = (ntohs(ofm->header.length) - sizeof *ofm) / sizeof *ofm->actions;
384 for (i=0; i<n_acts; i++) {
385 const struct ofp_action *a = &ofm->actions[i];
387 if (a->type == htons(OFPAT_OUTPUT)
388 && (a->arg.output.port == htons(OFPP_TABLE)
389 || a->arg.output.port == htons(OFPP_NONE))) {
390 /* xxx Send fancy new error message? */
395 /* Allocate memory. */
396 flow = flow_alloc(n_acts, GFP_ATOMIC);
401 flow_extract_match(&flow->key, &ofm->match);
402 flow->max_idle = ntohs(ofm->max_idle);
403 flow->priority = flow->key.wildcards ? ntohs(ofm->priority) : -1;
404 flow->timeout = jiffies + flow->max_idle * HZ;
405 flow->n_actions = n_acts;
406 flow->init_time = jiffies;
407 flow->byte_count = 0;
408 flow->packet_count = 0;
409 atomic_set(&flow->deleted, 0);
410 spin_lock_init(&flow->lock);
411 memcpy(flow->actions, ofm->actions, n_acts * sizeof *flow->actions);
414 error = chain_insert(chain, flow);
416 goto error_free_flow;
418 if (ntohl(ofm->buffer_id) != (uint32_t) -1) {
419 struct sk_buff *skb = retrieve_skb(ntohl(ofm->buffer_id));
421 struct sw_flow_key key;
422 flow_used(flow, skb);
423 flow_extract(skb, ntohs(ofm->match.in_port), &key);
424 execute_actions(chain->dp, skb, &key,
425 ofm->actions, n_acts);
435 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
436 discard_skb(ntohl(ofm->buffer_id));
441 recv_flow(struct sw_chain *chain, const struct sender *sender, const void *msg)
443 const struct ofp_flow_mod *ofm = msg;
444 uint16_t command = ntohs(ofm->command);
446 if (command == OFPFC_ADD) {
447 return add_flow(chain, ofm);
448 } else if (command == OFPFC_DELETE) {
449 struct sw_flow_key key;
450 flow_extract_match(&key, &ofm->match);
451 return chain_delete(chain, &key, 0, 0) ? 0 : -ESRCH;
452 } else if (command == OFPFC_DELETE_STRICT) {
453 struct sw_flow_key key;
455 flow_extract_match(&key, &ofm->match);
456 priority = key.wildcards ? ntohs(ofm->priority) : -1;
457 return chain_delete(chain, &key, priority, 1) ? 0 : -ESRCH;
463 /* 'msg', which is 'length' bytes long, was received across Netlink from
464 * 'sender'. Apply it to 'chain'. */
466 fwd_control_input(struct sw_chain *chain, const struct sender *sender,
467 const void *msg, size_t length)
470 struct openflow_packet {
472 int (*handler)(struct sw_chain *, const struct sender *,
476 static const struct openflow_packet packets[] = {
477 [OFPT_FEATURES_REQUEST] = {
478 sizeof (struct ofp_header),
479 recv_features_request,
481 [OFPT_GET_CONFIG_REQUEST] = {
482 sizeof (struct ofp_header),
483 recv_get_config_request,
485 [OFPT_SET_CONFIG] = {
486 sizeof (struct ofp_switch_config),
489 [OFPT_PACKET_OUT] = {
490 sizeof (struct ofp_packet_out),
494 sizeof (struct ofp_flow_mod),
498 sizeof (struct ofp_port_mod),
501 [OFPT_ECHO_REQUEST] = {
502 sizeof (struct ofp_header),
505 [OFPT_ECHO_REPLY] = {
506 sizeof (struct ofp_header),
511 const struct openflow_packet *pkt;
512 struct ofp_header *oh;
514 oh = (struct ofp_header *) msg;
515 if (oh->version != OFP_VERSION || oh->type >= ARRAY_SIZE(packets)
516 || ntohs(oh->length) > length)
519 pkt = &packets[oh->type];
522 if (length < pkt->min_size)
525 return pkt->handler(chain, sender, msg);
528 /* Packet buffering. */
530 #define OVERWRITE_SECS 1
531 #define OVERWRITE_JIFFIES (OVERWRITE_SECS * HZ)
533 struct packet_buffer {
536 unsigned long exp_jiffies;
539 static struct packet_buffer buffers[N_PKT_BUFFERS];
540 static unsigned int buffer_idx;
541 static DEFINE_SPINLOCK(buffer_lock);
543 uint32_t fwd_save_skb(struct sk_buff *skb)
545 struct packet_buffer *p;
546 unsigned long int flags;
549 spin_lock_irqsave(&buffer_lock, flags);
550 buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
551 p = &buffers[buffer_idx];
553 /* Don't buffer packet if existing entry is less than
554 * OVERWRITE_SECS old. */
555 if (time_before(jiffies, p->exp_jiffies)) {
556 spin_unlock_irqrestore(&buffer_lock, flags);
561 /* Don't use maximum cookie value since the all-bits-1 id is
563 if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
567 p->exp_jiffies = jiffies + OVERWRITE_JIFFIES;
568 id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
569 spin_unlock_irqrestore(&buffer_lock, flags);
574 static struct sk_buff *retrieve_skb(uint32_t id)
576 unsigned long int flags;
577 struct sk_buff *skb = NULL;
578 struct packet_buffer *p;
580 spin_lock_irqsave(&buffer_lock, flags);
581 p = &buffers[id & PKT_BUFFER_MASK];
582 if (p->cookie == id >> PKT_BUFFER_BITS) {
586 printk("cookie mismatch: %x != %x\n",
587 id >> PKT_BUFFER_BITS, p->cookie);
589 spin_unlock_irqrestore(&buffer_lock, flags);
594 void fwd_discard_all(void)
596 unsigned long int flags;
599 spin_lock_irqsave(&buffer_lock, flags);
600 for (i = 0; i < N_PKT_BUFFERS; i++) {
601 kfree_skb(buffers[i].skb);
602 buffers[i].skb = NULL;
604 spin_unlock_irqrestore(&buffer_lock, flags);
607 static void discard_skb(uint32_t id)
609 unsigned long int flags;
610 struct packet_buffer *p;
612 spin_lock_irqsave(&buffer_lock, flags);
613 p = &buffers[id & PKT_BUFFER_MASK];
614 if (p->cookie == id >> PKT_BUFFER_BITS) {
618 spin_unlock_irqrestore(&buffer_lock, flags);
626 /* Utility functions. */
628 /* Makes '*pskb' writable, possibly copying it and setting '*pskb' to point to
630 * Returns 1 if successful, 0 on failure. */
632 make_writable(struct sk_buff **pskb)
634 /* Based on skb_make_writable() in net/netfilter/core.c. */
635 struct sk_buff *nskb;
637 /* Not exclusive use of packet? Must copy. */
638 if (skb_shared(*pskb) || skb_cloned(*pskb))
641 return pskb_may_pull(*pskb, 40); /* FIXME? */
644 nskb = skb_copy(*pskb, GFP_ATOMIC);
647 BUG_ON(skb_is_nonlinear(nskb));
649 /* Rest of kernel will get very unhappy if we pass it a
650 suddenly-orphaned skbuff */
652 skb_set_owner_w(nskb, (*pskb)->sk);