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"
52 #include "switch-flow.h"
58 #define THIS_MODULE VLM_datapath
64 extern char serial_num;
66 /* Capabilities supported by this implementation. */
67 #define OFP_SUPPORTED_CAPABILITIES ( OFPC_FLOW_STATS \
72 /* Actions supported by this implementation. */
73 #define OFP_SUPPORTED_ACTIONS ( (1 << OFPAT_OUTPUT) \
74 | (1 << OFPAT_SET_VLAN_VID) \
75 | (1 << OFPAT_SET_VLAN_PCP) \
76 | (1 << OFPAT_STRIP_VLAN) \
77 | (1 << OFPAT_SET_DL_SRC) \
78 | (1 << OFPAT_SET_DL_DST) \
79 | (1 << OFPAT_SET_NW_SRC) \
80 | (1 << OFPAT_SET_NW_DST) \
81 | (1 << OFPAT_SET_TP_SRC) \
82 | (1 << OFPAT_SET_TP_DST) )
85 uint32_t config; /* Some subset of OFPPC_* flags. */
86 uint32_t state; /* Some subset of OFPPS_* flags. */
88 struct netdev *netdev;
89 struct list node; /* Element in datapath.ports. */
90 unsigned long long int rx_packets, tx_packets;
91 unsigned long long int rx_bytes, tx_bytes;
92 unsigned long long int tx_dropped;
95 /* The origin of a received OpenFlow message, to enable sending a reply. */
97 struct remote *remote; /* The device that sent the message. */
98 uint32_t xid; /* The OpenFlow transaction ID. */
101 /* A connection to a controller or a management device. */
105 #define TXQ_LIMIT 128 /* Max number of packets to queue for tx. */
106 int n_txq; /* Number of packets queued for tx on rconn. */
108 /* Support for reliable, multi-message replies to requests.
110 * If an incoming request needs to have a reliable reply that might
111 * require multiple messages, it can use remote_start_dump() to set up
112 * a callback that will be called as buffer space for replies. */
113 int (*cb_dump)(struct datapath *, void *aux);
114 void (*cb_done)(void *aux);
119 /* Remote connections. */
120 struct remote *controller; /* Connection to controller. */
121 struct list remotes; /* All connections (including controller). */
122 struct pvconn *listen_pvconn;
126 /* Unique identifier for this datapath */
129 struct sw_chain *chain; /* Forwarding rules. */
131 /* Configuration set from controller. */
133 uint16_t miss_send_len;
136 struct sw_port ports[OFPP_MAX];
137 struct list port_list; /* List of ports, for flooding. */
140 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
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 ofpbuf *,
148 int in_port, int out_port, bool ignore_no_fwd);
149 void dp_update_port_flags(struct datapath *dp, const struct ofp_port_mod *opm);
150 void dp_output_control(struct datapath *, struct ofpbuf *, 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 ofpbuf *,
158 int in_port, struct sw_flow_key *,
159 const struct ofp_action *, int n_actions,
161 static void modify_vlan_tci(struct ofpbuf *buffer, struct sw_flow_key *key,
162 uint16_t tci, uint16_t mask);
163 static void strip_vlan(struct ofpbuf *buffer);
164 static void modify_nh(struct ofpbuf *buffer, uint16_t eth_proto,
165 uint8_t nw_proto, const struct ofp_action *a);
166 static void modify_th(struct ofpbuf *buffer, uint16_t eth_proto,
167 uint8_t nw_proto, const struct ofp_action *a);
169 /* Buffers are identified to userspace by a 31-bit opaque ID. We divide the ID
170 * into a buffer number (low bits) and a cookie (high bits). The buffer number
171 * is an index into an array of buffers. The cookie distinguishes between
172 * different packets that have occupied a single buffer. Thus, the more
173 * buffers we have, the lower-quality the cookie... */
174 #define PKT_BUFFER_BITS 8
175 #define N_PKT_BUFFERS (1 << PKT_BUFFER_BITS)
176 #define PKT_BUFFER_MASK (N_PKT_BUFFERS - 1)
178 #define PKT_COOKIE_BITS (32 - PKT_BUFFER_BITS)
180 int run_flow_through_tables(struct datapath *, struct ofpbuf *,
182 void fwd_port_input(struct datapath *, struct ofpbuf *, struct sw_port *);
183 int fwd_control_input(struct datapath *, const struct sender *,
184 const void *, size_t);
186 uint32_t save_buffer(struct ofpbuf *);
187 static struct ofpbuf *retrieve_buffer(uint32_t id);
188 static void discard_buffer(uint32_t id);
190 static int port_no(struct datapath *dp, struct sw_port *p)
192 assert(p >= dp->ports && p < &dp->ports[ARRAY_SIZE(dp->ports)]);
193 return p - dp->ports;
196 /* Generates and returns a random datapath id. */
198 gen_datapath_id(void)
200 uint8_t ea[ETH_ADDR_LEN];
202 return eth_addr_to_uint64(ea);
206 dp_new(struct datapath **dp_, uint64_t dpid, struct rconn *rconn)
210 dp = calloc(1, sizeof *dp);
215 dp->last_timeout = time_now();
216 list_init(&dp->remotes);
217 dp->controller = remote_create(dp, rconn);
218 dp->listen_pvconn = NULL;
219 dp->id = dpid <= UINT64_C(0xffffffffffff) ? dpid : gen_datapath_id();
220 dp->chain = chain_create();
222 VLOG_ERR("could not create chain");
227 list_init(&dp->port_list);
229 dp->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
235 dp_add_port(struct datapath *dp, const char *name)
237 struct netdev *netdev;
243 error = netdev_open(name, NETDEV_ETH_TYPE_ANY, &netdev);
247 error = netdev_set_flags(netdev, NETDEV_UP | NETDEV_PROMISC, false);
249 VLOG_ERR("couldn't set promiscuous mode on %s device", name);
250 netdev_close(netdev);
253 if (netdev_get_in4(netdev, &in4)) {
254 VLOG_ERR("%s device has assigned IP address %s", name, inet_ntoa(in4));
256 if (netdev_get_in6(netdev, &in6)) {
257 char in6_name[INET6_ADDRSTRLEN + 1];
258 inet_ntop(AF_INET6, &in6, in6_name, sizeof in6_name);
259 VLOG_ERR("%s device has assigned IPv6 address %s", name, in6_name);
262 for (p = dp->ports; ; p++) {
263 if (p >= &dp->ports[ARRAY_SIZE(dp->ports)]) {
265 } else if (!p->netdev) {
270 memset(p, '\0', sizeof *p);
274 list_push_back(&dp->port_list, &p->node);
276 /* Notify the ctlpath that this port has been added */
277 send_port_status(p, OFPPR_ADD);
283 dp_add_listen_pvconn(struct datapath *dp, struct pvconn *listen_pvconn)
285 assert(!dp->listen_pvconn);
286 dp->listen_pvconn = listen_pvconn;
290 dp_run(struct datapath *dp)
292 time_t now = time_now();
293 struct sw_port *p, *pn;
294 struct remote *r, *rn;
295 struct ofpbuf *buffer = NULL;
297 if (now != dp->last_timeout) {
298 struct list deleted = LIST_INITIALIZER(&deleted);
299 struct sw_flow *f, *n;
301 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
302 if (update_port_status(p)) {
303 send_port_status(p, OFPPR_MODIFY);
307 chain_timeout(dp->chain, &deleted);
308 LIST_FOR_EACH_SAFE (f, n, struct sw_flow, node, &deleted) {
309 send_flow_expired(dp, f, f->reason);
310 list_remove(&f->node);
313 dp->last_timeout = now;
315 poll_timer_wait(1000);
317 LIST_FOR_EACH_SAFE (p, pn, struct sw_port, node, &dp->port_list) {
321 /* Allocate buffer with some headroom to add headers in forwarding
322 * to the controller or adding a vlan tag, plus an extra 2 bytes to
323 * allow IP headers to be aligned on a 4-byte boundary. */
324 const int headroom = 128 + 2;
325 const int hard_header = VLAN_ETH_HEADER_LEN;
326 const int mtu = netdev_get_mtu(p->netdev);
327 buffer = ofpbuf_new(headroom + hard_header + mtu);
328 buffer->data = (char*)buffer->data + headroom;
330 error = netdev_recv(p->netdev, buffer);
333 p->rx_bytes += buffer->size;
334 fwd_port_input(dp, buffer, p);
336 } else if (error != EAGAIN) {
337 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
338 netdev_get_name(p->netdev), strerror(error));
341 ofpbuf_delete(buffer);
343 /* Talk to remotes. */
344 LIST_FOR_EACH_SAFE (r, rn, struct remote, node, &dp->remotes) {
347 if (dp->listen_pvconn) {
349 struct vconn *new_vconn;
352 retval = pvconn_accept(dp->listen_pvconn, OFP_VERSION, &new_vconn);
354 if (retval != EAGAIN) {
355 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
359 remote_create(dp, rconn_new_from_vconn("passive", new_vconn));
365 remote_run(struct datapath *dp, struct remote *r)
371 /* Do some remote processing, but cap it at a reasonable amount so that
372 * other processing doesn't starve. */
373 for (i = 0; i < 50; i++) {
375 struct ofpbuf *buffer;
376 struct ofp_header *oh;
378 buffer = rconn_recv(r->rconn);
383 if (buffer->size >= sizeof *oh) {
384 struct sender sender;
388 sender.xid = oh->xid;
389 fwd_control_input(dp, &sender, buffer->data, buffer->size);
391 VLOG_WARN_RL(&rl, "received too-short OpenFlow message");
393 ofpbuf_delete(buffer);
395 if (r->n_txq < TXQ_LIMIT) {
396 int error = r->cb_dump(dp, r->cb_aux);
399 VLOG_WARN_RL(&rl, "dump callback error: %s",
402 r->cb_done(r->cb_aux);
411 if (!rconn_is_alive(r->rconn)) {
417 remote_wait(struct remote *r)
419 rconn_run_wait(r->rconn);
420 rconn_recv_wait(r->rconn);
424 remote_destroy(struct remote *r)
427 if (r->cb_dump && r->cb_done) {
428 r->cb_done(r->cb_aux);
430 list_remove(&r->node);
431 rconn_destroy(r->rconn);
436 static struct remote *
437 remote_create(struct datapath *dp, struct rconn *rconn)
439 struct remote *remote = xmalloc(sizeof *remote);
440 list_push_back(&dp->remotes, &remote->node);
441 remote->rconn = rconn;
442 remote->cb_dump = NULL;
447 /* Starts a callback-based, reliable, possibly multi-message reply to a
448 * request made by 'remote'.
450 * 'dump' designates a function that will be called when the 'remote' send
451 * queue has an empty slot. It should compose a message and send it on
452 * 'remote'. On success, it should return 1 if it should be called again when
453 * another send queue slot opens up, 0 if its transmissions are complete, or a
454 * negative errno value on failure.
456 * 'done' designates a function to clean up any resources allocated for the
457 * dump. It must handle being called before the dump is complete (which will
458 * happen if 'remote' is closed unexpectedly).
460 * 'aux' is passed to 'dump' and 'done'. */
462 remote_start_dump(struct remote *remote,
463 int (*dump)(struct datapath *, void *),
464 void (*done)(void *),
467 assert(!remote->cb_dump);
468 remote->cb_dump = dump;
469 remote->cb_done = done;
470 remote->cb_aux = aux;
474 dp_wait(struct datapath *dp)
479 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
480 netdev_recv_wait(p->netdev);
482 LIST_FOR_EACH (r, struct remote, node, &dp->remotes) {
485 if (dp->listen_pvconn) {
486 pvconn_wait(dp->listen_pvconn);
490 /* Delete 'p' from switch. */
492 del_switch_port(struct sw_port *p)
494 send_port_status(p, OFPPR_DELETE);
495 netdev_close(p->netdev);
497 list_remove(&p->node);
501 dp_destroy(struct datapath *dp)
503 struct sw_port *p, *n;
509 LIST_FOR_EACH_SAFE (p, n, struct sw_port, node, &dp->port_list) {
512 chain_destroy(dp->chain);
516 /* Send packets out all the ports except the originating one. If the
517 * "flood" argument is set, don't send out ports with flooding disabled.
520 output_all(struct datapath *dp, struct ofpbuf *buffer, int in_port, int flood)
526 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
527 if (port_no(dp, p) == in_port) {
530 if (flood && p->config & OFPPC_NO_FLOOD) {
533 if (prev_port != -1) {
534 dp_output_port(dp, ofpbuf_clone(buffer), in_port, prev_port,
537 prev_port = port_no(dp, p);
540 dp_output_port(dp, buffer, in_port, prev_port, false);
542 ofpbuf_delete(buffer);
548 output_packet(struct datapath *dp, struct ofpbuf *buffer, int out_port)
550 if (out_port >= 0 && out_port < OFPP_MAX) {
551 struct sw_port *p = &dp->ports[out_port];
552 if (p->netdev != NULL && !(p->config & OFPPC_PORT_DOWN)) {
553 if (!netdev_send(p->netdev, buffer)) {
555 p->tx_bytes += buffer->size;
563 ofpbuf_delete(buffer);
564 VLOG_DBG_RL(&rl, "can't forward to bad port %d\n", out_port);
567 /* Takes ownership of 'buffer' and transmits it to 'out_port' on 'dp'.
570 dp_output_port(struct datapath *dp, struct ofpbuf *buffer,
571 int in_port, int out_port, bool ignore_no_fwd)
575 if (out_port == OFPP_FLOOD) {
576 output_all(dp, buffer, in_port, 1);
577 } else if (out_port == OFPP_ALL) {
578 output_all(dp, buffer, in_port, 0);
579 } else if (out_port == OFPP_CONTROLLER) {
580 dp_output_control(dp, buffer, in_port, 0, OFPR_ACTION);
581 } else if (out_port == OFPP_IN_PORT) {
582 output_packet(dp, buffer, in_port);
583 } else if (out_port == OFPP_TABLE) {
584 struct sw_port *p = in_port < OFPP_MAX ? &dp->ports[in_port] : 0;
585 if (run_flow_through_tables(dp, buffer, p)) {
586 ofpbuf_delete(buffer);
589 if (in_port == out_port) {
590 VLOG_DBG_RL(&rl, "can't directly forward to input port");
593 output_packet(dp, buffer, out_port);
598 make_openflow_reply(size_t openflow_len, uint8_t type,
599 const struct sender *sender, struct ofpbuf **bufferp)
601 return make_openflow_xid(openflow_len, type, sender ? sender->xid : 0,
606 send_openflow_buffer(struct datapath *dp, struct ofpbuf *buffer,
607 const struct sender *sender)
609 struct remote *remote = sender ? sender->remote : dp->controller;
610 struct rconn *rconn = remote->rconn;
613 update_openflow_length(buffer);
614 retval = rconn_send_with_limit(rconn, buffer, &remote->n_txq, TXQ_LIMIT);
616 VLOG_WARN_RL(&rl, "send to %s failed: %s",
617 rconn_get_name(rconn), strerror(retval));
622 /* Takes ownership of 'buffer' and transmits it to 'dp''s controller. If the
623 * packet can be saved in a buffer, then only the first max_len bytes of
624 * 'buffer' are sent; otherwise, all of 'buffer' is sent. 'reason' indicates
625 * why 'buffer' is being sent. 'max_len' sets the maximum number of bytes that
626 * the caller wants to be sent; a value of 0 indicates the entire packet should
629 dp_output_control(struct datapath *dp, struct ofpbuf *buffer, int in_port,
630 size_t max_len, int reason)
632 struct ofp_packet_in *opi;
636 buffer_id = save_buffer(buffer);
637 total_len = buffer->size;
638 if (buffer_id != UINT32_MAX && max_len && buffer->size > max_len) {
639 buffer->size = max_len;
642 opi = ofpbuf_push_uninit(buffer, offsetof(struct ofp_packet_in, data));
643 opi->header.version = OFP_VERSION;
644 opi->header.type = OFPT_PACKET_IN;
645 opi->header.length = htons(buffer->size);
646 opi->header.xid = htonl(0);
647 opi->buffer_id = htonl(buffer_id);
648 opi->total_len = htons(total_len);
649 opi->in_port = htons(in_port);
650 opi->reason = reason;
652 send_openflow_buffer(dp, buffer, NULL);
655 static void fill_port_desc(struct datapath *dp, struct sw_port *p,
656 struct ofp_phy_port *desc)
658 desc->port_no = htons(port_no(dp, p));
659 strncpy((char *) desc->name, netdev_get_name(p->netdev),
661 desc->name[sizeof desc->name - 1] = '\0';
662 memcpy(desc->hw_addr, netdev_get_etheraddr(p->netdev), ETH_ADDR_LEN);
663 desc->config = htonl(p->config);
664 desc->state = htonl(p->state);
665 desc->curr = htonl(netdev_get_features(p->netdev, NETDEV_FEAT_CURRENT));
666 desc->supported = htonl(netdev_get_features(p->netdev,
667 NETDEV_FEAT_SUPPORTED));
668 desc->advertised = htonl(netdev_get_features(p->netdev,
669 NETDEV_FEAT_ADVERTISED));
670 desc->peer = htonl(netdev_get_features(p->netdev, NETDEV_FEAT_PEER));
674 dp_send_features_reply(struct datapath *dp, const struct sender *sender)
676 struct ofpbuf *buffer;
677 struct ofp_switch_features *ofr;
680 ofr = make_openflow_reply(sizeof *ofr, OFPT_FEATURES_REPLY,
682 ofr->datapath_id = htonll(dp->id);
683 ofr->n_tables = dp->chain->n_tables;
684 ofr->n_buffers = htonl(N_PKT_BUFFERS);
685 ofr->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
686 ofr->actions = htonl(OFP_SUPPORTED_ACTIONS);
687 LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
688 struct ofp_phy_port *opp = ofpbuf_put_uninit(buffer, sizeof *opp);
689 memset(opp, 0, sizeof *opp);
690 fill_port_desc(dp, p, opp);
692 send_openflow_buffer(dp, buffer, sender);
696 dp_update_port_flags(struct datapath *dp, const struct ofp_port_mod *opm)
698 int port_no = ntohs(opm->port_no);
699 if (port_no < OFPP_MAX) {
700 struct sw_port *p = &dp->ports[port_no];
702 /* Make sure the port id hasn't changed since this was sent */
703 if (!p || memcmp(opm->hw_addr, netdev_get_etheraddr(p->netdev),
704 ETH_ADDR_LEN) != 0) {
710 uint32_t config_mask = ntohl(opm->mask);
711 p->config &= ~config_mask;
712 p->config |= ntohl(opm->config) & config_mask;
715 if (opm->mask & htonl(OFPPC_PORT_DOWN)) {
716 if ((opm->config & htonl(OFPPC_PORT_DOWN))
717 && (p->config & OFPPC_PORT_DOWN) == 0) {
718 p->config |= OFPPC_PORT_DOWN;
719 netdev_turn_flags_off(p->netdev, NETDEV_UP, true);
720 } else if ((opm->config & htonl(OFPPC_PORT_DOWN)) == 0
721 && (p->config & OFPPC_PORT_DOWN)) {
722 p->config &= ~OFPPC_PORT_DOWN;
723 netdev_turn_flags_on(p->netdev, NETDEV_UP, true);
729 /* Update the port status field of the bridge port. A non-zero return
730 * value indicates some field has changed.
732 * NB: Callers of this function may hold the RCU read lock, so any
733 * additional checks must not sleep.
736 update_port_status(struct sw_port *p)
739 enum netdev_flags flags;
740 uint32_t orig_config = p->config;
741 uint32_t orig_state = p->state;
743 if (netdev_get_flags(p->netdev, &flags) < 0) {
744 VLOG_WARN_RL(&rl, "could not get netdev flags for %s",
745 netdev_get_name(p->netdev));
748 if (flags & NETDEV_UP) {
749 p->config &= ~OFPPC_PORT_DOWN;
751 p->config |= OFPPC_PORT_DOWN;
755 /* Not all cards support this getting link status, so don't warn on
757 retval = netdev_get_link_status(p->netdev);
759 p->state &= ~OFPPS_LINK_DOWN;
760 } else if (retval == 0) {
761 p->state |= OFPPS_LINK_DOWN;
764 return ((orig_config != p->config) || (orig_state != p->state));
768 send_port_status(struct sw_port *p, uint8_t status)
770 struct ofpbuf *buffer;
771 struct ofp_port_status *ops;
772 ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &buffer);
773 ops->reason = status;
774 memset(ops->pad, 0, sizeof ops->pad);
775 fill_port_desc(p->dp, p, &ops->desc);
777 send_openflow_buffer(p->dp, buffer, NULL);
781 send_flow_expired(struct datapath *dp, struct sw_flow *flow,
782 enum ofp_flow_expired_reason reason)
784 struct ofpbuf *buffer;
785 struct ofp_flow_expired *ofe;
786 ofe = make_openflow_xid(sizeof *ofe, OFPT_FLOW_EXPIRED, 0, &buffer);
787 flow_fill_match(&ofe->match, &flow->key);
789 ofe->priority = htons(flow->priority);
790 ofe->reason = reason;
791 memset(ofe->pad, 0, sizeof ofe->pad);
793 ofe->duration = htonl(time_now() - flow->created);
794 memset(ofe->pad2, 0, sizeof ofe->pad2);
795 ofe->packet_count = htonll(flow->packet_count);
796 ofe->byte_count = htonll(flow->byte_count);
797 send_openflow_buffer(dp, buffer, NULL);
801 dp_send_error_msg(struct datapath *dp, const struct sender *sender,
802 uint16_t type, uint16_t code, const void *data, size_t len)
804 struct ofpbuf *buffer;
805 struct ofp_error_msg *oem;
806 oem = make_openflow_reply(sizeof(*oem)+len, OFPT_ERROR, sender, &buffer);
807 oem->type = htons(type);
808 oem->code = htons(code);
809 memcpy(oem->data, data, len);
810 send_openflow_buffer(dp, buffer, sender);
814 fill_flow_stats(struct ofpbuf *buffer, struct sw_flow *flow,
815 int table_idx, time_t now)
817 struct ofp_flow_stats *ofs;
818 int length = sizeof *ofs + sizeof *ofs->actions * flow->sf_acts->n_actions;
819 ofs = ofpbuf_put_uninit(buffer, length);
820 ofs->length = htons(length);
821 ofs->table_id = table_idx;
823 ofs->match.wildcards = htonl(flow->key.wildcards);
824 ofs->match.in_port = flow->key.flow.in_port;
825 memcpy(ofs->match.dl_src, flow->key.flow.dl_src, ETH_ADDR_LEN);
826 memcpy(ofs->match.dl_dst, flow->key.flow.dl_dst, ETH_ADDR_LEN);
827 ofs->match.dl_vlan = flow->key.flow.dl_vlan;
828 ofs->match.dl_type = flow->key.flow.dl_type;
829 ofs->match.nw_src = flow->key.flow.nw_src;
830 ofs->match.nw_dst = flow->key.flow.nw_dst;
831 ofs->match.nw_proto = flow->key.flow.nw_proto;
833 ofs->match.tp_src = flow->key.flow.tp_src;
834 ofs->match.tp_dst = flow->key.flow.tp_dst;
835 ofs->duration = htonl(now - flow->created);
836 ofs->priority = htons(flow->priority);
837 ofs->idle_timeout = htons(flow->idle_timeout);
838 ofs->hard_timeout = htons(flow->hard_timeout);
839 memset(ofs->pad2, 0, sizeof ofs->pad2);
840 ofs->packet_count = htonll(flow->packet_count);
841 ofs->byte_count = htonll(flow->byte_count);
842 memcpy(ofs->actions, flow->sf_acts->actions,
843 sizeof *ofs->actions * flow->sf_acts->n_actions);
847 /* 'buffer' was received on 'p', which may be a a physical switch port or a
848 * null pointer. Process it according to 'dp''s flow table. Returns 0 if
849 * successful, in which case 'buffer' is destroyed, or -ESRCH if there is no
850 * matching flow, in which case 'buffer' still belongs to the caller. */
851 int run_flow_through_tables(struct datapath *dp, struct ofpbuf *buffer,
854 struct sw_flow_key key;
855 struct sw_flow *flow;
858 if (flow_extract(buffer, p ? port_no(dp, p) : OFPP_NONE, &key.flow)
859 && (dp->flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP) {
861 ofpbuf_delete(buffer);
864 if (p && p->config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP)
865 && p->config & (!eth_addr_equals(key.flow.dl_dst, stp_eth_addr)
866 ? OFPPC_NO_RECV : OFPPC_NO_RECV_STP)) {
867 ofpbuf_delete(buffer);
871 flow = chain_lookup(dp->chain, &key);
873 flow_used(flow, buffer);
874 execute_actions(dp, buffer, port_no(dp, p),
875 &key, flow->sf_acts->actions,
876 flow->sf_acts->n_actions, false);
883 /* 'buffer' was received on 'p', which may be a a physical switch port or a
884 * null pointer. Process it according to 'dp''s flow table, sending it up to
885 * the controller if no flow matches. Takes ownership of 'buffer'. */
886 void fwd_port_input(struct datapath *dp, struct ofpbuf *buffer,
889 if (run_flow_through_tables(dp, buffer, p)) {
890 dp_output_control(dp, buffer, port_no(dp, p),
891 dp->miss_send_len, OFPR_NO_MATCH);
896 do_output(struct datapath *dp, struct ofpbuf *buffer, int in_port,
897 size_t max_len, int out_port, bool ignore_no_fwd)
899 if (out_port != OFPP_CONTROLLER) {
900 dp_output_port(dp, buffer, in_port, out_port, ignore_no_fwd);
902 dp_output_control(dp, buffer, in_port, max_len, OFPR_ACTION);
907 execute_actions(struct datapath *dp, struct ofpbuf *buffer,
908 int in_port, struct sw_flow_key *key,
909 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,
934 switch (ntohs(a->type)) {
936 prev_port = ntohs(a->arg.output.port);
937 max_len = ntohs(a->arg.output.max_len);
940 case OFPAT_SET_VLAN_VID: {
941 uint16_t tci = ntohs(a->arg.vlan_vid);
942 modify_vlan_tci(buffer, key, tci, VLAN_VID_MASK);
946 case OFPAT_SET_VLAN_PCP: {
947 uint16_t tci = (uint16_t)a->arg.vlan_pcp << 13;
948 modify_vlan_tci(buffer, key, tci, VLAN_PCP_MASK);
952 case OFPAT_STRIP_VLAN:
954 key->flow.dl_vlan = htons(OFP_VLAN_NONE);
957 case OFPAT_SET_DL_SRC:
958 memcpy(eh->eth_src, a->arg.dl_addr, sizeof eh->eth_src);
961 case OFPAT_SET_DL_DST:
962 memcpy(eh->eth_dst, a->arg.dl_addr, sizeof eh->eth_dst);
965 case OFPAT_SET_NW_SRC:
966 case OFPAT_SET_NW_DST:
967 modify_nh(buffer, eth_proto, key->flow.nw_proto, a);
970 case OFPAT_SET_TP_SRC:
971 case OFPAT_SET_TP_DST:
972 modify_th(buffer, eth_proto, key->flow.nw_proto, a);
980 do_output(dp, buffer, in_port, max_len, prev_port, ignore_no_fwd);
982 ofpbuf_delete(buffer);
985 static void modify_nh(struct ofpbuf *buffer, uint16_t eth_proto,
986 uint8_t nw_proto, const struct ofp_action *a)
988 if (eth_proto == ETH_TYPE_IP) {
989 struct ip_header *nh = buffer->l3;
990 uint32_t new, *field;
992 new = a->arg.nw_addr;
993 field = a->type == OFPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
994 if (nw_proto == IP_TYPE_TCP) {
995 struct tcp_header *th = buffer->l4;
996 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, new);
997 } else if (nw_proto == IP_TYPE_UDP) {
998 struct udp_header *th = buffer->l4;
1000 th->udp_csum = recalc_csum32(th->udp_csum, *field, new);
1001 if (!th->udp_csum) {
1002 th->udp_csum = 0xffff;
1006 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, new);
1011 static void modify_th(struct ofpbuf *buffer, uint16_t eth_proto,
1012 uint8_t nw_proto, const struct ofp_action *a)
1014 if (eth_proto == ETH_TYPE_IP) {
1015 uint16_t new, *field;
1019 if (nw_proto == IP_TYPE_TCP) {
1020 struct tcp_header *th = buffer->l4;
1021 field = a->type == OFPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1022 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, new);
1024 } else if (nw_proto == IP_TYPE_UDP) {
1025 struct udp_header *th = buffer->l4;
1026 field = a->type == OFPAT_SET_TP_SRC ? &th->udp_src : &th->udp_dst;
1027 th->udp_csum = recalc_csum16(th->udp_csum, *field, new);
1033 /* Modify vlan tag control information (TCI). Only sets the TCI bits
1034 * indicated by 'mask'. If no vlan tag is present, one is added.
1037 modify_vlan_tci(struct ofpbuf *buffer, struct sw_flow_key *key,
1038 uint16_t tci, uint16_t mask)
1040 struct vlan_eth_header *veh;
1042 if (key->flow.dl_vlan != htons(OFP_VLAN_NONE)) {
1043 /* Modify vlan id, but maintain other TCI values */
1045 veh->veth_tci &= ~htons(mask);
1046 veh->veth_tci |= htons(tci);
1048 /* Insert new vlan id. */
1049 struct eth_header *eh = buffer->l2;
1050 struct vlan_eth_header tmp;
1051 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1052 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1053 tmp.veth_type = htons(ETH_TYPE_VLAN);
1054 tmp.veth_tci = htons(tci);
1055 tmp.veth_next_type = eh->eth_type;
1057 veh = ofpbuf_push_uninit(buffer, VLAN_HEADER_LEN);
1058 memcpy(veh, &tmp, sizeof tmp);
1059 buffer->l2 = (char*)buffer->l2 - VLAN_HEADER_LEN;
1062 key->flow.dl_vlan = veh->veth_tci & htons(VLAN_VID_MASK);
1065 /* Remove an existing vlan header if it exists. */
1067 strip_vlan(struct ofpbuf *buffer)
1069 struct vlan_eth_header *veh = buffer->l2;
1071 if (veh->veth_type == htons(ETH_TYPE_VLAN)) {
1072 struct eth_header tmp;
1074 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1075 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1076 tmp.eth_type = veh->veth_next_type;
1078 buffer->size -= VLAN_HEADER_LEN;
1079 buffer->data = (char*)buffer->data + VLAN_HEADER_LEN;
1080 buffer->l2 = (char*)buffer->l2 + VLAN_HEADER_LEN;
1081 memcpy(buffer->data, &tmp, sizeof tmp);
1086 recv_features_request(struct datapath *dp, const struct sender *sender,
1089 dp_send_features_reply(dp, sender);
1094 recv_get_config_request(struct datapath *dp, const struct sender *sender,
1097 struct ofpbuf *buffer;
1098 struct ofp_switch_config *osc;
1100 osc = make_openflow_reply(sizeof *osc, OFPT_GET_CONFIG_REPLY,
1103 osc->flags = htons(dp->flags);
1104 osc->miss_send_len = htons(dp->miss_send_len);
1106 return send_openflow_buffer(dp, buffer, sender);
1110 recv_set_config(struct datapath *dp, const struct sender *sender UNUSED,
1113 const struct ofp_switch_config *osc = msg;
1116 flags = ntohs(osc->flags) & (OFPC_SEND_FLOW_EXP | OFPC_FRAG_MASK);
1117 if ((flags & OFPC_FRAG_MASK) != OFPC_FRAG_NORMAL
1118 && (flags & OFPC_FRAG_MASK) != OFPC_FRAG_DROP) {
1119 flags = (flags & ~OFPC_FRAG_MASK) | OFPC_FRAG_DROP;
1122 dp->miss_send_len = ntohs(osc->miss_send_len);
1127 recv_packet_out(struct datapath *dp, const struct sender *sender UNUSED,
1130 const struct ofp_packet_out *opo = msg;
1131 struct sw_flow_key key;
1132 struct ofpbuf *buffer;
1133 int n_actions = ntohs(opo->n_actions);
1134 int act_len = n_actions * sizeof opo->actions[0];
1136 if (act_len > (ntohs(opo->header.length) - sizeof *opo)) {
1137 VLOG_DBG_RL(&rl, "message too short for number of actions");
1141 if (ntohl(opo->buffer_id) == (uint32_t) -1) {
1142 /* FIXME: can we avoid copying data here? */
1143 int data_len = ntohs(opo->header.length) - sizeof *opo - act_len;
1144 buffer = ofpbuf_new(data_len);
1145 ofpbuf_put(buffer, &opo->actions[n_actions], data_len);
1147 buffer = retrieve_buffer(ntohl(opo->buffer_id));
1153 flow_extract(buffer, ntohs(opo->in_port), &key.flow);
1154 execute_actions(dp, buffer, ntohs(opo->in_port),
1155 &key, opo->actions, n_actions, true);
1161 recv_port_mod(struct datapath *dp, const struct sender *sender UNUSED,
1164 const struct ofp_port_mod *opm = msg;
1166 dp_update_port_flags(dp, opm);
1172 add_flow(struct datapath *dp, const struct ofp_flow_mod *ofm)
1174 int error = -ENOMEM;
1177 struct sw_flow *flow;
1180 /* To prevent loops, make sure there's no action to send to the
1181 * OFP_TABLE virtual port.
1183 n_actions = (ntohs(ofm->header.length) - sizeof *ofm)
1184 / sizeof *ofm->actions;
1185 for (i=0; i<n_actions; i++) {
1186 const struct ofp_action *a = &ofm->actions[i];
1188 if (a->type == htons(OFPAT_OUTPUT)
1189 && (a->arg.output.port == htons(OFPP_TABLE)
1190 || a->arg.output.port == htons(OFPP_NONE)
1191 || a->arg.output.port == ofm->match.in_port)) {
1192 /* xxx Send fancy new error message? */
1197 /* Allocate memory. */
1198 flow = flow_alloc(n_actions);
1202 /* Fill out flow. */
1203 flow_extract_match(&flow->key, &ofm->match);
1204 flow->priority = flow->key.wildcards ? ntohs(ofm->priority) : -1;
1205 flow->idle_timeout = ntohs(ofm->idle_timeout);
1206 flow->hard_timeout = ntohs(ofm->hard_timeout);
1207 flow->used = flow->created = time_now();
1208 flow->sf_acts->n_actions = n_actions;
1209 flow->byte_count = 0;
1210 flow->packet_count = 0;
1211 memcpy(flow->sf_acts->actions, ofm->actions,
1212 n_actions * sizeof *flow->sf_acts->actions);
1215 error = chain_insert(dp->chain, flow);
1217 goto error_free_flow;
1220 if (ntohl(ofm->buffer_id) != UINT32_MAX) {
1221 struct ofpbuf *buffer = retrieve_buffer(ntohl(ofm->buffer_id));
1223 struct sw_flow_key key;
1224 uint16_t in_port = ntohs(ofm->match.in_port);
1225 flow_used(flow, buffer);
1226 flow_extract(buffer, in_port, &key.flow);
1227 execute_actions(dp, buffer, in_port, &key,
1228 ofm->actions, n_actions, false);
1238 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
1239 discard_buffer(ntohl(ofm->buffer_id));
1244 mod_flow(struct datapath *dp, const struct ofp_flow_mod *ofm)
1246 int error = -ENOMEM;
1249 struct sw_flow_key key;
1254 /* To prevent loops, make sure there's no action to send to the
1255 * OFP_TABLE virtual port.
1257 n_actions = (ntohs(ofm->header.length) - sizeof *ofm)
1258 / sizeof *ofm->actions;
1259 for (i=0; i<n_actions; i++) {
1260 const struct ofp_action *a = &ofm->actions[i];
1262 if (a->type == htons(OFPAT_OUTPUT)
1263 && (a->arg.output.port == htons(OFPP_TABLE)
1264 || a->arg.output.port == htons(OFPP_NONE)
1265 || a->arg.output.port == ofm->match.in_port)) {
1266 /* xxx Send fancy new error message? */
1271 flow_extract_match(&key, &ofm->match);
1272 priority = key.wildcards ? ntohs(ofm->priority) : -1;
1273 strict = (ofm->command == htons(OFPFC_MODIFY_STRICT)) ? 1 : 0;
1274 chain_modify(dp->chain, &key, priority, strict, ofm->actions, n_actions);
1276 if (ntohl(ofm->buffer_id) != UINT32_MAX) {
1277 struct ofpbuf *buffer = retrieve_buffer(ntohl(ofm->buffer_id));
1279 struct sw_flow_key skb_key;
1280 uint16_t in_port = ntohs(ofm->match.in_port);
1281 flow_extract(buffer, in_port, &skb_key.flow);
1282 execute_actions(dp, buffer, in_port, &skb_key,
1283 ofm->actions, n_actions, false);
1291 if (ntohl(ofm->buffer_id) != (uint32_t) -1)
1292 discard_buffer(ntohl(ofm->buffer_id));
1297 recv_flow(struct datapath *dp, const struct sender *sender UNUSED,
1300 const struct ofp_flow_mod *ofm = msg;
1301 uint16_t command = ntohs(ofm->command);
1303 if (command == OFPFC_ADD) {
1304 return add_flow(dp, ofm);
1305 } else if ((command == OFPFC_MODIFY) || (command == OFPFC_MODIFY_STRICT)) {
1306 return mod_flow(dp, ofm);
1307 } else if (command == OFPFC_DELETE) {
1308 struct sw_flow_key key;
1309 flow_extract_match(&key, &ofm->match);
1310 return chain_delete(dp->chain, &key, 0, 0) ? 0 : -ESRCH;
1311 } else if (command == OFPFC_DELETE_STRICT) {
1312 struct sw_flow_key key;
1314 flow_extract_match(&key, &ofm->match);
1315 priority = key.wildcards ? ntohs(ofm->priority) : -1;
1316 return chain_delete(dp->chain, &key, priority, 1) ? 0 : -ESRCH;
1322 static int desc_stats_dump(struct datapath *dp, void *state,
1323 struct ofpbuf *buffer)
1325 struct ofp_desc_stats *ods = ofpbuf_put_uninit(buffer, sizeof *ods);
1327 strncpy(ods->mfr_desc, &mfr_desc, sizeof ods->mfr_desc);
1328 strncpy(ods->hw_desc, &hw_desc, sizeof ods->hw_desc);
1329 strncpy(ods->sw_desc, &sw_desc, sizeof ods->sw_desc);
1330 strncpy(ods->serial_num, &serial_num, sizeof ods->serial_num);
1335 struct flow_stats_state {
1337 struct sw_table_position position;
1338 struct ofp_flow_stats_request rq;
1341 struct ofpbuf *buffer;
1344 #define MAX_FLOW_STATS_BYTES 4096
1346 static int flow_stats_init(struct datapath *dp, const void *body, int body_len,
1349 const struct ofp_flow_stats_request *fsr = body;
1350 struct flow_stats_state *s = xmalloc(sizeof *s);
1351 s->table_idx = fsr->table_id == 0xff ? 0 : fsr->table_id;
1352 memset(&s->position, 0, sizeof s->position);
1358 static int flow_stats_dump_callback(struct sw_flow *flow, void *private)
1360 struct flow_stats_state *s = private;
1361 fill_flow_stats(s->buffer, flow, s->table_idx, s->now);
1362 return s->buffer->size >= MAX_FLOW_STATS_BYTES;
1365 static int flow_stats_dump(struct datapath *dp, void *state,
1366 struct ofpbuf *buffer)
1368 struct flow_stats_state *s = state;
1369 struct sw_flow_key match_key;
1371 flow_extract_match(&match_key, &s->rq.match);
1373 s->now = time_now();
1374 while (s->table_idx < dp->chain->n_tables
1375 && (s->rq.table_id == 0xff || s->rq.table_id == s->table_idx))
1377 struct sw_table *table = dp->chain->tables[s->table_idx];
1379 if (table->iterate(table, &match_key, &s->position,
1380 flow_stats_dump_callback, s))
1384 memset(&s->position, 0, sizeof s->position);
1386 return s->buffer->size >= MAX_FLOW_STATS_BYTES;
1389 static void flow_stats_done(void *state)
1394 struct aggregate_stats_state {
1395 struct ofp_aggregate_stats_request rq;
1398 static int aggregate_stats_init(struct datapath *dp,
1399 const void *body, int body_len,
1402 const struct ofp_aggregate_stats_request *rq = body;
1403 struct aggregate_stats_state *s = xmalloc(sizeof *s);
1409 static int aggregate_stats_dump_callback(struct sw_flow *flow, void *private)
1411 struct ofp_aggregate_stats_reply *rpy = private;
1412 rpy->packet_count += flow->packet_count;
1413 rpy->byte_count += flow->byte_count;
1418 static int aggregate_stats_dump(struct datapath *dp, void *state,
1419 struct ofpbuf *buffer)
1421 struct aggregate_stats_state *s = state;
1422 struct ofp_aggregate_stats_request *rq = &s->rq;
1423 struct ofp_aggregate_stats_reply *rpy;
1424 struct sw_table_position position;
1425 struct sw_flow_key match_key;
1428 rpy = ofpbuf_put_uninit(buffer, sizeof *rpy);
1429 memset(rpy, 0, sizeof *rpy);
1431 flow_extract_match(&match_key, &rq->match);
1432 table_idx = rq->table_id == 0xff ? 0 : rq->table_id;
1433 memset(&position, 0, sizeof position);
1434 while (table_idx < dp->chain->n_tables
1435 && (rq->table_id == 0xff || rq->table_id == table_idx))
1437 struct sw_table *table = dp->chain->tables[table_idx];
1440 error = table->iterate(table, &match_key, &position,
1441 aggregate_stats_dump_callback, rpy);
1446 memset(&position, 0, sizeof position);
1449 rpy->packet_count = htonll(rpy->packet_count);
1450 rpy->byte_count = htonll(rpy->byte_count);
1451 rpy->flow_count = htonl(rpy->flow_count);
1455 static void aggregate_stats_done(void *state)
1460 static int table_stats_dump(struct datapath *dp, void *state,
1461 struct ofpbuf *buffer)
1464 for (i = 0; i < dp->chain->n_tables; i++) {
1465 struct ofp_table_stats *ots = ofpbuf_put_uninit(buffer, sizeof *ots);
1466 struct sw_table_stats stats;
1467 dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
1468 strncpy(ots->name, stats.name, sizeof ots->name);
1470 ots->wildcards = htonl(stats.wildcards);
1471 memset(ots->pad, 0, sizeof ots->pad);
1472 ots->max_entries = htonl(stats.max_flows);
1473 ots->active_count = htonl(stats.n_flows);
1474 ots->lookup_count = htonll(stats.n_lookup);
1475 ots->matched_count = htonll(stats.n_matched);
1480 struct port_stats_state {
1484 static int port_stats_init(struct datapath *dp, const void *body, int body_len,
1487 struct port_stats_state *s = xmalloc(sizeof *s);
1493 static int port_stats_dump(struct datapath *dp, void *state,
1494 struct ofpbuf *buffer)
1496 struct port_stats_state *s = state;
1499 for (i = s->port; i < OFPP_MAX; i++) {
1500 struct sw_port *p = &dp->ports[i];
1501 struct ofp_port_stats *ops;
1505 ops = ofpbuf_put_uninit(buffer, sizeof *ops);
1506 ops->port_no = htons(port_no(dp, p));
1507 memset(ops->pad, 0, sizeof ops->pad);
1508 ops->rx_packets = htonll(p->rx_packets);
1509 ops->tx_packets = htonll(p->tx_packets);
1510 ops->rx_bytes = htonll(p->rx_bytes);
1511 ops->tx_bytes = htonll(p->tx_bytes);
1512 ops->rx_dropped = htonll(-1);
1513 ops->tx_dropped = htonll(p->tx_dropped);
1514 ops->rx_errors = htonll(-1);
1515 ops->tx_errors = htonll(-1);
1516 ops->rx_frame_err = htonll(-1);
1517 ops->rx_over_err = htonll(-1);
1518 ops->rx_crc_err = htonll(-1);
1519 ops->collisions = htonll(-1);
1526 static void port_stats_done(void *state)
1532 /* Value for 'type' member of struct ofp_stats_request. */
1535 /* Minimum and maximum acceptable number of bytes in body member of
1536 * struct ofp_stats_request. */
1537 size_t min_body, max_body;
1539 /* Prepares to dump some kind of statistics on 'dp'. 'body' and
1540 * 'body_len' are the 'body' member of the struct ofp_stats_request.
1541 * Returns zero if successful, otherwise a negative error code.
1542 * May initialize '*state' to state information. May be null if no
1543 * initialization is required.*/
1544 int (*init)(struct datapath *dp, const void *body, int body_len,
1547 /* Appends statistics for 'dp' to 'buffer', which initially contains a
1548 * struct ofp_stats_reply. On success, it should return 1 if it should be
1549 * called again later with another buffer, 0 if it is done, or a negative
1550 * errno value on failure. */
1551 int (*dump)(struct datapath *dp, void *state, struct ofpbuf *buffer);
1553 /* Cleans any state created by the init or dump functions. May be null
1554 * if no cleanup is required. */
1555 void (*done)(void *state);
1558 static const struct stats_type stats[] = {
1569 sizeof(struct ofp_flow_stats_request),
1570 sizeof(struct ofp_flow_stats_request),
1577 sizeof(struct ofp_aggregate_stats_request),
1578 sizeof(struct ofp_aggregate_stats_request),
1579 aggregate_stats_init,
1580 aggregate_stats_dump,
1581 aggregate_stats_done
1601 struct stats_dump_cb {
1603 struct ofp_stats_request *rq;
1604 struct sender sender;
1605 const struct stats_type *s;
1610 stats_dump(struct datapath *dp, void *cb_)
1612 struct stats_dump_cb *cb = cb_;
1613 struct ofp_stats_reply *osr;
1614 struct ofpbuf *buffer;
1621 osr = make_openflow_reply(sizeof *osr, OFPT_STATS_REPLY, &cb->sender,
1623 osr->type = htons(cb->s->type);
1626 err = cb->s->dump(dp, cb->state, buffer);
1632 /* Buffer might have been reallocated, so find our data again. */
1633 osr = ofpbuf_at_assert(buffer, 0, sizeof *osr);
1634 osr->flags = ntohs(OFPSF_REPLY_MORE);
1636 err2 = send_openflow_buffer(dp, buffer, &cb->sender);
1646 stats_done(void *cb_)
1648 struct stats_dump_cb *cb = cb_;
1651 cb->s->done(cb->state);
1658 recv_stats_request(struct datapath *dp, const struct sender *sender,
1661 const struct ofp_stats_request *rq = oh;
1662 size_t rq_len = ntohs(rq->header.length);
1663 const struct stats_type *st;
1664 struct stats_dump_cb *cb;
1668 type = ntohs(rq->type);
1669 for (st = stats; ; st++) {
1670 if (st >= &stats[ARRAY_SIZE(stats)]) {
1671 VLOG_WARN_RL(&rl, "received stats request of unknown type %d",
1674 } else if (type == st->type) {
1679 cb = xmalloc(sizeof *cb);
1681 cb->rq = xmemdup(rq, rq_len);
1682 cb->sender = *sender;
1686 body_len = rq_len - offsetof(struct ofp_stats_request, body);
1687 if (body_len < cb->s->min_body || body_len > cb->s->max_body) {
1688 VLOG_WARN_RL(&rl, "stats request type %d with bad body length %d",
1695 err = cb->s->init(dp, rq->body, body_len, &cb->state);
1698 "failed initialization of stats request type %d: %s",
1699 type, strerror(-err));
1704 remote_start_dump(sender->remote, stats_dump, stats_done, cb);
1714 recv_echo_request(struct datapath *dp, const struct sender *sender,
1717 return send_openflow_buffer(dp, make_echo_reply(oh), sender);
1721 recv_echo_reply(struct datapath *dp UNUSED, const struct sender *sender UNUSED,
1722 const void *oh UNUSED)
1727 /* 'msg', which is 'length' bytes long, was received from the control path.
1728 * Apply it to 'chain'. */
1730 fwd_control_input(struct datapath *dp, const struct sender *sender,
1731 const void *msg, size_t length)
1733 int (*handler)(struct datapath *, const struct sender *, const void *);
1734 struct ofp_header *oh;
1737 /* Check encapsulated length. */
1738 oh = (struct ofp_header *) msg;
1739 if (ntohs(oh->length) > length) {
1742 assert(oh->version == OFP_VERSION);
1744 /* Figure out how to handle it. */
1746 case OFPT_FEATURES_REQUEST:
1747 min_size = sizeof(struct ofp_header);
1748 handler = recv_features_request;
1750 case OFPT_GET_CONFIG_REQUEST:
1751 min_size = sizeof(struct ofp_header);
1752 handler = recv_get_config_request;
1754 case OFPT_SET_CONFIG:
1755 min_size = sizeof(struct ofp_switch_config);
1756 handler = recv_set_config;
1758 case OFPT_PACKET_OUT:
1759 min_size = sizeof(struct ofp_packet_out);
1760 handler = recv_packet_out;
1763 min_size = sizeof(struct ofp_flow_mod);
1764 handler = recv_flow;
1767 min_size = sizeof(struct ofp_port_mod);
1768 handler = recv_port_mod;
1770 case OFPT_STATS_REQUEST:
1771 min_size = sizeof(struct ofp_stats_request);
1772 handler = recv_stats_request;
1774 case OFPT_ECHO_REQUEST:
1775 min_size = sizeof(struct ofp_header);
1776 handler = recv_echo_request;
1778 case OFPT_ECHO_REPLY:
1779 min_size = sizeof(struct ofp_header);
1780 handler = recv_echo_reply;
1783 dp_send_error_msg(dp, sender, OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE,
1789 if (length < min_size)
1791 return handler(dp, sender, msg);
1794 /* Packet buffering. */
1796 #define OVERWRITE_SECS 1
1798 struct packet_buffer {
1799 struct ofpbuf *buffer;
1804 static struct packet_buffer buffers[N_PKT_BUFFERS];
1805 static unsigned int buffer_idx;
1807 uint32_t save_buffer(struct ofpbuf *buffer)
1809 struct packet_buffer *p;
1812 buffer_idx = (buffer_idx + 1) & PKT_BUFFER_MASK;
1813 p = &buffers[buffer_idx];
1815 /* Don't buffer packet if existing entry is less than
1816 * OVERWRITE_SECS old. */
1817 if (time_now() < p->timeout) { /* FIXME */
1820 ofpbuf_delete(p->buffer);
1823 /* Don't use maximum cookie value since the all-bits-1 id is
1825 if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
1827 p->buffer = ofpbuf_clone(buffer); /* FIXME */
1828 p->timeout = time_now() + OVERWRITE_SECS; /* FIXME */
1829 id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
1834 static struct ofpbuf *retrieve_buffer(uint32_t id)
1836 struct ofpbuf *buffer = NULL;
1837 struct packet_buffer *p;
1839 p = &buffers[id & PKT_BUFFER_MASK];
1840 if (p->cookie == id >> PKT_BUFFER_BITS) {
1844 printf("cookie mismatch: %x != %x\n",
1845 id >> PKT_BUFFER_BITS, p->cookie);
1851 static void discard_buffer(uint32_t id)
1853 struct packet_buffer *p;
1855 p = &buffers[id & PKT_BUFFER_MASK];
1856 if (p->cookie == id >> PKT_BUFFER_BITS) {
1857 ofpbuf_delete(p->buffer);