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>
49 #include "poll-loop.h"
51 #include "switch-flow.h"
57 #define THIS_MODULE VLM_datapath
61 BRPF_NO_FLOOD = 1 << 0,
65 BRPS_PORT_DOWN = 1 << 0,
66 BRPS_LINK_DOWN = 1 << 1,
72 extern char serial_num;
74 /* Capabilities supported by this implementation. */
75 #define OFP_SUPPORTED_CAPABILITIES ( OFPC_FLOW_STATS \
80 /* Actions supported by this implementation. */
81 #define OFP_SUPPORTED_ACTIONS ( (1 << OFPAT_OUTPUT) \
82 | (1 << OFPAT_SET_DL_VLAN) \
83 | (1 << OFPAT_SET_DL_SRC) \
84 | (1 << OFPAT_SET_DL_DST) \
85 | (1 << OFPAT_SET_NW_SRC) \
86 | (1 << OFPAT_SET_NW_DST) \
87 | (1 << OFPAT_SET_TP_SRC) \
88 | (1 << OFPAT_SET_TP_DST) )
91 uint32_t flags; /* BRPF_* flags */
92 uint32_t status; /* BRPS_* flags */
94 struct netdev *netdev;
95 struct list node; /* Element in datapath.ports. */
96 unsigned long long int rx_packets, tx_packets;
97 unsigned long long int rx_bytes, tx_bytes;
98 unsigned long long int tx_dropped;
101 /* The origin of a received OpenFlow message, to enable sending a reply. */
103 struct remote *remote; /* The device that sent the message. */
104 uint32_t xid; /* The OpenFlow transaction ID. */
107 /* A connection to a controller or a management device. */
111 #define TXQ_LIMIT 128 /* Max number of packets to queue for tx. */
112 int n_txq; /* Number of packets queued for tx on rconn. */
114 /* Support for reliable, multi-message replies to requests.
116 * If an incoming request needs to have a reliable reply that might
117 * require multiple messages, it can use remote_start_dump() to set up
118 * a callback that will be called as buffer space for replies. */
119 int (*cb_dump)(struct datapath *, void *aux);
120 void (*cb_done)(void *aux);
125 /* Remote connections. */
126 struct remote *controller; /* Connection to controller. */
127 struct list remotes; /* All connections (including controller). */
128 struct vconn *listen_vconn;
132 /* Unique identifier for this datapath */
135 struct sw_chain *chain; /* Forwarding rules. */
137 /* Configuration set from controller. */
139 uint16_t miss_send_len;
142 struct sw_port ports[OFPP_MAX];
143 struct list port_list; /* List of ports, for flooding. */
146 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
148 static struct remote *remote_create(struct datapath *, struct rconn *);
149 static void remote_run(struct datapath *, struct remote *);
150 static void remote_wait(struct remote *);
151 static void remote_destroy(struct remote *);
153 void dp_output_port(struct datapath *, struct ofpbuf *,
154 int in_port, int out_port);
155 void dp_update_port_flags(struct datapath *dp, const struct ofp_port_mod *opm);
156 void dp_output_control(struct datapath *, struct ofpbuf *, int in_port,
157 size_t max_len, int reason);
158 static void send_flow_expired(struct datapath *, struct sw_flow *,
159 enum ofp_flow_expired_reason);
160 static int update_port_status(struct sw_port *p);
161 static void send_port_status(struct sw_port *p, uint8_t status);
162 static void del_switch_port(struct sw_port *p);
163 static void execute_actions(struct datapath *, struct ofpbuf *,
164 int in_port, const struct sw_flow_key *,
165 const struct ofp_action *, int n_actions);
166 static void modify_vlan(struct ofpbuf *buffer, const struct sw_flow_key *key,
167 const struct ofp_action *a);
168 static void modify_nh(struct ofpbuf *buffer, uint16_t eth_proto,
169 uint8_t nw_proto, const struct ofp_action *a);
170 static void modify_th(struct ofpbuf *buffer, uint16_t eth_proto,
171 uint8_t nw_proto, const struct ofp_action *a);
173 /* Buffers are identified to userspace by a 31-bit opaque ID. We divide the ID
174 * into a buffer number (low bits) and a cookie (high bits). The buffer number
175 * is an index into an array of buffers. The cookie distinguishes between
176 * different packets that have occupied a single buffer. Thus, the more
177 * buffers we have, the lower-quality the cookie... */
178 #define PKT_BUFFER_BITS 8
179 #define N_PKT_BUFFERS (1 << PKT_BUFFER_BITS)
180 #define PKT_BUFFER_MASK (N_PKT_BUFFERS - 1)
182 #define PKT_COOKIE_BITS (32 - PKT_BUFFER_BITS)
184 int run_flow_through_tables(struct datapath *, struct ofpbuf *, int in_port);
185 void fwd_port_input(struct datapath *, struct ofpbuf *, int in_port);
186 int fwd_control_input(struct datapath *, const struct sender *,
187 const void *, size_t);
189 uint32_t save_buffer(struct ofpbuf *);
190 static struct ofpbuf *retrieve_buffer(uint32_t id);
191 static void discard_buffer(uint32_t id);
193 static int port_no(struct datapath *dp, struct sw_port *p)
195 assert(p >= dp->ports && p < &dp->ports[ARRAY_SIZE(dp->ports)]);
196 return p - dp->ports;
199 /* Generates and returns a random datapath id. */
201 gen_datapath_id(void)
203 uint8_t ea[ETH_ADDR_LEN];
205 return eth_addr_to_uint64(ea);
209 dp_new(struct datapath **dp_, uint64_t dpid, struct rconn *rconn)
213 dp = calloc(1, sizeof *dp);
218 dp->last_timeout = time_now();
219 list_init(&dp->remotes);
220 dp->controller = remote_create(dp, rconn);
221 dp->listen_vconn = NULL;
222 dp->id = dpid <= UINT64_C(0xffffffffffff) ? dpid : gen_datapath_id();
223 dp->chain = chain_create();
225 VLOG_ERR("could not create chain");
230 list_init(&dp->port_list);
232 dp->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
238 dp_add_port(struct datapath *dp, const char *name)
240 struct netdev *netdev;
246 error = netdev_open(name, NETDEV_ETH_TYPE_ANY, &netdev);
250 error = netdev_set_flags(netdev, NETDEV_UP | NETDEV_PROMISC, false);
252 VLOG_ERR("couldn't set promiscuous mode on %s device", name);
253 netdev_close(netdev);
256 if (netdev_get_in4(netdev, &in4)) {
257 VLOG_ERR("%s device has assigned IP address %s", name, inet_ntoa(in4));
259 if (netdev_get_in6(netdev, &in6)) {
260 char in6_name[INET6_ADDRSTRLEN + 1];
261 inet_ntop(AF_INET6, &in6, in6_name, sizeof in6_name);
262 VLOG_ERR("%s device has assigned IPv6 address %s", name, in6_name);
265 for (p = dp->ports; ; p++) {
266 if (p >= &dp->ports[ARRAY_SIZE(dp->ports)]) {
268 } else if (!p->netdev) {
273 memset(p, '\0', sizeof *p);
277 list_push_back(&dp->port_list, &p->node);
279 /* Notify the ctlpath that this port has been added */
280 send_port_status(p, OFPPR_ADD);
286 dp_add_listen_vconn(struct datapath *dp, struct vconn *listen_vconn)
288 assert(!dp->listen_vconn);
289 dp->listen_vconn = listen_vconn;
293 dp_run(struct datapath *dp)
295 time_t now = time_now();
296 struct sw_port *p, *pn;
297 struct remote *r, *rn;
298 struct ofpbuf *buffer = NULL;
300 if (now != dp->last_timeout) {
301 struct list deleted = LIST_INITIALIZER(&deleted);
302 struct sw_flow *f, *n;
304 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
305 if (update_port_status(p)) {
306 send_port_status(p, OFPPR_MOD);
310 chain_timeout(dp->chain, &deleted);
311 LIST_FOR_EACH_SAFE (f, n, struct sw_flow, node, &deleted) {
312 send_flow_expired(dp, f, f->reason);
313 list_remove(&f->node);
316 dp->last_timeout = now;
318 poll_timer_wait(1000);
320 LIST_FOR_EACH_SAFE (p, pn, struct sw_port, node, &dp->port_list) {
324 /* Allocate buffer with some headroom to add headers in forwarding
325 * to the controller or adding a vlan tag, plus an extra 2 bytes to
326 * allow IP headers to be aligned on a 4-byte boundary. */
327 const int headroom = 128 + 2;
328 const int hard_header = VLAN_ETH_HEADER_LEN;
329 const int mtu = netdev_get_mtu(p->netdev);
330 buffer = ofpbuf_new(headroom + hard_header + mtu);
331 buffer->data = (char*)buffer->data + headroom;
333 error = netdev_recv(p->netdev, buffer);
336 p->rx_bytes += buffer->size;
337 fwd_port_input(dp, buffer, port_no(dp, p));
339 } else if (error != EAGAIN) {
340 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
341 netdev_get_name(p->netdev), strerror(error));
344 ofpbuf_delete(buffer);
346 /* Talk to remotes. */
347 LIST_FOR_EACH_SAFE (r, rn, struct remote, node, &dp->remotes) {
350 if (dp->listen_vconn) {
352 struct vconn *new_vconn;
355 retval = vconn_accept(dp->listen_vconn, &new_vconn);
357 if (retval != EAGAIN) {
358 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
362 remote_create(dp, rconn_new_from_vconn("passive", new_vconn));
368 remote_run(struct datapath *dp, struct remote *r)
374 /* Do some remote processing, but cap it at a reasonable amount so that
375 * other processing doesn't starve. */
376 for (i = 0; i < 50; i++) {
378 struct ofpbuf *buffer;
379 struct ofp_header *oh;
381 buffer = rconn_recv(r->rconn);
386 if (buffer->size >= sizeof *oh) {
387 struct sender sender;
391 sender.xid = oh->xid;
392 fwd_control_input(dp, &sender, buffer->data, buffer->size);
394 VLOG_WARN_RL(&rl, "received too-short OpenFlow message");
396 ofpbuf_delete(buffer);
398 if (r->n_txq < TXQ_LIMIT) {
399 int error = r->cb_dump(dp, r->cb_aux);
402 VLOG_WARN_RL(&rl, "dump callback error: %s",
405 r->cb_done(r->cb_aux);
414 if (!rconn_is_alive(r->rconn)) {
420 remote_wait(struct remote *r)
422 rconn_run_wait(r->rconn);
423 rconn_recv_wait(r->rconn);
427 remote_destroy(struct remote *r)
430 if (r->cb_dump && r->cb_done) {
431 r->cb_done(r->cb_aux);
433 list_remove(&r->node);
434 rconn_destroy(r->rconn);
439 static struct remote *
440 remote_create(struct datapath *dp, struct rconn *rconn)
442 struct remote *remote = xmalloc(sizeof *remote);
443 list_push_back(&dp->remotes, &remote->node);
444 remote->rconn = rconn;
445 remote->cb_dump = NULL;
450 /* Starts a callback-based, reliable, possibly multi-message reply to a
451 * request made by 'remote'.
453 * 'dump' designates a function that will be called when the 'remote' send
454 * queue has an empty slot. It should compose a message and send it on
455 * 'remote'. On success, it should return 1 if it should be called again when
456 * another send queue slot opens up, 0 if its transmissions are complete, or a
457 * negative errno value on failure.
459 * 'done' designates a function to clean up any resources allocated for the
460 * dump. It must handle being called before the dump is complete (which will
461 * happen if 'remote' is closed unexpectedly).
463 * 'aux' is passed to 'dump' and 'done'. */
465 remote_start_dump(struct remote *remote,
466 int (*dump)(struct datapath *, void *),
467 void (*done)(void *),
470 assert(!remote->cb_dump);
471 remote->cb_dump = dump;
472 remote->cb_done = done;
473 remote->cb_aux = aux;
477 dp_wait(struct datapath *dp)
482 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
483 netdev_recv_wait(p->netdev);
485 LIST_FOR_EACH (r, struct remote, node, &dp->remotes) {
488 if (dp->listen_vconn) {
489 vconn_accept_wait(dp->listen_vconn);
493 /* Delete 'p' from switch. */
495 del_switch_port(struct sw_port *p)
497 send_port_status(p, OFPPR_DELETE);
498 netdev_close(p->netdev);
500 list_remove(&p->node);
504 dp_destroy(struct datapath *dp)
506 struct sw_port *p, *n;
512 LIST_FOR_EACH_SAFE (p, n, struct sw_port, node, &dp->port_list) {
515 chain_destroy(dp->chain);
519 /* Send packets out all the ports except the originating one. If the
520 * "flood" argument is set, don't send out ports with flooding disabled.
523 output_all(struct datapath *dp, struct ofpbuf *buffer, int in_port, int flood)
529 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
530 if (port_no(dp, p) == in_port) {
533 if (flood && p->flags & BRPF_NO_FLOOD) {
536 if (prev_port != -1) {
537 dp_output_port(dp, ofpbuf_clone(buffer), in_port, prev_port);
539 prev_port = port_no(dp, p);
542 dp_output_port(dp, buffer, in_port, prev_port);
544 ofpbuf_delete(buffer);
550 output_packet(struct datapath *dp, struct ofpbuf *buffer, int out_port)
552 if (out_port >= 0 && out_port < OFPP_MAX) {
553 struct sw_port *p = &dp->ports[out_port];
554 if (p->netdev != NULL && !(p->status & BRPS_PORT_DOWN)) {
555 if (!netdev_send(p->netdev, buffer)) {
557 p->tx_bytes += buffer->size;
565 ofpbuf_delete(buffer);
566 VLOG_DBG_RL(&rl, "can't forward to bad port %d\n", out_port);
569 /* Takes ownership of 'buffer' and transmits it to 'out_port' on 'dp'.
572 dp_output_port(struct datapath *dp, struct ofpbuf *buffer,
573 int in_port, int out_port)
577 if (out_port == OFPP_FLOOD) {
578 output_all(dp, buffer, in_port, 1);
579 } else if (out_port == OFPP_ALL) {
580 output_all(dp, buffer, in_port, 0);
581 } else if (out_port == OFPP_CONTROLLER) {
582 dp_output_control(dp, buffer, in_port, 0, OFPR_ACTION);
583 } else if (out_port == OFPP_IN_PORT) {
584 output_packet(dp, buffer, in_port);
585 } else if (out_port == OFPP_TABLE) {
586 if (run_flow_through_tables(dp, buffer, in_port)) {
587 ofpbuf_delete(buffer);
590 if (in_port == out_port) {
591 VLOG_DBG_RL(&rl, "can't directly forward to input port");
594 output_packet(dp, buffer, out_port);
599 make_openflow_reply(size_t openflow_len, uint8_t type,
600 const struct sender *sender, struct ofpbuf **bufferp)
602 return make_openflow_xid(openflow_len, type, sender ? sender->xid : 0,
607 send_openflow_buffer(struct datapath *dp, struct ofpbuf *buffer,
608 const struct sender *sender)
610 struct remote *remote = sender ? sender->remote : dp->controller;
611 struct rconn *rconn = remote->rconn;
614 update_openflow_length(buffer);
615 retval = rconn_send_with_limit(rconn, buffer, &remote->n_txq, TXQ_LIMIT);
617 VLOG_WARN_RL(&rl, "send to %s failed: %s",
618 rconn_get_name(rconn), strerror(retval));
623 /* Takes ownership of 'buffer' and transmits it to 'dp''s controller. If the
624 * packet can be saved in a buffer, then only the first max_len bytes of
625 * 'buffer' are sent; otherwise, all of 'buffer' is sent. 'reason' indicates
626 * why 'buffer' is being sent. 'max_len' sets the maximum number of bytes that
627 * the caller wants to be sent; a value of 0 indicates the entire packet should
630 dp_output_control(struct datapath *dp, struct ofpbuf *buffer, int in_port,
631 size_t max_len, int reason)
633 struct ofp_packet_in *opi;
637 buffer_id = save_buffer(buffer);
638 total_len = buffer->size;
639 if (buffer_id != UINT32_MAX && max_len && buffer->size > max_len) {
640 buffer->size = max_len;
643 opi = ofpbuf_push_uninit(buffer, offsetof(struct ofp_packet_in, data));
644 opi->header.version = OFP_VERSION;
645 opi->header.type = OFPT_PACKET_IN;
646 opi->header.length = htons(buffer->size);
647 opi->header.xid = htonl(0);
648 opi->buffer_id = htonl(buffer_id);
649 opi->total_len = htons(total_len);
650 opi->in_port = htons(in_port);
651 opi->reason = reason;
653 send_openflow_buffer(dp, buffer, NULL);
656 static void fill_port_desc(struct datapath *dp, struct sw_port *p,
657 struct ofp_phy_port *desc)
659 desc->port_no = htons(port_no(dp, p));
660 strncpy((char *) desc->name, netdev_get_name(p->netdev),
662 desc->name[sizeof desc->name - 1] = '\0';
663 memcpy(desc->hw_addr, netdev_get_etheraddr(p->netdev), ETH_ADDR_LEN);
665 desc->features = htonl(netdev_get_features(p->netdev));
666 desc->speed = htonl(netdev_get_speed(p->netdev));
668 if (p->flags & BRPF_NO_FLOOD) {
669 desc->flags |= htonl(OFPPFL_NO_FLOOD);
670 } else if (p->status & BRPS_PORT_DOWN) {
671 desc->flags |= htonl(OFPPFL_PORT_DOWN);
672 } else if (p->status & BRPS_LINK_DOWN) {
673 desc->flags |= htonl(OFPPFL_LINK_DOWN);
678 dp_send_features_reply(struct datapath *dp, const struct sender *sender)
680 struct ofpbuf *buffer;
681 struct ofp_switch_features *ofr;
684 ofr = make_openflow_reply(sizeof *ofr, OFPT_FEATURES_REPLY,
686 ofr->datapath_id = htonll(dp->id);
687 ofr->n_exact = htonl(2 * TABLE_HASH_MAX_FLOWS);
688 ofr->n_compression = 0; /* Not supported */
689 ofr->n_general = htonl(TABLE_LINEAR_MAX_FLOWS);
690 ofr->buffer_mb = htonl(UINT32_MAX);
691 ofr->n_buffers = htonl(N_PKT_BUFFERS);
692 ofr->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
693 ofr->actions = htonl(OFP_SUPPORTED_ACTIONS);
694 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
695 struct ofp_phy_port *opp = ofpbuf_put_uninit(buffer, sizeof *opp);
696 memset(opp, 0, sizeof *opp);
697 fill_port_desc(dp, p, opp);
699 send_openflow_buffer(dp, buffer, sender);
703 dp_update_port_flags(struct datapath *dp, const struct ofp_port_mod *opm)
705 const struct ofp_phy_port *opp = &opm->desc;
706 int port_no = ntohs(opp->port_no);
707 if (port_no < OFPP_MAX) {
708 struct sw_port *p = &dp->ports[port_no];
710 /* Make sure the port id hasn't changed since this was sent */
711 if (!p || memcmp(opp->hw_addr, netdev_get_etheraddr(p->netdev),
712 ETH_ADDR_LEN) != 0) {
717 if (opm->mask & htonl(OFPPFL_NO_FLOOD)) {
718 if (opp->flags & htonl(OFPPFL_NO_FLOOD))
719 p->flags |= BRPF_NO_FLOOD;
721 p->flags &= ~BRPF_NO_FLOOD;
724 if (opm->mask & htonl(OFPPFL_PORT_DOWN)) {
725 if ((opp->flags & htonl(OFPPFL_PORT_DOWN))
726 && (p->status & BRPS_PORT_DOWN) == 0) {
727 p->status |= BRPS_PORT_DOWN;
728 netdev_turn_flags_off(p->netdev, NETDEV_UP, true);
729 } else if ((opp->flags & htonl(OFPPFL_PORT_DOWN)) == 0
730 && (p->status & BRPS_PORT_DOWN)) {
731 p->status &= ~BRPS_PORT_DOWN;
732 netdev_turn_flags_on(p->netdev, NETDEV_UP, true);
738 /* Update the port status field of the bridge port. A non-zero return
739 * value indicates some field has changed.
741 * NB: Callers of this function may hold the RCU read lock, so any
742 * additional checks must not sleep.
745 update_port_status(struct sw_port *p)
748 enum netdev_flags flags;
749 uint32_t orig_status = p->status;
751 if (netdev_get_flags(p->netdev, &flags) < 0) {
752 VLOG_WARN_RL(&rl, "could not get netdev flags for %s",
753 netdev_get_name(p->netdev));
756 if (flags & NETDEV_UP) {
757 p->status &= ~BRPS_PORT_DOWN;
759 p->status |= BRPS_PORT_DOWN;
763 /* Not all cards support this getting link status, so don't warn on
765 retval = netdev_get_link_status(p->netdev);
767 p->status &= ~BRPS_LINK_DOWN;
768 } else if (retval == 0) {
769 p->status |= BRPS_LINK_DOWN;
772 return (orig_status != p->status);
776 send_port_status(struct sw_port *p, uint8_t status)
778 struct ofpbuf *buffer;
779 struct ofp_port_status *ops;
780 ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &buffer);
781 ops->reason = status;
782 memset(ops->pad, 0, sizeof ops->pad);
783 fill_port_desc(p->dp, p, &ops->desc);
785 send_openflow_buffer(p->dp, buffer, NULL);
789 send_flow_expired(struct datapath *dp, struct sw_flow *flow,
790 enum ofp_flow_expired_reason reason)
792 struct ofpbuf *buffer;
793 struct ofp_flow_expired *ofe;
794 ofe = make_openflow_xid(sizeof *ofe, OFPT_FLOW_EXPIRED, 0, &buffer);
795 flow_fill_match(&ofe->match, &flow->key);
797 ofe->priority = htons(flow->priority);
798 ofe->reason = reason;
799 memset(ofe->pad, 0, sizeof ofe->pad);
801 ofe->duration = htonl(time_now() - flow->created);
802 memset(ofe->pad2, 0, sizeof ofe->pad2);
803 ofe->packet_count = htonll(flow->packet_count);
804 ofe->byte_count = htonll(flow->byte_count);
805 send_openflow_buffer(dp, buffer, NULL);
809 dp_send_error_msg(struct datapath *dp, const struct sender *sender,
810 uint16_t type, uint16_t code, const uint8_t *data, size_t len)
812 struct ofpbuf *buffer;
813 struct ofp_error_msg *oem;
814 oem = make_openflow_reply(sizeof(*oem)+len, OFPT_ERROR_MSG,
816 oem->type = htons(type);
817 oem->code = htons(code);
818 memcpy(oem->data, data, len);
819 send_openflow_buffer(dp, buffer, sender);
823 fill_flow_stats(struct ofpbuf *buffer, struct sw_flow *flow,
824 int table_idx, time_t now)
826 struct ofp_flow_stats *ofs;
827 int length = sizeof *ofs + sizeof *ofs->actions * flow->n_actions;
828 ofs = ofpbuf_put_uninit(buffer, length);
829 ofs->length = htons(length);
830 ofs->table_id = table_idx;
832 ofs->match.wildcards = htonl(flow->key.wildcards);
833 ofs->match.in_port = flow->key.flow.in_port;
834 memcpy(ofs->match.dl_src, flow->key.flow.dl_src, ETH_ADDR_LEN);
835 memcpy(ofs->match.dl_dst, flow->key.flow.dl_dst, ETH_ADDR_LEN);
836 ofs->match.dl_vlan = flow->key.flow.dl_vlan;
837 ofs->match.dl_type = flow->key.flow.dl_type;
838 ofs->match.nw_src = flow->key.flow.nw_src;
839 ofs->match.nw_dst = flow->key.flow.nw_dst;
840 ofs->match.nw_proto = flow->key.flow.nw_proto;
842 ofs->match.tp_src = flow->key.flow.tp_src;
843 ofs->match.tp_dst = flow->key.flow.tp_dst;
844 ofs->duration = htonl(now - flow->created);
845 ofs->priority = htons(flow->priority);
846 ofs->idle_timeout = htons(flow->idle_timeout);
847 ofs->hard_timeout = htons(flow->hard_timeout);
848 memset(ofs->pad2, 0, sizeof ofs->pad2);
849 ofs->packet_count = htonll(flow->packet_count);
850 ofs->byte_count = htonll(flow->byte_count);
851 memcpy(ofs->actions, flow->actions,
852 sizeof *ofs->actions * flow->n_actions);
856 /* 'buffer' was received on 'in_port', a physical switch port between 0 and
857 * OFPP_MAX. Process it according to 'dp''s flow table. Returns 0 if
858 * successful, in which case 'buffer' is destroyed, or -ESRCH if there is no
859 * matching flow, in which case 'buffer' still belongs to the caller. */
860 int run_flow_through_tables(struct datapath *dp, struct ofpbuf *buffer,
863 struct sw_flow_key key;
864 struct sw_flow *flow;
867 if (flow_extract(buffer, in_port, &key.flow)
868 && (dp->flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP) {
870 ofpbuf_delete(buffer);
874 flow = chain_lookup(dp->chain, &key);
876 flow_used(flow, buffer);
877 execute_actions(dp, buffer, in_port, &key,
878 flow->actions, flow->n_actions);
885 /* 'buffer' was received on 'in_port', a physical switch port between 0 and
886 * OFPP_MAX. Process it according to 'dp''s flow table, sending it up to the
887 * controller if no flow matches. Takes ownership of 'buffer'. */
888 void fwd_port_input(struct datapath *dp, struct ofpbuf *buffer, int in_port)
890 if (run_flow_through_tables(dp, buffer, in_port)) {
891 dp_output_control(dp, buffer, in_port, dp->miss_send_len,
897 do_output(struct datapath *dp, struct ofpbuf *buffer, int in_port,
898 size_t max_len, int out_port)
900 if (out_port != OFPP_CONTROLLER) {
901 dp_output_port(dp, buffer, in_port, out_port);
903 dp_output_control(dp, buffer, in_port, max_len, OFPR_ACTION);
908 execute_actions(struct datapath *dp, struct ofpbuf *buffer,
909 int in_port, const struct sw_flow_key *key,
910 const struct ofp_action *actions, int n_actions)
912 /* Every output action needs a separate clone of 'buffer', but the common
913 * case is just a single output action, so that doing a clone and then
914 * freeing the original buffer is wasteful. So the following code is
915 * slightly obscure just to avoid that. */
917 size_t max_len=0; /* Initialze to make compiler happy */
922 eth_proto = ntohs(key->flow.dl_type);
924 for (i = 0; i < n_actions; i++) {
925 const struct ofp_action *a = &actions[i];
926 struct eth_header *eh = buffer->l2;
928 if (prev_port != -1) {
929 do_output(dp, ofpbuf_clone(buffer), in_port, max_len, prev_port);
933 switch (ntohs(a->type)) {
935 prev_port = ntohs(a->arg.output.port);
936 max_len = ntohs(a->arg.output.max_len);
939 case OFPAT_SET_DL_VLAN:
940 modify_vlan(buffer, key, a);
943 case OFPAT_SET_DL_SRC:
944 memcpy(eh->eth_src, a->arg.dl_addr, sizeof eh->eth_src);
947 case OFPAT_SET_DL_DST:
948 memcpy(eh->eth_dst, a->arg.dl_addr, sizeof eh->eth_dst);
951 case OFPAT_SET_NW_SRC:
952 case OFPAT_SET_NW_DST:
953 modify_nh(buffer, eth_proto, key->flow.nw_proto, a);
956 case OFPAT_SET_TP_SRC:
957 case OFPAT_SET_TP_DST:
958 modify_th(buffer, eth_proto, key->flow.nw_proto, a);
966 do_output(dp, buffer, in_port, max_len, prev_port);
968 ofpbuf_delete(buffer);
971 static void modify_nh(struct ofpbuf *buffer, uint16_t eth_proto,
972 uint8_t nw_proto, const struct ofp_action *a)
974 if (eth_proto == ETH_TYPE_IP) {
975 struct ip_header *nh = buffer->l3;
976 uint32_t new, *field;
978 new = a->arg.nw_addr;
979 field = a->type == OFPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
980 if (nw_proto == IP_TYPE_TCP) {
981 struct tcp_header *th = buffer->l4;
982 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, new);
983 } else if (nw_proto == IP_TYPE_UDP) {
984 struct udp_header *th = buffer->l4;
986 th->udp_csum = recalc_csum32(th->udp_csum, *field, new);
988 th->udp_csum = 0xffff;
992 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, new);
997 static void modify_th(struct ofpbuf *buffer, uint16_t eth_proto,
998 uint8_t nw_proto, const struct ofp_action *a)
1000 if (eth_proto == ETH_TYPE_IP) {
1001 uint16_t new, *field;
1005 if (nw_proto == IP_TYPE_TCP) {
1006 struct tcp_header *th = buffer->l4;
1007 field = a->type == OFPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1008 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, new);
1010 } else if (nw_proto == IP_TYPE_UDP) {
1011 struct udp_header *th = buffer->l4;
1012 field = a->type == OFPAT_SET_TP_SRC ? &th->udp_src : &th->udp_dst;
1013 th->udp_csum = recalc_csum16(th->udp_csum, *field, new);
1020 modify_vlan(struct ofpbuf *buffer,
1021 const struct sw_flow_key *key, const struct ofp_action *a)
1023 uint16_t new_id = a->arg.vlan_id;
1024 struct vlan_eth_header *veh;
1026 if (new_id != htons(OFP_VLAN_NONE)) {
1027 if (key->flow.dl_vlan != htons(OFP_VLAN_NONE)) {
1028 /* Modify vlan id, but maintain other TCI values */
1030 veh->veth_tci &= ~htons(VLAN_VID);
1031 veh->veth_tci |= new_id;
1033 /* Insert new vlan id. */
1034 struct eth_header *eh = buffer->l2;
1035 struct vlan_eth_header tmp;
1036 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1037 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1038 tmp.veth_type = htons(ETH_TYPE_VLAN);
1039 tmp.veth_tci = new_id;
1040 tmp.veth_next_type = eh->eth_type;
1042 veh = ofpbuf_push_uninit(buffer, VLAN_HEADER_LEN);
1043 memcpy(veh, &tmp, sizeof tmp);
1044 buffer->l2 = (char*)buffer->l2 - VLAN_HEADER_LEN;
1047 /* Remove an existing vlan header if it exists */
1049 if (veh->veth_type == htons(ETH_TYPE_VLAN)) {
1050 struct eth_header tmp;
1052 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1053 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1054 tmp.eth_type = veh->veth_next_type;
1056 buffer->size -= VLAN_HEADER_LEN;
1057 buffer->data = (char*)buffer->data + VLAN_HEADER_LEN;
1058 buffer->l2 = (char*)buffer->l2 + VLAN_HEADER_LEN;
1059 memcpy(buffer->data, &tmp, sizeof tmp);
1065 recv_features_request(struct datapath *dp, const struct sender *sender,
1068 dp_send_features_reply(dp, sender);
1073 recv_get_config_request(struct datapath *dp, const struct sender *sender,
1076 struct ofpbuf *buffer;
1077 struct ofp_switch_config *osc;
1079 osc = make_openflow_reply(sizeof *osc, OFPT_GET_CONFIG_REPLY,
1082 osc->flags = htons(dp->flags);
1083 osc->miss_send_len = htons(dp->miss_send_len);
1085 return send_openflow_buffer(dp, buffer, sender);
1089 recv_set_config(struct datapath *dp, const struct sender *sender UNUSED,
1092 const struct ofp_switch_config *osc = msg;
1095 flags = ntohs(osc->flags) & (OFPC_SEND_FLOW_EXP | OFPC_FRAG_MASK);
1096 if ((flags & OFPC_FRAG_MASK) != OFPC_FRAG_NORMAL
1097 && (flags & OFPC_FRAG_MASK) != OFPC_FRAG_DROP) {
1098 flags = (flags & ~OFPC_FRAG_MASK) | OFPC_FRAG_DROP;
1101 dp->miss_send_len = ntohs(osc->miss_send_len);
1106 recv_packet_out(struct datapath *dp, const struct sender *sender UNUSED,
1109 const struct ofp_packet_out *opo = msg;
1110 struct sw_flow_key key;
1111 struct ofpbuf *buffer;
1112 int n_actions = ntohs(opo->n_actions);
1113 int act_len = n_actions * sizeof opo->actions[0];
1115 if (act_len > (ntohs(opo->header.length) - sizeof *opo)) {
1116 VLOG_DBG_RL(&rl, "message too short for number of actions");
1120 if (ntohl(opo->buffer_id) == (uint32_t) -1) {
1121 /* FIXME: can we avoid copying data here? */
1122 int data_len = ntohs(opo->header.length) - sizeof *opo - act_len;
1123 buffer = ofpbuf_new(data_len);
1124 ofpbuf_put(buffer, &opo->actions[n_actions], data_len);
1126 buffer = retrieve_buffer(ntohl(opo->buffer_id));
1132 flow_extract(buffer, ntohs(opo->in_port), &key.flow);
1133 execute_actions(dp, buffer, ntohs(opo->in_port),
1134 &key, opo->actions, n_actions);
1140 recv_port_mod(struct datapath *dp, const struct sender *sender UNUSED,
1143 const struct ofp_port_mod *opm = msg;
1145 dp_update_port_flags(dp, opm);
1151 add_flow(struct datapath *dp, const struct ofp_flow_mod *ofm)
1153 int error = -ENOMEM;
1156 struct sw_flow *flow;
1159 /* To prevent loops, make sure there's no action to send to the
1160 * OFP_TABLE virtual port.
1162 n_actions = (ntohs(ofm->header.length) - sizeof *ofm)
1163 / sizeof *ofm->actions;
1164 for (i=0; i<n_actions; i++) {
1165 const struct ofp_action *a = &ofm->actions[i];
1167 if (a->type == htons(OFPAT_OUTPUT)
1168 && (a->arg.output.port == htons(OFPP_TABLE)
1169 || a->arg.output.port == htons(OFPP_NONE)
1170 || a->arg.output.port == ofm->match.in_port)) {
1171 /* xxx Send fancy new error message? */
1176 /* Allocate memory. */
1177 flow = flow_alloc(n_actions);
1181 /* Fill out flow. */
1182 flow_extract_match(&flow->key, &ofm->match);
1183 flow->priority = flow->key.wildcards ? ntohs(ofm->priority) : -1;
1184 flow->idle_timeout = ntohs(ofm->idle_timeout);
1185 flow->hard_timeout = ntohs(ofm->hard_timeout);
1186 flow->used = flow->created = time_now();
1187 flow->n_actions = n_actions;
1188 flow->byte_count = 0;
1189 flow->packet_count = 0;
1190 memcpy(flow->actions, ofm->actions, n_actions * sizeof *flow->actions);
1193 error = chain_insert(dp->chain, flow);
1195 goto error_free_flow;
1198 if (ntohl(ofm->buffer_id) != UINT32_MAX) {
1199 struct ofpbuf *buffer = retrieve_buffer(ntohl(ofm->buffer_id));
1201 struct sw_flow_key key;
1202 uint16_t in_port = ntohs(ofm->match.in_port);
1203 flow_used(flow, buffer);
1204 flow_extract(buffer, in_port, &key.flow);
1205 execute_actions(dp, buffer, in_port, &key, ofm->actions, n_actions);
1215 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
1216 discard_buffer(ntohl(ofm->buffer_id));
1221 recv_flow(struct datapath *dp, const struct sender *sender UNUSED,
1224 const struct ofp_flow_mod *ofm = msg;
1225 uint16_t command = ntohs(ofm->command);
1227 if (command == OFPFC_ADD) {
1228 return add_flow(dp, ofm);
1229 } else if (command == OFPFC_DELETE) {
1230 struct sw_flow_key key;
1231 flow_extract_match(&key, &ofm->match);
1232 return chain_delete(dp->chain, &key, 0, 0) ? 0 : -ESRCH;
1233 } else if (command == OFPFC_DELETE_STRICT) {
1234 struct sw_flow_key key;
1236 flow_extract_match(&key, &ofm->match);
1237 priority = key.wildcards ? ntohs(ofm->priority) : -1;
1238 return chain_delete(dp->chain, &key, priority, 1) ? 0 : -ESRCH;
1244 static int desc_stats_dump(struct datapath *dp, void *state,
1245 struct ofpbuf *buffer)
1247 struct ofp_desc_stats *ods = ofpbuf_put_uninit(buffer, sizeof *ods);
1249 strncpy(ods->mfr_desc, &mfr_desc, sizeof ods->mfr_desc);
1250 strncpy(ods->hw_desc, &hw_desc, sizeof ods->hw_desc);
1251 strncpy(ods->sw_desc, &sw_desc, sizeof ods->sw_desc);
1252 strncpy(ods->serial_num, &serial_num, sizeof ods->serial_num);
1257 struct flow_stats_state {
1259 struct sw_table_position position;
1260 struct ofp_flow_stats_request rq;
1263 struct ofpbuf *buffer;
1266 #define MAX_FLOW_STATS_BYTES 4096
1268 static int flow_stats_init(struct datapath *dp, const void *body, int body_len,
1271 const struct ofp_flow_stats_request *fsr = body;
1272 struct flow_stats_state *s = xmalloc(sizeof *s);
1273 s->table_idx = fsr->table_id == 0xff ? 0 : fsr->table_id;
1274 memset(&s->position, 0, sizeof s->position);
1280 static int flow_stats_dump_callback(struct sw_flow *flow, void *private)
1282 struct flow_stats_state *s = private;
1283 fill_flow_stats(s->buffer, flow, s->table_idx, s->now);
1284 return s->buffer->size >= MAX_FLOW_STATS_BYTES;
1287 static int flow_stats_dump(struct datapath *dp, void *state,
1288 struct ofpbuf *buffer)
1290 struct flow_stats_state *s = state;
1291 struct sw_flow_key match_key;
1293 flow_extract_match(&match_key, &s->rq.match);
1295 s->now = time_now();
1296 while (s->table_idx < dp->chain->n_tables
1297 && (s->rq.table_id == 0xff || s->rq.table_id == s->table_idx))
1299 struct sw_table *table = dp->chain->tables[s->table_idx];
1301 if (table->iterate(table, &match_key, &s->position,
1302 flow_stats_dump_callback, s))
1306 memset(&s->position, 0, sizeof s->position);
1308 return s->buffer->size >= MAX_FLOW_STATS_BYTES;
1311 static void flow_stats_done(void *state)
1316 struct aggregate_stats_state {
1317 struct ofp_aggregate_stats_request rq;
1320 static int aggregate_stats_init(struct datapath *dp,
1321 const void *body, int body_len,
1324 const struct ofp_aggregate_stats_request *rq = body;
1325 struct aggregate_stats_state *s = xmalloc(sizeof *s);
1331 static int aggregate_stats_dump_callback(struct sw_flow *flow, void *private)
1333 struct ofp_aggregate_stats_reply *rpy = private;
1334 rpy->packet_count += flow->packet_count;
1335 rpy->byte_count += flow->byte_count;
1340 static int aggregate_stats_dump(struct datapath *dp, void *state,
1341 struct ofpbuf *buffer)
1343 struct aggregate_stats_state *s = state;
1344 struct ofp_aggregate_stats_request *rq = &s->rq;
1345 struct ofp_aggregate_stats_reply *rpy;
1346 struct sw_table_position position;
1347 struct sw_flow_key match_key;
1350 rpy = ofpbuf_put_uninit(buffer, sizeof *rpy);
1351 memset(rpy, 0, sizeof *rpy);
1353 flow_extract_match(&match_key, &rq->match);
1354 table_idx = rq->table_id == 0xff ? 0 : rq->table_id;
1355 memset(&position, 0, sizeof position);
1356 while (table_idx < dp->chain->n_tables
1357 && (rq->table_id == 0xff || rq->table_id == table_idx))
1359 struct sw_table *table = dp->chain->tables[table_idx];
1362 error = table->iterate(table, &match_key, &position,
1363 aggregate_stats_dump_callback, rpy);
1368 memset(&position, 0, sizeof position);
1371 rpy->packet_count = htonll(rpy->packet_count);
1372 rpy->byte_count = htonll(rpy->byte_count);
1373 rpy->flow_count = htonl(rpy->flow_count);
1377 static void aggregate_stats_done(void *state)
1382 static int table_stats_dump(struct datapath *dp, void *state,
1383 struct ofpbuf *buffer)
1386 for (i = 0; i < dp->chain->n_tables; i++) {
1387 struct ofp_table_stats *ots = ofpbuf_put_uninit(buffer, sizeof *ots);
1388 struct sw_table_stats stats;
1389 dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
1390 strncpy(ots->name, stats.name, sizeof ots->name);
1392 memset(ots->pad, 0, sizeof ots->pad);
1393 ots->max_entries = htonl(stats.max_flows);
1394 ots->active_count = htonl(stats.n_flows);
1395 ots->matched_count = htonll(stats.n_matched);
1400 struct port_stats_state {
1404 static int port_stats_init(struct datapath *dp, const void *body, int body_len,
1407 struct port_stats_state *s = xmalloc(sizeof *s);
1413 static int port_stats_dump(struct datapath *dp, void *state,
1414 struct ofpbuf *buffer)
1416 struct port_stats_state *s = state;
1419 for (i = s->port; i < OFPP_MAX; i++) {
1420 struct sw_port *p = &dp->ports[i];
1421 struct ofp_port_stats *ops;
1425 ops = ofpbuf_put_uninit(buffer, sizeof *ops);
1426 ops->port_no = htons(port_no(dp, p));
1427 memset(ops->pad, 0, sizeof ops->pad);
1428 ops->rx_packets = htonll(p->rx_packets);
1429 ops->tx_packets = htonll(p->tx_packets);
1430 ops->rx_bytes = htonll(p->rx_bytes);
1431 ops->tx_bytes = htonll(p->tx_bytes);
1432 ops->rx_dropped = htonll(-1);
1433 ops->tx_dropped = htonll(p->tx_dropped);
1434 ops->rx_errors = htonll(-1);
1435 ops->tx_errors = htonll(-1);
1436 ops->rx_frame_err = htonll(-1);
1437 ops->rx_over_err = htonll(-1);
1438 ops->rx_crc_err = htonll(-1);
1439 ops->collisions = htonll(-1);
1446 static void port_stats_done(void *state)
1452 /* Value for 'type' member of struct ofp_stats_request. */
1455 /* Minimum and maximum acceptable number of bytes in body member of
1456 * struct ofp_stats_request. */
1457 size_t min_body, max_body;
1459 /* Prepares to dump some kind of statistics on 'dp'. 'body' and
1460 * 'body_len' are the 'body' member of the struct ofp_stats_request.
1461 * Returns zero if successful, otherwise a negative error code.
1462 * May initialize '*state' to state information. May be null if no
1463 * initialization is required.*/
1464 int (*init)(struct datapath *dp, const void *body, int body_len,
1467 /* Appends statistics for 'dp' to 'buffer', which initially contains a
1468 * struct ofp_stats_reply. On success, it should return 1 if it should be
1469 * called again later with another buffer, 0 if it is done, or a negative
1470 * errno value on failure. */
1471 int (*dump)(struct datapath *dp, void *state, struct ofpbuf *buffer);
1473 /* Cleans any state created by the init or dump functions. May be null
1474 * if no cleanup is required. */
1475 void (*done)(void *state);
1478 static const struct stats_type stats[] = {
1489 sizeof(struct ofp_flow_stats_request),
1490 sizeof(struct ofp_flow_stats_request),
1497 sizeof(struct ofp_aggregate_stats_request),
1498 sizeof(struct ofp_aggregate_stats_request),
1499 aggregate_stats_init,
1500 aggregate_stats_dump,
1501 aggregate_stats_done
1521 struct stats_dump_cb {
1523 struct ofp_stats_request *rq;
1524 struct sender sender;
1525 const struct stats_type *s;
1530 stats_dump(struct datapath *dp, void *cb_)
1532 struct stats_dump_cb *cb = cb_;
1533 struct ofp_stats_reply *osr;
1534 struct ofpbuf *buffer;
1541 osr = make_openflow_reply(sizeof *osr, OFPT_STATS_REPLY, &cb->sender,
1543 osr->type = htons(cb->s->type);
1546 err = cb->s->dump(dp, cb->state, buffer);
1552 /* Buffer might have been reallocated, so find our data again. */
1553 osr = ofpbuf_at_assert(buffer, 0, sizeof *osr);
1554 osr->flags = ntohs(OFPSF_REPLY_MORE);
1556 err2 = send_openflow_buffer(dp, buffer, &cb->sender);
1566 stats_done(void *cb_)
1568 struct stats_dump_cb *cb = cb_;
1571 cb->s->done(cb->state);
1578 recv_stats_request(struct datapath *dp, const struct sender *sender,
1581 const struct ofp_stats_request *rq = oh;
1582 size_t rq_len = ntohs(rq->header.length);
1583 const struct stats_type *st;
1584 struct stats_dump_cb *cb;
1588 type = ntohs(rq->type);
1589 for (st = stats; ; st++) {
1590 if (st >= &stats[ARRAY_SIZE(stats)]) {
1591 VLOG_WARN_RL(&rl, "received stats request of unknown type %d",
1594 } else if (type == st->type) {
1599 cb = xmalloc(sizeof *cb);
1601 cb->rq = xmemdup(rq, rq_len);
1602 cb->sender = *sender;
1606 body_len = rq_len - offsetof(struct ofp_stats_request, body);
1607 if (body_len < cb->s->min_body || body_len > cb->s->max_body) {
1608 VLOG_WARN_RL(&rl, "stats request type %d with bad body length %d",
1615 err = cb->s->init(dp, rq->body, body_len, &cb->state);
1618 "failed initialization of stats request type %d: %s",
1619 type, strerror(-err));
1624 remote_start_dump(sender->remote, stats_dump, stats_done, cb);
1634 recv_echo_request(struct datapath *dp, const struct sender *sender,
1637 return send_openflow_buffer(dp, make_echo_reply(oh), sender);
1641 recv_echo_reply(struct datapath *dp UNUSED, const struct sender *sender UNUSED,
1642 const void *oh UNUSED)
1647 /* 'msg', which is 'length' bytes long, was received from the control path.
1648 * Apply it to 'chain'. */
1650 fwd_control_input(struct datapath *dp, const struct sender *sender,
1651 const void *msg, size_t length)
1653 int (*handler)(struct datapath *, const struct sender *, const void *);
1654 struct ofp_header *oh;
1657 /* Check encapsulated length. */
1658 oh = (struct ofp_header *) msg;
1659 if (ntohs(oh->length) > length) {
1662 assert(oh->version == OFP_VERSION);
1664 /* Figure out how to handle it. */
1666 case OFPT_FEATURES_REQUEST:
1667 min_size = sizeof(struct ofp_header);
1668 handler = recv_features_request;
1670 case OFPT_GET_CONFIG_REQUEST:
1671 min_size = sizeof(struct ofp_header);
1672 handler = recv_get_config_request;
1674 case OFPT_SET_CONFIG:
1675 min_size = sizeof(struct ofp_switch_config);
1676 handler = recv_set_config;
1678 case OFPT_PACKET_OUT:
1679 min_size = sizeof(struct ofp_packet_out);
1680 handler = recv_packet_out;
1683 min_size = sizeof(struct ofp_flow_mod);
1684 handler = recv_flow;
1687 min_size = sizeof(struct ofp_port_mod);
1688 handler = recv_port_mod;
1690 case OFPT_STATS_REQUEST:
1691 min_size = sizeof(struct ofp_stats_request);
1692 handler = recv_stats_request;
1694 case OFPT_ECHO_REQUEST:
1695 min_size = sizeof(struct ofp_header);
1696 handler = recv_echo_request;
1698 case OFPT_ECHO_REPLY:
1699 min_size = sizeof(struct ofp_header);
1700 handler = recv_echo_reply;
1707 if (length < min_size)
1709 return handler(dp, sender, msg);
1712 /* Packet buffering. */
1714 #define OVERWRITE_SECS 1
1716 struct packet_buffer {
1717 struct ofpbuf *buffer;
1722 static struct packet_buffer buffers[N_PKT_BUFFERS];
1723 static unsigned int buffer_idx;
1725 uint32_t save_buffer(struct ofpbuf *buffer)
1727 struct packet_buffer *p;
1730 buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
1731 p = &buffers[buffer_idx];
1733 /* Don't buffer packet if existing entry is less than
1734 * OVERWRITE_SECS old. */
1735 if (time_now() < p->timeout) { /* FIXME */
1738 ofpbuf_delete(p->buffer);
1741 /* Don't use maximum cookie value since the all-bits-1 id is
1743 if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
1745 p->buffer = ofpbuf_clone(buffer); /* FIXME */
1746 p->timeout = time_now() + OVERWRITE_SECS; /* FIXME */
1747 id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
1752 static struct ofpbuf *retrieve_buffer(uint32_t id)
1754 struct ofpbuf *buffer = NULL;
1755 struct packet_buffer *p;
1757 p = &buffers[id & PKT_BUFFER_MASK];
1758 if (p->cookie == id >> PKT_BUFFER_BITS) {
1762 printf("cookie mismatch: %x != %x\n",
1763 id >> PKT_BUFFER_BITS, p->cookie);
1769 static void discard_buffer(uint32_t id)
1771 struct packet_buffer *p;
1773 p = &buffers[id & PKT_BUFFER_MASK];
1774 if (p->cookie == id >> PKT_BUFFER_BITS) {
1775 ofpbuf_delete(p->buffer);