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)) {
90 printk("make_writable failed\n");
93 skb = execute_setter(skb, eth_proto, key, a);
97 do_output(dp, skb, max_len, prev_port);
102 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
103 * covered by the sum has been changed from 'from' to 'to'. If set,
104 * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
105 * Based on nf_proto_csum_replace4. */
106 static void update_csum(__sum16 *sum, struct sk_buff *skb,
107 __be32 from, __be32 to, int pseudohdr)
109 __be32 diff[] = { ~from, to };
110 if (skb->ip_summed != CHECKSUM_PARTIAL) {
111 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
112 ~csum_unfold(*sum)));
113 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
114 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
116 } else if (pseudohdr)
117 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
121 static void modify_nh(struct sk_buff *skb, uint16_t eth_proto,
122 uint8_t nw_proto, const struct ofp_action *a)
124 if (eth_proto == ETH_P_IP) {
125 struct iphdr *nh = ip_hdr(skb);
126 uint32_t new, *field;
128 new = a->arg.nw_addr;
130 if (a->type == htons(OFPAT_SET_NW_SRC))
135 if (nw_proto == IPPROTO_TCP) {
136 struct tcphdr *th = tcp_hdr(skb);
137 update_csum(&th->check, skb, *field, new, 1);
138 } else if (nw_proto == IPPROTO_UDP) {
139 struct udphdr *th = udp_hdr(skb);
140 update_csum(&th->check, skb, *field, new, 1);
142 update_csum(&nh->check, skb, *field, new, 0);
147 static void modify_th(struct sk_buff *skb, uint16_t eth_proto,
148 uint8_t nw_proto, const struct ofp_action *a)
150 if (eth_proto == ETH_P_IP) {
151 uint16_t new, *field;
155 if (nw_proto == IPPROTO_TCP) {
156 struct tcphdr *th = tcp_hdr(skb);
158 if (a->type == htons(OFPAT_SET_TP_SRC))
163 update_csum(&th->check, skb, *field, new, 1);
165 } else if (nw_proto == IPPROTO_UDP) {
166 struct udphdr *th = udp_hdr(skb);
168 if (a->type == htons(OFPAT_SET_TP_SRC))
173 update_csum(&th->check, skb, *field, new, 1);
179 static struct sk_buff *vlan_pull_tag(struct sk_buff *skb)
181 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
185 /* Verify we were given a vlan packet */
186 if (vh->h_vlan_proto != htons(ETH_P_8021Q))
189 memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
191 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
193 skb->protocol = eh->h_proto;
194 skb->mac_header += VLAN_HLEN;
199 static struct sk_buff *modify_vlan(struct sk_buff *skb,
200 const struct sw_flow_key *key, const struct ofp_action *a)
202 uint16_t new_id = a->arg.vlan_id;
204 if (new_id != OFP_VLAN_NONE) {
205 if (key->dl_vlan != htons(OFP_VLAN_NONE)) {
206 /* Modify vlan id, but maintain other TCI values */
207 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
208 vh->h_vlan_TCI = (vh->h_vlan_TCI
209 & ~(htons(VLAN_VID_MASK))) | htons(new_id);
211 /* Add vlan header */
212 skb = vlan_put_tag(skb, new_id);
215 /* Remove an existing vlan header if it exists */
222 struct sk_buff *execute_setter(struct sk_buff *skb, uint16_t eth_proto,
223 const struct sw_flow_key *key, const struct ofp_action *a)
225 switch (ntohs(a->type)) {
226 case OFPAT_SET_DL_VLAN:
227 skb = modify_vlan(skb, key, a);
230 case OFPAT_SET_DL_SRC: {
231 struct ethhdr *eh = eth_hdr(skb);
232 memcpy(eh->h_source, a->arg.dl_addr, sizeof eh->h_source);
235 case OFPAT_SET_DL_DST: {
236 struct ethhdr *eh = eth_hdr(skb);
237 memcpy(eh->h_dest, a->arg.dl_addr, sizeof eh->h_dest);
241 case OFPAT_SET_NW_SRC:
242 case OFPAT_SET_NW_DST:
243 modify_nh(skb, eth_proto, key->nw_proto, a);
246 case OFPAT_SET_TP_SRC:
247 case OFPAT_SET_TP_DST:
248 modify_th(skb, eth_proto, key->nw_proto, a);
253 printk("execute_setter: unknown action: %d\n", ntohs(a->type));
260 recv_features_request(struct sw_chain *chain, const struct sender *sender,
263 return dp_send_features_reply(chain->dp, sender);
267 recv_get_config_request(struct sw_chain *chain, const struct sender *sender,
270 return dp_send_config_reply(chain->dp, sender);
274 recv_set_config(struct sw_chain *chain, const struct sender *sender,
277 const struct ofp_switch_config *osc = msg;
279 chain->dp->flags = ntohs(osc->flags);
280 chain->dp->miss_send_len = ntohs(osc->miss_send_len);
286 recv_packet_out(struct sw_chain *chain, const struct sender *sender,
289 const struct ofp_packet_out *opo = msg;
291 struct vlan_ethhdr *mac;
294 if (ntohl(opo->buffer_id) == (uint32_t) -1) {
295 int data_len = ntohs(opo->header.length) - sizeof *opo;
297 /* FIXME: there is likely a way to reuse the data in msg. */
298 skb = alloc_skb(data_len, GFP_ATOMIC);
302 /* FIXME? We don't reserve NET_IP_ALIGN or NET_SKB_PAD since
303 * we're just transmitting this raw without examining anything
304 * at those layers. */
305 memcpy(skb_put(skb, data_len), opo->u.data, data_len);
306 dp_set_origin(chain->dp, ntohs(opo->in_port), skb);
308 skb_set_mac_header(skb, 0);
309 mac = vlan_eth_hdr(skb);
310 if (likely(mac->h_vlan_proto != htons(ETH_P_8021Q)))
311 nh_ofs = sizeof(struct ethhdr);
313 nh_ofs = sizeof(struct vlan_ethhdr);
314 skb_set_network_header(skb, nh_ofs);
316 dp_output_port(chain->dp, skb, ntohs(opo->out_port));
318 struct sw_flow_key key;
321 skb = retrieve_skb(ntohl(opo->buffer_id));
324 dp_set_origin(chain->dp, ntohs(opo->in_port), skb);
326 n_acts = (ntohs(opo->header.length) - sizeof *opo)
327 / sizeof *opo->u.actions;
328 flow_extract(skb, ntohs(opo->in_port), &key);
329 execute_actions(chain->dp, skb, &key, opo->u.actions, n_acts);
335 recv_port_mod(struct sw_chain *chain, const struct sender *sender,
338 const struct ofp_port_mod *opm = msg;
340 dp_update_port_flags(chain->dp, &opm->desc);
346 add_flow(struct sw_chain *chain, const struct ofp_flow_mod *ofm)
351 struct sw_flow *flow;
354 /* Check number of actions. */
355 n_acts = (ntohs(ofm->header.length) - sizeof *ofm) / sizeof *ofm->actions;
356 if (n_acts > MAX_ACTIONS) {
361 /* To prevent loops, make sure there's no action to send to the
362 * OFP_TABLE virtual port.
364 for (i=0; i<n_acts; i++) {
365 const struct ofp_action *a = &ofm->actions[i];
367 if (a->type == htons(OFPAT_OUTPUT)
368 && a->arg.output.port == htons(OFPP_TABLE)) {
369 /* xxx Send fancy new error message? */
374 /* Allocate memory. */
375 flow = flow_alloc(n_acts, GFP_ATOMIC);
380 flow_extract_match(&flow->key, &ofm->match);
381 flow->group_id = ntohl(ofm->group_id);
382 flow->max_idle = ntohs(ofm->max_idle);
383 flow->timeout = jiffies + flow->max_idle * HZ;
384 flow->n_actions = n_acts;
385 flow->init_time = jiffies;
386 flow->byte_count = 0;
387 flow->packet_count = 0;
388 atomic_set(&flow->deleted, 0);
389 spin_lock_init(&flow->lock);
390 memcpy(flow->actions, ofm->actions, n_acts * sizeof *flow->actions);
393 error = chain_insert(chain, flow);
395 goto error_free_flow;
397 if (ntohl(ofm->buffer_id) != (uint32_t) -1) {
398 struct sk_buff *skb = retrieve_skb(ntohl(ofm->buffer_id));
400 struct sw_flow_key key;
401 flow_used(flow, skb);
402 flow_extract(skb, ntohs(ofm->match.in_port), &key);
403 execute_actions(chain->dp, skb, &key,
404 ofm->actions, n_acts);
414 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
415 discard_skb(ntohl(ofm->buffer_id));
420 recv_flow(struct sw_chain *chain, const struct sender *sender, const void *msg)
422 const struct ofp_flow_mod *ofm = msg;
423 uint16_t command = ntohs(ofm->command);
425 if (command == OFPFC_ADD) {
426 return add_flow(chain, ofm);
427 } else if (command == OFPFC_DELETE) {
428 struct sw_flow_key key;
429 flow_extract_match(&key, &ofm->match);
430 return chain_delete(chain, &key, 0) ? 0 : -ESRCH;
431 } else if (command == OFPFC_DELETE_STRICT) {
432 struct sw_flow_key key;
433 flow_extract_match(&key, &ofm->match);
434 return chain_delete(chain, &key, 1) ? 0 : -ESRCH;
441 recv_flow_status_request(struct sw_chain *chain, const struct sender *sender,
444 const struct ofp_flow_stat_request *fsr = msg;
445 if (fsr->type == OFPFS_INDIV) {
446 return dp_send_flow_stats(chain->dp, sender, &fsr->match);
454 recv_port_status_request(struct sw_chain *chain, const struct sender *sender,
457 return dp_send_port_stats(chain->dp, sender);
461 recv_table_status_request(struct sw_chain *chain, const struct sender *sender,
464 return dp_send_table_stats(chain->dp, sender);
467 /* 'msg', which is 'length' bytes long, was received across Netlink from
468 * 'sender'. Apply it to 'chain'. */
470 fwd_control_input(struct sw_chain *chain, const struct sender *sender,
471 const void *msg, size_t length)
474 struct openflow_packet {
476 int (*handler)(struct sw_chain *, const struct sender *,
480 static const struct openflow_packet packets[] = {
481 [OFPT_FEATURES_REQUEST] = {
482 sizeof (struct ofp_header),
483 recv_features_request,
485 [OFPT_GET_CONFIG_REQUEST] = {
486 sizeof (struct ofp_header),
487 recv_get_config_request,
489 [OFPT_SET_CONFIG] = {
490 sizeof (struct ofp_switch_config),
493 [OFPT_PACKET_OUT] = {
494 sizeof (struct ofp_packet_out),
498 sizeof (struct ofp_flow_mod),
502 sizeof (struct ofp_port_mod),
505 [OFPT_FLOW_STAT_REQUEST] = {
506 sizeof (struct ofp_flow_stat_request),
507 recv_flow_status_request,
509 [OFPT_PORT_STAT_REQUEST] = {
510 sizeof (struct ofp_port_stat_request),
511 recv_port_status_request,
513 [OFPT_TABLE_STAT_REQUEST] = {
514 sizeof (struct ofp_table_stat_request),
515 recv_table_status_request,
519 const struct openflow_packet *pkt;
520 struct ofp_header *oh;
522 oh = (struct ofp_header *) msg;
523 if (oh->version != OFP_VERSION || oh->type >= ARRAY_SIZE(packets)
524 || ntohs(oh->length) > length)
527 pkt = &packets[oh->type];
530 if (length < pkt->min_size)
533 return pkt->handler(chain, sender, msg);
536 /* Packet buffering. */
538 #define OVERWRITE_SECS 1
539 #define OVERWRITE_JIFFIES (OVERWRITE_SECS * HZ)
541 struct packet_buffer {
544 unsigned long exp_jiffies;
547 static struct packet_buffer buffers[N_PKT_BUFFERS];
548 static unsigned int buffer_idx;
549 static DEFINE_SPINLOCK(buffer_lock);
551 uint32_t fwd_save_skb(struct sk_buff *skb)
553 struct packet_buffer *p;
554 unsigned long int flags;
557 spin_lock_irqsave(&buffer_lock, flags);
558 buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
559 p = &buffers[buffer_idx];
561 /* Don't buffer packet if existing entry is less than
562 * OVERWRITE_SECS old. */
563 if (time_before(jiffies, p->exp_jiffies)) {
564 spin_unlock_irqrestore(&buffer_lock, flags);
569 /* Don't use maximum cookie value since the all-bits-1 id is
571 if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
575 p->exp_jiffies = jiffies + OVERWRITE_JIFFIES;
576 id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
577 spin_unlock_irqrestore(&buffer_lock, flags);
582 static struct sk_buff *retrieve_skb(uint32_t id)
584 unsigned long int flags;
585 struct sk_buff *skb = NULL;
586 struct packet_buffer *p;
588 spin_lock_irqsave(&buffer_lock, flags);
589 p = &buffers[id & PKT_BUFFER_MASK];
590 if (p->cookie == id >> PKT_BUFFER_BITS) {
594 printk("cookie mismatch: %x != %x\n",
595 id >> PKT_BUFFER_BITS, p->cookie);
597 spin_unlock_irqrestore(&buffer_lock, flags);
602 static void discard_skb(uint32_t id)
604 unsigned long int flags;
605 struct packet_buffer *p;
607 spin_lock_irqsave(&buffer_lock, flags);
608 p = &buffers[id & PKT_BUFFER_MASK];
609 if (p->cookie == id >> PKT_BUFFER_BITS) {
613 spin_unlock_irqrestore(&buffer_lock, flags);
620 for (i = 0; i < N_PKT_BUFFERS; i++)
621 kfree_skb(buffers[i].skb);
624 /* Utility functions. */
626 /* Makes '*pskb' writable, possibly copying it and setting '*pskb' to point to
628 * Returns 1 if successful, 0 on failure. */
630 make_writable(struct sk_buff **pskb)
632 /* Based on skb_make_writable() in net/netfilter/core.c. */
633 struct sk_buff *nskb;
635 /* Not exclusive use of packet? Must copy. */
636 if (skb_shared(*pskb) || skb_cloned(*pskb))
639 return pskb_may_pull(*pskb, 64); /* FIXME? */
642 nskb = skb_copy(*pskb, GFP_ATOMIC);
645 BUG_ON(skb_is_nonlinear(nskb));
647 /* Rest of kernel will get very unhappy if we pass it a
648 suddenly-orphaned skbuff */
650 skb_set_owner_w(nskb, (*pskb)->sk);