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>
42 #include "controller.h"
46 #include "poll-loop.h"
50 #define THIS_MODULE VLM_datapath
53 #define BRIDGE_PORT_NO_FLOOD 0x00000001
55 /* Capabilities supported by this implementation. */
56 #define OFP_SUPPORTED_CAPABILITIES (OFPC_MULTI_PHY_TX)
58 /* Actions supported by this implementation. */
59 #define OFP_SUPPORTED_ACTIONS ( (1 << OFPAT_OUTPUT) \
60 | (1 << OFPAT_SET_DL_VLAN) \
61 | (1 << OFPAT_SET_DL_SRC) \
62 | (1 << OFPAT_SET_DL_DST) \
63 | (1 << OFPAT_SET_NW_SRC) \
64 | (1 << OFPAT_SET_NW_DST) \
65 | (1 << OFPAT_SET_TP_SRC) \
66 | (1 << OFPAT_SET_TP_DST) )
71 struct netdev *netdev;
72 struct list node; /* Element in datapath.ports. */
76 struct controller_connection *cc;
80 /* Unique identifier for this datapath */
83 struct sw_chain *chain; /* Forwarding rules. */
85 /* Flags from the control hello message */
88 /* Maximum number of bytes that should be sent for flow misses */
89 uint16_t miss_send_len;
92 struct sw_port ports[OFPP_MAX];
93 struct list port_list; /* List of ports, for flooding. */
96 void dp_output_port(struct datapath *, struct buffer *,
97 int in_port, int out_port);
98 void dp_send_hello(struct datapath *);
99 void dp_update_port_flags(struct datapath *dp, const struct ofp_phy_port *opp);
100 void dp_output_control(struct datapath *, struct buffer *, int in_port,
101 size_t max_len, int reason);
102 static void send_port_status(struct sw_port *p, uint8_t status);
103 static void del_switch_port(struct sw_port *p);
104 static void execute_actions(struct datapath *, struct buffer *,
105 int in_port, const struct sw_flow_key *,
106 const struct ofp_action *, int n_actions);
107 static void modify_vlan(struct buffer *buffer, const struct sw_flow_key *key,
108 const struct ofp_action *a);
109 static void modify_nh(struct buffer *buffer, uint16_t eth_proto,
110 uint8_t nw_proto, const struct ofp_action *a);
111 static void modify_th(struct buffer *buffer, uint16_t eth_proto,
112 uint8_t nw_proto, const struct ofp_action *a);
114 /* Buffers are identified to userspace by a 31-bit opaque ID. We divide the ID
115 * into a buffer number (low bits) and a cookie (high bits). The buffer number
116 * is an index into an array of buffers. The cookie distinguishes between
117 * different packets that have occupied a single buffer. Thus, the more
118 * buffers we have, the lower-quality the cookie... */
119 #define PKT_BUFFER_BITS 8
120 #define N_PKT_BUFFERS (1 << PKT_BUFFER_BITS)
121 #define PKT_BUFFER_MASK (N_PKT_BUFFERS - 1)
123 #define PKT_COOKIE_BITS (32 - PKT_BUFFER_BITS)
125 void fwd_port_input(struct datapath *, struct buffer *, int in_port);
126 int fwd_control_input(struct datapath *, const void *, size_t);
128 uint32_t save_buffer(struct buffer *);
129 static struct buffer *retrieve_buffer(uint32_t id);
130 static void discard_buffer(uint32_t id);
132 static int port_no(struct datapath *dp, struct sw_port *p)
134 assert(p >= dp->ports && p < &dp->ports[ARRAY_SIZE(dp->ports)]);
135 return p - dp->ports;
138 /* Generates a unique datapath id. It incorporates the datapath index
139 * and a hardware address, if available. If not, it generates a random
143 gen_datapath_id(void)
145 /* Choose a random datapath id. */
151 for (i = 0; i < ETH_ADDR_LEN; i++) {
152 id |= (uint64_t)(rand() & 0xff) << (8*(ETH_ADDR_LEN-1 - i));
159 dp_new(struct datapath **dp_, uint64_t dpid, struct controller_connection *cc)
163 dp = calloc(1, sizeof *dp);
168 dp->last_timeout = time(0);
170 dp->id = dpid <= UINT64_C(0xffffffffffff) ? dpid : gen_datapath_id();
171 dp->chain = chain_create();
173 VLOG_ERR("could not create chain");
178 list_init(&dp->port_list);
179 dp->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
185 dp_add_port(struct datapath *dp, const char *name)
187 struct netdev *netdev;
191 error = netdev_open(name, &netdev);
196 for (p = dp->ports; ; p++) {
197 if (p >= &dp->ports[ARRAY_SIZE(dp->ports)]) {
199 } else if (!p->netdev) {
206 list_push_back(&dp->port_list, &p->node);
208 /* Notify the ctlpath that this port has been added */
209 send_port_status(p, OFPPR_ADD);
215 dp_run(struct datapath *dp)
217 time_t now = time(0);
218 struct sw_port *p, *n;
219 struct buffer *buffer = NULL;
222 if (now != dp->last_timeout) {
223 chain_timeout(dp->chain, dp);
224 dp->last_timeout = now;
226 poll_timer_wait(1000);
228 LIST_FOR_EACH_SAFE (p, n, struct sw_port, node, &dp->port_list) {
232 /* Allocate buffer with some headroom to add headers in forwarding
233 * to the controller or adding a vlan tag, plus an extra 2 bytes to
234 * allow IP headers to be aligned on a 4-byte boundary. */
235 const int headroom = 128 + 2;
236 const int hard_header = VLAN_ETH_HEADER_LEN;
237 const int mtu = netdev_get_mtu(p->netdev);
238 buffer = buffer_new(headroom + hard_header + mtu);
239 buffer->data += headroom;
241 error = netdev_recv(p->netdev, buffer);
243 fwd_port_input(dp, buffer, port_no(dp, p));
245 } else if (error != EAGAIN) {
246 VLOG_ERR("Error receiving data from %s: %s",
247 netdev_get_name(p->netdev), strerror(error));
251 buffer_delete(buffer);
255 dp_wait(struct datapath *dp)
259 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
260 netdev_recv_wait(p->netdev);
264 /* Delete 'p' from switch. */
266 del_switch_port(struct sw_port *p)
268 send_port_status(p, OFPPR_DELETE);
269 netdev_close(p->netdev);
271 list_remove(&p->node);
275 dp_destroy(struct datapath *dp)
277 struct sw_port *p, *n;
283 LIST_FOR_EACH_SAFE (p, n, struct sw_port, node, &dp->port_list) {
286 chain_destroy(dp->chain);
291 flood(struct datapath *dp, struct buffer *buffer, int in_port)
297 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
298 if (port_no(dp, p) == in_port || p->flags & BRIDGE_PORT_NO_FLOOD) {
301 if (prev_port != -1) {
302 dp_output_port(dp, buffer_clone(buffer), in_port, prev_port);
304 prev_port = port_no(dp, p);
307 dp_output_port(dp, buffer, in_port, prev_port);
309 buffer_delete(buffer);
315 output_packet(struct datapath *dp, struct buffer *buffer, int out_port)
317 if (out_port >= 0 && out_port < OFPP_MAX) {
318 struct sw_port *p = &dp->ports[out_port];
319 if (p->netdev != NULL) {
320 netdev_send(p->netdev, buffer);
325 buffer_delete(buffer);
326 /* FIXME: ratelimit */
327 VLOG_DBG("can't forward to bad port %d\n", out_port);
330 /* Takes ownership of 'buffer' and transmits it to 'out_port' on 'dp'.
333 dp_output_port(struct datapath *dp, struct buffer *buffer,
334 int in_port, int out_port)
338 if (out_port == OFPP_FLOOD) {
339 flood(dp, buffer, in_port);
340 } else if (out_port == OFPP_CONTROLLER) {
341 dp_output_control(dp, buffer, in_port, 0, OFPR_ACTION);
343 output_packet(dp, buffer, out_port);
347 /* Takes ownership of 'buffer' and transmits it to 'dp''s controller. If
348 * 'buffer_id' != -1, then only the first 64 bytes of 'buffer' are sent;
349 * otherwise, all of 'buffer' is sent. 'reason' indicates why 'buffer' is
350 * being sent. 'max_len' sets the maximum number of bytes that the caller wants
351 * to be sent; a value of 0 indicates the entire packet should be sent. */
353 dp_output_control(struct datapath *dp, struct buffer *buffer, int in_port,
354 size_t max_len, int reason)
356 struct ofp_packet_in *opi;
360 buffer_id = save_buffer(buffer);
361 total_len = buffer->size;
362 if (buffer_id != UINT32_MAX && max_len > buffer->size) {
363 buffer->size = max_len;
366 opi = buffer_push_uninit(buffer, offsetof(struct ofp_packet_in, data));
367 opi->header.version = OFP_VERSION;
368 opi->header.type = OFPT_PACKET_IN;
369 opi->header.length = htons(buffer->size);
370 opi->header.xid = htonl(0);
371 opi->buffer_id = htonl(buffer_id);
372 opi->total_len = htons(total_len);
373 opi->in_port = htons(in_port);
374 opi->reason = reason;
376 controller_send(dp->cc, buffer);
379 static void fill_port_desc(struct datapath *dp, struct sw_port *p,
380 struct ofp_phy_port *desc)
382 desc->port_no = htons(port_no(dp, p));
383 strncpy((char *) desc->name, netdev_get_name(p->netdev),
385 desc->name[sizeof desc->name - 1] = '\0';
386 memcpy(desc->hw_addr, netdev_get_etheraddr(p->netdev), ETH_ADDR_LEN);
387 desc->flags = htonl(p->flags);
388 desc->features = htonl(netdev_get_features(p->netdev));
389 desc->speed = htonl(netdev_get_speed(p->netdev));
393 dp_send_hello(struct datapath *dp)
395 struct buffer *buffer;
396 struct ofp_data_hello *odh;
399 buffer = buffer_new(sizeof *odh);
400 odh = buffer_put_uninit(buffer, sizeof *odh);
401 memset(odh, 0, sizeof *odh);
402 odh->header.version = OFP_VERSION;
403 odh->header.type = OFPT_DATA_HELLO;
404 odh->header.xid = htonl(0);
405 odh->datapath_id = htonll(dp->id);
406 odh->n_exact = htonl(2 * TABLE_HASH_MAX_FLOWS);
407 odh->n_mac_only = htonl(TABLE_MAC_MAX_FLOWS);
408 odh->n_compression = 0; /* Not supported */
409 odh->n_general = htonl(TABLE_LINEAR_MAX_FLOWS);
410 odh->buffer_mb = htonl(UINT32_MAX);
411 odh->n_buffers = htonl(N_PKT_BUFFERS);
412 odh->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
413 odh->actions = htonl(OFP_SUPPORTED_ACTIONS);
414 odh->miss_send_len = htons(dp->miss_send_len);
415 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
416 struct ofp_phy_port *opp = buffer_put_uninit(buffer, sizeof *opp);
417 memset(opp, 0, sizeof *opp);
418 fill_port_desc(dp, p, opp);
420 odh = buffer_at_assert(buffer, 0, sizeof *odh);
421 odh->header.length = htons(buffer->size);
422 controller_send(dp->cc, buffer);
426 dp_update_port_flags(struct datapath *dp, const struct ofp_phy_port *opp)
430 p = &dp->ports[htons(opp->port_no)];
432 /* Make sure the port id hasn't changed since this was sent */
433 if (!p || memcmp(opp->hw_addr, netdev_get_etheraddr(p->netdev),
437 p->flags = htonl(opp->flags);
441 send_port_status(struct sw_port *p, uint8_t status)
443 struct buffer *buffer;
444 struct ofp_port_status *ops;
445 buffer = buffer_new(sizeof *ops);
446 ops = buffer_put_uninit(buffer, sizeof *ops);
447 ops->header.version = OFP_VERSION;
448 ops->header.type = OFPT_PORT_STATUS;
449 ops->header.length = htons(sizeof(*ops));
450 ops->header.xid = htonl(0);
451 ops->reason = status;
452 fill_port_desc(p->dp, p, &ops->desc);
453 controller_send(p->dp->cc, buffer);
457 dp_send_flow_expired(struct datapath *dp, struct sw_flow *flow)
459 struct buffer *buffer;
460 struct ofp_flow_expired *ofe;
461 buffer = buffer_new(sizeof *ofe);
462 ofe = buffer_put_uninit(buffer, sizeof *ofe);
463 ofe->header.version = OFP_VERSION;
464 ofe->header.type = OFPT_FLOW_EXPIRED;
465 ofe->header.length = htons(sizeof(*ofe));
466 ofe->header.xid = htonl(0);
467 flow_fill_match(&ofe->match, &flow->key);
468 ofe->duration = htonl(flow->timeout - flow->max_idle - flow->created);
469 ofe->packet_count = htonll(flow->packet_count);
470 ofe->byte_count = htonll(flow->byte_count);
471 controller_send(dp->cc, buffer);
474 /* 'buffer' was received on 'in_port', a physical switch port between 0 and
475 * OFPP_MAX. Process it according to 'chain'. */
476 void fwd_port_input(struct datapath *dp, struct buffer *buffer, int in_port)
478 struct sw_flow_key key;
479 struct sw_flow *flow;
482 flow_extract(buffer, in_port, &key.flow);
483 flow = chain_lookup(dp->chain, &key);
485 flow_used(flow, buffer);
486 execute_actions(dp, buffer, in_port, &key,
487 flow->actions, flow->n_actions);
489 dp_output_control(dp, buffer, in_port, dp->miss_send_len,
495 do_output(struct datapath *dp, struct buffer *buffer, int in_port,
496 size_t max_len, int out_port)
498 if (out_port != OFPP_CONTROLLER) {
499 dp_output_port(dp, buffer, in_port, out_port);
501 dp_output_control(dp, buffer, in_port, max_len, OFPR_ACTION);
506 execute_actions(struct datapath *dp, struct buffer *buffer,
507 int in_port, const struct sw_flow_key *key,
508 const struct ofp_action *actions, int n_actions)
510 /* Every output action needs a separate clone of 'buffer', but the common
511 * case is just a single output action, so that doing a clone and then
512 * freeing the original buffer is wasteful. So the following code is
513 * slightly obscure just to avoid that. */
515 size_t max_len=0; /* Initialze to make compiler happy */
520 eth_proto = ntohs(key->flow.dl_type);
522 for (i = 0; i < n_actions; i++) {
523 const struct ofp_action *a = &actions[i];
524 struct eth_header *eh = buffer->l2;
526 if (prev_port != -1) {
527 do_output(dp, buffer_clone(buffer), in_port, max_len, prev_port);
531 switch (ntohs(a->type)) {
533 prev_port = ntohs(a->arg.output.port);
534 max_len = ntohs(a->arg.output.max_len);
537 case OFPAT_SET_DL_VLAN:
538 modify_vlan(buffer, key, a);
541 case OFPAT_SET_DL_SRC:
542 memcpy(eh->eth_src, a->arg.dl_addr, sizeof eh->eth_src);
545 case OFPAT_SET_DL_DST:
546 memcpy(eh->eth_dst, a->arg.dl_addr, sizeof eh->eth_dst);
549 case OFPAT_SET_NW_SRC:
550 case OFPAT_SET_NW_DST:
551 modify_nh(buffer, eth_proto, key->flow.nw_proto, a);
554 case OFPAT_SET_TP_SRC:
555 case OFPAT_SET_TP_DST:
556 modify_th(buffer, eth_proto, key->flow.nw_proto, a);
564 do_output(dp, buffer, in_port, max_len, prev_port);
566 buffer_delete(buffer);
569 /* Returns the new checksum for a packet in which the checksum field previously
570 * contained 'old_csum' and in which a field that contained 'old_u16' was
571 * changed to contain 'new_u16'. */
573 recalc_csum16(uint16_t old_csum, uint16_t old_u16, uint16_t new_u16)
575 /* Ones-complement arithmetic is endian-independent, so this code does not
576 * use htons() or ntohs().
578 * See RFC 1624 for formula and explanation. */
579 uint16_t hc_complement = ~old_csum;
580 uint16_t m_complement = ~old_u16;
581 uint16_t m_prime = new_u16;
582 uint32_t sum = hc_complement + m_complement + m_prime;
583 uint16_t hc_prime_complement = sum + (sum >> 16);
584 return ~hc_prime_complement;
587 /* Returns the new checksum for a packet in which the checksum field previously
588 * contained 'old_csum' and in which a field that contained 'old_u32' was
589 * changed to contain 'new_u32'. */
591 recalc_csum32(uint16_t old_csum, uint32_t old_u32, uint32_t new_u32)
593 return recalc_csum16(recalc_csum16(old_csum, old_u32, new_u32),
594 old_u32 >> 16, new_u32 >> 16);
597 static void modify_nh(struct buffer *buffer, uint16_t eth_proto,
598 uint8_t nw_proto, const struct ofp_action *a)
600 if (eth_proto == ETH_TYPE_IP) {
601 struct ip_header *nh = buffer->l3;
602 uint32_t new, *field;
604 new = a->arg.nw_addr;
605 field = a->type == OFPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
606 if (nw_proto == IP_TYPE_TCP) {
607 struct tcp_header *th = buffer->l4;
608 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, new);
609 } else if (nw_proto == IP_TYPE_UDP) {
610 struct udp_header *th = buffer->l4;
612 th->udp_csum = recalc_csum32(th->udp_csum, *field, new);
614 th->udp_csum = 0xffff;
618 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, new);
623 static void modify_th(struct buffer *buffer, uint16_t eth_proto,
624 uint8_t nw_proto, const struct ofp_action *a)
626 if (eth_proto == ETH_TYPE_IP) {
627 uint16_t new, *field;
631 if (nw_proto == IP_TYPE_TCP) {
632 struct tcp_header *th = buffer->l4;
633 field = a->type == OFPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
634 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, new);
636 } else if (nw_proto == IP_TYPE_UDP) {
637 struct udp_header *th = buffer->l4;
638 field = a->type == OFPAT_SET_TP_SRC ? &th->udp_src : &th->udp_dst;
639 th->udp_csum = recalc_csum16(th->udp_csum, *field, new);
646 modify_vlan(struct buffer *buffer,
647 const struct sw_flow_key *key, const struct ofp_action *a)
649 uint16_t new_id = a->arg.vlan_id;
650 struct vlan_eth_header *veh;
652 if (new_id != OFP_VLAN_NONE) {
653 if (key->flow.dl_vlan != htons(OFP_VLAN_NONE)) {
654 /* Modify vlan id, but maintain other TCI values */
656 veh->veth_tci &= ~htons(VLAN_VID);
657 veh->veth_tci |= htons(new_id);
659 /* Insert new vlan id. */
660 struct eth_header *eh = buffer->l2;
661 struct vlan_eth_header tmp;
662 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
663 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
664 tmp.veth_type = htons(ETH_TYPE_VLAN);
665 tmp.veth_tci = new_id;
666 tmp.veth_next_type = eh->eth_type;
668 veh = buffer_push_uninit(buffer, VLAN_HEADER_LEN);
669 memcpy(veh, &tmp, sizeof tmp);
670 buffer->l2 -= VLAN_HEADER_LEN;
673 /* Remove an existing vlan header if it exists */
675 if (veh->veth_type == htons(ETH_TYPE_VLAN)) {
676 struct eth_header tmp;
678 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
679 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
680 tmp.eth_type = veh->veth_next_type;
682 buffer->size -= VLAN_HEADER_LEN;
683 buffer->data += VLAN_HEADER_LEN;
684 buffer->l2 += VLAN_HEADER_LEN;
685 memcpy(buffer->data, &tmp, sizeof tmp);
691 recv_control_hello(struct datapath *dp, const void *msg)
693 const struct ofp_control_hello *och = msg;
695 printf("control_hello(version=%d)\n", ntohl(och->version));
697 if (ntohs(och->miss_send_len) != OFP_MISS_SEND_LEN_UNCHANGED) {
698 dp->miss_send_len = ntohs(och->miss_send_len);
701 dp->hello_flags = ntohs(och->flags);
709 recv_packet_out(struct datapath *dp, const void *msg)
711 const struct ofp_packet_out *opo = msg;
713 if (ntohl(opo->buffer_id) == (uint32_t) -1) {
714 /* FIXME: can we avoid copying data here? */
715 int data_len = ntohs(opo->header.length) - sizeof *opo;
716 struct buffer *buffer = buffer_new(data_len);
717 buffer_put(buffer, opo->u.data, data_len);
718 dp_output_port(dp, buffer,
719 ntohs(opo->in_port), ntohs(opo->out_port));
721 struct sw_flow_key key;
722 struct buffer *buffer;
725 buffer = retrieve_buffer(ntohl(opo->buffer_id));
730 n_acts = (ntohs(opo->header.length) - sizeof *opo)
731 / sizeof *opo->u.actions;
732 flow_extract(buffer, ntohs(opo->in_port), &key.flow);
733 execute_actions(dp, buffer, ntohs(opo->in_port),
734 &key, opo->u.actions, n_acts);
740 recv_port_mod(struct datapath *dp, const void *msg)
742 const struct ofp_port_mod *opm = msg;
744 dp_update_port_flags(dp, &opm->desc);
750 add_flow(struct datapath *dp, const struct ofp_flow_mod *ofm)
754 struct sw_flow *flow;
757 /* Check number of actions. */
758 n_acts = (ntohs(ofm->header.length) - sizeof *ofm) / sizeof *ofm->actions;
759 if (n_acts > MAX_ACTIONS) {
764 /* Allocate memory. */
765 flow = flow_alloc(n_acts);
770 flow_extract_match(&flow->key, &ofm->match);
771 flow->group_id = ntohl(ofm->group_id);
772 flow->max_idle = ntohs(ofm->max_idle);
773 flow->timeout = time(0) + flow->max_idle; /* FIXME */
774 flow->n_actions = n_acts;
775 flow->created = time(0); /* FIXME */
776 flow->byte_count = 0;
777 flow->packet_count = 0;
778 memcpy(flow->actions, ofm->actions, n_acts * sizeof *flow->actions);
781 error = chain_insert(dp->chain, flow);
783 goto error_free_flow;
786 if (ntohl(ofm->buffer_id) != UINT32_MAX) {
787 struct buffer *buffer = retrieve_buffer(ntohl(ofm->buffer_id));
789 struct sw_flow_key key;
790 uint16_t in_port = ntohs(ofm->match.in_port);
791 flow_used(flow, buffer);
792 flow_extract(buffer, in_port, &key.flow);
793 execute_actions(dp, buffer, in_port, &key, ofm->actions, n_acts);
803 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
804 discard_buffer(ntohl(ofm->buffer_id));
809 recv_flow(struct datapath *dp, const void *msg)
811 const struct ofp_flow_mod *ofm = msg;
812 uint16_t command = ntohs(ofm->command);
814 if (command == OFPFC_ADD) {
815 return add_flow(dp, ofm);
816 } else if (command == OFPFC_DELETE) {
817 struct sw_flow_key key;
818 flow_extract_match(&key, &ofm->match);
819 return chain_delete(dp->chain, &key, 0) ? 0 : -ESRCH;
820 } else if (command == OFPFC_DELETE_STRICT) {
821 struct sw_flow_key key;
822 flow_extract_match(&key, &ofm->match);
823 return chain_delete(dp->chain, &key, 1) ? 0 : -ESRCH;
829 /* 'msg', which is 'length' bytes long, was received from the control path.
830 * Apply it to 'chain'. */
832 fwd_control_input(struct datapath *dp, const void *msg, size_t length)
835 struct openflow_packet {
837 int (*handler)(struct datapath *, const void *);
840 static const struct openflow_packet packets[] = {
841 [OFPT_CONTROL_HELLO] = {
842 sizeof (struct ofp_control_hello),
845 [OFPT_PACKET_OUT] = {
846 sizeof (struct ofp_packet_out),
850 sizeof (struct ofp_flow_mod),
854 sizeof (struct ofp_port_mod),
859 const struct openflow_packet *pkt;
860 struct ofp_header *oh;
862 if (length < sizeof(struct ofp_header))
865 oh = (struct ofp_header *) msg;
866 if (oh->version != 1 || oh->type >= ARRAY_SIZE(packets)
867 || ntohs(oh->length) > length)
870 pkt = &packets[oh->type];
873 if (length < pkt->min_size)
876 return pkt->handler(dp, msg);
879 /* Packet buffering. */
881 #define OVERWRITE_SECS 1
883 struct packet_buffer {
884 struct buffer *buffer;
889 static struct packet_buffer buffers[N_PKT_BUFFERS];
890 static unsigned int buffer_idx;
892 uint32_t save_buffer(struct buffer *buffer)
894 struct packet_buffer *p;
897 buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
898 p = &buffers[buffer_idx];
900 /* Don't buffer packet if existing entry is less than
901 * OVERWRITE_SECS old. */
902 if (time(0) < p->timeout) { /* FIXME */
905 buffer_delete(p->buffer);
908 /* Don't use maximum cookie value since the all-bits-1 id is
910 if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
912 p->buffer = buffer_clone(buffer); /* FIXME */
913 p->timeout = time(0) + OVERWRITE_SECS; /* FIXME */
914 id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
919 static struct buffer *retrieve_buffer(uint32_t id)
921 struct buffer *buffer = NULL;
922 struct packet_buffer *p;
924 p = &buffers[id & PKT_BUFFER_MASK];
925 if (p->cookie == id >> PKT_BUFFER_BITS) {
929 printf("cookie mismatch: %x != %x\n",
930 id >> PKT_BUFFER_BITS, p->cookie);
936 static void discard_buffer(uint32_t id)
938 struct packet_buffer *p;
940 p = &buffers[id & PKT_BUFFER_MASK];
941 if (p->cookie == id >> PKT_BUFFER_BITS) {
942 buffer_delete(p->buffer);