1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
35 #include <arpa/inet.h>
40 #include "controller.h"
46 static void execute_actions(struct datapath *, struct buffer *,
47 int in_port, const struct sw_flow_key *,
48 const struct ofp_action *, int n_actions);
50 static struct buffer *retrieve_buffer(uint32_t id);
51 static void discard_buffer(uint32_t id);
54 fwd_run(struct datapath *dp)
58 for (i = 0; i < 50; i++) {
59 struct buffer *buffer = controller_recv(dp->cc);
63 fwd_control_input(dp, buffer->data, buffer->size);
64 buffer_delete(buffer);
69 fwd_run_wait(struct datapath *dp)
71 controller_recv_wait(dp->cc);
74 /* 'buffer' was received on 'in_port', a physical switch port between 0 and
75 * OFPP_MAX. Process it according to 'chain'. */
76 void fwd_port_input(struct datapath *dp, struct buffer *buffer, int in_port)
78 struct sw_flow_key key;
82 flow_extract(buffer, in_port, &key.flow);
83 flow = chain_lookup(dp->chain, &key);
85 flow_used(flow, buffer);
86 execute_actions(dp, buffer, in_port, &key,
87 flow->actions, flow->n_actions);
89 dp_output_control(dp, buffer, in_port, fwd_save_buffer(buffer),
90 dp->miss_send_len, OFPR_NO_MATCH);
95 do_output(struct datapath *dp, struct buffer *buffer, int in_port,
96 size_t max_len, int out_port)
98 if (out_port != OFPP_CONTROLLER) {
99 dp_output_port(dp, buffer, in_port, out_port);
101 dp_output_control(dp, buffer, in_port, fwd_save_buffer(buffer),
102 max_len, OFPR_ACTION);
106 static void execute_actions(struct datapath *dp, struct buffer *buffer,
107 int in_port, const struct sw_flow_key *key,
108 const struct ofp_action *actions, int n_actions)
110 /* Every output action needs a separate clone of 'buffer', but the common
111 * case is just a single output action, so that doing a clone and then
112 * freeing the original buffer is wasteful. So the following code is
113 * slightly obscure just to avoid that. */
115 size_t max_len=0; /* Initialze to make compiler happy */
120 eth_proto = ntohs(key->flow.dl_type);
122 for (i = 0; i < n_actions; i++) {
123 const struct ofp_action *a = &actions[i];
125 if (prev_port != -1) {
126 do_output(dp, buffer_clone(buffer), in_port, max_len, prev_port);
130 if (a->type == ntohs(OFPAT_OUTPUT)) {
131 prev_port = ntohs(a->arg.output.port);
132 max_len = ntohs(a->arg.output.max_len);
134 buffer = execute_setter(buffer, eth_proto, key, a);
138 do_output(dp, buffer, in_port, max_len, prev_port);
140 buffer_delete(buffer);
143 /* Returns the new checksum for a packet in which the checksum field previously
144 * contained 'old_csum' and in which a field that contained 'old_u16' was
145 * changed to contain 'new_u16'. */
147 recalc_csum16(uint16_t old_csum, uint16_t old_u16, uint16_t new_u16)
149 /* Ones-complement arithmetic is endian-independent, so this code does not
150 * use htons() or ntohs().
152 * See RFC 1624 for formula and explanation. */
153 uint16_t hc_complement = ~old_csum;
154 uint16_t m_complement = ~old_u16;
155 uint16_t m_prime = new_u16;
156 uint32_t sum = hc_complement + m_complement + m_prime;
157 uint16_t hc_prime_complement = sum + (sum >> 16);
158 return ~hc_prime_complement;
161 /* Returns the new checksum for a packet in which the checksum field previously
162 * contained 'old_csum' and in which a field that contained 'old_u32' was
163 * changed to contain 'new_u32'. */
165 recalc_csum32(uint16_t old_csum, uint32_t old_u32, uint32_t new_u32)
167 return recalc_csum16(recalc_csum16(old_csum, old_u32, new_u32),
168 old_u32 >> 16, new_u32 >> 16);
171 static void modify_nh(struct buffer *buffer, uint16_t eth_proto,
172 uint8_t nw_proto, const struct ofp_action *a)
174 if (eth_proto == ETH_TYPE_IP) {
175 struct ip_header *nh = buffer->l3;
176 uint32_t new, *field;
178 new = a->arg.nw_addr;
179 field = a->type == OFPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
180 if (nw_proto == IP_TYPE_TCP) {
181 struct tcp_header *th = buffer->l4;
182 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, new);
183 } else if (nw_proto == IP_TYPE_UDP) {
184 struct udp_header *th = buffer->l4;
186 th->udp_csum = recalc_csum32(th->udp_csum, *field, new);
188 th->udp_csum = 0xffff;
192 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, new);
197 static void modify_th(struct buffer *buffer, uint16_t eth_proto,
198 uint8_t nw_proto, const struct ofp_action *a)
200 if (eth_proto == ETH_TYPE_IP) {
201 uint16_t new, *field;
205 if (nw_proto == IP_TYPE_TCP) {
206 struct tcp_header *th = buffer->l4;
207 field = a->type == OFPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
208 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, new);
210 } else if (nw_proto == IP_TYPE_UDP) {
211 struct udp_header *th = buffer->l4;
212 field = a->type == OFPAT_SET_TP_SRC ? &th->udp_src : &th->udp_dst;
213 th->udp_csum = recalc_csum16(th->udp_csum, *field, new);
219 static struct buffer *
220 modify_vlan(struct buffer *buffer,
221 const struct sw_flow_key *key, const struct ofp_action *a)
223 uint16_t new_id = a->arg.vlan_id;
224 struct vlan_eth_header *veh;
226 if (new_id != OFP_VLAN_NONE) {
227 if (key->flow.dl_vlan != htons(OFP_VLAN_NONE)) {
228 /* Modify vlan id, but maintain other TCI values */
230 veh->veth_tci &= ~htons(VLAN_VID);
231 veh->veth_tci |= htons(new_id);
233 /* Insert new vlan id. */
234 struct eth_header *eh = buffer->l2;
235 struct vlan_eth_header tmp;
236 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
237 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
238 tmp.veth_type = htons(ETH_TYPE_VLAN);
239 tmp.veth_tci = new_id;
240 tmp.veth_next_type = eh->eth_type;
242 veh = buffer_push_uninit(buffer, VLAN_HEADER_LEN);
243 memcpy(veh, &tmp, sizeof tmp);
244 buffer->l2 -= VLAN_HEADER_LEN;
247 /* Remove an existing vlan header if it exists */
249 if (veh->veth_type == htons(ETH_TYPE_VLAN)) {
250 struct eth_header tmp;
252 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
253 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
254 tmp.eth_type = veh->veth_next_type;
256 buffer->size -= VLAN_HEADER_LEN;
257 buffer->data += VLAN_HEADER_LEN;
258 buffer->l2 += VLAN_HEADER_LEN;
259 memcpy(buffer->data, &tmp, sizeof tmp);
266 struct buffer *execute_setter(struct buffer *buffer, uint16_t eth_proto,
267 const struct sw_flow_key *key, const struct ofp_action *a)
270 case OFPAT_SET_DL_VLAN:
271 buffer = modify_vlan(buffer, key, a);
274 case OFPAT_SET_DL_SRC: {
275 struct eth_header *eh = buffer->l2;
276 memcpy(eh->eth_src, a->arg.dl_addr, sizeof eh->eth_src);
279 case OFPAT_SET_DL_DST: {
280 struct eth_header *eh = buffer->l2;
281 memcpy(eh->eth_dst, a->arg.dl_addr, sizeof eh->eth_dst);
285 case OFPAT_SET_NW_SRC:
286 case OFPAT_SET_NW_DST:
287 modify_nh(buffer, eth_proto, key->flow.nw_proto, a);
290 case OFPAT_SET_TP_SRC:
291 case OFPAT_SET_TP_DST:
292 modify_th(buffer, eth_proto, key->flow.nw_proto, a);
303 recv_control_hello(struct datapath *dp, const void *msg)
305 const struct ofp_control_hello *och = msg;
307 printf("control_hello(version=%d)\n", ntohl(och->version));
309 if (ntohs(och->miss_send_len) != OFP_MISS_SEND_LEN_UNCHANGED) {
310 dp->miss_send_len = ntohs(och->miss_send_len);
313 dp->hello_flags = ntohs(och->flags);
321 recv_packet_out(struct datapath *dp, const void *msg)
323 const struct ofp_packet_out *opo = msg;
325 if (ntohl(opo->buffer_id) == (uint32_t) -1) {
326 /* FIXME: can we avoid copying data here? */
327 int data_len = ntohs(opo->header.length) - sizeof *opo;
328 struct buffer *buffer = buffer_new(data_len);
329 buffer_put(buffer, opo->u.data, data_len);
330 dp_output_port(dp, buffer,
331 ntohs(opo->in_port), ntohs(opo->out_port));
333 struct sw_flow_key key;
334 struct buffer *buffer;
337 buffer = retrieve_buffer(ntohl(opo->buffer_id));
342 n_acts = (ntohs(opo->header.length) - sizeof *opo)
343 / sizeof *opo->u.actions;
344 flow_extract(buffer, ntohs(opo->in_port), &key.flow);
345 execute_actions(dp, buffer, ntohs(opo->in_port),
346 &key, opo->u.actions, n_acts);
352 recv_port_mod(struct datapath *dp, const void *msg)
354 const struct ofp_port_mod *opm = msg;
356 dp_update_port_flags(dp, &opm->desc);
362 add_flow(struct datapath *dp, const struct ofp_flow_mod *ofm)
366 struct sw_flow *flow;
369 /* Check number of actions. */
370 n_acts = (ntohs(ofm->header.length) - sizeof *ofm) / sizeof *ofm->actions;
371 if (n_acts > MAX_ACTIONS) {
376 /* Allocate memory. */
377 flow = flow_alloc(n_acts);
382 flow_extract_match(&flow->key, &ofm->match);
383 flow->group_id = ntohl(ofm->group_id);
384 flow->max_idle = ntohs(ofm->max_idle);
385 flow->timeout = time(0) + flow->max_idle; /* FIXME */
386 flow->n_actions = n_acts;
387 flow->created = time(0); /* FIXME */
388 flow->byte_count = 0;
389 flow->packet_count = 0;
390 memcpy(flow->actions, ofm->actions, n_acts * sizeof *flow->actions);
393 error = chain_insert(dp->chain, flow);
395 goto error_free_flow;
398 if (ntohl(ofm->buffer_id) != UINT32_MAX) {
399 struct buffer *buffer = retrieve_buffer(ntohl(ofm->buffer_id));
401 struct sw_flow_key key;
402 uint16_t in_port = ntohs(ofm->match.in_port);
403 flow_used(flow, buffer);
404 flow_extract(buffer, in_port, &key.flow);
405 execute_actions(dp, buffer, in_port,
406 &key, ofm->actions, n_acts);
416 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
417 discard_buffer(ntohl(ofm->buffer_id));
422 recv_flow(struct datapath *dp, const void *msg)
424 const struct ofp_flow_mod *ofm = msg;
425 uint16_t command = ntohs(ofm->command);
427 if (command == OFPFC_ADD) {
428 return add_flow(dp, ofm);
429 } else if (command == OFPFC_DELETE) {
430 struct sw_flow_key key;
431 flow_extract_match(&key, &ofm->match);
432 return chain_delete(dp->chain, &key, 0) ? 0 : -ESRCH;
433 } else if (command == OFPFC_DELETE_STRICT) {
434 struct sw_flow_key key;
435 flow_extract_match(&key, &ofm->match);
436 return chain_delete(dp->chain, &key, 1) ? 0 : -ESRCH;
442 /* 'msg', which is 'length' bytes long, was received from the control path.
443 * Apply it to 'chain'. */
445 fwd_control_input(struct datapath *dp, const void *msg, size_t length)
448 struct openflow_packet {
450 int (*handler)(struct datapath *, const void *);
453 static const struct openflow_packet packets[] = {
454 [OFPT_CONTROL_HELLO] = {
455 sizeof (struct ofp_control_hello),
458 [OFPT_PACKET_OUT] = {
459 sizeof (struct ofp_packet_out),
463 sizeof (struct ofp_flow_mod),
467 sizeof (struct ofp_port_mod),
472 const struct openflow_packet *pkt;
473 struct ofp_header *oh;
475 if (length < sizeof(struct ofp_header))
478 oh = (struct ofp_header *) msg;
479 if (oh->version != 1 || oh->type >= ARRAY_SIZE(packets)
480 || ntohs(oh->length) > length)
483 pkt = &packets[oh->type];
486 if (length < pkt->min_size)
489 return pkt->handler(dp, msg);
492 /* Packet buffering. */
494 #define OVERWRITE_SECS 1
496 struct packet_buffer {
497 struct buffer *buffer;
502 static struct packet_buffer buffers[N_PKT_BUFFERS];
503 static unsigned int buffer_idx;
505 uint32_t fwd_save_buffer(struct buffer *buffer)
507 struct packet_buffer *p;
510 buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
511 p = &buffers[buffer_idx];
513 /* Don't buffer packet if existing entry is less than
514 * OVERWRITE_SECS old. */
515 if (time(0) < p->timeout) { /* FIXME */
518 buffer_delete(p->buffer);
521 /* Don't use maximum cookie value since the all-bits-1 id is
523 if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
525 p->buffer = buffer_clone(buffer); /* FIXME */
526 p->timeout = time(0) + OVERWRITE_SECS; /* FIXME */
527 id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
532 static struct buffer *retrieve_buffer(uint32_t id)
534 struct buffer *buffer = NULL;
535 struct packet_buffer *p;
537 p = &buffers[id & PKT_BUFFER_MASK];
538 if (p->cookie == id >> PKT_BUFFER_BITS) {
542 printf("cookie mismatch: %x != %x\n",
543 id >> PKT_BUFFER_BITS, p->cookie);
549 static void discard_buffer(uint32_t id)
551 struct packet_buffer *p;
553 p = &buffers[id & PKT_BUFFER_MASK];
554 if (p->cookie == id >> PKT_BUFFER_BITS) {
555 buffer_delete(p->buffer);
564 for (i = 0; i < N_PKT_BUFFERS; i++)
565 buffer_delete(buffers[i].buffer);