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>
47 #include "poll-loop.h"
54 #define THIS_MODULE VLM_datapath
58 BRPF_NO_FLOOD = 1 << 0,
62 BRPS_PORT_DOWN = 1 << 0,
63 BRPS_LINK_DOWN = 1 << 1,
70 /* Capabilities supported by this implementation. */
71 #define OFP_SUPPORTED_CAPABILITIES ( OFPC_FLOW_STATS \
76 /* Actions supported by this implementation. */
77 #define OFP_SUPPORTED_ACTIONS ( (1 << OFPAT_OUTPUT) \
78 | (1 << OFPAT_SET_DL_VLAN) \
79 | (1 << OFPAT_SET_DL_SRC) \
80 | (1 << OFPAT_SET_DL_DST) \
81 | (1 << OFPAT_SET_NW_SRC) \
82 | (1 << OFPAT_SET_NW_DST) \
83 | (1 << OFPAT_SET_TP_SRC) \
84 | (1 << OFPAT_SET_TP_DST) )
87 uint32_t flags; /* BRPF_* flags */
88 uint32_t status; /* BRPS_* flags */
90 struct netdev *netdev;
91 struct list node; /* Element in datapath.ports. */
92 unsigned long long int rx_packets, tx_packets;
93 unsigned long long int rx_bytes, tx_bytes;
94 unsigned long long int tx_dropped;
97 /* The origin of a received OpenFlow message, to enable sending a reply. */
99 struct remote *remote; /* The device that sent the message. */
100 uint32_t xid; /* The OpenFlow transaction ID. */
103 /* A connection to a controller or a management device. */
107 #define TXQ_LIMIT 128 /* Max number of packets to queue for tx. */
108 int n_txq; /* Number of packets queued for tx on rconn. */
110 /* Support for reliable, multi-message replies to requests.
112 * If an incoming request needs to have a reliable reply that might
113 * require multiple messages, it can use remote_start_dump() to set up
114 * a callback that will be called as buffer space for replies. */
115 int (*cb_dump)(struct datapath *, void *aux);
116 void (*cb_done)(void *aux);
121 /* Remote connections. */
122 struct remote *controller; /* Connection to controller. */
123 struct list remotes; /* All connections (including controller). */
124 struct vconn *listen_vconn;
128 /* Unique identifier for this datapath */
131 struct sw_chain *chain; /* Forwarding rules. */
133 /* Configuration set from controller. */
135 uint16_t miss_send_len;
138 struct sw_port ports[OFPP_MAX];
139 struct list port_list; /* List of ports, for flooding. */
142 static struct remote *remote_create(struct datapath *, struct rconn *);
143 static void remote_run(struct datapath *, struct remote *);
144 static void remote_wait(struct remote *);
145 static void remote_destroy(struct remote *);
147 void dp_output_port(struct datapath *, struct buffer *,
148 int in_port, int out_port);
149 void dp_update_port_flags(struct datapath *dp, const struct ofp_port_mod *opm);
150 void dp_output_control(struct datapath *, struct buffer *, int in_port,
151 size_t max_len, int reason);
152 static void send_flow_expired(struct datapath *, struct sw_flow *,
153 enum ofp_flow_expired_reason);
154 static int update_port_status(struct sw_port *p);
155 static void send_port_status(struct sw_port *p, uint8_t status);
156 static void del_switch_port(struct sw_port *p);
157 static void execute_actions(struct datapath *, struct buffer *,
158 int in_port, const struct sw_flow_key *,
159 const struct ofp_action *, int n_actions);
160 static void modify_vlan(struct buffer *buffer, const struct sw_flow_key *key,
161 const struct ofp_action *a);
162 static void modify_nh(struct buffer *buffer, uint16_t eth_proto,
163 uint8_t nw_proto, const struct ofp_action *a);
164 static void modify_th(struct buffer *buffer, uint16_t eth_proto,
165 uint8_t nw_proto, const struct ofp_action *a);
167 /* Buffers are identified to userspace by a 31-bit opaque ID. We divide the ID
168 * into a buffer number (low bits) and a cookie (high bits). The buffer number
169 * is an index into an array of buffers. The cookie distinguishes between
170 * different packets that have occupied a single buffer. Thus, the more
171 * buffers we have, the lower-quality the cookie... */
172 #define PKT_BUFFER_BITS 8
173 #define N_PKT_BUFFERS (1 << PKT_BUFFER_BITS)
174 #define PKT_BUFFER_MASK (N_PKT_BUFFERS - 1)
176 #define PKT_COOKIE_BITS (32 - PKT_BUFFER_BITS)
178 int run_flow_through_tables(struct datapath *, struct buffer *, int in_port);
179 void fwd_port_input(struct datapath *, struct buffer *, int in_port);
180 int fwd_control_input(struct datapath *, const struct sender *,
181 const void *, size_t);
183 uint32_t save_buffer(struct buffer *);
184 static struct buffer *retrieve_buffer(uint32_t id);
185 static void discard_buffer(uint32_t id);
187 static int port_no(struct datapath *dp, struct sw_port *p)
189 assert(p >= dp->ports && p < &dp->ports[ARRAY_SIZE(dp->ports)]);
190 return p - dp->ports;
193 /* Generates and returns a random datapath id. */
195 gen_datapath_id(void)
197 uint8_t ea[ETH_ADDR_LEN];
199 return eth_addr_to_uint64(ea);
203 dp_new(struct datapath **dp_, uint64_t dpid, struct rconn *rconn)
207 dp = calloc(1, sizeof *dp);
212 dp->last_timeout = time_now();
213 list_init(&dp->remotes);
214 dp->controller = remote_create(dp, rconn);
215 dp->listen_vconn = NULL;
216 dp->id = dpid <= UINT64_C(0xffffffffffff) ? dpid : gen_datapath_id();
217 dp->chain = chain_create();
219 VLOG_ERR("could not create chain");
224 list_init(&dp->port_list);
226 dp->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
232 dp_add_port(struct datapath *dp, const char *name)
234 struct netdev *netdev;
240 error = netdev_open(name, NETDEV_ETH_TYPE_ANY, &netdev);
244 error = netdev_set_flags(netdev, NETDEV_UP | NETDEV_PROMISC, false);
246 VLOG_ERR("couldn't set promiscuous mode on %s device", name);
247 netdev_close(netdev);
250 if (netdev_get_in4(netdev, &in4)) {
251 VLOG_ERR("%s device has assigned IP address %s", name, inet_ntoa(in4));
253 if (netdev_get_in6(netdev, &in6)) {
254 char in6_name[INET6_ADDRSTRLEN + 1];
255 inet_ntop(AF_INET6, &in6, in6_name, sizeof in6_name);
256 VLOG_ERR("%s device has assigned IPv6 address %s", name, in6_name);
259 for (p = dp->ports; ; p++) {
260 if (p >= &dp->ports[ARRAY_SIZE(dp->ports)]) {
262 } else if (!p->netdev) {
267 memset(p, '\0', sizeof *p);
271 list_push_back(&dp->port_list, &p->node);
273 /* Notify the ctlpath that this port has been added */
274 send_port_status(p, OFPPR_ADD);
280 dp_add_listen_vconn(struct datapath *dp, struct vconn *listen_vconn)
282 assert(!dp->listen_vconn);
283 dp->listen_vconn = listen_vconn;
287 dp_run(struct datapath *dp)
289 time_t now = time_now();
290 struct sw_port *p, *pn;
291 struct remote *r, *rn;
292 struct buffer *buffer = NULL;
294 if (now != dp->last_timeout) {
295 struct list deleted = LIST_INITIALIZER(&deleted);
296 struct sw_flow *f, *n;
298 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
299 if (update_port_status(p)) {
300 send_port_status(p, OFPPR_MOD);
304 chain_timeout(dp->chain, &deleted);
305 LIST_FOR_EACH_SAFE (f, n, struct sw_flow, node, &deleted) {
306 send_flow_expired(dp, f, f->reason);
307 list_remove(&f->node);
310 dp->last_timeout = now;
312 poll_timer_wait(1000);
314 LIST_FOR_EACH_SAFE (p, pn, struct sw_port, node, &dp->port_list) {
318 /* Allocate buffer with some headroom to add headers in forwarding
319 * to the controller or adding a vlan tag, plus an extra 2 bytes to
320 * allow IP headers to be aligned on a 4-byte boundary. */
321 const int headroom = 128 + 2;
322 const int hard_header = VLAN_ETH_HEADER_LEN;
323 const int mtu = netdev_get_mtu(p->netdev);
324 buffer = buffer_new(headroom + hard_header + mtu);
325 buffer->data += headroom;
327 error = netdev_recv(p->netdev, buffer);
330 p->rx_bytes += buffer->size;
331 fwd_port_input(dp, buffer, port_no(dp, p));
333 } else if (error != EAGAIN) {
334 VLOG_ERR("error receiving data from %s: %s",
335 netdev_get_name(p->netdev), strerror(error));
338 buffer_delete(buffer);
340 /* Talk to remotes. */
341 LIST_FOR_EACH_SAFE (r, rn, struct remote, node, &dp->remotes) {
344 if (dp->listen_vconn) {
346 struct vconn *new_vconn;
349 retval = vconn_accept(dp->listen_vconn, &new_vconn);
351 if (retval != EAGAIN) {
352 VLOG_WARN("accept failed (%s)", strerror(retval));
356 remote_create(dp, rconn_new_from_vconn("passive", new_vconn));
362 remote_run(struct datapath *dp, struct remote *r)
368 /* Do some remote processing, but cap it at a reasonable amount so that
369 * other processing doesn't starve. */
370 for (i = 0; i < 50; i++) {
372 struct buffer *buffer;
373 struct ofp_header *oh;
375 buffer = rconn_recv(r->rconn);
380 if (buffer->size >= sizeof *oh) {
381 struct sender sender;
385 sender.xid = oh->xid;
386 fwd_control_input(dp, &sender, buffer->data, buffer->size);
388 VLOG_WARN("received too-short OpenFlow message");
390 buffer_delete(buffer);
392 if (r->n_txq < TXQ_LIMIT) {
393 int error = r->cb_dump(dp, r->cb_aux);
396 VLOG_WARN("dump callback error: %s", strerror(-error));
398 r->cb_done(r->cb_aux);
407 if (!rconn_is_alive(r->rconn)) {
413 remote_wait(struct remote *r)
415 rconn_run_wait(r->rconn);
416 rconn_recv_wait(r->rconn);
420 remote_destroy(struct remote *r)
423 if (r->cb_dump && r->cb_done) {
424 r->cb_done(r->cb_aux);
426 list_remove(&r->node);
427 rconn_destroy(r->rconn);
432 static struct remote *
433 remote_create(struct datapath *dp, struct rconn *rconn)
435 struct remote *remote = xmalloc(sizeof *remote);
436 list_push_back(&dp->remotes, &remote->node);
437 remote->rconn = rconn;
438 remote->cb_dump = NULL;
442 /* Starts a callback-based, reliable, possibly multi-message reply to a
443 * request made by 'remote'.
445 * 'dump' designates a function that will be called when the 'remote' send
446 * queue has an empty slot. It should compose a message and send it on
447 * 'remote'. On success, it should return 1 if it should be called again when
448 * another send queue slot opens up, 0 if its transmissions are complete, or a
449 * negative errno value on failure.
451 * 'done' designates a function to clean up any resources allocated for the
452 * dump. It must handle being called before the dump is complete (which will
453 * happen if 'remote' is closed unexpectedly).
455 * 'aux' is passed to 'dump' and 'done'. */
457 remote_start_dump(struct remote *remote,
458 int (*dump)(struct datapath *, void *),
459 void (*done)(void *),
462 assert(!remote->cb_dump);
463 remote->cb_dump = dump;
464 remote->cb_done = done;
465 remote->cb_aux = aux;
469 dp_wait(struct datapath *dp)
474 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
475 netdev_recv_wait(p->netdev);
477 LIST_FOR_EACH (r, struct remote, node, &dp->remotes) {
480 if (dp->listen_vconn) {
481 vconn_accept_wait(dp->listen_vconn);
485 /* Delete 'p' from switch. */
487 del_switch_port(struct sw_port *p)
489 send_port_status(p, OFPPR_DELETE);
490 netdev_close(p->netdev);
492 list_remove(&p->node);
496 dp_destroy(struct datapath *dp)
498 struct sw_port *p, *n;
504 LIST_FOR_EACH_SAFE (p, n, struct sw_port, node, &dp->port_list) {
507 chain_destroy(dp->chain);
511 /* Send packets out all the ports except the originating one. If the
512 * "flood" argument is set, don't send out ports with flooding disabled.
515 output_all(struct datapath *dp, struct buffer *buffer, int in_port, int flood)
521 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
522 if (port_no(dp, p) == in_port) {
525 if (flood && p->flags & BRPF_NO_FLOOD) {
528 if (prev_port != -1) {
529 dp_output_port(dp, buffer_clone(buffer), in_port, prev_port);
531 prev_port = port_no(dp, p);
534 dp_output_port(dp, buffer, in_port, prev_port);
536 buffer_delete(buffer);
542 output_packet(struct datapath *dp, struct buffer *buffer, int out_port)
544 if (out_port >= 0 && out_port < OFPP_MAX) {
545 struct sw_port *p = &dp->ports[out_port];
546 if (p->netdev != NULL && !(p->status & BRPS_PORT_DOWN)) {
547 if (!netdev_send(p->netdev, buffer)) {
549 p->tx_bytes += buffer->size;
557 buffer_delete(buffer);
558 /* FIXME: ratelimit */
559 VLOG_DBG("can't forward to bad port %d\n", out_port);
562 /* Takes ownership of 'buffer' and transmits it to 'out_port' on 'dp'.
565 dp_output_port(struct datapath *dp, struct buffer *buffer,
566 int in_port, int out_port)
570 if (out_port == OFPP_FLOOD) {
571 output_all(dp, buffer, in_port, 1);
572 } else if (out_port == OFPP_ALL) {
573 output_all(dp, buffer, in_port, 0);
574 } else if (out_port == OFPP_CONTROLLER) {
575 dp_output_control(dp, buffer, in_port, 0, OFPR_ACTION);
576 } else if (out_port == OFPP_IN_PORT) {
577 output_packet(dp, buffer, in_port);
578 } else if (out_port == OFPP_TABLE) {
579 if (run_flow_through_tables(dp, buffer, in_port)) {
580 buffer_delete(buffer);
583 if (in_port == out_port) {
584 /* FIXME: ratelimit */
585 VLOG_DBG("can't directly forward to input port");
588 output_packet(dp, buffer, out_port);
593 make_openflow_reply(size_t openflow_len, uint8_t type,
594 const struct sender *sender, struct buffer **bufferp)
596 return make_openflow_xid(openflow_len, type, sender ? sender->xid : 0,
601 send_openflow_buffer(struct datapath *dp, struct buffer *buffer,
602 const struct sender *sender)
604 struct remote *remote = sender ? sender->remote : dp->controller;
605 struct rconn *rconn = remote->rconn;
608 update_openflow_length(buffer);
609 retval = (remote->n_txq < TXQ_LIMIT
610 ? rconn_send(rconn, buffer, &remote->n_txq)
613 VLOG_WARN("send to %s failed: %s",
614 rconn_get_name(rconn), strerror(retval));
615 buffer_delete(buffer);
620 /* Takes ownership of 'buffer' and transmits it to 'dp''s controller. If the
621 * packet can be saved in a buffer, then only the first max_len bytes of
622 * 'buffer' are sent; otherwise, all of 'buffer' is sent. 'reason' indicates
623 * why 'buffer' is being sent. 'max_len' sets the maximum number of bytes that
624 * the caller wants to be sent; a value of 0 indicates the entire packet should
627 dp_output_control(struct datapath *dp, struct buffer *buffer, int in_port,
628 size_t max_len, int reason)
630 struct ofp_packet_in *opi;
634 buffer_id = save_buffer(buffer);
635 total_len = buffer->size;
636 if (buffer_id != UINT32_MAX && buffer->size > max_len) {
637 buffer->size = max_len;
640 opi = buffer_push_uninit(buffer, offsetof(struct ofp_packet_in, data));
641 opi->header.version = OFP_VERSION;
642 opi->header.type = OFPT_PACKET_IN;
643 opi->header.length = htons(buffer->size);
644 opi->header.xid = htonl(0);
645 opi->buffer_id = htonl(buffer_id);
646 opi->total_len = htons(total_len);
647 opi->in_port = htons(in_port);
648 opi->reason = reason;
650 send_openflow_buffer(dp, buffer, NULL);
653 static void fill_port_desc(struct datapath *dp, struct sw_port *p,
654 struct ofp_phy_port *desc)
656 desc->port_no = htons(port_no(dp, p));
657 strncpy((char *) desc->name, netdev_get_name(p->netdev),
659 desc->name[sizeof desc->name - 1] = '\0';
660 memcpy(desc->hw_addr, netdev_get_etheraddr(p->netdev), ETH_ADDR_LEN);
662 desc->features = htonl(netdev_get_features(p->netdev));
663 desc->speed = htonl(netdev_get_speed(p->netdev));
665 if (p->flags & BRPF_NO_FLOOD) {
666 desc->flags |= htonl(OFPPFL_NO_FLOOD);
667 } else if (p->status & BRPS_PORT_DOWN) {
668 desc->flags |= htonl(OFPPFL_PORT_DOWN);
669 } else if (p->status & BRPS_LINK_DOWN) {
670 desc->flags |= htonl(OFPPFL_LINK_DOWN);
675 dp_send_features_reply(struct datapath *dp, const struct sender *sender)
677 struct buffer *buffer;
678 struct ofp_switch_features *ofr;
681 ofr = make_openflow_reply(sizeof *ofr, OFPT_FEATURES_REPLY,
683 ofr->datapath_id = htonll(dp->id);
684 ofr->n_exact = htonl(2 * TABLE_HASH_MAX_FLOWS);
685 ofr->n_compression = 0; /* Not supported */
686 ofr->n_general = htonl(TABLE_LINEAR_MAX_FLOWS);
687 ofr->buffer_mb = htonl(UINT32_MAX);
688 ofr->n_buffers = htonl(N_PKT_BUFFERS);
689 ofr->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
690 ofr->actions = htonl(OFP_SUPPORTED_ACTIONS);
691 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
692 struct ofp_phy_port *opp = buffer_put_uninit(buffer, sizeof *opp);
693 memset(opp, 0, sizeof *opp);
694 fill_port_desc(dp, p, opp);
696 send_openflow_buffer(dp, buffer, sender);
700 dp_update_port_flags(struct datapath *dp, const struct ofp_port_mod *opm)
702 const struct ofp_phy_port *opp = &opm->desc;
703 int port_no = ntohs(opp->port_no);
704 if (port_no < OFPP_MAX) {
705 struct sw_port *p = &dp->ports[port_no];
707 /* Make sure the port id hasn't changed since this was sent */
708 if (!p || memcmp(opp->hw_addr, netdev_get_etheraddr(p->netdev),
709 ETH_ADDR_LEN) != 0) {
714 if (opm->mask & htonl(OFPPFL_NO_FLOOD)) {
715 if (opp->flags & htonl(OFPPFL_NO_FLOOD))
716 p->flags |= BRPF_NO_FLOOD;
718 p->flags &= ~BRPF_NO_FLOOD;
721 if (opm->mask & htonl(OFPPFL_PORT_DOWN)) {
722 if ((opp->flags & htonl(OFPPFL_PORT_DOWN))
723 && (p->status & BRPS_PORT_DOWN) == 0) {
724 p->status |= BRPS_PORT_DOWN;
725 netdev_turn_flags_off(p->netdev, NETDEV_UP, true);
726 } else if ((opp->flags & htonl(OFPPFL_PORT_DOWN)) == 0
727 && (p->status & BRPS_PORT_DOWN)) {
728 p->status &= ~BRPS_PORT_DOWN;
729 netdev_turn_flags_on(p->netdev, NETDEV_UP, true);
735 /* Update the port status field of the bridge port. A non-zero return
736 * value indicates some field has changed.
738 * NB: Callers of this function may hold the RCU read lock, so any
739 * additional checks must not sleep.
742 update_port_status(struct sw_port *p)
745 enum netdev_flags flags;
746 uint32_t orig_status = p->status;
748 if (netdev_get_flags(p->netdev, &flags) < 0) {
749 VLOG_WARN("could not get netdev flags for %s",
750 netdev_get_name(p->netdev));
753 if (flags & NETDEV_UP) {
754 p->status &= ~BRPS_PORT_DOWN;
756 p->status |= BRPS_PORT_DOWN;
760 /* Not all cards support this getting link status, so don't warn on
762 retval = netdev_get_link_status(p->netdev);
764 p->status &= ~BRPS_LINK_DOWN;
765 } else if (retval == 0) {
766 p->status |= BRPS_LINK_DOWN;
769 return (orig_status != p->status);
773 send_port_status(struct sw_port *p, uint8_t status)
775 struct buffer *buffer;
776 struct ofp_port_status *ops;
777 ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &buffer);
778 ops->reason = status;
779 memset(ops->pad, 0, sizeof ops->pad);
780 fill_port_desc(p->dp, p, &ops->desc);
782 send_openflow_buffer(p->dp, buffer, NULL);
786 send_flow_expired(struct datapath *dp, struct sw_flow *flow,
787 enum ofp_flow_expired_reason reason)
789 struct buffer *buffer;
790 struct ofp_flow_expired *ofe;
791 ofe = make_openflow_xid(sizeof *ofe, OFPT_FLOW_EXPIRED, 0, &buffer);
792 flow_fill_match(&ofe->match, &flow->key);
794 ofe->priority = htons(flow->priority);
795 ofe->reason = reason;
796 memset(ofe->pad, 0, sizeof ofe->pad);
798 ofe->duration = htonl(time_now() - flow->created);
799 memset(ofe->pad2, 0, sizeof ofe->pad2);
800 ofe->packet_count = htonll(flow->packet_count);
801 ofe->byte_count = htonll(flow->byte_count);
802 send_openflow_buffer(dp, buffer, NULL);
806 dp_send_error_msg(struct datapath *dp, const struct sender *sender,
807 uint16_t type, uint16_t code, const uint8_t *data, size_t len)
809 struct buffer *buffer;
810 struct ofp_error_msg *oem;
811 oem = make_openflow_reply(sizeof(*oem)+len, OFPT_ERROR_MSG,
813 oem->type = htons(type);
814 oem->code = htons(code);
815 memcpy(oem->data, data, len);
816 send_openflow_buffer(dp, buffer, sender);
820 fill_flow_stats(struct buffer *buffer, struct sw_flow *flow,
821 int table_idx, time_t now)
823 struct ofp_flow_stats *ofs;
824 int length = sizeof *ofs + sizeof *ofs->actions * flow->n_actions;
825 ofs = buffer_put_uninit(buffer, length);
826 ofs->length = htons(length);
827 ofs->table_id = table_idx;
829 ofs->match.wildcards = htonl(flow->key.wildcards);
830 ofs->match.in_port = flow->key.flow.in_port;
831 memcpy(ofs->match.dl_src, flow->key.flow.dl_src, ETH_ADDR_LEN);
832 memcpy(ofs->match.dl_dst, flow->key.flow.dl_dst, ETH_ADDR_LEN);
833 ofs->match.dl_vlan = flow->key.flow.dl_vlan;
834 ofs->match.dl_type = flow->key.flow.dl_type;
835 ofs->match.nw_src = flow->key.flow.nw_src;
836 ofs->match.nw_dst = flow->key.flow.nw_dst;
837 ofs->match.nw_proto = flow->key.flow.nw_proto;
839 ofs->match.tp_src = flow->key.flow.tp_src;
840 ofs->match.tp_dst = flow->key.flow.tp_dst;
841 ofs->duration = htonl(now - flow->created);
842 ofs->priority = htons(flow->priority);
843 ofs->idle_timeout = htons(flow->idle_timeout);
844 ofs->hard_timeout = htons(flow->hard_timeout);
845 memset(ofs->pad2, 0, sizeof ofs->pad2);
846 ofs->packet_count = htonll(flow->packet_count);
847 ofs->byte_count = htonll(flow->byte_count);
848 memcpy(ofs->actions, flow->actions,
849 sizeof *ofs->actions * flow->n_actions);
853 /* 'buffer' was received on 'in_port', a physical switch port between 0 and
854 * OFPP_MAX. Process it according to 'dp''s flow table. Returns 0 if
855 * successful, in which case 'buffer' is destroyed, or -ESRCH if there is no
856 * matching flow, in which case 'buffer' still belongs to the caller. */
857 int run_flow_through_tables(struct datapath *dp, struct buffer *buffer,
860 struct sw_flow_key key;
861 struct sw_flow *flow;
864 if (flow_extract(buffer, in_port, &key.flow)
865 && (dp->flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP) {
867 buffer_delete(buffer);
871 flow = chain_lookup(dp->chain, &key);
873 flow_used(flow, buffer);
874 execute_actions(dp, buffer, in_port, &key,
875 flow->actions, flow->n_actions);
882 /* 'buffer' was received on 'in_port', a physical switch port between 0 and
883 * OFPP_MAX. Process it according to 'dp''s flow table, sending it up to the
884 * controller if no flow matches. Takes ownership of 'buffer'. */
885 void fwd_port_input(struct datapath *dp, struct buffer *buffer, int in_port)
887 if (run_flow_through_tables(dp, buffer, in_port)) {
888 dp_output_control(dp, buffer, in_port, dp->miss_send_len,
894 do_output(struct datapath *dp, struct buffer *buffer, int in_port,
895 size_t max_len, int out_port)
897 if (out_port != OFPP_CONTROLLER) {
898 dp_output_port(dp, buffer, in_port, out_port);
900 dp_output_control(dp, buffer, in_port, max_len, OFPR_ACTION);
905 execute_actions(struct datapath *dp, struct buffer *buffer,
906 int in_port, const struct sw_flow_key *key,
907 const struct ofp_action *actions, int n_actions)
909 /* Every output action needs a separate clone of 'buffer', but the common
910 * case is just a single output action, so that doing a clone and then
911 * freeing the original buffer is wasteful. So the following code is
912 * slightly obscure just to avoid that. */
914 size_t max_len=0; /* Initialze to make compiler happy */
919 eth_proto = ntohs(key->flow.dl_type);
921 for (i = 0; i < n_actions; i++) {
922 const struct ofp_action *a = &actions[i];
923 struct eth_header *eh = buffer->l2;
925 if (prev_port != -1) {
926 do_output(dp, buffer_clone(buffer), in_port, max_len, prev_port);
930 switch (ntohs(a->type)) {
932 prev_port = ntohs(a->arg.output.port);
933 max_len = ntohs(a->arg.output.max_len);
936 case OFPAT_SET_DL_VLAN:
937 modify_vlan(buffer, key, a);
940 case OFPAT_SET_DL_SRC:
941 memcpy(eh->eth_src, a->arg.dl_addr, sizeof eh->eth_src);
944 case OFPAT_SET_DL_DST:
945 memcpy(eh->eth_dst, a->arg.dl_addr, sizeof eh->eth_dst);
948 case OFPAT_SET_NW_SRC:
949 case OFPAT_SET_NW_DST:
950 modify_nh(buffer, eth_proto, key->flow.nw_proto, a);
953 case OFPAT_SET_TP_SRC:
954 case OFPAT_SET_TP_DST:
955 modify_th(buffer, eth_proto, key->flow.nw_proto, a);
963 do_output(dp, buffer, in_port, max_len, prev_port);
965 buffer_delete(buffer);
968 static void modify_nh(struct buffer *buffer, uint16_t eth_proto,
969 uint8_t nw_proto, const struct ofp_action *a)
971 if (eth_proto == ETH_TYPE_IP) {
972 struct ip_header *nh = buffer->l3;
973 uint32_t new, *field;
975 new = a->arg.nw_addr;
976 field = a->type == OFPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
977 if (nw_proto == IP_TYPE_TCP) {
978 struct tcp_header *th = buffer->l4;
979 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, new);
980 } else if (nw_proto == IP_TYPE_UDP) {
981 struct udp_header *th = buffer->l4;
983 th->udp_csum = recalc_csum32(th->udp_csum, *field, new);
985 th->udp_csum = 0xffff;
989 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, new);
994 static void modify_th(struct buffer *buffer, uint16_t eth_proto,
995 uint8_t nw_proto, const struct ofp_action *a)
997 if (eth_proto == ETH_TYPE_IP) {
998 uint16_t new, *field;
1002 if (nw_proto == IP_TYPE_TCP) {
1003 struct tcp_header *th = buffer->l4;
1004 field = a->type == OFPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1005 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, new);
1007 } else if (nw_proto == IP_TYPE_UDP) {
1008 struct udp_header *th = buffer->l4;
1009 field = a->type == OFPAT_SET_TP_SRC ? &th->udp_src : &th->udp_dst;
1010 th->udp_csum = recalc_csum16(th->udp_csum, *field, new);
1017 modify_vlan(struct buffer *buffer,
1018 const struct sw_flow_key *key, const struct ofp_action *a)
1020 uint16_t new_id = a->arg.vlan_id;
1021 struct vlan_eth_header *veh;
1023 if (new_id != htons(OFP_VLAN_NONE)) {
1024 if (key->flow.dl_vlan != htons(OFP_VLAN_NONE)) {
1025 /* Modify vlan id, but maintain other TCI values */
1027 veh->veth_tci &= ~htons(VLAN_VID);
1028 veh->veth_tci |= new_id;
1030 /* Insert new vlan id. */
1031 struct eth_header *eh = buffer->l2;
1032 struct vlan_eth_header tmp;
1033 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1034 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1035 tmp.veth_type = htons(ETH_TYPE_VLAN);
1036 tmp.veth_tci = new_id;
1037 tmp.veth_next_type = eh->eth_type;
1039 veh = buffer_push_uninit(buffer, VLAN_HEADER_LEN);
1040 memcpy(veh, &tmp, sizeof tmp);
1041 buffer->l2 -= VLAN_HEADER_LEN;
1044 /* Remove an existing vlan header if it exists */
1046 if (veh->veth_type == htons(ETH_TYPE_VLAN)) {
1047 struct eth_header tmp;
1049 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1050 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1051 tmp.eth_type = veh->veth_next_type;
1053 buffer->size -= VLAN_HEADER_LEN;
1054 buffer->data += VLAN_HEADER_LEN;
1055 buffer->l2 += VLAN_HEADER_LEN;
1056 memcpy(buffer->data, &tmp, sizeof tmp);
1062 recv_features_request(struct datapath *dp, const struct sender *sender,
1065 dp_send_features_reply(dp, sender);
1070 recv_get_config_request(struct datapath *dp, const struct sender *sender,
1073 struct buffer *buffer;
1074 struct ofp_switch_config *osc;
1076 osc = make_openflow_reply(sizeof *osc, OFPT_GET_CONFIG_REPLY,
1079 osc->flags = htons(dp->flags);
1080 osc->miss_send_len = htons(dp->miss_send_len);
1082 return send_openflow_buffer(dp, buffer, sender);
1086 recv_set_config(struct datapath *dp, const struct sender *sender UNUSED,
1089 const struct ofp_switch_config *osc = msg;
1092 flags = ntohs(osc->flags) & (OFPC_SEND_FLOW_EXP | OFPC_FRAG_MASK);
1093 if ((flags & OFPC_FRAG_MASK) != OFPC_FRAG_NORMAL
1094 && (flags & OFPC_FRAG_MASK) != OFPC_FRAG_DROP) {
1095 flags = (flags & ~OFPC_FRAG_MASK) | OFPC_FRAG_DROP;
1098 dp->miss_send_len = ntohs(osc->miss_send_len);
1103 recv_packet_out(struct datapath *dp, const struct sender *sender UNUSED,
1106 const struct ofp_packet_out *opo = msg;
1107 struct sw_flow_key key;
1108 struct buffer *buffer;
1109 int n_actions = ntohs(opo->n_actions);
1110 int act_len = n_actions * sizeof opo->actions[0];
1112 if (act_len > (ntohs(opo->header.length) - sizeof *opo)) {
1113 VLOG_DBG("message too short for number of actions");
1117 if (ntohl(opo->buffer_id) == (uint32_t) -1) {
1118 /* FIXME: can we avoid copying data here? */
1119 int data_len = ntohs(opo->header.length) - sizeof *opo - act_len;
1120 buffer = buffer_new(data_len);
1121 buffer_put(buffer, &opo->actions[n_actions], data_len);
1123 buffer = retrieve_buffer(ntohl(opo->buffer_id));
1129 flow_extract(buffer, ntohs(opo->in_port), &key.flow);
1130 execute_actions(dp, buffer, ntohs(opo->in_port),
1131 &key, opo->actions, n_actions);
1137 recv_port_mod(struct datapath *dp, const struct sender *sender UNUSED,
1140 const struct ofp_port_mod *opm = msg;
1142 dp_update_port_flags(dp, opm);
1148 add_flow(struct datapath *dp, const struct ofp_flow_mod *ofm)
1150 int error = -ENOMEM;
1153 struct sw_flow *flow;
1156 /* To prevent loops, make sure there's no action to send to the
1157 * OFP_TABLE virtual port.
1159 n_actions = (ntohs(ofm->header.length) - sizeof *ofm)
1160 / sizeof *ofm->actions;
1161 for (i=0; i<n_actions; i++) {
1162 const struct ofp_action *a = &ofm->actions[i];
1164 if (a->type == htons(OFPAT_OUTPUT)
1165 && (a->arg.output.port == htons(OFPP_TABLE)
1166 || a->arg.output.port == htons(OFPP_NONE)
1167 || a->arg.output.port == ofm->match.in_port)) {
1168 /* xxx Send fancy new error message? */
1173 /* Allocate memory. */
1174 flow = flow_alloc(n_actions);
1178 /* Fill out flow. */
1179 flow_extract_match(&flow->key, &ofm->match);
1180 flow->priority = flow->key.wildcards ? ntohs(ofm->priority) : -1;
1181 flow->idle_timeout = ntohs(ofm->idle_timeout);
1182 flow->hard_timeout = ntohs(ofm->hard_timeout);
1183 flow->used = flow->created = time_now();
1184 flow->n_actions = n_actions;
1185 flow->byte_count = 0;
1186 flow->packet_count = 0;
1187 memcpy(flow->actions, ofm->actions, n_actions * sizeof *flow->actions);
1190 error = chain_insert(dp->chain, flow);
1192 goto error_free_flow;
1195 if (ntohl(ofm->buffer_id) != UINT32_MAX) {
1196 struct buffer *buffer = retrieve_buffer(ntohl(ofm->buffer_id));
1198 struct sw_flow_key key;
1199 uint16_t in_port = ntohs(ofm->match.in_port);
1200 flow_used(flow, buffer);
1201 flow_extract(buffer, in_port, &key.flow);
1202 execute_actions(dp, buffer, in_port, &key, ofm->actions, n_actions);
1212 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
1213 discard_buffer(ntohl(ofm->buffer_id));
1218 recv_flow(struct datapath *dp, const struct sender *sender UNUSED,
1221 const struct ofp_flow_mod *ofm = msg;
1222 uint16_t command = ntohs(ofm->command);
1224 if (command == OFPFC_ADD) {
1225 return add_flow(dp, ofm);
1226 } else if (command == OFPFC_DELETE) {
1227 struct sw_flow_key key;
1228 flow_extract_match(&key, &ofm->match);
1229 return chain_delete(dp->chain, &key, 0, 0) ? 0 : -ESRCH;
1230 } else if (command == OFPFC_DELETE_STRICT) {
1231 struct sw_flow_key key;
1233 flow_extract_match(&key, &ofm->match);
1234 priority = key.wildcards ? ntohs(ofm->priority) : -1;
1235 return chain_delete(dp->chain, &key, priority, 1) ? 0 : -ESRCH;
1241 static int version_stats_dump(struct datapath *dp, void *state,
1242 struct buffer *buffer)
1244 struct ofp_version_stats *ovs = buffer_put_uninit(buffer, sizeof *ovs);
1246 strncpy(ovs->mfr_desc, &mfr_desc, sizeof ovs->mfr_desc);
1247 strncpy(ovs->hw_desc, &hw_desc, sizeof ovs->hw_desc);
1248 strncpy(ovs->sw_desc, &sw_desc, sizeof ovs->sw_desc);
1253 struct flow_stats_state {
1255 struct sw_table_position position;
1256 struct ofp_flow_stats_request rq;
1259 struct buffer *buffer;
1262 #define MAX_FLOW_STATS_BYTES 4096
1264 static int flow_stats_init(struct datapath *dp, const void *body, int body_len,
1267 const struct ofp_flow_stats_request *fsr = body;
1268 struct flow_stats_state *s = xmalloc(sizeof *s);
1269 s->table_idx = fsr->table_id == 0xff ? 0 : fsr->table_id;
1270 memset(&s->position, 0, sizeof s->position);
1276 static int flow_stats_dump_callback(struct sw_flow *flow, void *private)
1278 struct flow_stats_state *s = private;
1279 fill_flow_stats(s->buffer, flow, s->table_idx, s->now);
1280 return s->buffer->size >= MAX_FLOW_STATS_BYTES;
1283 static int flow_stats_dump(struct datapath *dp, void *state,
1284 struct buffer *buffer)
1286 struct flow_stats_state *s = state;
1287 struct sw_flow_key match_key;
1289 flow_extract_match(&match_key, &s->rq.match);
1291 s->now = time_now();
1292 while (s->table_idx < dp->chain->n_tables
1293 && (s->rq.table_id == 0xff || s->rq.table_id == s->table_idx))
1295 struct sw_table *table = dp->chain->tables[s->table_idx];
1297 if (table->iterate(table, &match_key, &s->position,
1298 flow_stats_dump_callback, s))
1302 memset(&s->position, 0, sizeof s->position);
1304 return s->buffer->size >= MAX_FLOW_STATS_BYTES;
1307 static void flow_stats_done(void *state)
1312 struct aggregate_stats_state {
1313 struct ofp_aggregate_stats_request rq;
1316 static int aggregate_stats_init(struct datapath *dp,
1317 const void *body, int body_len,
1320 const struct ofp_aggregate_stats_request *rq = body;
1321 struct aggregate_stats_state *s = xmalloc(sizeof *s);
1327 static int aggregate_stats_dump_callback(struct sw_flow *flow, void *private)
1329 struct ofp_aggregate_stats_reply *rpy = private;
1330 rpy->packet_count += flow->packet_count;
1331 rpy->byte_count += flow->byte_count;
1336 static int aggregate_stats_dump(struct datapath *dp, void *state,
1337 struct buffer *buffer)
1339 struct aggregate_stats_state *s = state;
1340 struct ofp_aggregate_stats_request *rq = &s->rq;
1341 struct ofp_aggregate_stats_reply *rpy;
1342 struct sw_table_position position;
1343 struct sw_flow_key match_key;
1346 rpy = buffer_put_uninit(buffer, sizeof *rpy);
1347 memset(rpy, 0, sizeof *rpy);
1349 flow_extract_match(&match_key, &rq->match);
1350 table_idx = rq->table_id == 0xff ? 0 : rq->table_id;
1351 memset(&position, 0, sizeof position);
1352 while (table_idx < dp->chain->n_tables
1353 && (rq->table_id == 0xff || rq->table_id == table_idx))
1355 struct sw_table *table = dp->chain->tables[table_idx];
1358 error = table->iterate(table, &match_key, &position,
1359 aggregate_stats_dump_callback, rpy);
1364 memset(&position, 0, sizeof position);
1367 rpy->packet_count = htonll(rpy->packet_count);
1368 rpy->byte_count = htonll(rpy->byte_count);
1369 rpy->flow_count = htonl(rpy->flow_count);
1373 static void aggregate_stats_done(void *state)
1378 static int table_stats_dump(struct datapath *dp, void *state,
1379 struct buffer *buffer)
1382 for (i = 0; i < dp->chain->n_tables; i++) {
1383 struct ofp_table_stats *ots = buffer_put_uninit(buffer, sizeof *ots);
1384 struct sw_table_stats stats;
1385 dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
1386 strncpy(ots->name, stats.name, sizeof ots->name);
1388 memset(ots->pad, 0, sizeof ots->pad);
1389 ots->max_entries = htonl(stats.max_flows);
1390 ots->active_count = htonl(stats.n_flows);
1391 ots->matched_count = htonll(stats.n_matched);
1396 struct port_stats_state {
1400 static int port_stats_init(struct datapath *dp, const void *body, int body_len,
1403 struct port_stats_state *s = xmalloc(sizeof *s);
1409 static int port_stats_dump(struct datapath *dp, void *state,
1410 struct buffer *buffer)
1412 struct port_stats_state *s = state;
1415 for (i = s->port; i < OFPP_MAX; i++) {
1416 struct sw_port *p = &dp->ports[i];
1417 struct ofp_port_stats *ops;
1421 ops = buffer_put_uninit(buffer, sizeof *ops);
1422 ops->port_no = htons(port_no(dp, p));
1423 memset(ops->pad, 0, sizeof ops->pad);
1424 ops->rx_packets = htonll(p->rx_packets);
1425 ops->tx_packets = htonll(p->tx_packets);
1426 ops->rx_bytes = htonll(p->rx_bytes);
1427 ops->tx_bytes = htonll(p->tx_bytes);
1428 ops->rx_dropped = htonll(-1);
1429 ops->tx_dropped = htonll(p->tx_dropped);
1430 ops->rx_errors = htonll(-1);
1431 ops->tx_errors = htonll(-1);
1432 ops->rx_frame_err = htonll(-1);
1433 ops->rx_over_err = htonll(-1);
1434 ops->rx_crc_err = htonll(-1);
1435 ops->collisions = htonll(-1);
1442 static void port_stats_done(void *state)
1448 /* Minimum and maximum acceptable number of bytes in body member of
1449 * struct ofp_stats_request. */
1450 size_t min_body, max_body;
1452 /* Prepares to dump some kind of statistics on 'dp'. 'body' and
1453 * 'body_len' are the 'body' member of the struct ofp_stats_request.
1454 * Returns zero if successful, otherwise a negative error code.
1455 * May initialize '*state' to state information. May be null if no
1456 * initialization is required.*/
1457 int (*init)(struct datapath *dp, const void *body, int body_len,
1460 /* Appends statistics for 'dp' to 'buffer', which initially contains a
1461 * struct ofp_stats_reply. On success, it should return 1 if it should be
1462 * called again later with another buffer, 0 if it is done, or a negative
1463 * errno value on failure. */
1464 int (*dump)(struct datapath *dp, void *state, struct buffer *buffer);
1466 /* Cleans any state created by the init or dump functions. May be null
1467 * if no cleanup is required. */
1468 void (*done)(void *state);
1471 static const struct stats_type stats[] = {
1480 sizeof(struct ofp_flow_stats_request),
1481 sizeof(struct ofp_flow_stats_request),
1486 [OFPST_AGGREGATE] = {
1487 sizeof(struct ofp_aggregate_stats_request),
1488 sizeof(struct ofp_aggregate_stats_request),
1489 aggregate_stats_init,
1490 aggregate_stats_dump,
1491 aggregate_stats_done
1509 struct stats_dump_cb {
1511 struct ofp_stats_request *rq;
1512 struct sender sender;
1513 const struct stats_type *s;
1518 stats_dump(struct datapath *dp, void *cb_)
1520 struct stats_dump_cb *cb = cb_;
1521 struct ofp_stats_reply *osr;
1522 struct buffer *buffer;
1529 osr = make_openflow_reply(sizeof *osr, OFPT_STATS_REPLY, &cb->sender,
1531 osr->type = htons(cb->s - stats);
1534 err = cb->s->dump(dp, cb->state, buffer);
1540 /* Buffer might have been reallocated, so find our data again. */
1541 osr = buffer_at_assert(buffer, 0, sizeof *osr);
1542 osr->flags = ntohs(OFPSF_REPLY_MORE);
1544 err2 = send_openflow_buffer(dp, buffer, &cb->sender);
1554 stats_done(void *cb_)
1556 struct stats_dump_cb *cb = cb_;
1559 cb->s->done(cb->state);
1566 recv_stats_request(struct datapath *dp, const struct sender *sender,
1569 const struct ofp_stats_request *rq = oh;
1570 size_t rq_len = ntohs(rq->header.length);
1571 struct stats_dump_cb *cb;
1575 type = ntohs(rq->type);
1576 if (type >= ARRAY_SIZE(stats) || !stats[type].dump) {
1577 VLOG_WARN("received stats request of unknown type %d", type);
1581 cb = xmalloc(sizeof *cb);
1583 cb->rq = xmemdup(rq, rq_len);
1584 cb->sender = *sender;
1585 cb->s = &stats[type];
1588 body_len = rq_len - offsetof(struct ofp_stats_request, body);
1589 if (body_len < cb->s->min_body || body_len > cb->s->max_body) {
1590 VLOG_WARN("stats request type %d with bad body length %d",
1597 err = cb->s->init(dp, rq->body, body_len, &cb->state);
1599 VLOG_WARN("failed initialization of stats request type %d: %s",
1600 type, strerror(-err));
1605 remote_start_dump(sender->remote, stats_dump, stats_done, cb);
1615 recv_echo_request(struct datapath *dp, const struct sender *sender,
1618 return send_openflow_buffer(dp, make_echo_reply(oh), sender);
1622 recv_echo_reply(struct datapath *dp UNUSED, const struct sender *sender UNUSED,
1623 const void *oh UNUSED)
1628 /* 'msg', which is 'length' bytes long, was received from the control path.
1629 * Apply it to 'chain'. */
1631 fwd_control_input(struct datapath *dp, const struct sender *sender,
1632 const void *msg, size_t length)
1634 struct openflow_packet {
1636 int (*handler)(struct datapath *, const struct sender *, const void *);
1639 static const struct openflow_packet packets[] = {
1640 [OFPT_FEATURES_REQUEST] = {
1641 sizeof (struct ofp_header),
1642 recv_features_request,
1644 [OFPT_GET_CONFIG_REQUEST] = {
1645 sizeof (struct ofp_header),
1646 recv_get_config_request,
1648 [OFPT_SET_CONFIG] = {
1649 sizeof (struct ofp_switch_config),
1652 [OFPT_PACKET_OUT] = {
1653 sizeof (struct ofp_packet_out),
1657 sizeof (struct ofp_flow_mod),
1661 sizeof (struct ofp_port_mod),
1664 [OFPT_STATS_REQUEST] = {
1665 sizeof (struct ofp_stats_request),
1668 [OFPT_ECHO_REQUEST] = {
1669 sizeof (struct ofp_header),
1672 [OFPT_ECHO_REPLY] = {
1673 sizeof (struct ofp_header),
1678 const struct openflow_packet *pkt;
1679 struct ofp_header *oh;
1681 oh = (struct ofp_header *) msg;
1682 assert(oh->version == OFP_VERSION);
1683 if (oh->type >= ARRAY_SIZE(packets) || ntohs(oh->length) > length)
1686 pkt = &packets[oh->type];
1689 if (length < pkt->min_size)
1692 return pkt->handler(dp, sender, msg);
1695 /* Packet buffering. */
1697 #define OVERWRITE_SECS 1
1699 struct packet_buffer {
1700 struct buffer *buffer;
1705 static struct packet_buffer buffers[N_PKT_BUFFERS];
1706 static unsigned int buffer_idx;
1708 uint32_t save_buffer(struct buffer *buffer)
1710 struct packet_buffer *p;
1713 buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
1714 p = &buffers[buffer_idx];
1716 /* Don't buffer packet if existing entry is less than
1717 * OVERWRITE_SECS old. */
1718 if (time_now() < p->timeout) { /* FIXME */
1721 buffer_delete(p->buffer);
1724 /* Don't use maximum cookie value since the all-bits-1 id is
1726 if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
1728 p->buffer = buffer_clone(buffer); /* FIXME */
1729 p->timeout = time_now() + OVERWRITE_SECS; /* FIXME */
1730 id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
1735 static struct buffer *retrieve_buffer(uint32_t id)
1737 struct buffer *buffer = NULL;
1738 struct packet_buffer *p;
1740 p = &buffers[id & PKT_BUFFER_MASK];
1741 if (p->cookie == id >> PKT_BUFFER_BITS) {
1745 printf("cookie mismatch: %x != %x\n",
1746 id >> PKT_BUFFER_BITS, p->cookie);
1752 static void discard_buffer(uint32_t id)
1754 struct packet_buffer *p;
1756 p = &buffers[id & PKT_BUFFER_MASK];
1757 if (p->cookie == id >> PKT_BUFFER_BITS) {
1758 buffer_delete(p->buffer);