2 * Copyright (c) 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <netinet/in.h>
25 #include "classifier.h"
27 #include "discovery.h"
28 #include "dynamic-string.h"
29 #include "fail-open.h"
31 #include "mac-learning.h"
34 #include "ofp-print.h"
35 #include "ofproto-sflow.h"
37 #include "openflow/nicira-ext.h"
38 #include "openflow/openflow.h"
39 #include "openvswitch/xflow.h"
43 #include "poll-loop.h"
44 #include "port-array.h"
49 #include "stream-ssl.h"
59 #define THIS_MODULE VLM_ofproto
62 #include "sflow_api.h"
66 TABLEID_CLASSIFIER = 1
70 uint64_t flow_cookie; /* Controller-issued identifier.
71 (Kept in network-byte order.) */
72 bool send_flow_removed; /* Send a flow removed message? */
73 tag_type tags; /* Tags (set only by hooks). */
76 static struct ofproto_rule *
77 ofproto_rule_cast(const struct wdp_rule *wdp_rule)
79 return wdp_rule->client_data;
83 ofproto_rule_init(struct wdp_rule *wdp_rule)
85 wdp_rule->client_data = xzalloc(sizeof(struct ofproto_rule));
90 rule_is_hidden(const struct wdp_rule *rule)
92 /* Rules with priority higher than UINT16_MAX are set up by ofproto itself
93 * (e.g. by in-band control) and are intentionally hidden from the
95 if (rule->cr.flow.priority > UINT16_MAX) {
102 static void delete_flow(struct ofproto *, struct wdp_rule *, uint8_t reason);
107 struct pktbuf *pktbuf;
110 struct rconn_packet_counter *packet_in_counter;
112 /* Number of OpenFlow messages queued as replies to OpenFlow requests, and
113 * the maximum number before we stop reading OpenFlow requests. */
114 #define OFCONN_REPLY_MAX 100
115 struct rconn_packet_counter *reply_counter;
118 static struct ofconn *ofconn_create(struct ofproto *, struct rconn *);
119 static void ofconn_destroy(struct ofconn *);
120 static void ofconn_run(struct ofconn *, struct ofproto *);
121 static void ofconn_wait(struct ofconn *);
122 static void queue_tx(struct ofpbuf *msg, const struct ofconn *ofconn,
123 struct rconn_packet_counter *counter);
127 uint64_t datapath_id; /* Datapath ID. */
128 uint64_t fallback_dpid; /* Datapath ID if no better choice found. */
129 char *mfr_desc; /* Manufacturer. */
130 char *hw_desc; /* Hardware. */
131 char *sw_desc; /* Software version. */
132 char *serial_desc; /* Serial number. */
133 char *dp_desc; /* Datapath description. */
140 struct switch_status *switch_status;
141 struct status_category *ss_cat;
142 struct in_band *in_band;
143 struct discovery *discovery;
144 struct fail_open *fail_open;
145 struct pinsched *miss_sched, *action_sched;
146 struct netflow *netflow;
147 struct ofproto_sflow *sflow;
149 /* OpenFlow connections. */
150 struct list all_conns;
151 struct ofconn *controller;
152 struct pvconn **listeners;
154 struct pvconn **snoops;
157 /* Hooks for ovs-vswitchd. */
158 const struct ofhooks *ofhooks;
161 /* Used by default ofhooks. */
162 struct mac_learning *ml;
165 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
167 static const struct ofhooks default_ofhooks;
169 static uint64_t pick_datapath_id(const struct ofproto *);
170 static uint64_t pick_fallback_dpid(void);
171 static void send_packet_in_miss(struct wdp_packet *, void *ofproto);
172 static void send_packet_in_action(struct wdp_packet *, void *ofproto);
174 static void handle_wdp_packet(struct ofproto *, struct wdp_packet *);
176 static void handle_openflow(struct ofconn *, struct ofproto *,
180 ofproto_create(const char *datapath, const char *datapath_type,
181 const struct ofhooks *ofhooks, void *aux,
182 struct ofproto **ofprotop)
184 struct wdp_stats stats;
191 /* Connect to datapath and start listening for messages. */
192 error = wdp_open(datapath, datapath_type, &wdp);
194 VLOG_ERR("failed to open datapath %s: %s", datapath, strerror(error));
197 error = wdp_get_wdp_stats(wdp, &stats);
199 VLOG_ERR("failed to obtain stats for datapath %s: %s",
200 datapath, strerror(error));
204 error = wdp_recv_set_mask(wdp, ((1 << WDP_CHAN_MISS)
205 | (1 << WDP_CHAN_ACTION)
206 | (1 << WDP_CHAN_SFLOW)));
208 VLOG_ERR("failed to listen on datapath %s: %s",
209 datapath, strerror(error));
216 /* Initialize settings. */
217 p = xzalloc(sizeof *p);
218 p->fallback_dpid = pick_fallback_dpid();
219 p->datapath_id = p->fallback_dpid;
220 p->mfr_desc = xstrdup(DEFAULT_MFR_DESC);
221 p->hw_desc = xstrdup(DEFAULT_HW_DESC);
222 p->sw_desc = xstrdup(DEFAULT_SW_DESC);
223 p->serial_desc = xstrdup(DEFAULT_SERIAL_DESC);
224 p->dp_desc = xstrdup(DEFAULT_DP_DESC);
226 /* Initialize datapath. */
228 p->max_ports = stats.max_ports;
230 /* Initialize submodules. */
231 p->switch_status = switch_status_create(p);
235 p->miss_sched = p->action_sched = NULL;
239 /* Initialize OpenFlow connections. */
240 list_init(&p->all_conns);
241 p->controller = ofconn_create(p, rconn_create(5, 8));
242 p->controller->pktbuf = pktbuf_create();
243 p->controller->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
249 /* Initialize hooks. */
251 p->ofhooks = ofhooks;
255 p->ofhooks = &default_ofhooks;
257 p->ml = mac_learning_create();
260 /* Register switch status category. */
261 p->ss_cat = switch_status_register(p->switch_status, "remote",
262 rconn_status_cb, p->controller->rconn);
264 /* Pick final datapath ID. */
265 p->datapath_id = pick_datapath_id(p);
266 VLOG_INFO("using datapath ID %016"PRIx64, p->datapath_id);
273 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
275 uint64_t old_dpid = p->datapath_id;
276 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
277 if (p->datapath_id != old_dpid) {
278 VLOG_INFO("datapath ID changed to %016"PRIx64, p->datapath_id);
279 rconn_reconnect(p->controller->rconn);
284 ofproto_set_probe_interval(struct ofproto *p, int probe_interval)
286 probe_interval = probe_interval ? MAX(probe_interval, 5) : 0;
287 rconn_set_probe_interval(p->controller->rconn, probe_interval);
289 int trigger_duration = probe_interval ? probe_interval * 3 : 15;
290 fail_open_set_trigger_duration(p->fail_open, trigger_duration);
295 ofproto_set_max_backoff(struct ofproto *p, int max_backoff)
297 rconn_set_max_backoff(p->controller->rconn, max_backoff);
301 ofproto_set_desc(struct ofproto *p,
302 const char *mfr_desc, const char *hw_desc,
303 const char *sw_desc, const char *serial_desc,
306 struct ofp_desc_stats *ods;
309 if (strlen(mfr_desc) >= sizeof ods->mfr_desc) {
310 VLOG_WARN("truncating mfr_desc, must be less than %zu characters",
311 sizeof ods->mfr_desc);
314 p->mfr_desc = xstrdup(mfr_desc);
317 if (strlen(hw_desc) >= sizeof ods->hw_desc) {
318 VLOG_WARN("truncating hw_desc, must be less than %zu characters",
319 sizeof ods->hw_desc);
322 p->hw_desc = xstrdup(hw_desc);
325 if (strlen(sw_desc) >= sizeof ods->sw_desc) {
326 VLOG_WARN("truncating sw_desc, must be less than %zu characters",
327 sizeof ods->sw_desc);
330 p->sw_desc = xstrdup(sw_desc);
333 if (strlen(serial_desc) >= sizeof ods->serial_num) {
334 VLOG_WARN("truncating serial_desc, must be less than %zu "
336 sizeof ods->serial_num);
338 free(p->serial_desc);
339 p->serial_desc = xstrdup(serial_desc);
342 if (strlen(dp_desc) >= sizeof ods->dp_desc) {
343 VLOG_WARN("truncating dp_desc, must be less than %zu characters",
344 sizeof ods->dp_desc);
347 p->dp_desc = xstrdup(dp_desc);
352 ofproto_set_in_band(struct ofproto *p, bool in_band)
354 if (in_band != (p->in_band != NULL)) {
356 return in_band_create(p, p->wdp, p->switch_status,
357 p->controller->rconn, &p->in_band);
359 ofproto_set_discovery(p, false, NULL, true);
360 in_band_destroy(p->in_band);
363 rconn_reconnect(p->controller->rconn);
369 ofproto_set_discovery(struct ofproto *p, bool discovery,
370 const char *re, bool update_resolv_conf)
372 if (discovery != (p->discovery != NULL)) {
374 int error = ofproto_set_in_band(p, true);
378 error = discovery_create(re, update_resolv_conf,
379 p->wdp, p->switch_status,
385 discovery_destroy(p->discovery);
388 rconn_disconnect(p->controller->rconn);
389 } else if (discovery) {
390 discovery_set_update_resolv_conf(p->discovery, update_resolv_conf);
391 return discovery_set_accept_controller_re(p->discovery, re);
397 ofproto_set_controller(struct ofproto *ofproto, const char *controller)
399 if (ofproto->discovery) {
401 } else if (controller) {
402 if (strcmp(rconn_get_name(ofproto->controller->rconn), controller)) {
403 return rconn_connect(ofproto->controller->rconn, controller);
408 rconn_disconnect(ofproto->controller->rconn);
414 set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
415 const struct svec *svec)
417 struct pvconn **pvconns = *pvconnsp;
418 size_t n_pvconns = *n_pvconnsp;
422 for (i = 0; i < n_pvconns; i++) {
423 pvconn_close(pvconns[i]);
427 pvconns = xmalloc(svec->n * sizeof *pvconns);
429 for (i = 0; i < svec->n; i++) {
430 const char *name = svec->names[i];
431 struct pvconn *pvconn;
434 error = pvconn_open(name, &pvconn);
436 pvconns[n_pvconns++] = pvconn;
438 VLOG_ERR("failed to listen on %s: %s", name, strerror(error));
446 *n_pvconnsp = n_pvconns;
452 ofproto_set_listeners(struct ofproto *ofproto, const struct svec *listeners)
454 return set_pvconns(&ofproto->listeners, &ofproto->n_listeners, listeners);
458 ofproto_set_snoops(struct ofproto *ofproto, const struct svec *snoops)
460 return set_pvconns(&ofproto->snoops, &ofproto->n_snoops, snoops);
464 ofproto_set_netflow(struct ofproto *ofproto,
465 const struct netflow_options *nf_options)
467 if (nf_options && nf_options->collectors.n) {
468 if (!ofproto->netflow) {
469 ofproto->netflow = netflow_create();
471 return netflow_set_options(ofproto->netflow, nf_options);
473 netflow_destroy(ofproto->netflow);
474 ofproto->netflow = NULL;
480 ofproto_set_sflow(struct ofproto *ofproto,
481 const struct ofproto_sflow_options *oso)
483 struct ofproto_sflow *os = ofproto->sflow;
486 os = ofproto->sflow = ofproto_sflow_create(ofproto->wdp);
489 ofproto_sflow_set_options(os, oso);
491 ofproto_sflow_destroy(os);
492 ofproto->sflow = NULL;
497 ofproto_set_failure(struct ofproto *ofproto, bool fail_open)
500 struct rconn *rconn = ofproto->controller->rconn;
501 int trigger_duration = rconn_get_probe_interval(rconn) * 3;
502 if (!ofproto->fail_open) {
503 ofproto->fail_open = fail_open_create(ofproto, trigger_duration,
504 ofproto->switch_status,
507 fail_open_set_trigger_duration(ofproto->fail_open,
511 fail_open_destroy(ofproto->fail_open);
512 ofproto->fail_open = NULL;
517 ofproto_set_rate_limit(struct ofproto *ofproto,
518 int rate_limit, int burst_limit)
520 if (rate_limit > 0) {
521 if (!ofproto->miss_sched) {
522 ofproto->miss_sched = pinsched_create(rate_limit, burst_limit,
523 ofproto->switch_status);
524 ofproto->action_sched = pinsched_create(rate_limit, burst_limit,
527 pinsched_set_limits(ofproto->miss_sched, rate_limit, burst_limit);
528 pinsched_set_limits(ofproto->action_sched,
529 rate_limit, burst_limit);
532 pinsched_destroy(ofproto->miss_sched);
533 ofproto->miss_sched = NULL;
534 pinsched_destroy(ofproto->action_sched);
535 ofproto->action_sched = NULL;
540 ofproto_set_stp(struct ofproto *ofproto OVS_UNUSED, bool enable_stp)
544 VLOG_WARN("STP is not yet implemented");
552 ofproto_get_datapath_id(const struct ofproto *ofproto)
554 return ofproto->datapath_id;
558 ofproto_get_probe_interval(const struct ofproto *ofproto)
560 return rconn_get_probe_interval(ofproto->controller->rconn);
564 ofproto_get_max_backoff(const struct ofproto *ofproto)
566 return rconn_get_max_backoff(ofproto->controller->rconn);
570 ofproto_get_in_band(const struct ofproto *ofproto)
572 return ofproto->in_band != NULL;
576 ofproto_get_discovery(const struct ofproto *ofproto)
578 return ofproto->discovery != NULL;
582 ofproto_get_controller(const struct ofproto *ofproto)
584 return rconn_get_name(ofproto->controller->rconn);
588 ofproto_get_listeners(const struct ofproto *ofproto, struct svec *listeners)
592 for (i = 0; i < ofproto->n_listeners; i++) {
593 svec_add(listeners, pvconn_get_name(ofproto->listeners[i]));
598 ofproto_get_snoops(const struct ofproto *ofproto, struct svec *snoops)
602 for (i = 0; i < ofproto->n_snoops; i++) {
603 svec_add(snoops, pvconn_get_name(ofproto->snoops[i]));
608 ofproto_destroy(struct ofproto *p)
610 struct ofconn *ofconn, *next_ofconn;
617 /* Destroy fail-open early, because it touches the classifier. */
618 ofproto_set_failure(p, false);
620 ofproto_flush_flows(p);
622 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, struct ofconn, node,
624 ofconn_destroy(ofconn);
629 switch_status_destroy(p->switch_status);
630 in_band_destroy(p->in_band);
631 discovery_destroy(p->discovery);
632 pinsched_destroy(p->miss_sched);
633 pinsched_destroy(p->action_sched);
634 netflow_destroy(p->netflow);
635 ofproto_sflow_destroy(p->sflow);
637 switch_status_unregister(p->ss_cat);
639 for (i = 0; i < p->n_listeners; i++) {
640 pvconn_close(p->listeners[i]);
644 for (i = 0; i < p->n_snoops; i++) {
645 pvconn_close(p->snoops[i]);
649 mac_learning_destroy(p->ml);
654 free(p->serial_desc);
661 ofproto_run(struct ofproto *p)
663 int error = ofproto_run1(p);
665 error = ofproto_run2(p, false);
671 ofproto_run1(struct ofproto *p)
673 struct ofconn *ofconn, *next_ofconn;
676 for (i = 0; i < 50; i++) {
677 struct wdp_packet packet;
680 error = wdp_recv(p->wdp, &packet);
682 if (error == ENODEV) {
683 /* Someone destroyed the datapath behind our back. The caller
684 * better destroy us and give up, because we're just going to
685 * spin from here on out. */
686 static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 5);
687 VLOG_ERR_RL(&rl2, "%s: datapath was destroyed externally",
694 handle_wdp_packet(p, xmemdup(&packet, sizeof packet));
698 in_band_run(p->in_band);
701 char *controller_name;
702 if (rconn_is_connectivity_questionable(p->controller->rconn)) {
703 discovery_question_connectivity(p->discovery);
705 if (discovery_run(p->discovery, &controller_name)) {
706 if (controller_name) {
707 rconn_connect(p->controller->rconn, controller_name);
709 rconn_disconnect(p->controller->rconn);
713 pinsched_run(p->miss_sched, send_packet_in_miss, p);
714 pinsched_run(p->action_sched, send_packet_in_action, p);
716 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, struct ofconn, node,
718 ofconn_run(ofconn, p);
721 /* Fail-open maintenance. Do this after processing the ofconns since
722 * fail-open checks the status of the controller rconn. */
724 fail_open_run(p->fail_open);
727 for (i = 0; i < p->n_listeners; i++) {
731 retval = pvconn_accept(p->listeners[i], OFP_VERSION, &vconn);
733 ofconn_create(p, rconn_new_from_vconn("passive", vconn));
734 } else if (retval != EAGAIN) {
735 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
739 for (i = 0; i < p->n_snoops; i++) {
743 retval = pvconn_accept(p->snoops[i], OFP_VERSION, &vconn);
745 rconn_add_monitor(p->controller->rconn, vconn);
746 } else if (retval != EAGAIN) {
747 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
752 netflow_run(p->netflow);
755 ofproto_sflow_run(p->sflow);
761 struct revalidate_cbdata {
762 struct ofproto *ofproto;
763 bool revalidate_all; /* Revalidate all exact-match rules? */
764 bool revalidate_subrules; /* Revalidate all exact-match subrules? */
765 struct tag_set revalidate_set; /* Set of tags to revalidate. */
769 ofproto_run2(struct ofproto *p OVS_UNUSED, bool revalidate_all OVS_UNUSED)
775 ofproto_wait(struct ofproto *p)
777 struct ofconn *ofconn;
780 wdp_recv_wait(p->wdp);
781 wdp_port_poll_wait(p->wdp);
782 LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
786 in_band_wait(p->in_band);
789 discovery_wait(p->discovery);
792 fail_open_wait(p->fail_open);
794 pinsched_wait(p->miss_sched);
795 pinsched_wait(p->action_sched);
797 ofproto_sflow_wait(p->sflow);
799 for (i = 0; i < p->n_listeners; i++) {
800 pvconn_wait(p->listeners[i]);
802 for (i = 0; i < p->n_snoops; i++) {
803 pvconn_wait(p->snoops[i]);
808 ofproto_revalidate(struct ofproto *ofproto OVS_UNUSED, tag_type tag OVS_UNUSED)
810 //XXX tag_set_add(&ofproto->revalidate_set, tag);
814 ofproto_is_alive(const struct ofproto *p)
816 return p->discovery || rconn_is_alive(p->controller->rconn);
820 ofproto_send_packet(struct ofproto *p, const flow_t *flow,
821 const union ofp_action *actions, size_t n_actions,
822 const struct ofpbuf *packet)
824 /* XXX Should we translate the wdp_execute() errno value into an OpenFlow
826 wdp_execute(p->wdp, flow->in_port, actions, n_actions, packet);
831 ofproto_add_flow(struct ofproto *p, const flow_t *flow,
832 const union ofp_action *actions, size_t n_actions,
835 struct wdp_flow_put put;
836 struct wdp_rule *rule;
838 put.flags = WDP_PUT_CREATE | WDP_PUT_MODIFY | WDP_PUT_ALL;
840 put.actions = actions;
841 put.n_actions = n_actions;
842 put.idle_timeout = idle_timeout;
843 put.hard_timeout = 0;
845 if (!wdp_flow_put(p->wdp, &put, NULL, &rule)) {
846 ofproto_rule_init(rule);
851 ofproto_delete_flow(struct ofproto *ofproto, const flow_t *flow)
853 struct wdp_rule *rule = wdp_flow_get(ofproto->wdp, flow);
855 delete_flow(ofproto, rule, OFPRR_DELETE);
860 ofproto_flush_flows(struct ofproto *ofproto)
862 COVERAGE_INC(ofproto_flush);
863 wdp_flow_flush(ofproto->wdp);
864 if (ofproto->in_band) {
865 in_band_flushed(ofproto->in_band);
867 if (ofproto->fail_open) {
868 fail_open_flushed(ofproto->fail_open);
872 static struct ofconn *
873 ofconn_create(struct ofproto *p, struct rconn *rconn)
875 struct ofconn *ofconn = xmalloc(sizeof *ofconn);
876 list_push_back(&p->all_conns, &ofconn->node);
877 ofconn->rconn = rconn;
878 ofconn->pktbuf = NULL;
879 ofconn->miss_send_len = 0;
880 ofconn->packet_in_counter = rconn_packet_counter_create ();
881 ofconn->reply_counter = rconn_packet_counter_create ();
886 ofconn_destroy(struct ofconn *ofconn)
888 list_remove(&ofconn->node);
889 rconn_destroy(ofconn->rconn);
890 rconn_packet_counter_destroy(ofconn->packet_in_counter);
891 rconn_packet_counter_destroy(ofconn->reply_counter);
892 pktbuf_destroy(ofconn->pktbuf);
897 ofconn_run(struct ofconn *ofconn, struct ofproto *p)
901 rconn_run(ofconn->rconn);
903 if (rconn_packet_counter_read (ofconn->reply_counter) < OFCONN_REPLY_MAX) {
904 /* Limit the number of iterations to prevent other tasks from
906 for (iteration = 0; iteration < 50; iteration++) {
907 struct ofpbuf *of_msg = rconn_recv(ofconn->rconn);
912 fail_open_maybe_recover(p->fail_open);
914 handle_openflow(ofconn, p, of_msg);
915 ofpbuf_delete(of_msg);
919 if (ofconn != p->controller && !rconn_is_alive(ofconn->rconn)) {
920 ofconn_destroy(ofconn);
925 ofconn_wait(struct ofconn *ofconn)
927 rconn_run_wait(ofconn->rconn);
928 if (rconn_packet_counter_read (ofconn->reply_counter) < OFCONN_REPLY_MAX) {
929 rconn_recv_wait(ofconn->rconn);
931 COVERAGE_INC(ofproto_ofconn_stuck);
936 rule_has_out_port(const struct wdp_rule *rule, uint16_t out_port)
938 const union ofp_action *oa;
939 struct actions_iterator i;
941 if (out_port == htons(OFPP_NONE)) {
944 for (oa = actions_first(&i, rule->actions, rule->n_actions); oa;
945 oa = actions_next(&i)) {
946 if (oa->type == htons(OFPAT_OUTPUT) && oa->output.port == out_port) {
954 queue_tx(struct ofpbuf *msg, const struct ofconn *ofconn,
955 struct rconn_packet_counter *counter)
957 update_openflow_length(msg);
958 if (rconn_send(ofconn->rconn, msg, counter)) {
964 send_error(const struct ofconn *ofconn, const struct ofp_header *oh,
965 int error, const void *data, size_t len)
968 struct ofp_error_msg *oem;
970 if (!(error >> 16)) {
971 VLOG_WARN_RL(&rl, "not sending bad error code %d to controller",
976 COVERAGE_INC(ofproto_error);
977 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR,
978 oh ? oh->xid : 0, &buf);
979 oem->type = htons((unsigned int) error >> 16);
980 oem->code = htons(error & 0xffff);
981 memcpy(oem->data, data, len);
982 queue_tx(buf, ofconn, ofconn->reply_counter);
986 send_error_oh(const struct ofconn *ofconn, const struct ofp_header *oh,
989 size_t oh_length = ntohs(oh->length);
990 send_error(ofconn, oh, error, oh, MIN(oh_length, 64));
994 handle_echo_request(struct ofconn *ofconn, struct ofp_header *oh)
996 struct ofp_header *rq = oh;
997 queue_tx(make_echo_reply(rq), ofconn, ofconn->reply_counter);
1002 handle_features_request(struct ofproto *p, struct ofconn *ofconn,
1003 struct ofp_header *oh)
1005 struct ofpbuf *features;
1008 error = wdp_get_features(p->wdp, &features);
1010 struct ofp_switch_features *osf = features->data;
1012 osf->header.xid = oh->xid;
1013 osf->datapath_id = htonll(p->datapath_id);
1014 osf->n_buffers = htonl(pktbuf_capacity());
1015 osf->capabilities |= htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS |
1018 queue_tx(features, ofconn, ofconn->reply_counter);
1024 handle_get_config_request(struct ofproto *p, struct ofconn *ofconn,
1025 struct ofp_header *oh)
1028 struct ofp_switch_config *osc;
1032 /* Figure out flags. */
1033 wdp_get_drop_frags(p->wdp, &drop_frags);
1034 flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL;
1037 osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf);
1038 osc->flags = htons(flags);
1039 osc->miss_send_len = htons(ofconn->miss_send_len);
1040 queue_tx(buf, ofconn, ofconn->reply_counter);
1046 handle_set_config(struct ofproto *p, struct ofconn *ofconn,
1047 struct ofp_switch_config *osc)
1052 error = check_ofp_message(&osc->header, OFPT_SET_CONFIG, sizeof *osc);
1056 flags = ntohs(osc->flags);
1058 if (ofconn == p->controller) {
1059 switch (flags & OFPC_FRAG_MASK) {
1060 case OFPC_FRAG_NORMAL:
1061 wdp_set_drop_frags(p->wdp, false);
1063 case OFPC_FRAG_DROP:
1064 wdp_set_drop_frags(p->wdp, true);
1067 VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")",
1073 if ((ntohs(osc->miss_send_len) != 0) != (ofconn->miss_send_len != 0)) {
1074 if (ntohs(osc->miss_send_len) != 0) {
1075 ofconn->pktbuf = pktbuf_create();
1077 pktbuf_destroy(ofconn->pktbuf);
1081 ofconn->miss_send_len = ntohs(osc->miss_send_len);
1087 handle_packet_out(struct ofproto *p, struct ofconn *ofconn,
1088 struct ofp_header *oh)
1090 struct ofp_packet_out *opo;
1091 struct ofpbuf payload, *buffer;
1092 struct ofp_action_header *actions;
1098 error = check_ofp_packet_out(oh, &payload, &n_actions, p->max_ports);
1102 opo = (struct ofp_packet_out *) oh;
1103 actions = opo->actions;
1105 COVERAGE_INC(ofproto_packet_out);
1106 if (opo->buffer_id != htonl(UINT32_MAX)) {
1107 error = pktbuf_retrieve(ofconn->pktbuf, ntohl(opo->buffer_id),
1109 if (error || !buffer) {
1117 flow_extract(&payload, ntohs(opo->in_port), &flow);
1118 wdp_execute(p->wdp, flow.in_port, (const union ofp_action *) actions,
1119 n_actions, &payload);
1120 ofpbuf_delete(buffer);
1126 handle_port_mod(struct ofproto *p, struct ofp_header *oh)
1128 const struct ofp_port_mod *opm;
1129 struct wdp_port *port;
1132 error = check_ofp_message(oh, OFPT_PORT_MOD, sizeof *opm);
1136 opm = (struct ofp_port_mod *) oh;
1138 wdp_port_query_by_number(p->wdp, ntohs(opm->port_no), &port);
1140 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT);
1141 } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) {
1142 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR);
1144 uint32_t mask, new_config;
1146 mask = ntohl(opm->mask) & (OFPPC_PORT_DOWN | OFPPC_NO_STP
1147 | OFPPC_NO_RECV | OFPPC_NO_RECV_STP
1148 | OFPPC_NO_FLOOD | OFPPC_NO_FWD
1149 | OFPPC_NO_PACKET_IN);
1150 new_config = (port->opp.config & ~mask) | (ntohl(opm->config) & mask);
1151 if (new_config != port->opp.config) {
1152 wdp_port_set_config(p->wdp, ntohs(opm->port_no), new_config);
1154 if (opm->advertise) {
1155 netdev_set_advertisements(port->netdev, ntohl(opm->advertise));
1162 static struct ofpbuf *
1163 make_stats_reply(uint32_t xid, uint16_t type, size_t body_len)
1165 struct ofp_stats_reply *osr;
1168 msg = ofpbuf_new(MIN(sizeof *osr + body_len, UINT16_MAX));
1169 osr = put_openflow_xid(sizeof *osr, OFPT_STATS_REPLY, xid, msg);
1171 osr->flags = htons(0);
1175 static struct ofpbuf *
1176 start_stats_reply(const struct ofp_stats_request *request, size_t body_len)
1178 return make_stats_reply(request->header.xid, request->type, body_len);
1182 append_stats_reply(size_t nbytes, struct ofconn *ofconn, struct ofpbuf **msgp)
1184 struct ofpbuf *msg = *msgp;
1185 assert(nbytes <= UINT16_MAX - sizeof(struct ofp_stats_reply));
1186 if (nbytes + msg->size > UINT16_MAX) {
1187 struct ofp_stats_reply *reply = msg->data;
1188 reply->flags = htons(OFPSF_REPLY_MORE);
1189 *msgp = make_stats_reply(reply->header.xid, reply->type, nbytes);
1190 queue_tx(msg, ofconn, ofconn->reply_counter);
1192 return ofpbuf_put_uninit(*msgp, nbytes);
1196 handle_desc_stats_request(struct ofproto *p, struct ofconn *ofconn,
1197 struct ofp_stats_request *request)
1199 struct ofp_desc_stats *ods;
1202 msg = start_stats_reply(request, sizeof *ods);
1203 ods = append_stats_reply(sizeof *ods, ofconn, &msg);
1204 memset(ods, 0, sizeof *ods);
1205 ovs_strlcpy(ods->mfr_desc, p->mfr_desc, sizeof ods->mfr_desc);
1206 ovs_strlcpy(ods->hw_desc, p->hw_desc, sizeof ods->hw_desc);
1207 ovs_strlcpy(ods->sw_desc, p->sw_desc, sizeof ods->sw_desc);
1208 ovs_strlcpy(ods->serial_num, p->serial_desc, sizeof ods->serial_num);
1209 ovs_strlcpy(ods->dp_desc, p->dp_desc, sizeof ods->dp_desc);
1210 queue_tx(msg, ofconn, ofconn->reply_counter);
1216 handle_table_stats_request(struct ofproto *p, struct ofconn *ofconn,
1217 struct ofp_stats_request *request)
1219 struct ofp_table_stats *ots;
1221 struct wdp_stats dpstats;
1223 msg = start_stats_reply(request, sizeof *ots * 2);
1225 wdp_get_wdp_stats(p->wdp, &dpstats);
1228 ots = append_stats_reply(sizeof *ots, ofconn, &msg);
1229 memset(ots, 0, sizeof *ots);
1230 ots->table_id = TABLEID_HASH;
1231 strcpy(ots->name, "hash");
1232 ots->wildcards = htonl(0);
1233 ots->max_entries = htonl(dpstats.exact.max_capacity);
1234 ots->active_count = htonl(dpstats.exact.n_flows);
1235 ots->lookup_count = htonll(dpstats.exact.n_hit + dpstats.exact.n_missed);
1236 ots->matched_count = htonll(dpstats.exact.n_hit);
1238 /* Classifier table. */
1239 ots = append_stats_reply(sizeof *ots, ofconn, &msg);
1240 memset(ots, 0, sizeof *ots);
1241 ots->table_id = TABLEID_CLASSIFIER;
1242 strcpy(ots->name, "classifier");
1243 ots->wildcards = htonl(OFPFW_ALL);
1244 ots->max_entries = htonl(dpstats.wild.max_capacity);
1245 ots->active_count = htonl(dpstats.wild.n_flows);
1246 ots->lookup_count = htonll(dpstats.wild.n_hit + dpstats.wild.n_missed);
1247 ots->matched_count = htonll(dpstats.wild.n_hit);
1249 queue_tx(msg, ofconn, ofconn->reply_counter);
1254 append_port_stat(struct wdp_port *port, struct ofconn *ofconn,
1257 struct netdev_stats stats;
1258 struct ofp_port_stats *ops;
1260 /* Intentionally ignore return value, since errors will set
1261 * 'stats' to all-1s, which is correct for OpenFlow, and
1262 * netdev_get_stats() will log errors. */
1263 netdev_get_stats(port->netdev, &stats);
1265 ops = append_stats_reply(sizeof *ops, ofconn, &msg);
1266 ops->port_no = htons(port->opp.port_no);
1267 memset(ops->pad, 0, sizeof ops->pad);
1268 ops->rx_packets = htonll(stats.rx_packets);
1269 ops->tx_packets = htonll(stats.tx_packets);
1270 ops->rx_bytes = htonll(stats.rx_bytes);
1271 ops->tx_bytes = htonll(stats.tx_bytes);
1272 ops->rx_dropped = htonll(stats.rx_dropped);
1273 ops->tx_dropped = htonll(stats.tx_dropped);
1274 ops->rx_errors = htonll(stats.rx_errors);
1275 ops->tx_errors = htonll(stats.tx_errors);
1276 ops->rx_frame_err = htonll(stats.rx_frame_errors);
1277 ops->rx_over_err = htonll(stats.rx_over_errors);
1278 ops->rx_crc_err = htonll(stats.rx_crc_errors);
1279 ops->collisions = htonll(stats.collisions);
1283 handle_port_stats_request(struct ofproto *p, struct ofconn *ofconn,
1284 struct ofp_stats_request *osr,
1287 struct ofp_port_stats_request *psr;
1288 struct ofp_port_stats *ops;
1291 if (arg_size != sizeof *psr) {
1292 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1294 psr = (struct ofp_port_stats_request *) osr->body;
1296 msg = start_stats_reply(osr, sizeof *ops * 16);
1297 if (psr->port_no != htons(OFPP_NONE)) {
1298 struct wdp_port *port;
1300 wdp_port_query_by_number(p->wdp, ntohs(psr->port_no), &port);
1302 append_port_stat(port, ofconn, msg);
1305 struct wdp_port **ports;
1309 wdp_port_list(p->wdp, &ports, &n_ports);
1310 for (i = 0; i < n_ports; i++) {
1311 append_port_stat(ports[i], ofconn, msg);
1316 queue_tx(msg, ofconn, ofconn->reply_counter);
1320 struct flow_stats_cbdata {
1321 struct ofproto *ofproto;
1322 struct ofconn *ofconn;
1327 /* Obtains statistic counters for 'rule' within 'p' and stores them into
1328 * '*packet_countp' and '*byte_countp'. If 'rule' is a wildcarded rule, the
1329 * returned statistic include statistics for all of 'rule''s subrules. */
1331 query_stats(struct ofproto *p, struct wdp_rule *rule,
1332 uint64_t *packet_countp, uint64_t *byte_countp)
1334 struct wdp_flow_stats stats;
1336 if (!wdp_flow_get_stats(p->wdp, rule, &stats)) {
1337 *packet_countp = stats.n_packets;
1338 *byte_countp = stats.n_bytes;
1346 flow_stats_cb(struct wdp_rule *rule, void *cbdata_)
1348 struct flow_stats_cbdata *cbdata = cbdata_;
1349 struct ofp_flow_stats *ofs;
1350 uint64_t packet_count, byte_count;
1351 size_t act_len, len;
1352 long long int tdiff = time_msec() - rule->created;
1353 uint32_t sec = tdiff / 1000;
1354 uint32_t msec = tdiff - (sec * 1000);
1356 if (rule_is_hidden(rule)
1357 || !rule_has_out_port(rule, cbdata->out_port)) {
1361 act_len = sizeof *rule->actions * rule->n_actions;
1362 len = offsetof(struct ofp_flow_stats, actions) + act_len;
1364 query_stats(cbdata->ofproto, rule, &packet_count, &byte_count);
1366 ofs = append_stats_reply(len, cbdata->ofconn, &cbdata->msg);
1367 ofs->length = htons(len);
1368 ofs->table_id = rule->cr.flow.wildcards ? TABLEID_CLASSIFIER : TABLEID_HASH;
1370 flow_to_match(&rule->cr.flow, &ofs->match);
1371 ofs->duration_sec = htonl(sec);
1372 ofs->duration_nsec = htonl(msec * 1000000);
1373 ofs->cookie = ofproto_rule_cast(rule)->flow_cookie;
1374 ofs->priority = htons(rule->cr.flow.priority);
1375 ofs->idle_timeout = htons(rule->idle_timeout);
1376 ofs->hard_timeout = htons(rule->hard_timeout);
1377 memset(ofs->pad2, 0, sizeof ofs->pad2);
1378 ofs->packet_count = htonll(packet_count);
1379 ofs->byte_count = htonll(byte_count);
1380 memcpy(ofs->actions, rule->actions, act_len);
1384 table_id_to_include(uint8_t table_id)
1386 return (table_id == TABLEID_HASH ? CLS_INC_EXACT
1387 : table_id == TABLEID_CLASSIFIER ? CLS_INC_WILD
1388 : table_id == 0xff ? CLS_INC_ALL
1393 handle_flow_stats_request(struct ofproto *p, struct ofconn *ofconn,
1394 const struct ofp_stats_request *osr,
1397 struct ofp_flow_stats_request *fsr;
1398 struct flow_stats_cbdata cbdata;
1401 if (arg_size != sizeof *fsr) {
1402 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1404 fsr = (struct ofp_flow_stats_request *) osr->body;
1406 COVERAGE_INC(ofproto_flows_req);
1408 cbdata.ofconn = ofconn;
1409 cbdata.out_port = fsr->out_port;
1410 cbdata.msg = start_stats_reply(osr, 1024);
1411 flow_from_match(&target, 0, &fsr->match);
1412 wdp_flow_for_each_match(p->wdp, &target,
1413 table_id_to_include(fsr->table_id),
1414 flow_stats_cb, &cbdata);
1415 queue_tx(cbdata.msg, ofconn, ofconn->reply_counter);
1419 struct flow_stats_ds_cbdata {
1420 struct ofproto *ofproto;
1425 flow_stats_ds_cb(struct wdp_rule *rule, void *cbdata_)
1427 struct flow_stats_ds_cbdata *cbdata = cbdata_;
1428 struct ds *results = cbdata->results;
1429 struct ofp_match match;
1430 uint64_t packet_count, byte_count;
1431 size_t act_len = sizeof *rule->actions * rule->n_actions;
1433 query_stats(cbdata->ofproto, rule, &packet_count, &byte_count);
1434 flow_to_match(&rule->cr.flow, &match);
1436 ds_put_format(results, "duration=%llds, ",
1437 (time_msec() - rule->created) / 1000);
1438 ds_put_format(results, "priority=%u, ", rule->cr.flow.priority);
1439 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
1440 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
1441 ofp_print_match(results, &match, true);
1442 ofp_print_actions(results, &rule->actions->header, act_len);
1443 ds_put_cstr(results, "\n");
1446 /* Adds a pretty-printed description of all flows to 'results', including
1447 * those marked hidden by secchan (e.g., by in-band control). */
1449 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
1451 struct flow_stats_ds_cbdata cbdata;
1452 struct ofp_match match;
1455 memset(&match, 0, sizeof match);
1456 match.wildcards = htonl(OFPFW_ALL);
1459 cbdata.results = results;
1461 flow_from_match(&target, 0, &match);
1462 wdp_flow_for_each_match(p->wdp, &target, CLS_INC_ALL,
1463 flow_stats_ds_cb, &cbdata);
1466 struct aggregate_stats_cbdata {
1467 struct ofproto *ofproto;
1469 uint64_t packet_count;
1470 uint64_t byte_count;
1475 aggregate_stats_cb(struct wdp_rule *rule, void *cbdata_)
1477 struct aggregate_stats_cbdata *cbdata = cbdata_;
1478 uint64_t packet_count, byte_count;
1480 if (rule_is_hidden(rule) || !rule_has_out_port(rule, cbdata->out_port)) {
1484 query_stats(cbdata->ofproto, rule, &packet_count, &byte_count);
1486 cbdata->packet_count += packet_count;
1487 cbdata->byte_count += byte_count;
1492 handle_aggregate_stats_request(struct ofproto *p, struct ofconn *ofconn,
1493 const struct ofp_stats_request *osr,
1496 struct ofp_aggregate_stats_request *asr;
1497 struct ofp_aggregate_stats_reply *reply;
1498 struct aggregate_stats_cbdata cbdata;
1502 if (arg_size != sizeof *asr) {
1503 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1505 asr = (struct ofp_aggregate_stats_request *) osr->body;
1507 COVERAGE_INC(ofproto_agg_request);
1509 cbdata.out_port = asr->out_port;
1510 cbdata.packet_count = 0;
1511 cbdata.byte_count = 0;
1513 flow_from_match(&target, 0, &asr->match);
1514 wdp_flow_for_each_match(p->wdp, &target,
1515 table_id_to_include(asr->table_id),
1516 aggregate_stats_cb, &cbdata);
1518 msg = start_stats_reply(osr, sizeof *reply);
1519 reply = append_stats_reply(sizeof *reply, ofconn, &msg);
1520 reply->flow_count = htonl(cbdata.n_flows);
1521 reply->packet_count = htonll(cbdata.packet_count);
1522 reply->byte_count = htonll(cbdata.byte_count);
1523 queue_tx(msg, ofconn, ofconn->reply_counter);
1528 handle_stats_request(struct ofproto *p, struct ofconn *ofconn,
1529 struct ofp_header *oh)
1531 struct ofp_stats_request *osr;
1535 error = check_ofp_message_array(oh, OFPT_STATS_REQUEST, sizeof *osr,
1540 osr = (struct ofp_stats_request *) oh;
1542 switch (ntohs(osr->type)) {
1544 return handle_desc_stats_request(p, ofconn, osr);
1547 return handle_flow_stats_request(p, ofconn, osr, arg_size);
1549 case OFPST_AGGREGATE:
1550 return handle_aggregate_stats_request(p, ofconn, osr, arg_size);
1553 return handle_table_stats_request(p, ofconn, osr);
1556 return handle_port_stats_request(p, ofconn, osr, arg_size);
1559 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
1562 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT);
1567 add_flow(struct ofproto *p, struct ofconn *ofconn,
1568 struct ofp_flow_mod *ofm, size_t n_actions)
1570 struct wdp_rule *rule;
1571 struct wdp_flow_put put;
1572 struct ofpbuf *packet;
1577 flow_from_match(&flow, ntohs(ofm->priority), &ofm->match);
1578 if (ofm->flags & htons(OFPFF_CHECK_OVERLAP)
1579 && wdp_flow_overlaps(p->wdp, &flow)) {
1580 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP);
1583 put.flags = WDP_PUT_CREATE | WDP_PUT_MODIFY | WDP_PUT_ALL;
1585 put.actions = (const union ofp_action *) ofm->actions;
1586 put.n_actions = n_actions;
1587 put.idle_timeout = ntohs(ofm->idle_timeout);
1588 put.hard_timeout = ntohs(ofm->hard_timeout);
1589 error = wdp_flow_put(p->wdp, &put, NULL, &rule);
1591 /* XXX wdp_flow_put should return OpenFlow error code. */
1594 ofproto_rule_init(rule);
1596 if (ofm->buffer_id != htonl(UINT32_MAX)) {
1597 error = pktbuf_retrieve(ofconn->pktbuf, ntohl(ofm->buffer_id),
1600 wdp_flow_inject(p->wdp, rule, in_port, packet);
1601 ofpbuf_delete(packet);
1609 modify_flow(struct ofproto *p, const struct ofp_flow_mod *ofm,
1610 size_t n_actions, uint16_t command, struct wdp_rule *rule)
1612 if (rule_is_hidden(rule)) {
1616 if (command == OFPFC_DELETE) {
1617 delete_flow(p, rule, OFPPR_DELETE);
1619 const struct ofp_action_header *actions = ofm->actions;
1620 struct wdp_flow_put put;
1622 ofproto_rule_cast(rule)->flow_cookie = ofm->cookie;
1624 put.flags = WDP_PUT_MODIFY | WDP_PUT_ACTIONS;
1625 put.flow = &rule->cr.flow;
1626 put.actions = (const union ofp_action *) actions;
1627 put.n_actions = n_actions;
1628 put.idle_timeout = put.hard_timeout = 0;
1629 wdp_flow_put(p->wdp, &put, NULL, NULL);
1636 modify_flows_strict(struct ofproto *p, const struct ofp_flow_mod *ofm,
1637 size_t n_actions, uint16_t command)
1639 struct wdp_rule *rule;
1642 flow_from_match(&flow, ntohs(ofm->priority), &ofm->match);
1643 rule = wdp_flow_get(p->wdp, &flow);
1646 if (command == OFPFC_DELETE
1647 && ofm->out_port != htons(OFPP_NONE)
1648 && !rule_has_out_port(rule, ofm->out_port)) {
1652 modify_flow(p, ofm, n_actions, command, rule);
1657 struct modify_flows_cbdata {
1658 struct ofproto *ofproto;
1659 const struct ofp_flow_mod *ofm;
1666 modify_flows_cb(struct wdp_rule *rule, void *cbdata_)
1668 struct modify_flows_cbdata *cbdata = cbdata_;
1670 if (cbdata->out_port != htons(OFPP_NONE)
1671 && !rule_has_out_port(rule, cbdata->out_port)) {
1675 modify_flow(cbdata->ofproto, cbdata->ofm, cbdata->n_actions,
1676 cbdata->command, rule);
1680 modify_flows_loose(struct ofproto *p, const struct ofp_flow_mod *ofm,
1681 size_t n_actions, uint16_t command)
1683 struct modify_flows_cbdata cbdata;
1688 cbdata.out_port = (command == OFPFC_DELETE ? ofm->out_port
1689 : htons(OFPP_NONE));
1690 cbdata.n_actions = n_actions;
1691 cbdata.command = command;
1693 flow_from_match(&target, 0, &ofm->match);
1694 wdp_flow_for_each_match(p->wdp, &target, CLS_INC_ALL,
1695 modify_flows_cb, &cbdata);
1700 handle_flow_mod(struct ofproto *p, struct ofconn *ofconn,
1701 struct ofp_flow_mod *ofm)
1706 error = check_ofp_message_array(&ofm->header, OFPT_FLOW_MOD, sizeof *ofm,
1707 sizeof *ofm->actions, &n_actions);
1712 /* We do not support the emergency flow cache. It will hopefully
1713 * get dropped from OpenFlow in the near future. */
1714 if (ofm->flags & htons(OFPFF_EMERG)) {
1715 /* There isn't a good fit for an error code, so just state that the
1716 * flow table is full. */
1717 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL);
1720 normalize_match(&ofm->match);
1721 if (!ofm->match.wildcards) {
1722 ofm->priority = htons(UINT16_MAX);
1725 error = validate_actions((const union ofp_action *) ofm->actions,
1726 n_actions, p->max_ports);
1731 switch (ntohs(ofm->command)) {
1733 return add_flow(p, ofconn, ofm, n_actions);
1736 return modify_flows_loose(p, ofm, n_actions, OFPFC_MODIFY);
1738 case OFPFC_MODIFY_STRICT:
1739 return modify_flows_strict(p, ofm, n_actions, OFPFC_MODIFY);
1742 return modify_flows_loose(p, ofm, n_actions, OFPFC_DELETE);
1744 case OFPFC_DELETE_STRICT:
1745 return modify_flows_strict(p, ofm, n_actions, OFPFC_DELETE);
1748 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND);
1753 handle_vendor(struct ofproto *p, struct ofconn *ofconn, void *msg)
1755 struct ofp_vendor_header *ovh = msg;
1756 struct nicira_header *nh;
1758 if (ntohs(ovh->header.length) < sizeof(struct ofp_vendor_header)) {
1759 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1761 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
1762 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
1764 if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) {
1765 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1769 switch (ntohl(nh->subtype)) {
1770 case NXT_STATUS_REQUEST:
1771 return switch_status_handle_request(p->switch_status, ofconn->rconn,
1775 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE);
1779 handle_barrier_request(struct ofconn *ofconn, struct ofp_header *oh)
1781 struct ofp_header *ob;
1784 /* Currently, everything executes synchronously, so we can just
1785 * immediately send the barrier reply. */
1786 ob = make_openflow_xid(sizeof *ob, OFPT_BARRIER_REPLY, oh->xid, &buf);
1787 queue_tx(buf, ofconn, ofconn->reply_counter);
1792 handle_openflow(struct ofconn *ofconn, struct ofproto *p,
1793 struct ofpbuf *ofp_msg)
1795 struct ofp_header *oh = ofp_msg->data;
1798 COVERAGE_INC(ofproto_recv_openflow);
1800 case OFPT_ECHO_REQUEST:
1801 error = handle_echo_request(ofconn, oh);
1804 case OFPT_ECHO_REPLY:
1808 case OFPT_FEATURES_REQUEST:
1809 error = handle_features_request(p, ofconn, oh);
1812 case OFPT_GET_CONFIG_REQUEST:
1813 error = handle_get_config_request(p, ofconn, oh);
1816 case OFPT_SET_CONFIG:
1817 error = handle_set_config(p, ofconn, ofp_msg->data);
1820 case OFPT_PACKET_OUT:
1821 error = handle_packet_out(p, ofconn, ofp_msg->data);
1825 error = handle_port_mod(p, oh);
1829 error = handle_flow_mod(p, ofconn, ofp_msg->data);
1832 case OFPT_STATS_REQUEST:
1833 error = handle_stats_request(p, ofconn, oh);
1837 error = handle_vendor(p, ofconn, ofp_msg->data);
1840 case OFPT_BARRIER_REQUEST:
1841 error = handle_barrier_request(ofconn, oh);
1845 if (VLOG_IS_WARN_ENABLED()) {
1846 char *s = ofp_to_string(oh, ntohs(oh->length), 2);
1847 VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s);
1850 error = ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
1855 send_error_oh(ofconn, ofp_msg->data, error);
1860 handle_flow_miss(struct ofproto *p, struct wdp_packet *packet)
1862 struct wdp_rule *rule;
1865 flow_extract(packet->payload, packet->in_port, &flow);
1866 rule = wdp_flow_match(p->wdp, &flow);
1868 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
1869 struct wdp_port *port;
1871 wdp_port_query_by_number(p->wdp, packet->in_port, &port);
1873 if (port->opp.config & OFPPC_NO_PACKET_IN) {
1874 COVERAGE_INC(ofproto_no_packet_in);
1875 wdp_packet_destroy(packet);
1879 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
1883 COVERAGE_INC(ofproto_packet_in);
1884 pinsched_send(p->miss_sched, packet->in_port, packet,
1885 send_packet_in_miss, p);
1889 wdp_flow_inject(p->wdp, rule, packet->in_port, packet->payload);
1891 if (rule->cr.flow.priority == FAIL_OPEN_PRIORITY
1892 && rconn_is_connected(p->controller->rconn)) {
1894 * Extra-special case for fail-open mode.
1896 * We are in fail-open mode and the packet matched the fail-open rule,
1897 * but we are connected to a controller too. We should send the packet
1898 * up to the controller in the hope that it will try to set up a flow
1899 * and thereby allow us to exit fail-open.
1901 * See the top-level comment in fail-open.c for more information.
1903 pinsched_send(p->miss_sched, packet->in_port, packet,
1904 send_packet_in_miss, p);
1906 wdp_packet_destroy(packet);
1911 handle_wdp_packet(struct ofproto *p, struct wdp_packet *packet)
1913 switch (packet->channel) {
1914 case WDP_CHAN_ACTION:
1915 COVERAGE_INC(ofproto_ctlr_action);
1916 pinsched_send(p->action_sched, packet->in_port, packet,
1917 send_packet_in_action, p);
1920 case WDP_CHAN_SFLOW:
1922 wdp_packet_destroy(packet);
1926 handle_flow_miss(p, packet);
1931 wdp_packet_destroy(packet);
1932 VLOG_WARN_RL(&rl, "received message on unexpected channel %d",
1933 (int) packet->channel);
1938 static struct ofpbuf *
1939 compose_flow_removed(const struct wdp_rule *rule, uint8_t reason)
1941 long long int tdiff = time_msec() - rule->created;
1942 uint32_t sec = tdiff / 1000;
1943 uint32_t msec = tdiff - (sec * 1000);
1944 struct ofp_flow_removed *ofr;
1947 ofr = make_openflow(sizeof *ofr, OFPT_FLOW_REMOVED, &buf);
1948 flow_to_match(&rule->cr.flow, &ofr->match);
1949 ofr->cookie = ofproto_rule_cast(rule)->flow_cookie;
1950 ofr->priority = htons(rule->cr.flow.priority);
1951 ofr->reason = reason;
1952 ofr->duration_sec = htonl(sec);
1953 ofr->duration_nsec = htonl(msec * 1000000);
1954 ofr->idle_timeout = htons(rule->idle_timeout);
1960 delete_flow(struct ofproto *p, struct wdp_rule *rule, uint8_t reason)
1962 /* We limit the maximum number of queued flow expirations it by accounting
1963 * them under the counter for replies. That works because preventing
1964 * OpenFlow requests from being processed also prevents new flows from
1965 * being added (and expiring). (It also prevents processing OpenFlow
1966 * requests that would not add new flows, so it is imperfect.) */
1968 struct ofproto_rule *ofproto_rule = ofproto_rule_cast(rule);
1969 struct wdp_flow_stats stats;
1972 if (ofproto_rule->send_flow_removed) {
1973 /* Compose most of the ofp_flow_removed before 'rule' is destroyed. */
1974 buf = compose_flow_removed(rule, reason);
1979 if (wdp_flow_delete(p->wdp, rule, &stats)) {
1984 struct ofp_flow_removed *ofr;
1985 struct ofconn *prev = NULL;
1986 struct ofconn *ofconn;
1988 /* Compose the parts of the ofp_flow_removed that require stats. */
1990 ofr->packet_count = htonll(stats.n_packets);
1991 ofr->byte_count = htonll(stats.n_bytes);
1993 LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
1994 if (rconn_is_connected(ofconn->rconn)) {
1996 queue_tx(ofpbuf_clone(buf), prev, prev->reply_counter);
2002 queue_tx(buf, prev, prev->reply_counter);
2011 do_send_packet_in(struct ofconn *ofconn, uint32_t buffer_id,
2012 const struct wdp_packet *packet, int send_len)
2017 reason = packet->channel == WDP_CHAN_ACTION ? OFPR_ACTION : OFPR_NO_MATCH;
2018 opi = make_packet_in(buffer_id, packet->in_port, reason,
2019 packet->payload, send_len);
2020 rconn_send_with_limit(ofconn->rconn, opi, ofconn->packet_in_counter, 100);
2024 send_packet_in_action(struct wdp_packet *packet, void *p_)
2026 struct ofproto *p = p_;
2027 struct ofconn *ofconn;
2029 LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
2030 if (ofconn == p->controller || ofconn->miss_send_len) {
2031 do_send_packet_in(ofconn, UINT32_MAX, packet, packet->send_len);
2034 wdp_packet_destroy(packet);
2038 send_packet_in_miss(struct wdp_packet *packet, void *p_)
2040 struct ofproto *p = p_;
2041 bool in_fail_open = p->fail_open && fail_open_is_active(p->fail_open);
2042 struct ofconn *ofconn;
2044 LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
2045 if (ofconn->miss_send_len) {
2046 struct pktbuf *pb = ofconn->pktbuf;
2047 uint32_t buffer_id = (in_fail_open
2049 : pktbuf_save(pb, packet->payload,
2051 int send_len = (buffer_id != UINT32_MAX ? ofconn->miss_send_len
2053 do_send_packet_in(ofconn, buffer_id, packet, send_len);
2056 wdp_packet_destroy(packet);
2060 pick_datapath_id(const struct ofproto *ofproto)
2062 struct wdp_port *port;
2064 wdp_port_query_by_number(ofproto->wdp, OFPP_LOCAL, &port);
2066 uint8_t ea[ETH_ADDR_LEN];
2069 error = netdev_get_etheraddr(port->netdev, ea);
2071 return eth_addr_to_uint64(ea);
2073 VLOG_WARN("could not get MAC address for %s (%s)",
2074 netdev_get_name(port->netdev), strerror(error));
2077 return ofproto->fallback_dpid;
2081 pick_fallback_dpid(void)
2083 uint8_t ea[ETH_ADDR_LEN];
2084 eth_addr_nicira_random(ea);
2085 return eth_addr_to_uint64(ea);