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>
13 #include <linux/tcp.h>
14 #include <linux/udp.h>
15 #include <linux/in6.h>
16 #include <asm/uaccess.h>
17 #include <linux/types.h>
18 #include <net/checksum.h>
24 /* FIXME: do we need to use GFP_ATOMIC everywhere here? */
26 static int make_writable(struct sk_buff **);
28 static struct sk_buff *retrieve_skb(uint32_t id);
29 static void discard_skb(uint32_t id);
31 /* 'skb' was received on port 'p', which may be a physical switch port, the
32 * local port, or a null pointer. Process it according to 'chain'. Returns 0
33 * if successful, in which case 'skb' is destroyed, or -ESRCH if there is no
34 * matching flow, in which case 'skb' still belongs to the caller. */
35 int run_flow_through_tables(struct sw_chain *chain, struct sk_buff *skb,
36 struct net_bridge_port *p)
38 /* Ethernet address used as the destination for STP frames. */
39 static const uint8_t stp_eth_addr[ETH_ALEN]
40 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x01 };
41 struct sw_flow_key key;
44 if (flow_extract(skb, p ? p->port_no : OFPP_NONE, &key)
45 && (chain->dp->flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP) {
50 if (p && p->flags & (OFPPFL_NO_RECV | OFPPFL_NO_RECV_STP) &&
51 p->flags & (compare_ether_addr(key.dl_dst, stp_eth_addr)
52 ? OFPPFL_NO_RECV : OFPPFL_NO_RECV_STP)) {
57 flow = chain_lookup(chain, &key);
58 if (likely(flow != NULL)) {
59 struct sw_flow_actions *sf_acts = rcu_dereference(flow->sf_acts);
61 execute_actions(chain->dp, skb, &key,
62 sf_acts->actions, sf_acts->n_actions, 0);
69 /* 'skb' was received on port 'p', which may be a physical switch port, the
70 * local port, or a null pointer. Process it according to 'chain', sending it
71 * up to the controller if no flow matches. Takes ownership of 'skb'. */
72 void fwd_port_input(struct sw_chain *chain, struct sk_buff *skb,
73 struct net_bridge_port *p)
75 if (run_flow_through_tables(chain, skb, p))
76 dp_output_control(chain->dp, skb, fwd_save_skb(skb),
77 chain->dp->miss_send_len,
81 static int do_output(struct datapath *dp, struct sk_buff *skb, size_t max_len,
82 int out_port, int ignore_no_fwd)
86 return (likely(out_port != OFPP_CONTROLLER)
87 ? dp_output_port(dp, skb, out_port, ignore_no_fwd)
88 : dp_output_control(dp, skb, fwd_save_skb(skb),
89 max_len, OFPR_ACTION));
92 void execute_actions(struct datapath *dp, struct sk_buff *skb,
93 const struct sw_flow_key *key,
94 const struct ofp_action *actions, int n_actions,
97 /* Every output action needs a separate clone of 'skb', but the common
98 * case is just a single output action, so that doing a clone and
99 * then freeing the original skbuff is wasteful. So the following code
100 * is slightly obscure just to avoid that. */
102 size_t max_len=0; /* Initialze to make compiler happy */
107 eth_proto = ntohs(key->dl_type);
109 for (i = 0; i < n_actions; i++) {
110 const struct ofp_action *a = &actions[i];
112 if (prev_port != -1) {
113 do_output(dp, skb_clone(skb, GFP_ATOMIC),
114 max_len, prev_port, ignore_no_fwd);
118 if (likely(a->type == htons(OFPAT_OUTPUT))) {
119 prev_port = ntohs(a->arg.output.port);
120 max_len = ntohs(a->arg.output.max_len);
122 if (!make_writable(&skb)) {
124 printk("make_writable failed\n");
127 skb = execute_setter(skb, eth_proto, key, a);
130 printk("execute_setter lost skb\n");
136 do_output(dp, skb, max_len, prev_port, ignore_no_fwd);
141 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
142 * covered by the sum has been changed from 'from' to 'to'. If set,
143 * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
144 * Based on nf_proto_csum_replace4. */
145 static void update_csum(__sum16 *sum, struct sk_buff *skb,
146 __be32 from, __be32 to, int pseudohdr)
148 __be32 diff[] = { ~from, to };
149 if (skb->ip_summed != CHECKSUM_PARTIAL) {
150 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
151 ~csum_unfold(*sum)));
152 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
153 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
155 } else if (pseudohdr)
156 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
160 static void modify_nh(struct sk_buff *skb, uint16_t eth_proto,
161 uint8_t nw_proto, const struct ofp_action *a)
163 if (eth_proto == ETH_P_IP) {
164 struct iphdr *nh = ip_hdr(skb);
165 uint32_t new, *field;
167 new = a->arg.nw_addr;
169 if (a->type == htons(OFPAT_SET_NW_SRC))
174 if (nw_proto == IPPROTO_TCP) {
175 struct tcphdr *th = tcp_hdr(skb);
176 update_csum(&th->check, skb, *field, new, 1);
177 } else if (nw_proto == IPPROTO_UDP) {
178 struct udphdr *th = udp_hdr(skb);
179 update_csum(&th->check, skb, *field, new, 1);
181 update_csum(&nh->check, skb, *field, new, 0);
186 static void modify_th(struct sk_buff *skb, uint16_t eth_proto,
187 uint8_t nw_proto, const struct ofp_action *a)
189 if (eth_proto == ETH_P_IP) {
190 uint16_t new, *field;
194 if (nw_proto == IPPROTO_TCP) {
195 struct tcphdr *th = tcp_hdr(skb);
197 if (a->type == htons(OFPAT_SET_TP_SRC))
202 update_csum(&th->check, skb, *field, new, 1);
204 } else if (nw_proto == IPPROTO_UDP) {
205 struct udphdr *th = udp_hdr(skb);
207 if (a->type == htons(OFPAT_SET_TP_SRC))
212 update_csum(&th->check, skb, *field, new, 1);
218 static struct sk_buff *vlan_pull_tag(struct sk_buff *skb)
220 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
224 /* Verify we were given a vlan packet */
225 if (vh->h_vlan_proto != htons(ETH_P_8021Q))
228 memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
230 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
232 skb->protocol = eh->h_proto;
233 skb->mac_header += VLAN_HLEN;
238 static struct sk_buff *modify_vlan(struct sk_buff *skb,
239 const struct sw_flow_key *key, const struct ofp_action *a)
241 uint16_t new_id = ntohs(a->arg.vlan_id);
243 if (new_id != OFP_VLAN_NONE) {
244 if (key->dl_vlan != htons(OFP_VLAN_NONE)) {
245 /* Modify vlan id, but maintain other TCI values */
246 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
247 vh->h_vlan_TCI = (vh->h_vlan_TCI
248 & ~(htons(VLAN_VID_MASK))) | a->arg.vlan_id;
250 /* Add vlan header */
252 /* xxx The vlan_put_tag function, doesn't seem to work
253 * xxx reliably when it attempts to use the hardware-accelerated
254 * xxx version. We'll directly use the software version
255 * xxx until the problem can be diagnosed.
257 skb = __vlan_put_tag(skb, new_id);
260 /* Remove an existing vlan header if it exists */
267 struct sk_buff *execute_setter(struct sk_buff *skb, uint16_t eth_proto,
268 const struct sw_flow_key *key, const struct ofp_action *a)
270 switch (ntohs(a->type)) {
271 case OFPAT_SET_DL_VLAN:
272 skb = modify_vlan(skb, key, a);
275 case OFPAT_SET_DL_SRC: {
276 struct ethhdr *eh = eth_hdr(skb);
277 memcpy(eh->h_source, a->arg.dl_addr, sizeof eh->h_source);
280 case OFPAT_SET_DL_DST: {
281 struct ethhdr *eh = eth_hdr(skb);
282 memcpy(eh->h_dest, a->arg.dl_addr, sizeof eh->h_dest);
286 case OFPAT_SET_NW_SRC:
287 case OFPAT_SET_NW_DST:
288 modify_nh(skb, eth_proto, key->nw_proto, a);
291 case OFPAT_SET_TP_SRC:
292 case OFPAT_SET_TP_DST:
293 modify_th(skb, eth_proto, key->nw_proto, a);
298 printk("execute_setter: unknown action: %d\n", ntohs(a->type));
305 recv_hello(struct sw_chain *chain, const struct sender *sender,
308 return dp_send_hello(chain->dp, sender, msg);
312 recv_features_request(struct sw_chain *chain, const struct sender *sender,
315 return dp_send_features_reply(chain->dp, sender);
319 recv_get_config_request(struct sw_chain *chain, const struct sender *sender,
322 return dp_send_config_reply(chain->dp, sender);
326 recv_set_config(struct sw_chain *chain, const struct sender *sender,
329 const struct ofp_switch_config *osc = msg;
332 flags = ntohs(osc->flags) & (OFPC_SEND_FLOW_EXP | OFPC_FRAG_MASK);
333 if ((flags & OFPC_FRAG_MASK) != OFPC_FRAG_NORMAL
334 && (flags & OFPC_FRAG_MASK) != OFPC_FRAG_DROP) {
335 flags = (flags & ~OFPC_FRAG_MASK) | OFPC_FRAG_DROP;
337 chain->dp->flags = flags;
339 chain->dp->miss_send_len = ntohs(osc->miss_send_len);
345 recv_packet_out(struct sw_chain *chain, const struct sender *sender,
348 const struct ofp_packet_out *opo = msg;
350 struct vlan_ethhdr *mac;
352 struct sw_flow_key key;
353 int n_actions = ntohs(opo->n_actions);
354 int act_len = n_actions * sizeof opo->actions[0];
356 if (act_len > (ntohs(opo->header.length) - sizeof *opo)) {
358 printk("message too short for number of actions\n");
362 if (ntohl(opo->buffer_id) == (uint32_t) -1) {
363 int data_len = ntohs(opo->header.length) - sizeof *opo - act_len;
365 /* FIXME: there is likely a way to reuse the data in msg. */
366 skb = alloc_skb(data_len, GFP_ATOMIC);
370 /* FIXME? We don't reserve NET_IP_ALIGN or NET_SKB_PAD since
371 * we're just transmitting this raw without examining anything
372 * at those layers. */
373 memcpy(skb_put(skb, data_len), &opo->actions[n_actions], data_len);
375 skb_set_mac_header(skb, 0);
376 mac = vlan_eth_hdr(skb);
377 if (likely(mac->h_vlan_proto != htons(ETH_P_8021Q)))
378 nh_ofs = sizeof(struct ethhdr);
380 nh_ofs = sizeof(struct vlan_ethhdr);
381 skb_set_network_header(skb, nh_ofs);
383 skb = retrieve_skb(ntohl(opo->buffer_id));
388 dp_set_origin(chain->dp, ntohs(opo->in_port), skb);
390 flow_extract(skb, ntohs(opo->in_port), &key);
391 execute_actions(chain->dp, skb, &key, opo->actions, n_actions, 1);
397 recv_port_mod(struct sw_chain *chain, const struct sender *sender,
400 const struct ofp_port_mod *opm = msg;
402 dp_update_port_flags(chain->dp, opm);
408 recv_echo_request(struct sw_chain *chain, const struct sender *sender,
411 return dp_send_echo_reply(chain->dp, sender, msg);
415 recv_echo_reply(struct sw_chain *chain, const struct sender *sender,
422 add_flow(struct sw_chain *chain, const struct ofp_flow_mod *ofm)
427 struct sw_flow *flow;
430 /* To prevent loops, make sure there's no action to send to the
431 * OFP_TABLE virtual port.
433 n_actions = (ntohs(ofm->header.length) - sizeof *ofm)
434 / sizeof *ofm->actions;
435 for (i=0; i<n_actions; i++) {
436 const struct ofp_action *a = &ofm->actions[i];
438 if (a->type == htons(OFPAT_OUTPUT)
439 && (a->arg.output.port == htons(OFPP_TABLE)
440 || a->arg.output.port == htons(OFPP_NONE)
441 || a->arg.output.port == ofm->match.in_port)) {
442 /* xxx Send fancy new error message? */
447 /* Allocate memory. */
448 flow = flow_alloc(n_actions, GFP_ATOMIC);
453 flow_extract_match(&flow->key, &ofm->match);
454 flow->priority = flow->key.wildcards ? ntohs(ofm->priority) : -1;
455 flow->idle_timeout = ntohs(ofm->idle_timeout);
456 flow->hard_timeout = ntohs(ofm->hard_timeout);
457 flow->used = jiffies;
458 flow->init_time = jiffies;
459 flow->byte_count = 0;
460 flow->packet_count = 0;
461 spin_lock_init(&flow->lock);
462 memcpy(flow->sf_acts->actions, ofm->actions,
463 n_actions * sizeof *flow->sf_acts->actions);
466 error = chain_insert(chain, flow);
468 goto error_free_flow;
470 if (ntohl(ofm->buffer_id) != (uint32_t) -1) {
471 struct sk_buff *skb = retrieve_skb(ntohl(ofm->buffer_id));
473 struct sw_flow_key key;
474 flow_used(flow, skb);
475 flow_extract(skb, ntohs(ofm->match.in_port), &key);
476 execute_actions(chain->dp, skb, &key, ofm->actions, n_actions, 0);
486 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
487 discard_skb(ntohl(ofm->buffer_id));
492 mod_flow(struct sw_chain *chain, const struct ofp_flow_mod *ofm)
497 struct sw_flow_key key;
499 /* To prevent loops, make sure there's no action to send to the
500 * OFP_TABLE virtual port.
502 n_actions = (ntohs(ofm->header.length) - sizeof *ofm)
503 / sizeof *ofm->actions;
504 for (i=0; i<n_actions; i++) {
505 const struct ofp_action *a = &ofm->actions[i];
507 if (a->type == htons(OFPAT_OUTPUT)
508 && (a->arg.output.port == htons(OFPP_TABLE)
509 || a->arg.output.port == htons(OFPP_NONE)
510 || a->arg.output.port == ofm->match.in_port)) {
511 /* xxx Send fancy new error message? */
516 flow_extract_match(&key, &ofm->match);
517 chain_modify(chain, &key, ofm->actions, n_actions);
519 if (ntohl(ofm->buffer_id) != (uint32_t) -1) {
520 struct sk_buff *skb = retrieve_skb(ntohl(ofm->buffer_id));
522 struct sw_flow_key skb_key;
523 flow_extract(skb, ntohs(ofm->match.in_port), &skb_key);
524 execute_actions(chain->dp, skb, &skb_key,
525 ofm->actions, n_actions, 0);
533 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
534 discard_skb(ntohl(ofm->buffer_id));
539 recv_flow(struct sw_chain *chain, const struct sender *sender, const void *msg)
541 const struct ofp_flow_mod *ofm = msg;
542 uint16_t command = ntohs(ofm->command);
544 if (command == OFPFC_ADD) {
545 return add_flow(chain, ofm);
546 } else if (command == OFPFC_MODIFY) {
547 return mod_flow(chain, ofm);
548 } else if (command == OFPFC_DELETE) {
549 struct sw_flow_key key;
550 flow_extract_match(&key, &ofm->match);
551 return chain_delete(chain, &key, 0, 0) ? 0 : -ESRCH;
552 } else if (command == OFPFC_DELETE_STRICT) {
553 struct sw_flow_key key;
555 flow_extract_match(&key, &ofm->match);
556 priority = key.wildcards ? ntohs(ofm->priority) : -1;
557 return chain_delete(chain, &key, priority, 1) ? 0 : -ESRCH;
563 /* 'msg', which is 'length' bytes long, was received across Netlink from
564 * 'sender'. Apply it to 'chain'. */
566 fwd_control_input(struct sw_chain *chain, const struct sender *sender,
567 const void *msg, size_t length)
570 struct openflow_packet {
572 int (*handler)(struct sw_chain *, const struct sender *,
576 static const struct openflow_packet packets[] = {
578 sizeof (struct ofp_header),
581 [OFPT_FEATURES_REQUEST] = {
582 sizeof (struct ofp_header),
583 recv_features_request,
585 [OFPT_GET_CONFIG_REQUEST] = {
586 sizeof (struct ofp_header),
587 recv_get_config_request,
589 [OFPT_SET_CONFIG] = {
590 sizeof (struct ofp_switch_config),
593 [OFPT_PACKET_OUT] = {
594 sizeof (struct ofp_packet_out),
598 sizeof (struct ofp_flow_mod),
602 sizeof (struct ofp_port_mod),
605 [OFPT_ECHO_REQUEST] = {
606 sizeof (struct ofp_header),
609 [OFPT_ECHO_REPLY] = {
610 sizeof (struct ofp_header),
615 struct ofp_header *oh;
617 oh = (struct ofp_header *) msg;
618 if (oh->version != OFP_VERSION
619 && oh->type != OFPT_HELLO
620 && oh->type != OFPT_ERROR
621 && oh->type != OFPT_ECHO_REQUEST
622 && oh->type != OFPT_ECHO_REPLY
623 && oh->type != OFPT_VENDOR)
625 dp_send_error_msg(chain->dp, sender, OFPET_BAD_REQUEST,
626 OFPBRC_BAD_VERSION, msg, length);
629 if (ntohs(oh->length) > length)
632 if (oh->type < ARRAY_SIZE(packets)) {
633 const struct openflow_packet *pkt = &packets[oh->type];
635 if (length < pkt->min_size)
637 return pkt->handler(chain, sender, msg);
640 dp_send_error_msg(chain->dp, sender, OFPET_BAD_REQUEST,
641 OFPBRC_BAD_TYPE, msg, length);
645 /* Packet buffering. */
647 #define OVERWRITE_SECS 1
648 #define OVERWRITE_JIFFIES (OVERWRITE_SECS * HZ)
650 struct packet_buffer {
653 unsigned long exp_jiffies;
656 static struct packet_buffer buffers[N_PKT_BUFFERS];
657 static unsigned int buffer_idx;
658 static DEFINE_SPINLOCK(buffer_lock);
660 uint32_t fwd_save_skb(struct sk_buff *skb)
662 struct sk_buff *old_skb = NULL;
663 struct packet_buffer *p;
664 unsigned long int flags;
667 spin_lock_irqsave(&buffer_lock, flags);
668 buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
669 p = &buffers[buffer_idx];
671 /* Don't buffer packet if existing entry is less than
672 * OVERWRITE_SECS old. */
673 if (time_before(jiffies, p->exp_jiffies)) {
674 spin_unlock_irqrestore(&buffer_lock, flags);
677 /* Defer kfree_skb() until interrupts re-enabled. */
681 /* Don't use maximum cookie value since the all-bits-1 id is
683 if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
687 p->exp_jiffies = jiffies + OVERWRITE_JIFFIES;
688 id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
689 spin_unlock_irqrestore(&buffer_lock, flags);
697 static struct sk_buff *retrieve_skb(uint32_t id)
699 unsigned long int flags;
700 struct sk_buff *skb = NULL;
701 struct packet_buffer *p;
703 spin_lock_irqsave(&buffer_lock, flags);
704 p = &buffers[id & PKT_BUFFER_MASK];
705 if (p->cookie == id >> PKT_BUFFER_BITS) {
709 printk("cookie mismatch: %x != %x\n",
710 id >> PKT_BUFFER_BITS, p->cookie);
712 spin_unlock_irqrestore(&buffer_lock, flags);
717 void fwd_discard_all(void)
721 for (i = 0; i < N_PKT_BUFFERS; i++) {
723 unsigned long int flags;
725 /* Defer kfree_skb() until interrupts re-enabled. */
726 spin_lock_irqsave(&buffer_lock, flags);
727 skb = buffers[i].skb;
728 buffers[i].skb = NULL;
729 spin_unlock_irqrestore(&buffer_lock, flags);
735 static void discard_skb(uint32_t id)
737 struct sk_buff *old_skb = NULL;
738 unsigned long int flags;
739 struct packet_buffer *p;
741 spin_lock_irqsave(&buffer_lock, flags);
742 p = &buffers[id & PKT_BUFFER_MASK];
743 if (p->cookie == id >> PKT_BUFFER_BITS) {
744 /* Defer kfree_skb() until interrupts re-enabled. */
748 spin_unlock_irqrestore(&buffer_lock, flags);
759 /* Utility functions. */
761 /* Makes '*pskb' writable, possibly copying it and setting '*pskb' to point to
763 * Returns 1 if successful, 0 on failure. */
765 make_writable(struct sk_buff **pskb)
767 /* Based on skb_make_writable() in net/netfilter/core.c. */
768 struct sk_buff *nskb;
770 /* Not exclusive use of packet? Must copy. */
771 if (skb_shared(*pskb) || skb_cloned(*pskb))
774 return pskb_may_pull(*pskb, 40); /* FIXME? */
777 nskb = skb_copy(*pskb, GFP_ATOMIC);
780 BUG_ON(skb_is_nonlinear(nskb));
782 /* Rest of kernel will get very unhappy if we pass it a
783 suddenly-orphaned skbuff */
785 skb_set_owner_w(nskb, (*pskb)->sk);