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)) {
60 execute_actions(chain->dp, skb, &key,
61 flow->actions, flow->n_actions, 0);
68 /* 'skb' was received on port 'p', which may be a physical switch port, the
69 * local port, or a null pointer. Process it according to 'chain', sending it
70 * up to the controller if no flow matches. Takes ownership of 'skb'. */
71 void fwd_port_input(struct sw_chain *chain, struct sk_buff *skb,
72 struct net_bridge_port *p)
74 if (run_flow_through_tables(chain, skb, p))
75 dp_output_control(chain->dp, skb, fwd_save_skb(skb),
76 chain->dp->miss_send_len,
80 static int do_output(struct datapath *dp, struct sk_buff *skb, size_t max_len,
81 int out_port, int ignore_no_fwd)
85 return (likely(out_port != OFPP_CONTROLLER)
86 ? dp_output_port(dp, skb, out_port, ignore_no_fwd)
87 : dp_output_control(dp, skb, fwd_save_skb(skb),
88 max_len, OFPR_ACTION));
91 void execute_actions(struct datapath *dp, struct sk_buff *skb,
92 const struct sw_flow_key *key,
93 const struct ofp_action *actions, int n_actions,
96 /* Every output action needs a separate clone of 'skb', but the common
97 * case is just a single output action, so that doing a clone and
98 * then freeing the original skbuff is wasteful. So the following code
99 * is slightly obscure just to avoid that. */
101 size_t max_len=0; /* Initialze to make compiler happy */
106 eth_proto = ntohs(key->dl_type);
108 for (i = 0; i < n_actions; i++) {
109 const struct ofp_action *a = &actions[i];
111 if (prev_port != -1) {
112 do_output(dp, skb_clone(skb, GFP_ATOMIC),
113 max_len, prev_port, ignore_no_fwd);
117 if (likely(a->type == htons(OFPAT_OUTPUT))) {
118 prev_port = ntohs(a->arg.output.port);
119 max_len = ntohs(a->arg.output.max_len);
121 if (!make_writable(&skb)) {
123 printk("make_writable failed\n");
126 skb = execute_setter(skb, eth_proto, key, a);
129 printk("execute_setter lost skb\n");
135 do_output(dp, skb, max_len, prev_port, ignore_no_fwd);
140 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
141 * covered by the sum has been changed from 'from' to 'to'. If set,
142 * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
143 * Based on nf_proto_csum_replace4. */
144 static void update_csum(__sum16 *sum, struct sk_buff *skb,
145 __be32 from, __be32 to, int pseudohdr)
147 __be32 diff[] = { ~from, to };
148 if (skb->ip_summed != CHECKSUM_PARTIAL) {
149 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
150 ~csum_unfold(*sum)));
151 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
152 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
154 } else if (pseudohdr)
155 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
159 static void modify_nh(struct sk_buff *skb, uint16_t eth_proto,
160 uint8_t nw_proto, const struct ofp_action *a)
162 if (eth_proto == ETH_P_IP) {
163 struct iphdr *nh = ip_hdr(skb);
164 uint32_t new, *field;
166 new = a->arg.nw_addr;
168 if (a->type == htons(OFPAT_SET_NW_SRC))
173 if (nw_proto == IPPROTO_TCP) {
174 struct tcphdr *th = tcp_hdr(skb);
175 update_csum(&th->check, skb, *field, new, 1);
176 } else if (nw_proto == IPPROTO_UDP) {
177 struct udphdr *th = udp_hdr(skb);
178 update_csum(&th->check, skb, *field, new, 1);
180 update_csum(&nh->check, skb, *field, new, 0);
185 static void modify_th(struct sk_buff *skb, uint16_t eth_proto,
186 uint8_t nw_proto, const struct ofp_action *a)
188 if (eth_proto == ETH_P_IP) {
189 uint16_t new, *field;
193 if (nw_proto == IPPROTO_TCP) {
194 struct tcphdr *th = tcp_hdr(skb);
196 if (a->type == htons(OFPAT_SET_TP_SRC))
201 update_csum(&th->check, skb, *field, new, 1);
203 } else if (nw_proto == IPPROTO_UDP) {
204 struct udphdr *th = udp_hdr(skb);
206 if (a->type == htons(OFPAT_SET_TP_SRC))
211 update_csum(&th->check, skb, *field, new, 1);
217 static struct sk_buff *vlan_pull_tag(struct sk_buff *skb)
219 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
223 /* Verify we were given a vlan packet */
224 if (vh->h_vlan_proto != htons(ETH_P_8021Q))
227 memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
229 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
231 skb->protocol = eh->h_proto;
232 skb->mac_header += VLAN_HLEN;
237 static struct sk_buff *modify_vlan(struct sk_buff *skb,
238 const struct sw_flow_key *key, const struct ofp_action *a)
240 uint16_t new_id = ntohs(a->arg.vlan_id);
242 if (new_id != OFP_VLAN_NONE) {
243 if (key->dl_vlan != htons(OFP_VLAN_NONE)) {
244 /* Modify vlan id, but maintain other TCI values */
245 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
246 vh->h_vlan_TCI = (vh->h_vlan_TCI
247 & ~(htons(VLAN_VID_MASK))) | a->arg.vlan_id;
249 /* Add vlan header */
251 /* xxx The vlan_put_tag function, doesn't seem to work
252 * xxx reliably when it attempts to use the hardware-accelerated
253 * xxx version. We'll directly use the software version
254 * xxx until the problem can be diagnosed.
256 skb = __vlan_put_tag(skb, new_id);
259 /* Remove an existing vlan header if it exists */
266 struct sk_buff *execute_setter(struct sk_buff *skb, uint16_t eth_proto,
267 const struct sw_flow_key *key, const struct ofp_action *a)
269 switch (ntohs(a->type)) {
270 case OFPAT_SET_DL_VLAN:
271 skb = modify_vlan(skb, key, a);
274 case OFPAT_SET_DL_SRC: {
275 struct ethhdr *eh = eth_hdr(skb);
276 memcpy(eh->h_source, a->arg.dl_addr, sizeof eh->h_source);
279 case OFPAT_SET_DL_DST: {
280 struct ethhdr *eh = eth_hdr(skb);
281 memcpy(eh->h_dest, a->arg.dl_addr, sizeof eh->h_dest);
285 case OFPAT_SET_NW_SRC:
286 case OFPAT_SET_NW_DST:
287 modify_nh(skb, eth_proto, key->nw_proto, a);
290 case OFPAT_SET_TP_SRC:
291 case OFPAT_SET_TP_DST:
292 modify_th(skb, eth_proto, key->nw_proto, a);
297 printk("execute_setter: unknown action: %d\n", ntohs(a->type));
304 recv_features_request(struct sw_chain *chain, const struct sender *sender,
307 return dp_send_features_reply(chain->dp, sender);
311 recv_get_config_request(struct sw_chain *chain, const struct sender *sender,
314 return dp_send_config_reply(chain->dp, sender);
318 recv_set_config(struct sw_chain *chain, const struct sender *sender,
321 const struct ofp_switch_config *osc = msg;
324 flags = ntohs(osc->flags) & (OFPC_SEND_FLOW_EXP | OFPC_FRAG_MASK);
325 if ((flags & OFPC_FRAG_MASK) != OFPC_FRAG_NORMAL
326 && (flags & OFPC_FRAG_MASK) != OFPC_FRAG_DROP) {
327 flags = (flags & ~OFPC_FRAG_MASK) | OFPC_FRAG_DROP;
329 chain->dp->flags = flags;
331 chain->dp->miss_send_len = ntohs(osc->miss_send_len);
337 recv_packet_out(struct sw_chain *chain, const struct sender *sender,
340 const struct ofp_packet_out *opo = msg;
342 struct vlan_ethhdr *mac;
344 struct sw_flow_key key;
345 int n_actions = ntohs(opo->n_actions);
346 int act_len = n_actions * sizeof opo->actions[0];
348 if (act_len > (ntohs(opo->header.length) - sizeof *opo)) {
350 printk("message too short for number of actions\n");
354 if (ntohl(opo->buffer_id) == (uint32_t) -1) {
355 int data_len = ntohs(opo->header.length) - sizeof *opo - act_len;
357 /* FIXME: there is likely a way to reuse the data in msg. */
358 skb = alloc_skb(data_len, GFP_ATOMIC);
362 /* FIXME? We don't reserve NET_IP_ALIGN or NET_SKB_PAD since
363 * we're just transmitting this raw without examining anything
364 * at those layers. */
365 memcpy(skb_put(skb, data_len), &opo->actions[n_actions], data_len);
367 skb_set_mac_header(skb, 0);
368 mac = vlan_eth_hdr(skb);
369 if (likely(mac->h_vlan_proto != htons(ETH_P_8021Q)))
370 nh_ofs = sizeof(struct ethhdr);
372 nh_ofs = sizeof(struct vlan_ethhdr);
373 skb_set_network_header(skb, nh_ofs);
375 skb = retrieve_skb(ntohl(opo->buffer_id));
380 dp_set_origin(chain->dp, ntohs(opo->in_port), skb);
382 flow_extract(skb, ntohs(opo->in_port), &key);
383 execute_actions(chain->dp, skb, &key, opo->actions, n_actions, 1);
389 recv_port_mod(struct sw_chain *chain, const struct sender *sender,
392 const struct ofp_port_mod *opm = msg;
394 dp_update_port_flags(chain->dp, opm);
400 recv_echo_request(struct sw_chain *chain, const struct sender *sender,
403 return dp_send_echo_reply(chain->dp, sender, msg);
407 recv_echo_reply(struct sw_chain *chain, const struct sender *sender,
414 add_flow(struct sw_chain *chain, const struct ofp_flow_mod *ofm)
419 struct sw_flow *flow;
422 /* To prevent loops, make sure there's no action to send to the
423 * OFP_TABLE virtual port.
425 n_actions = (ntohs(ofm->header.length) - sizeof *ofm)
426 / sizeof *ofm->actions;
427 for (i=0; i<n_actions; i++) {
428 const struct ofp_action *a = &ofm->actions[i];
430 if (a->type == htons(OFPAT_OUTPUT)
431 && (a->arg.output.port == htons(OFPP_TABLE)
432 || a->arg.output.port == htons(OFPP_NONE)
433 || a->arg.output.port == ofm->match.in_port)) {
434 /* xxx Send fancy new error message? */
439 /* Allocate memory. */
440 flow = flow_alloc(n_actions, GFP_ATOMIC);
445 flow_extract_match(&flow->key, &ofm->match);
446 flow->priority = flow->key.wildcards ? ntohs(ofm->priority) : -1;
447 flow->idle_timeout = ntohs(ofm->idle_timeout);
448 flow->hard_timeout = ntohs(ofm->hard_timeout);
449 flow->used = jiffies;
450 flow->n_actions = n_actions;
451 flow->init_time = jiffies;
452 flow->byte_count = 0;
453 flow->packet_count = 0;
454 spin_lock_init(&flow->lock);
455 memcpy(flow->actions, ofm->actions, n_actions * sizeof *flow->actions);
458 error = chain_insert(chain, flow);
460 goto error_free_flow;
462 if (ntohl(ofm->buffer_id) != (uint32_t) -1) {
463 struct sk_buff *skb = retrieve_skb(ntohl(ofm->buffer_id));
465 struct sw_flow_key key;
466 flow_used(flow, skb);
467 flow_extract(skb, ntohs(ofm->match.in_port), &key);
468 execute_actions(chain->dp, skb, &key, ofm->actions, n_actions, 0);
478 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
479 discard_skb(ntohl(ofm->buffer_id));
484 recv_flow(struct sw_chain *chain, const struct sender *sender, const void *msg)
486 const struct ofp_flow_mod *ofm = msg;
487 uint16_t command = ntohs(ofm->command);
489 if (command == OFPFC_ADD) {
490 return add_flow(chain, ofm);
491 } else if (command == OFPFC_DELETE) {
492 struct sw_flow_key key;
493 flow_extract_match(&key, &ofm->match);
494 return chain_delete(chain, &key, 0, 0) ? 0 : -ESRCH;
495 } else if (command == OFPFC_DELETE_STRICT) {
496 struct sw_flow_key key;
498 flow_extract_match(&key, &ofm->match);
499 priority = key.wildcards ? ntohs(ofm->priority) : -1;
500 return chain_delete(chain, &key, priority, 1) ? 0 : -ESRCH;
506 /* 'msg', which is 'length' bytes long, was received across Netlink from
507 * 'sender'. Apply it to 'chain'. */
509 fwd_control_input(struct sw_chain *chain, const struct sender *sender,
510 const void *msg, size_t length)
513 struct openflow_packet {
515 int (*handler)(struct sw_chain *, const struct sender *,
519 static const struct openflow_packet packets[] = {
520 [OFPT_FEATURES_REQUEST] = {
521 sizeof (struct ofp_header),
522 recv_features_request,
524 [OFPT_GET_CONFIG_REQUEST] = {
525 sizeof (struct ofp_header),
526 recv_get_config_request,
528 [OFPT_SET_CONFIG] = {
529 sizeof (struct ofp_switch_config),
532 [OFPT_PACKET_OUT] = {
533 sizeof (struct ofp_packet_out),
537 sizeof (struct ofp_flow_mod),
541 sizeof (struct ofp_port_mod),
544 [OFPT_ECHO_REQUEST] = {
545 sizeof (struct ofp_header),
548 [OFPT_ECHO_REPLY] = {
549 sizeof (struct ofp_header),
554 struct ofp_header *oh;
556 oh = (struct ofp_header *) msg;
557 if (oh->version != OFP_VERSION) {
558 dp_send_error_msg(chain->dp, sender, OFPET_BAD_REQUEST,
559 OFPBRC_BAD_VERSION, msg, length);
562 if (ntohs(oh->length) > length)
565 if (oh->type < ARRAY_SIZE(packets)) {
566 const struct openflow_packet *pkt = &packets[oh->type];
568 if (length < pkt->min_size)
570 return pkt->handler(chain, sender, msg);
573 dp_send_error_msg(chain->dp, sender, OFPET_BAD_REQUEST,
574 OFPBRC_BAD_TYPE, msg, length);
578 /* Packet buffering. */
580 #define OVERWRITE_SECS 1
581 #define OVERWRITE_JIFFIES (OVERWRITE_SECS * HZ)
583 struct packet_buffer {
586 unsigned long exp_jiffies;
589 static struct packet_buffer buffers[N_PKT_BUFFERS];
590 static unsigned int buffer_idx;
591 static DEFINE_SPINLOCK(buffer_lock);
593 uint32_t fwd_save_skb(struct sk_buff *skb)
595 struct sk_buff *old_skb = NULL;
596 struct packet_buffer *p;
597 unsigned long int flags;
600 spin_lock_irqsave(&buffer_lock, flags);
601 buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
602 p = &buffers[buffer_idx];
604 /* Don't buffer packet if existing entry is less than
605 * OVERWRITE_SECS old. */
606 if (time_before(jiffies, p->exp_jiffies)) {
607 spin_unlock_irqrestore(&buffer_lock, flags);
610 /* Defer kfree_skb() until interrupts re-enabled. */
614 /* Don't use maximum cookie value since the all-bits-1 id is
616 if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
620 p->exp_jiffies = jiffies + OVERWRITE_JIFFIES;
621 id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
622 spin_unlock_irqrestore(&buffer_lock, flags);
630 static struct sk_buff *retrieve_skb(uint32_t id)
632 unsigned long int flags;
633 struct sk_buff *skb = NULL;
634 struct packet_buffer *p;
636 spin_lock_irqsave(&buffer_lock, flags);
637 p = &buffers[id & PKT_BUFFER_MASK];
638 if (p->cookie == id >> PKT_BUFFER_BITS) {
642 printk("cookie mismatch: %x != %x\n",
643 id >> PKT_BUFFER_BITS, p->cookie);
645 spin_unlock_irqrestore(&buffer_lock, flags);
650 void fwd_discard_all(void)
654 for (i = 0; i < N_PKT_BUFFERS; i++) {
656 unsigned long int flags;
658 /* Defer kfree_skb() until interrupts re-enabled. */
659 spin_lock_irqsave(&buffer_lock, flags);
660 skb = buffers[i].skb;
661 buffers[i].skb = NULL;
662 spin_unlock_irqrestore(&buffer_lock, flags);
668 static void discard_skb(uint32_t id)
670 struct sk_buff *old_skb = NULL;
671 unsigned long int flags;
672 struct packet_buffer *p;
674 spin_lock_irqsave(&buffer_lock, flags);
675 p = &buffers[id & PKT_BUFFER_MASK];
676 if (p->cookie == id >> PKT_BUFFER_BITS) {
677 /* Defer kfree_skb() until interrupts re-enabled. */
681 spin_unlock_irqrestore(&buffer_lock, flags);
692 /* Utility functions. */
694 /* Makes '*pskb' writable, possibly copying it and setting '*pskb' to point to
696 * Returns 1 if successful, 0 on failure. */
698 make_writable(struct sk_buff **pskb)
700 /* Based on skb_make_writable() in net/netfilter/core.c. */
701 struct sk_buff *nskb;
703 /* Not exclusive use of packet? Must copy. */
704 if (skb_shared(*pskb) || skb_cloned(*pskb))
707 return pskb_may_pull(*pskb, 40); /* FIXME? */
710 nskb = skb_copy(*pskb, GFP_ATOMIC);
713 BUG_ON(skb_is_nonlinear(nskb));
715 /* Rest of kernel will get very unhappy if we pass it a
716 suddenly-orphaned skbuff */
718 skb_set_owner_w(nskb, (*pskb)->sk);