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_flow_expired(struct datapath *, struct sw_flow *);
103 static void send_port_status(struct sw_port *p, uint8_t status);
104 static void del_switch_port(struct sw_port *p);
105 static void execute_actions(struct datapath *, struct buffer *,
106 int in_port, const struct sw_flow_key *,
107 const struct ofp_action *, int n_actions);
108 static void modify_vlan(struct buffer *buffer, const struct sw_flow_key *key,
109 const struct ofp_action *a);
110 static void modify_nh(struct buffer *buffer, uint16_t eth_proto,
111 uint8_t nw_proto, const struct ofp_action *a);
112 static void modify_th(struct buffer *buffer, uint16_t eth_proto,
113 uint8_t nw_proto, const struct ofp_action *a);
115 /* Buffers are identified to userspace by a 31-bit opaque ID. We divide the ID
116 * into a buffer number (low bits) and a cookie (high bits). The buffer number
117 * is an index into an array of buffers. The cookie distinguishes between
118 * different packets that have occupied a single buffer. Thus, the more
119 * buffers we have, the lower-quality the cookie... */
120 #define PKT_BUFFER_BITS 8
121 #define N_PKT_BUFFERS (1 << PKT_BUFFER_BITS)
122 #define PKT_BUFFER_MASK (N_PKT_BUFFERS - 1)
124 #define PKT_COOKIE_BITS (32 - PKT_BUFFER_BITS)
126 void fwd_port_input(struct datapath *, struct buffer *, int in_port);
127 int fwd_control_input(struct datapath *, const void *, size_t);
129 uint32_t save_buffer(struct buffer *);
130 static struct buffer *retrieve_buffer(uint32_t id);
131 static void discard_buffer(uint32_t id);
133 static int port_no(struct datapath *dp, struct sw_port *p)
135 assert(p >= dp->ports && p < &dp->ports[ARRAY_SIZE(dp->ports)]);
136 return p - dp->ports;
139 /* Generates a unique datapath id. It incorporates the datapath index
140 * and a hardware address, if available. If not, it generates a random
144 gen_datapath_id(void)
146 /* Choose a random datapath id. */
152 for (i = 0; i < ETH_ADDR_LEN; i++) {
153 id |= (uint64_t)(rand() & 0xff) << (8*(ETH_ADDR_LEN-1 - i));
160 dp_new(struct datapath **dp_, uint64_t dpid, struct controller_connection *cc)
164 dp = calloc(1, sizeof *dp);
169 dp->last_timeout = time(0);
171 dp->id = dpid <= UINT64_C(0xffffffffffff) ? dpid : gen_datapath_id();
172 dp->chain = chain_create();
174 VLOG_ERR("could not create chain");
179 list_init(&dp->port_list);
180 dp->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
186 dp_add_port(struct datapath *dp, const char *name)
188 struct netdev *netdev;
192 error = netdev_open(name, &netdev);
197 for (p = dp->ports; ; p++) {
198 if (p >= &dp->ports[ARRAY_SIZE(dp->ports)]) {
200 } else if (!p->netdev) {
207 list_push_back(&dp->port_list, &p->node);
209 /* Notify the ctlpath that this port has been added */
210 send_port_status(p, OFPPR_ADD);
216 dp_run(struct datapath *dp)
218 time_t now = time(0);
219 struct sw_port *p, *n;
220 struct buffer *buffer = NULL;
223 if (now != dp->last_timeout) {
224 struct list deleted = LIST_INITIALIZER(&deleted);
225 struct sw_flow *f, *n;
227 chain_timeout(dp->chain, &deleted);
228 LIST_FOR_EACH_SAFE (f, n, struct sw_flow, node, &deleted) {
229 send_flow_expired(dp, f);
230 list_remove(&f->node);
233 dp->last_timeout = now;
235 poll_timer_wait(1000);
237 LIST_FOR_EACH_SAFE (p, n, struct sw_port, node, &dp->port_list) {
241 /* Allocate buffer with some headroom to add headers in forwarding
242 * to the controller or adding a vlan tag, plus an extra 2 bytes to
243 * allow IP headers to be aligned on a 4-byte boundary. */
244 const int headroom = 128 + 2;
245 const int hard_header = VLAN_ETH_HEADER_LEN;
246 const int mtu = netdev_get_mtu(p->netdev);
247 buffer = buffer_new(headroom + hard_header + mtu);
248 buffer->data += headroom;
250 error = netdev_recv(p->netdev, buffer);
252 fwd_port_input(dp, buffer, port_no(dp, p));
254 } else if (error != EAGAIN) {
255 VLOG_ERR("Error receiving data from %s: %s",
256 netdev_get_name(p->netdev), strerror(error));
260 buffer_delete(buffer);
262 for (i = 0; i < 50; i++) {
263 struct buffer *buffer = controller_recv(dp->cc);
267 fwd_control_input(dp, buffer->data, buffer->size);
268 buffer_delete(buffer);
271 controller_run(dp->cc);
275 dp_wait(struct datapath *dp)
279 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
280 netdev_recv_wait(p->netdev);
282 controller_recv_wait(dp->cc);
285 /* Delete 'p' from switch. */
287 del_switch_port(struct sw_port *p)
289 send_port_status(p, OFPPR_DELETE);
290 netdev_close(p->netdev);
292 list_remove(&p->node);
296 dp_destroy(struct datapath *dp)
298 struct sw_port *p, *n;
304 LIST_FOR_EACH_SAFE (p, n, struct sw_port, node, &dp->port_list) {
307 chain_destroy(dp->chain);
312 flood(struct datapath *dp, struct buffer *buffer, int in_port)
318 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
319 if (port_no(dp, p) == in_port || p->flags & BRIDGE_PORT_NO_FLOOD) {
322 if (prev_port != -1) {
323 dp_output_port(dp, buffer_clone(buffer), in_port, prev_port);
325 prev_port = port_no(dp, p);
328 dp_output_port(dp, buffer, in_port, prev_port);
330 buffer_delete(buffer);
336 output_packet(struct datapath *dp, struct buffer *buffer, int out_port)
338 if (out_port >= 0 && out_port < OFPP_MAX) {
339 struct sw_port *p = &dp->ports[out_port];
340 if (p->netdev != NULL) {
341 netdev_send(p->netdev, buffer);
346 buffer_delete(buffer);
347 /* FIXME: ratelimit */
348 VLOG_DBG("can't forward to bad port %d\n", out_port);
351 /* Takes ownership of 'buffer' and transmits it to 'out_port' on 'dp'.
354 dp_output_port(struct datapath *dp, struct buffer *buffer,
355 int in_port, int out_port)
359 if (out_port == OFPP_FLOOD) {
360 flood(dp, buffer, in_port);
361 } else if (out_port == OFPP_CONTROLLER) {
362 dp_output_control(dp, buffer, in_port, 0, OFPR_ACTION);
364 output_packet(dp, buffer, out_port);
368 /* Takes ownership of 'buffer' and transmits it to 'dp''s controller. If
369 * 'buffer_id' != -1, then only the first 64 bytes of 'buffer' are sent;
370 * otherwise, all of 'buffer' is sent. 'reason' indicates why 'buffer' is
371 * being sent. 'max_len' sets the maximum number of bytes that the caller wants
372 * to be sent; a value of 0 indicates the entire packet should be sent. */
374 dp_output_control(struct datapath *dp, struct buffer *buffer, int in_port,
375 size_t max_len, int reason)
377 struct ofp_packet_in *opi;
381 buffer_id = save_buffer(buffer);
382 total_len = buffer->size;
383 if (buffer_id != UINT32_MAX && max_len > buffer->size) {
384 buffer->size = max_len;
387 opi = buffer_push_uninit(buffer, offsetof(struct ofp_packet_in, data));
388 opi->header.version = OFP_VERSION;
389 opi->header.type = OFPT_PACKET_IN;
390 opi->header.length = htons(buffer->size);
391 opi->header.xid = htonl(0);
392 opi->buffer_id = htonl(buffer_id);
393 opi->total_len = htons(total_len);
394 opi->in_port = htons(in_port);
395 opi->reason = reason;
397 controller_send(dp->cc, buffer);
400 static void fill_port_desc(struct datapath *dp, struct sw_port *p,
401 struct ofp_phy_port *desc)
403 desc->port_no = htons(port_no(dp, p));
404 strncpy((char *) desc->name, netdev_get_name(p->netdev),
406 desc->name[sizeof desc->name - 1] = '\0';
407 memcpy(desc->hw_addr, netdev_get_etheraddr(p->netdev), ETH_ADDR_LEN);
408 desc->flags = htonl(p->flags);
409 desc->features = htonl(netdev_get_features(p->netdev));
410 desc->speed = htonl(netdev_get_speed(p->netdev));
414 dp_send_hello(struct datapath *dp)
416 struct buffer *buffer;
417 struct ofp_data_hello *odh;
420 buffer = buffer_new(sizeof *odh);
421 odh = buffer_put_uninit(buffer, sizeof *odh);
422 memset(odh, 0, sizeof *odh);
423 odh->header.version = OFP_VERSION;
424 odh->header.type = OFPT_DATA_HELLO;
425 odh->header.xid = htonl(0);
426 odh->datapath_id = htonll(dp->id);
427 odh->n_exact = htonl(2 * TABLE_HASH_MAX_FLOWS);
428 odh->n_mac_only = htonl(TABLE_MAC_MAX_FLOWS);
429 odh->n_compression = 0; /* Not supported */
430 odh->n_general = htonl(TABLE_LINEAR_MAX_FLOWS);
431 odh->buffer_mb = htonl(UINT32_MAX);
432 odh->n_buffers = htonl(N_PKT_BUFFERS);
433 odh->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
434 odh->actions = htonl(OFP_SUPPORTED_ACTIONS);
435 odh->miss_send_len = htons(dp->miss_send_len);
436 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
437 struct ofp_phy_port *opp = buffer_put_uninit(buffer, sizeof *opp);
438 memset(opp, 0, sizeof *opp);
439 fill_port_desc(dp, p, opp);
441 odh = buffer_at_assert(buffer, 0, sizeof *odh);
442 odh->header.length = htons(buffer->size);
443 controller_send(dp->cc, buffer);
447 dp_update_port_flags(struct datapath *dp, const struct ofp_phy_port *opp)
451 p = &dp->ports[htons(opp->port_no)];
453 /* Make sure the port id hasn't changed since this was sent */
454 if (!p || memcmp(opp->hw_addr, netdev_get_etheraddr(p->netdev),
458 p->flags = htonl(opp->flags);
462 send_port_status(struct sw_port *p, uint8_t status)
464 struct buffer *buffer;
465 struct ofp_port_status *ops;
466 buffer = buffer_new(sizeof *ops);
467 ops = buffer_put_uninit(buffer, sizeof *ops);
468 ops->header.version = OFP_VERSION;
469 ops->header.type = OFPT_PORT_STATUS;
470 ops->header.length = htons(sizeof(*ops));
471 ops->header.xid = htonl(0);
472 ops->reason = status;
473 fill_port_desc(p->dp, p, &ops->desc);
474 controller_send(p->dp->cc, buffer);
478 send_flow_expired(struct datapath *dp, struct sw_flow *flow)
480 struct buffer *buffer;
481 struct ofp_flow_expired *ofe;
482 buffer = buffer_new(sizeof *ofe);
483 ofe = buffer_put_uninit(buffer, sizeof *ofe);
484 ofe->header.version = OFP_VERSION;
485 ofe->header.type = OFPT_FLOW_EXPIRED;
486 ofe->header.length = htons(sizeof(*ofe));
487 ofe->header.xid = htonl(0);
488 flow_fill_match(&ofe->match, &flow->key);
489 ofe->duration = htonl(flow->timeout - flow->max_idle - flow->created);
490 ofe->packet_count = htonll(flow->packet_count);
491 ofe->byte_count = htonll(flow->byte_count);
492 controller_send(dp->cc, buffer);
495 /* 'buffer' was received on 'in_port', a physical switch port between 0 and
496 * OFPP_MAX. Process it according to 'chain'. */
497 void fwd_port_input(struct datapath *dp, struct buffer *buffer, int in_port)
499 struct sw_flow_key key;
500 struct sw_flow *flow;
503 flow_extract(buffer, in_port, &key.flow);
504 flow = chain_lookup(dp->chain, &key);
506 flow_used(flow, buffer);
507 execute_actions(dp, buffer, in_port, &key,
508 flow->actions, flow->n_actions);
510 dp_output_control(dp, buffer, in_port, dp->miss_send_len,
516 do_output(struct datapath *dp, struct buffer *buffer, int in_port,
517 size_t max_len, int out_port)
519 if (out_port != OFPP_CONTROLLER) {
520 dp_output_port(dp, buffer, in_port, out_port);
522 dp_output_control(dp, buffer, in_port, max_len, OFPR_ACTION);
527 execute_actions(struct datapath *dp, struct buffer *buffer,
528 int in_port, const struct sw_flow_key *key,
529 const struct ofp_action *actions, int n_actions)
531 /* Every output action needs a separate clone of 'buffer', but the common
532 * case is just a single output action, so that doing a clone and then
533 * freeing the original buffer is wasteful. So the following code is
534 * slightly obscure just to avoid that. */
536 size_t max_len=0; /* Initialze to make compiler happy */
541 eth_proto = ntohs(key->flow.dl_type);
543 for (i = 0; i < n_actions; i++) {
544 const struct ofp_action *a = &actions[i];
545 struct eth_header *eh = buffer->l2;
547 if (prev_port != -1) {
548 do_output(dp, buffer_clone(buffer), in_port, max_len, prev_port);
552 switch (ntohs(a->type)) {
554 prev_port = ntohs(a->arg.output.port);
555 max_len = ntohs(a->arg.output.max_len);
558 case OFPAT_SET_DL_VLAN:
559 modify_vlan(buffer, key, a);
562 case OFPAT_SET_DL_SRC:
563 memcpy(eh->eth_src, a->arg.dl_addr, sizeof eh->eth_src);
566 case OFPAT_SET_DL_DST:
567 memcpy(eh->eth_dst, a->arg.dl_addr, sizeof eh->eth_dst);
570 case OFPAT_SET_NW_SRC:
571 case OFPAT_SET_NW_DST:
572 modify_nh(buffer, eth_proto, key->flow.nw_proto, a);
575 case OFPAT_SET_TP_SRC:
576 case OFPAT_SET_TP_DST:
577 modify_th(buffer, eth_proto, key->flow.nw_proto, a);
585 do_output(dp, buffer, in_port, max_len, prev_port);
587 buffer_delete(buffer);
590 /* Returns the new checksum for a packet in which the checksum field previously
591 * contained 'old_csum' and in which a field that contained 'old_u16' was
592 * changed to contain 'new_u16'. */
594 recalc_csum16(uint16_t old_csum, uint16_t old_u16, uint16_t new_u16)
596 /* Ones-complement arithmetic is endian-independent, so this code does not
597 * use htons() or ntohs().
599 * See RFC 1624 for formula and explanation. */
600 uint16_t hc_complement = ~old_csum;
601 uint16_t m_complement = ~old_u16;
602 uint16_t m_prime = new_u16;
603 uint32_t sum = hc_complement + m_complement + m_prime;
604 uint16_t hc_prime_complement = sum + (sum >> 16);
605 return ~hc_prime_complement;
608 /* Returns the new checksum for a packet in which the checksum field previously
609 * contained 'old_csum' and in which a field that contained 'old_u32' was
610 * changed to contain 'new_u32'. */
612 recalc_csum32(uint16_t old_csum, uint32_t old_u32, uint32_t new_u32)
614 return recalc_csum16(recalc_csum16(old_csum, old_u32, new_u32),
615 old_u32 >> 16, new_u32 >> 16);
618 static void modify_nh(struct buffer *buffer, uint16_t eth_proto,
619 uint8_t nw_proto, const struct ofp_action *a)
621 if (eth_proto == ETH_TYPE_IP) {
622 struct ip_header *nh = buffer->l3;
623 uint32_t new, *field;
625 new = a->arg.nw_addr;
626 field = a->type == OFPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
627 if (nw_proto == IP_TYPE_TCP) {
628 struct tcp_header *th = buffer->l4;
629 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, new);
630 } else if (nw_proto == IP_TYPE_UDP) {
631 struct udp_header *th = buffer->l4;
633 th->udp_csum = recalc_csum32(th->udp_csum, *field, new);
635 th->udp_csum = 0xffff;
639 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, new);
644 static void modify_th(struct buffer *buffer, uint16_t eth_proto,
645 uint8_t nw_proto, const struct ofp_action *a)
647 if (eth_proto == ETH_TYPE_IP) {
648 uint16_t new, *field;
652 if (nw_proto == IP_TYPE_TCP) {
653 struct tcp_header *th = buffer->l4;
654 field = a->type == OFPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
655 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, new);
657 } else if (nw_proto == IP_TYPE_UDP) {
658 struct udp_header *th = buffer->l4;
659 field = a->type == OFPAT_SET_TP_SRC ? &th->udp_src : &th->udp_dst;
660 th->udp_csum = recalc_csum16(th->udp_csum, *field, new);
667 modify_vlan(struct buffer *buffer,
668 const struct sw_flow_key *key, const struct ofp_action *a)
670 uint16_t new_id = a->arg.vlan_id;
671 struct vlan_eth_header *veh;
673 if (new_id != OFP_VLAN_NONE) {
674 if (key->flow.dl_vlan != htons(OFP_VLAN_NONE)) {
675 /* Modify vlan id, but maintain other TCI values */
677 veh->veth_tci &= ~htons(VLAN_VID);
678 veh->veth_tci |= htons(new_id);
680 /* Insert new vlan id. */
681 struct eth_header *eh = buffer->l2;
682 struct vlan_eth_header tmp;
683 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
684 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
685 tmp.veth_type = htons(ETH_TYPE_VLAN);
686 tmp.veth_tci = new_id;
687 tmp.veth_next_type = eh->eth_type;
689 veh = buffer_push_uninit(buffer, VLAN_HEADER_LEN);
690 memcpy(veh, &tmp, sizeof tmp);
691 buffer->l2 -= VLAN_HEADER_LEN;
694 /* Remove an existing vlan header if it exists */
696 if (veh->veth_type == htons(ETH_TYPE_VLAN)) {
697 struct eth_header tmp;
699 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
700 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
701 tmp.eth_type = veh->veth_next_type;
703 buffer->size -= VLAN_HEADER_LEN;
704 buffer->data += VLAN_HEADER_LEN;
705 buffer->l2 += VLAN_HEADER_LEN;
706 memcpy(buffer->data, &tmp, sizeof tmp);
712 recv_control_hello(struct datapath *dp, const void *msg)
714 const struct ofp_control_hello *och = msg;
716 printf("control_hello(version=%d)\n", ntohl(och->version));
718 if (ntohs(och->miss_send_len) != OFP_MISS_SEND_LEN_UNCHANGED) {
719 dp->miss_send_len = ntohs(och->miss_send_len);
722 dp->hello_flags = ntohs(och->flags);
730 recv_packet_out(struct datapath *dp, const void *msg)
732 const struct ofp_packet_out *opo = msg;
734 if (ntohl(opo->buffer_id) == (uint32_t) -1) {
735 /* FIXME: can we avoid copying data here? */
736 int data_len = ntohs(opo->header.length) - sizeof *opo;
737 struct buffer *buffer = buffer_new(data_len);
738 buffer_put(buffer, opo->u.data, data_len);
739 dp_output_port(dp, buffer,
740 ntohs(opo->in_port), ntohs(opo->out_port));
742 struct sw_flow_key key;
743 struct buffer *buffer;
746 buffer = retrieve_buffer(ntohl(opo->buffer_id));
751 n_acts = (ntohs(opo->header.length) - sizeof *opo)
752 / sizeof *opo->u.actions;
753 flow_extract(buffer, ntohs(opo->in_port), &key.flow);
754 execute_actions(dp, buffer, ntohs(opo->in_port),
755 &key, opo->u.actions, n_acts);
761 recv_port_mod(struct datapath *dp, const void *msg)
763 const struct ofp_port_mod *opm = msg;
765 dp_update_port_flags(dp, &opm->desc);
771 add_flow(struct datapath *dp, const struct ofp_flow_mod *ofm)
775 struct sw_flow *flow;
778 /* Check number of actions. */
779 n_acts = (ntohs(ofm->header.length) - sizeof *ofm) / sizeof *ofm->actions;
780 if (n_acts > MAX_ACTIONS) {
785 /* Allocate memory. */
786 flow = flow_alloc(n_acts);
791 flow_extract_match(&flow->key, &ofm->match);
792 flow->group_id = ntohl(ofm->group_id);
793 flow->max_idle = ntohs(ofm->max_idle);
794 flow->timeout = time(0) + flow->max_idle; /* FIXME */
795 flow->n_actions = n_acts;
796 flow->created = time(0); /* FIXME */
797 flow->byte_count = 0;
798 flow->packet_count = 0;
799 memcpy(flow->actions, ofm->actions, n_acts * sizeof *flow->actions);
802 error = chain_insert(dp->chain, flow);
804 goto error_free_flow;
807 if (ntohl(ofm->buffer_id) != UINT32_MAX) {
808 struct buffer *buffer = retrieve_buffer(ntohl(ofm->buffer_id));
810 struct sw_flow_key key;
811 uint16_t in_port = ntohs(ofm->match.in_port);
812 flow_used(flow, buffer);
813 flow_extract(buffer, in_port, &key.flow);
814 execute_actions(dp, buffer, in_port, &key, ofm->actions, n_acts);
824 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
825 discard_buffer(ntohl(ofm->buffer_id));
830 recv_flow(struct datapath *dp, const void *msg)
832 const struct ofp_flow_mod *ofm = msg;
833 uint16_t command = ntohs(ofm->command);
835 if (command == OFPFC_ADD) {
836 return add_flow(dp, ofm);
837 } else if (command == OFPFC_DELETE) {
838 struct sw_flow_key key;
839 flow_extract_match(&key, &ofm->match);
840 return chain_delete(dp->chain, &key, 0) ? 0 : -ESRCH;
841 } else if (command == OFPFC_DELETE_STRICT) {
842 struct sw_flow_key key;
843 flow_extract_match(&key, &ofm->match);
844 return chain_delete(dp->chain, &key, 1) ? 0 : -ESRCH;
850 /* 'msg', which is 'length' bytes long, was received from the control path.
851 * Apply it to 'chain'. */
853 fwd_control_input(struct datapath *dp, const void *msg, size_t length)
856 struct openflow_packet {
858 int (*handler)(struct datapath *, const void *);
861 static const struct openflow_packet packets[] = {
862 [OFPT_CONTROL_HELLO] = {
863 sizeof (struct ofp_control_hello),
866 [OFPT_PACKET_OUT] = {
867 sizeof (struct ofp_packet_out),
871 sizeof (struct ofp_flow_mod),
875 sizeof (struct ofp_port_mod),
880 const struct openflow_packet *pkt;
881 struct ofp_header *oh;
883 if (length < sizeof(struct ofp_header))
886 oh = (struct ofp_header *) msg;
887 if (oh->version != 1 || oh->type >= ARRAY_SIZE(packets)
888 || ntohs(oh->length) > length)
891 pkt = &packets[oh->type];
894 if (length < pkt->min_size)
897 return pkt->handler(dp, msg);
900 /* Packet buffering. */
902 #define OVERWRITE_SECS 1
904 struct packet_buffer {
905 struct buffer *buffer;
910 static struct packet_buffer buffers[N_PKT_BUFFERS];
911 static unsigned int buffer_idx;
913 uint32_t save_buffer(struct buffer *buffer)
915 struct packet_buffer *p;
918 buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
919 p = &buffers[buffer_idx];
921 /* Don't buffer packet if existing entry is less than
922 * OVERWRITE_SECS old. */
923 if (time(0) < p->timeout) { /* FIXME */
926 buffer_delete(p->buffer);
929 /* Don't use maximum cookie value since the all-bits-1 id is
931 if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
933 p->buffer = buffer_clone(buffer); /* FIXME */
934 p->timeout = time(0) + OVERWRITE_SECS; /* FIXME */
935 id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
940 static struct buffer *retrieve_buffer(uint32_t id)
942 struct buffer *buffer = NULL;
943 struct packet_buffer *p;
945 p = &buffers[id & PKT_BUFFER_MASK];
946 if (p->cookie == id >> PKT_BUFFER_BITS) {
950 printf("cookie mismatch: %x != %x\n",
951 id >> PKT_BUFFER_BITS, p->cookie);
957 static void discard_buffer(uint32_t id)
959 struct packet_buffer *p;
961 p = &buffers[id & PKT_BUFFER_MASK];
962 if (p->cookie == id >> PKT_BUFFER_BITS) {
963 buffer_delete(p->buffer);