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"
29 #include "dynamic-string.h"
31 #include "fail-open.h"
33 #include "mac-learning.h"
37 #include "ofp-print.h"
39 #include "openflow/nicira-ext.h"
40 #include "openflow/openflow.h"
41 #include "openflow/openflow-mgmt.h"
42 #include "openvswitch/datapath-protocol.h"
46 #include "poll-loop.h"
47 #include "port-array.h"
57 #include "vconn-ssl.h"
60 #define THIS_MODULE VLM_ofproto
70 TABLEID_CLASSIFIER = 1
74 struct netdev *netdev;
75 struct ofp_phy_port opp; /* In host byte order. */
78 static void ofport_free(struct ofport *);
79 static void hton_ofp_phy_port(struct ofp_phy_port *);
81 static int xlate_actions(const union ofp_action *in, size_t n_in,
82 const flow_t *flow, struct ofproto *ofproto,
83 const struct ofpbuf *packet,
84 struct odp_actions *out, tag_type *tags,
85 bool *may_set_up_flow, uint16_t *nf_output_iface);
90 uint64_t flow_cookie; /* Controller-issued identifier.
91 (Kept in network-byte order.) */
92 uint16_t idle_timeout; /* In seconds from time of last use. */
93 uint16_t hard_timeout; /* In seconds from time of creation. */
94 bool send_flow_removed; /* Send a flow removed message? */
95 long long int used; /* Last-used time (0 if never used). */
96 long long int created; /* Creation time. */
97 uint64_t packet_count; /* Number of packets received. */
98 uint64_t byte_count; /* Number of bytes received. */
99 uint64_t accounted_bytes; /* Number of bytes passed to account_cb. */
100 tag_type tags; /* Tags (set only by hooks). */
101 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
103 /* If 'super' is non-NULL, this rule is a subrule, that is, it is an
104 * exact-match rule (having cr.wc.wildcards of 0) generated from the
105 * wildcard rule 'super'. In this case, 'list' is an element of the
108 * If 'super' is NULL, this rule is a super-rule, and 'list' is the head of
109 * a list of subrules. A super-rule with no wildcards (where
110 * cr.wc.wildcards is 0) will never have any subrules. */
116 * A subrule has no actions (it uses the super-rule's actions). */
118 union ofp_action *actions;
122 * A super-rule with wildcard fields never has ODP actions (since the
123 * datapath only supports exact-match flows). */
124 bool installed; /* Installed in datapath? */
125 bool may_install; /* True ordinarily; false if actions must
126 * be reassessed for every packet. */
128 union odp_action *odp_actions;
132 rule_is_hidden(const struct rule *rule)
134 /* Subrules are merely an implementation detail, so hide them from the
136 if (rule->super != NULL) {
140 /* Rules with priority higher than UINT16_MAX are set up by ofproto itself
141 * (e.g. by in-band control) and are intentionally hidden from the
143 if (rule->cr.priority > UINT16_MAX) {
150 static struct rule *rule_create(struct ofproto *, struct rule *super,
151 const union ofp_action *, size_t n_actions,
152 uint16_t idle_timeout, uint16_t hard_timeout,
153 uint64_t flow_cookie, bool send_flow_removed);
154 static void rule_free(struct rule *);
155 static void rule_destroy(struct ofproto *, struct rule *);
156 static struct rule *rule_from_cls_rule(const struct cls_rule *);
157 static void rule_insert(struct ofproto *, struct rule *,
158 struct ofpbuf *packet, uint16_t in_port);
159 static void rule_remove(struct ofproto *, struct rule *);
160 static bool rule_make_actions(struct ofproto *, struct rule *,
161 const struct ofpbuf *packet);
162 static void rule_install(struct ofproto *, struct rule *,
163 struct rule *displaced_rule);
164 static void rule_uninstall(struct ofproto *, struct rule *);
165 static void rule_post_uninstall(struct ofproto *, struct rule *);
166 static void send_flow_removed(struct ofproto *p, struct rule *rule,
167 long long int now, uint8_t reason);
172 struct pktbuf *pktbuf;
175 struct rconn_packet_counter *packet_in_counter;
177 /* Number of OpenFlow messages queued as replies to OpenFlow requests, and
178 * the maximum number before we stop reading OpenFlow requests. */
179 #define OFCONN_REPLY_MAX 100
180 struct rconn_packet_counter *reply_counter;
183 static struct ofconn *ofconn_create(struct ofproto *, struct rconn *);
184 static void ofconn_destroy(struct ofconn *, struct ofproto *);
185 static void ofconn_run(struct ofconn *, struct ofproto *);
186 static void ofconn_wait(struct ofconn *);
187 static void queue_tx(struct ofpbuf *msg, const struct ofconn *ofconn,
188 struct rconn_packet_counter *counter);
192 uint64_t datapath_id; /* Datapath ID. */
193 uint64_t fallback_dpid; /* Datapath ID if no better choice found. */
194 uint64_t mgmt_id; /* Management channel identifier. */
195 char *manufacturer; /* Manufacturer. */
196 char *hardware; /* Hardware. */
197 char *software; /* Software version. */
198 char *serial; /* Serial number. */
199 char *dp_desc; /* Datapath description. */
203 struct netdev_monitor *netdev_monitor;
204 struct port_array ports; /* Index is ODP port nr; ofport->opp.port_no is
206 struct shash port_by_name;
210 struct switch_status *switch_status;
211 struct status_category *ss_cat;
212 struct in_band *in_band;
213 struct discovery *discovery;
214 struct fail_open *fail_open;
215 struct pinsched *miss_sched, *action_sched;
216 struct executer *executer;
217 struct netflow *netflow;
220 struct classifier cls;
221 bool need_revalidate;
222 long long int next_expiration;
223 struct tag_set revalidate_set;
225 /* OpenFlow connections. */
226 struct list all_conns;
227 struct ofconn *controller;
228 struct pvconn **listeners;
230 struct pvconn **snoops;
233 /* Hooks for ovs-vswitchd. */
234 const struct ofhooks *ofhooks;
237 /* Used by default ofhooks. */
238 struct mac_learning *ml;
241 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
243 static const struct ofhooks default_ofhooks;
245 static uint64_t pick_datapath_id(const struct ofproto *);
246 static uint64_t pick_fallback_dpid(void);
247 static void send_packet_in_miss(struct ofpbuf *, void *ofproto);
248 static void send_packet_in_action(struct ofpbuf *, void *ofproto);
249 static void update_used(struct ofproto *);
250 static void update_stats(struct ofproto *, struct rule *,
251 const struct odp_flow_stats *);
252 static void expire_rule(struct cls_rule *, void *ofproto);
253 static void active_timeout(struct ofproto *ofproto, struct rule *rule);
254 static bool revalidate_rule(struct ofproto *p, struct rule *rule);
255 static void revalidate_cb(struct cls_rule *rule_, void *p_);
257 static void handle_odp_msg(struct ofproto *, struct ofpbuf *);
259 static void handle_openflow(struct ofconn *, struct ofproto *,
262 static void refresh_port_group(struct ofproto *, unsigned int group);
263 static void update_port(struct ofproto *, const char *devname);
264 static int init_ports(struct ofproto *);
265 static void reinit_ports(struct ofproto *);
268 ofproto_create(const char *datapath, const struct ofhooks *ofhooks, void *aux,
269 struct ofproto **ofprotop)
271 struct odp_stats stats;
278 /* Connect to datapath and start listening for messages. */
279 error = dpif_open(datapath, &dpif);
281 VLOG_ERR("failed to open datapath %s: %s", datapath, strerror(error));
284 error = dpif_get_dp_stats(dpif, &stats);
286 VLOG_ERR("failed to obtain stats for datapath %s: %s",
287 datapath, strerror(error));
291 error = dpif_recv_set_mask(dpif, ODPL_MISS | ODPL_ACTION);
293 VLOG_ERR("failed to listen on datapath %s: %s",
294 datapath, strerror(error));
298 dpif_flow_flush(dpif);
299 dpif_recv_purge(dpif);
301 /* Initialize settings. */
302 p = xcalloc(1, sizeof *p);
303 p->fallback_dpid = pick_fallback_dpid();
304 p->datapath_id = p->fallback_dpid;
305 p->manufacturer = xstrdup("Nicira Networks, Inc.");
306 p->hardware = xstrdup("Reference Implementation");
307 p->software = xstrdup(VERSION BUILDNR);
308 p->serial = xstrdup("None");
309 p->dp_desc = xstrdup("None");
311 /* Initialize datapath. */
313 p->netdev_monitor = netdev_monitor_create();
314 port_array_init(&p->ports);
315 shash_init(&p->port_by_name);
316 p->max_ports = stats.max_ports;
318 /* Initialize submodules. */
319 p->switch_status = switch_status_create(p);
323 p->miss_sched = p->action_sched = NULL;
327 /* Initialize flow table. */
328 classifier_init(&p->cls);
329 p->need_revalidate = false;
330 p->next_expiration = time_msec() + 1000;
331 tag_set_init(&p->revalidate_set);
333 /* Initialize OpenFlow connections. */
334 list_init(&p->all_conns);
335 p->controller = ofconn_create(p, rconn_create(5, 8));
336 p->controller->pktbuf = pktbuf_create();
337 p->controller->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
343 /* Initialize hooks. */
345 p->ofhooks = ofhooks;
349 p->ofhooks = &default_ofhooks;
351 p->ml = mac_learning_create();
354 /* Register switch status category. */
355 p->ss_cat = switch_status_register(p->switch_status, "remote",
356 rconn_status_cb, p->controller->rconn);
359 error = init_ports(p);
365 /* Pick final datapath ID. */
366 p->datapath_id = pick_datapath_id(p);
367 VLOG_INFO("using datapath ID %016"PRIx64, p->datapath_id);
374 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
376 uint64_t old_dpid = p->datapath_id;
377 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
378 if (p->datapath_id != old_dpid) {
379 VLOG_INFO("datapath ID changed to %016"PRIx64, p->datapath_id);
380 rconn_reconnect(p->controller->rconn);
385 ofproto_set_mgmt_id(struct ofproto *p, uint64_t mgmt_id)
387 p->mgmt_id = mgmt_id;
391 ofproto_set_probe_interval(struct ofproto *p, int probe_interval)
393 probe_interval = probe_interval ? MAX(probe_interval, 5) : 0;
394 rconn_set_probe_interval(p->controller->rconn, probe_interval);
396 int trigger_duration = probe_interval ? probe_interval * 3 : 15;
397 fail_open_set_trigger_duration(p->fail_open, trigger_duration);
402 ofproto_set_max_backoff(struct ofproto *p, int max_backoff)
404 rconn_set_max_backoff(p->controller->rconn, max_backoff);
408 ofproto_set_desc(struct ofproto *p,
409 const char *manufacturer, const char *hardware,
410 const char *software, const char *serial,
414 free(p->manufacturer);
415 p->manufacturer = xstrdup(manufacturer);
419 p->hardware = xstrdup(hardware);
423 p->software = xstrdup(software);
427 p->serial = xstrdup(serial);
431 p->dp_desc = xstrdup(dp_desc);
436 ofproto_set_in_band(struct ofproto *p, bool in_band)
438 if (in_band != (p->in_band != NULL)) {
440 return in_band_create(p, p->dpif, p->switch_status,
441 p->controller->rconn, &p->in_band);
443 ofproto_set_discovery(p, false, NULL, true);
444 in_band_destroy(p->in_band);
447 rconn_reconnect(p->controller->rconn);
453 ofproto_set_discovery(struct ofproto *p, bool discovery,
454 const char *re, bool update_resolv_conf)
456 if (discovery != (p->discovery != NULL)) {
458 int error = ofproto_set_in_band(p, true);
462 error = discovery_create(re, update_resolv_conf,
463 p->dpif, p->switch_status,
469 discovery_destroy(p->discovery);
472 rconn_disconnect(p->controller->rconn);
473 } else if (discovery) {
474 discovery_set_update_resolv_conf(p->discovery, update_resolv_conf);
475 return discovery_set_accept_controller_re(p->discovery, re);
481 ofproto_set_controller(struct ofproto *ofproto, const char *controller)
483 if (ofproto->discovery) {
485 } else if (controller) {
486 if (strcmp(rconn_get_name(ofproto->controller->rconn), controller)) {
487 return rconn_connect(ofproto->controller->rconn, controller);
492 rconn_disconnect(ofproto->controller->rconn);
498 set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
499 const struct svec *svec)
501 struct pvconn **pvconns = *pvconnsp;
502 size_t n_pvconns = *n_pvconnsp;
506 for (i = 0; i < n_pvconns; i++) {
507 pvconn_close(pvconns[i]);
511 pvconns = xmalloc(svec->n * sizeof *pvconns);
513 for (i = 0; i < svec->n; i++) {
514 const char *name = svec->names[i];
515 struct pvconn *pvconn;
518 error = pvconn_open(name, &pvconn);
520 pvconns[n_pvconns++] = pvconn;
522 VLOG_ERR("failed to listen on %s: %s", name, strerror(error));
530 *n_pvconnsp = n_pvconns;
536 ofproto_set_listeners(struct ofproto *ofproto, const struct svec *listeners)
538 return set_pvconns(&ofproto->listeners, &ofproto->n_listeners, listeners);
542 ofproto_set_snoops(struct ofproto *ofproto, const struct svec *snoops)
544 return set_pvconns(&ofproto->snoops, &ofproto->n_snoops, snoops);
548 ofproto_set_netflow(struct ofproto *ofproto,
549 const struct netflow_options *nf_options)
551 if (nf_options->collectors.n) {
552 if (!ofproto->netflow) {
553 ofproto->netflow = netflow_create();
555 return netflow_set_options(ofproto->netflow, nf_options);
557 netflow_destroy(ofproto->netflow);
558 ofproto->netflow = NULL;
564 ofproto_set_failure(struct ofproto *ofproto, bool fail_open)
567 struct rconn *rconn = ofproto->controller->rconn;
568 int trigger_duration = rconn_get_probe_interval(rconn) * 3;
569 if (!ofproto->fail_open) {
570 ofproto->fail_open = fail_open_create(ofproto, trigger_duration,
571 ofproto->switch_status,
574 fail_open_set_trigger_duration(ofproto->fail_open,
578 fail_open_destroy(ofproto->fail_open);
579 ofproto->fail_open = NULL;
584 ofproto_set_rate_limit(struct ofproto *ofproto,
585 int rate_limit, int burst_limit)
587 if (rate_limit > 0) {
588 if (!ofproto->miss_sched) {
589 ofproto->miss_sched = pinsched_create(rate_limit, burst_limit,
590 ofproto->switch_status);
591 ofproto->action_sched = pinsched_create(rate_limit, burst_limit,
594 pinsched_set_limits(ofproto->miss_sched, rate_limit, burst_limit);
595 pinsched_set_limits(ofproto->action_sched,
596 rate_limit, burst_limit);
599 pinsched_destroy(ofproto->miss_sched);
600 ofproto->miss_sched = NULL;
601 pinsched_destroy(ofproto->action_sched);
602 ofproto->action_sched = NULL;
607 ofproto_set_stp(struct ofproto *ofproto UNUSED, bool enable_stp)
611 VLOG_WARN("STP is not yet implemented");
619 ofproto_set_remote_execution(struct ofproto *ofproto, const char *command_acl,
620 const char *command_dir)
623 if (!ofproto->executer) {
624 return executer_create(command_acl, command_dir,
627 executer_set_acl(ofproto->executer, command_acl, command_dir);
630 executer_destroy(ofproto->executer);
631 ofproto->executer = NULL;
637 ofproto_get_datapath_id(const struct ofproto *ofproto)
639 return ofproto->datapath_id;
643 ofproto_get_mgmt_id(const struct ofproto *ofproto)
645 return ofproto->mgmt_id;
649 ofproto_get_probe_interval(const struct ofproto *ofproto)
651 return rconn_get_probe_interval(ofproto->controller->rconn);
655 ofproto_get_max_backoff(const struct ofproto *ofproto)
657 return rconn_get_max_backoff(ofproto->controller->rconn);
661 ofproto_get_in_band(const struct ofproto *ofproto)
663 return ofproto->in_band != NULL;
667 ofproto_get_discovery(const struct ofproto *ofproto)
669 return ofproto->discovery != NULL;
673 ofproto_get_controller(const struct ofproto *ofproto)
675 return rconn_get_name(ofproto->controller->rconn);
679 ofproto_get_listeners(const struct ofproto *ofproto, struct svec *listeners)
683 for (i = 0; i < ofproto->n_listeners; i++) {
684 svec_add(listeners, pvconn_get_name(ofproto->listeners[i]));
689 ofproto_get_snoops(const struct ofproto *ofproto, struct svec *snoops)
693 for (i = 0; i < ofproto->n_snoops; i++) {
694 svec_add(snoops, pvconn_get_name(ofproto->snoops[i]));
699 ofproto_destroy(struct ofproto *p)
701 struct ofconn *ofconn, *next_ofconn;
702 struct ofport *ofport;
703 unsigned int port_no;
710 ofproto_flush_flows(p);
711 classifier_destroy(&p->cls);
713 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, struct ofconn, node,
715 ofconn_destroy(ofconn, p);
719 netdev_monitor_destroy(p->netdev_monitor);
720 PORT_ARRAY_FOR_EACH (ofport, &p->ports, port_no) {
723 shash_destroy(&p->port_by_name);
725 switch_status_destroy(p->switch_status);
726 in_band_destroy(p->in_band);
727 discovery_destroy(p->discovery);
728 fail_open_destroy(p->fail_open);
729 pinsched_destroy(p->miss_sched);
730 pinsched_destroy(p->action_sched);
731 executer_destroy(p->executer);
732 netflow_destroy(p->netflow);
734 switch_status_unregister(p->ss_cat);
736 for (i = 0; i < p->n_listeners; i++) {
737 pvconn_close(p->listeners[i]);
741 for (i = 0; i < p->n_snoops; i++) {
742 pvconn_close(p->snoops[i]);
746 mac_learning_destroy(p->ml);
752 ofproto_run(struct ofproto *p)
754 int error = ofproto_run1(p);
756 error = ofproto_run2(p, false);
762 process_port_change(struct ofproto *ofproto, int error, char *devname)
764 if (error == ENOBUFS) {
765 reinit_ports(ofproto);
767 update_port(ofproto, devname);
773 ofproto_run1(struct ofproto *p)
775 struct ofconn *ofconn, *next_ofconn;
780 for (i = 0; i < 50; i++) {
784 error = dpif_recv(p->dpif, &buf);
786 if (error == ENODEV) {
787 /* Someone destroyed the datapath behind our back. The caller
788 * better destroy us and give up, because we're just going to
789 * spin from here on out. */
790 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
791 VLOG_ERR_RL(&rl, "%s: datapath was destroyed externally",
798 handle_odp_msg(p, buf);
801 while ((error = dpif_port_poll(p->dpif, &devname)) != EAGAIN) {
802 process_port_change(p, error, devname);
804 while ((error = netdev_monitor_poll(p->netdev_monitor,
805 &devname)) != EAGAIN) {
806 process_port_change(p, error, devname);
810 in_band_run(p->in_band);
813 char *controller_name;
814 if (rconn_is_connectivity_questionable(p->controller->rconn)) {
815 discovery_question_connectivity(p->discovery);
817 if (discovery_run(p->discovery, &controller_name)) {
818 if (controller_name) {
819 rconn_connect(p->controller->rconn, controller_name);
821 rconn_disconnect(p->controller->rconn);
825 pinsched_run(p->miss_sched, send_packet_in_miss, p);
826 pinsched_run(p->action_sched, send_packet_in_action, p);
828 executer_run(p->executer);
831 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, struct ofconn, node,
833 ofconn_run(ofconn, p);
836 /* Fail-open maintenance. Do this after processing the ofconns since
837 * fail-open checks the status of the controller rconn. */
839 fail_open_run(p->fail_open);
842 for (i = 0; i < p->n_listeners; i++) {
846 retval = pvconn_accept(p->listeners[i], OFP_VERSION, &vconn);
848 ofconn_create(p, rconn_new_from_vconn("passive", vconn));
849 } else if (retval != EAGAIN) {
850 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
854 for (i = 0; i < p->n_snoops; i++) {
858 retval = pvconn_accept(p->snoops[i], OFP_VERSION, &vconn);
860 rconn_add_monitor(p->controller->rconn, vconn);
861 } else if (retval != EAGAIN) {
862 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
866 if (time_msec() >= p->next_expiration) {
867 COVERAGE_INC(ofproto_expiration);
868 p->next_expiration = time_msec() + 1000;
871 classifier_for_each(&p->cls, CLS_INC_ALL, expire_rule, p);
873 /* Let the hook know that we're at a stable point: all outstanding data
874 * in existing flows has been accounted to the account_cb. Thus, the
875 * hook can now reasonably do operations that depend on having accurate
876 * flow volume accounting (currently, that's just bond rebalancing). */
877 if (p->ofhooks->account_checkpoint_cb) {
878 p->ofhooks->account_checkpoint_cb(p->aux);
883 netflow_run(p->netflow);
889 struct revalidate_cbdata {
890 struct ofproto *ofproto;
891 bool revalidate_all; /* Revalidate all exact-match rules? */
892 bool revalidate_subrules; /* Revalidate all exact-match subrules? */
893 struct tag_set revalidate_set; /* Set of tags to revalidate. */
897 ofproto_run2(struct ofproto *p, bool revalidate_all)
899 if (p->need_revalidate || revalidate_all
900 || !tag_set_is_empty(&p->revalidate_set)) {
901 struct revalidate_cbdata cbdata;
903 cbdata.revalidate_all = revalidate_all;
904 cbdata.revalidate_subrules = p->need_revalidate;
905 cbdata.revalidate_set = p->revalidate_set;
906 tag_set_init(&p->revalidate_set);
907 COVERAGE_INC(ofproto_revalidate);
908 classifier_for_each(&p->cls, CLS_INC_EXACT, revalidate_cb, &cbdata);
909 p->need_revalidate = false;
916 ofproto_wait(struct ofproto *p)
918 struct ofconn *ofconn;
921 dpif_recv_wait(p->dpif);
922 dpif_port_poll_wait(p->dpif);
923 netdev_monitor_poll_wait(p->netdev_monitor);
924 LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
928 in_band_wait(p->in_band);
931 discovery_wait(p->discovery);
934 fail_open_wait(p->fail_open);
936 pinsched_wait(p->miss_sched);
937 pinsched_wait(p->action_sched);
939 executer_wait(p->executer);
941 if (!tag_set_is_empty(&p->revalidate_set)) {
942 poll_immediate_wake();
944 if (p->need_revalidate) {
945 /* Shouldn't happen, but if it does just go around again. */
946 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
947 poll_immediate_wake();
948 } else if (p->next_expiration != LLONG_MAX) {
949 poll_timer_wait(p->next_expiration - time_msec());
951 for (i = 0; i < p->n_listeners; i++) {
952 pvconn_wait(p->listeners[i]);
954 for (i = 0; i < p->n_snoops; i++) {
955 pvconn_wait(p->snoops[i]);
960 ofproto_revalidate(struct ofproto *ofproto, tag_type tag)
962 tag_set_add(&ofproto->revalidate_set, tag);
966 ofproto_get_revalidate_set(struct ofproto *ofproto)
968 return &ofproto->revalidate_set;
972 ofproto_is_alive(const struct ofproto *p)
974 return p->discovery || rconn_is_alive(p->controller->rconn);
978 ofproto_send_packet(struct ofproto *p, const flow_t *flow,
979 const union ofp_action *actions, size_t n_actions,
980 const struct ofpbuf *packet)
982 struct odp_actions odp_actions;
985 error = xlate_actions(actions, n_actions, flow, p, packet, &odp_actions,
991 /* XXX Should we translate the dpif_execute() errno value into an OpenFlow
993 dpif_execute(p->dpif, flow->in_port, odp_actions.actions,
994 odp_actions.n_actions, packet);
999 ofproto_add_flow(struct ofproto *p,
1000 const flow_t *flow, uint32_t wildcards, unsigned int priority,
1001 const union ofp_action *actions, size_t n_actions,
1005 rule = rule_create(p, NULL, actions, n_actions,
1006 idle_timeout >= 0 ? idle_timeout : 5 /* XXX */,
1008 cls_rule_from_flow(&rule->cr, flow, wildcards, priority);
1009 rule_insert(p, rule, NULL, 0);
1013 ofproto_delete_flow(struct ofproto *ofproto, const flow_t *flow,
1014 uint32_t wildcards, unsigned int priority)
1018 rule = rule_from_cls_rule(classifier_find_rule_exactly(&ofproto->cls,
1022 rule_remove(ofproto, rule);
1027 destroy_rule(struct cls_rule *rule_, void *ofproto_)
1029 struct rule *rule = rule_from_cls_rule(rule_);
1030 struct ofproto *ofproto = ofproto_;
1032 /* Mark the flow as not installed, even though it might really be
1033 * installed, so that rule_remove() doesn't bother trying to uninstall it.
1034 * There is no point in uninstalling it individually since we are about to
1035 * blow away all the flows with dpif_flow_flush(). */
1036 rule->installed = false;
1038 rule_remove(ofproto, rule);
1042 ofproto_flush_flows(struct ofproto *ofproto)
1044 COVERAGE_INC(ofproto_flush);
1045 classifier_for_each(&ofproto->cls, CLS_INC_ALL, destroy_rule, ofproto);
1046 dpif_flow_flush(ofproto->dpif);
1047 if (ofproto->in_band) {
1048 in_band_flushed(ofproto->in_band);
1050 if (ofproto->fail_open) {
1051 fail_open_flushed(ofproto->fail_open);
1056 reinit_ports(struct ofproto *p)
1058 struct svec devnames;
1059 struct ofport *ofport;
1060 unsigned int port_no;
1061 struct odp_port *odp_ports;
1065 svec_init(&devnames);
1066 PORT_ARRAY_FOR_EACH (ofport, &p->ports, port_no) {
1067 svec_add (&devnames, (char *) ofport->opp.name);
1069 dpif_port_list(p->dpif, &odp_ports, &n_odp_ports);
1070 for (i = 0; i < n_odp_ports; i++) {
1071 svec_add (&devnames, odp_ports[i].devname);
1075 svec_sort_unique(&devnames);
1076 for (i = 0; i < devnames.n; i++) {
1077 update_port(p, devnames.names[i]);
1079 svec_destroy(&devnames);
1083 refresh_port_group(struct ofproto *p, unsigned int group)
1087 struct ofport *port;
1088 unsigned int port_no;
1090 assert(group == DP_GROUP_ALL || group == DP_GROUP_FLOOD);
1092 ports = xmalloc(port_array_count(&p->ports) * sizeof *ports);
1094 PORT_ARRAY_FOR_EACH (port, &p->ports, port_no) {
1095 if (group == DP_GROUP_ALL || !(port->opp.config & OFPPC_NO_FLOOD)) {
1096 ports[n_ports++] = port_no;
1099 dpif_port_group_set(p->dpif, group, ports, n_ports);
1104 refresh_port_groups(struct ofproto *p)
1106 refresh_port_group(p, DP_GROUP_FLOOD);
1107 refresh_port_group(p, DP_GROUP_ALL);
1110 static struct ofport *
1111 make_ofport(const struct odp_port *odp_port)
1113 enum netdev_flags flags;
1114 struct ofport *ofport;
1115 struct netdev *netdev;
1119 error = netdev_open(odp_port->devname, NETDEV_ETH_TYPE_NONE, &netdev);
1121 VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
1122 "cannot be opened (%s)",
1123 odp_port->devname, odp_port->port,
1124 odp_port->devname, strerror(error));
1128 ofport = xmalloc(sizeof *ofport);
1129 ofport->netdev = netdev;
1130 ofport->opp.port_no = odp_port_to_ofp_port(odp_port->port);
1131 netdev_get_etheraddr(netdev, ofport->opp.hw_addr);
1132 memcpy(ofport->opp.name, odp_port->devname,
1133 MIN(sizeof ofport->opp.name, sizeof odp_port->devname));
1134 ofport->opp.name[sizeof ofport->opp.name - 1] = '\0';
1136 netdev_get_flags(netdev, &flags);
1137 ofport->opp.config = flags & NETDEV_UP ? 0 : OFPPC_PORT_DOWN;
1139 netdev_get_carrier(netdev, &carrier);
1140 ofport->opp.state = carrier ? 0 : OFPPS_LINK_DOWN;
1142 netdev_get_features(netdev,
1143 &ofport->opp.curr, &ofport->opp.advertised,
1144 &ofport->opp.supported, &ofport->opp.peer);
1149 ofport_conflicts(const struct ofproto *p, const struct odp_port *odp_port)
1151 if (port_array_get(&p->ports, odp_port->port)) {
1152 VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
1155 } else if (shash_find(&p->port_by_name, odp_port->devname)) {
1156 VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
1165 ofport_equal(const struct ofport *a_, const struct ofport *b_)
1167 const struct ofp_phy_port *a = &a_->opp;
1168 const struct ofp_phy_port *b = &b_->opp;
1170 BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */
1171 return (a->port_no == b->port_no
1172 && !memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr)
1173 && !strcmp((char *) a->name, (char *) b->name)
1174 && a->state == b->state
1175 && a->config == b->config
1176 && a->curr == b->curr
1177 && a->advertised == b->advertised
1178 && a->supported == b->supported
1179 && a->peer == b->peer);
1183 send_port_status(struct ofproto *p, const struct ofport *ofport,
1186 /* XXX Should limit the number of queued port status change messages. */
1187 struct ofconn *ofconn;
1188 LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
1189 struct ofp_port_status *ops;
1192 ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &b);
1193 ops->reason = reason;
1194 ops->desc = ofport->opp;
1195 hton_ofp_phy_port(&ops->desc);
1196 queue_tx(b, ofconn, NULL);
1198 if (p->ofhooks->port_changed_cb) {
1199 p->ofhooks->port_changed_cb(reason, &ofport->opp, p->aux);
1204 ofport_install(struct ofproto *p, struct ofport *ofport)
1206 netdev_monitor_add(p->netdev_monitor, ofport->netdev);
1207 port_array_set(&p->ports, ofp_port_to_odp_port(ofport->opp.port_no),
1209 shash_add(&p->port_by_name, (char *) ofport->opp.name, ofport);
1213 ofport_remove(struct ofproto *p, struct ofport *ofport)
1215 netdev_monitor_remove(p->netdev_monitor, ofport->netdev);
1216 port_array_set(&p->ports, ofp_port_to_odp_port(ofport->opp.port_no), NULL);
1217 shash_delete(&p->port_by_name,
1218 shash_find(&p->port_by_name, (char *) ofport->opp.name));
1222 ofport_free(struct ofport *ofport)
1225 netdev_close(ofport->netdev);
1231 update_port(struct ofproto *p, const char *devname)
1233 struct odp_port odp_port;
1234 struct ofport *old_ofport;
1235 struct ofport *new_ofport;
1238 COVERAGE_INC(ofproto_update_port);
1240 /* Query the datapath for port information. */
1241 error = dpif_port_query_by_name(p->dpif, devname, &odp_port);
1243 /* Find the old ofport. */
1244 old_ofport = shash_find_data(&p->port_by_name, devname);
1247 /* There's no port named 'devname' but there might be a port with
1248 * the same port number. This could happen if a port is deleted
1249 * and then a new one added in its place very quickly, or if a port
1250 * is renamed. In the former case we want to send an OFPPR_DELETE
1251 * and an OFPPR_ADD, and in the latter case we want to send a
1252 * single OFPPR_MODIFY. We can distinguish the cases by comparing
1253 * the old port's ifindex against the new port, or perhaps less
1254 * reliably but more portably by comparing the old port's MAC
1255 * against the new port's MAC. However, this code isn't that smart
1256 * and always sends an OFPPR_MODIFY (XXX). */
1257 old_ofport = port_array_get(&p->ports, odp_port.port);
1259 } else if (error != ENOENT && error != ENODEV) {
1260 VLOG_WARN_RL(&rl, "dpif_port_query_by_name returned unexpected error "
1261 "%s", strerror(error));
1265 /* Create a new ofport. */
1266 new_ofport = !error ? make_ofport(&odp_port) : NULL;
1268 /* Eliminate a few pathological cases. */
1269 if (!old_ofport && !new_ofport) {
1271 } else if (old_ofport && new_ofport) {
1272 /* Most of the 'config' bits are OpenFlow soft state, but
1273 * OFPPC_PORT_DOWN is maintained the kernel. So transfer the OpenFlow
1274 * bits from old_ofport. (make_ofport() only sets OFPPC_PORT_DOWN and
1275 * leaves the other bits 0.) */
1276 new_ofport->opp.config |= old_ofport->opp.config & ~OFPPC_PORT_DOWN;
1278 if (ofport_equal(old_ofport, new_ofport)) {
1279 /* False alarm--no change. */
1280 ofport_free(new_ofport);
1285 /* Now deal with the normal cases. */
1287 ofport_remove(p, old_ofport);
1290 ofport_install(p, new_ofport);
1292 send_port_status(p, new_ofport ? new_ofport : old_ofport,
1293 (!old_ofport ? OFPPR_ADD
1294 : !new_ofport ? OFPPR_DELETE
1296 ofport_free(old_ofport);
1298 /* Update port groups. */
1299 refresh_port_groups(p);
1303 init_ports(struct ofproto *p)
1305 struct odp_port *ports;
1310 error = dpif_port_list(p->dpif, &ports, &n_ports);
1315 for (i = 0; i < n_ports; i++) {
1316 const struct odp_port *odp_port = &ports[i];
1317 if (!ofport_conflicts(p, odp_port)) {
1318 struct ofport *ofport = make_ofport(odp_port);
1320 ofport_install(p, ofport);
1325 refresh_port_groups(p);
1329 static struct ofconn *
1330 ofconn_create(struct ofproto *p, struct rconn *rconn)
1332 struct ofconn *ofconn = xmalloc(sizeof *ofconn);
1333 list_push_back(&p->all_conns, &ofconn->node);
1334 ofconn->rconn = rconn;
1335 ofconn->pktbuf = NULL;
1336 ofconn->miss_send_len = 0;
1337 ofconn->packet_in_counter = rconn_packet_counter_create ();
1338 ofconn->reply_counter = rconn_packet_counter_create ();
1343 ofconn_destroy(struct ofconn *ofconn, struct ofproto *p)
1346 executer_rconn_closing(p->executer, ofconn->rconn);
1349 list_remove(&ofconn->node);
1350 rconn_destroy(ofconn->rconn);
1351 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1352 rconn_packet_counter_destroy(ofconn->reply_counter);
1353 pktbuf_destroy(ofconn->pktbuf);
1358 ofconn_run(struct ofconn *ofconn, struct ofproto *p)
1362 rconn_run(ofconn->rconn);
1364 if (rconn_packet_counter_read (ofconn->reply_counter) < OFCONN_REPLY_MAX) {
1365 /* Limit the number of iterations to prevent other tasks from
1367 for (iteration = 0; iteration < 50; iteration++) {
1368 struct ofpbuf *of_msg = rconn_recv(ofconn->rconn);
1373 fail_open_maybe_recover(p->fail_open);
1375 handle_openflow(ofconn, p, of_msg);
1376 ofpbuf_delete(of_msg);
1380 if (ofconn != p->controller && !rconn_is_alive(ofconn->rconn)) {
1381 ofconn_destroy(ofconn, p);
1386 ofconn_wait(struct ofconn *ofconn)
1388 rconn_run_wait(ofconn->rconn);
1389 if (rconn_packet_counter_read (ofconn->reply_counter) < OFCONN_REPLY_MAX) {
1390 rconn_recv_wait(ofconn->rconn);
1392 COVERAGE_INC(ofproto_ofconn_stuck);
1396 /* Caller is responsible for initializing the 'cr' member of the returned
1398 static struct rule *
1399 rule_create(struct ofproto *ofproto, struct rule *super,
1400 const union ofp_action *actions, size_t n_actions,
1401 uint16_t idle_timeout, uint16_t hard_timeout,
1402 uint64_t flow_cookie, bool send_flow_removed)
1404 struct rule *rule = xcalloc(1, sizeof *rule);
1405 rule->idle_timeout = idle_timeout;
1406 rule->hard_timeout = hard_timeout;
1407 rule->flow_cookie = flow_cookie;
1408 rule->used = rule->created = time_msec();
1409 rule->send_flow_removed = send_flow_removed;
1410 rule->super = super;
1412 list_push_back(&super->list, &rule->list);
1414 list_init(&rule->list);
1416 rule->n_actions = n_actions;
1417 rule->actions = xmemdup(actions, n_actions * sizeof *actions);
1418 netflow_flow_clear(&rule->nf_flow);
1419 netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, rule->created);
1424 static struct rule *
1425 rule_from_cls_rule(const struct cls_rule *cls_rule)
1427 return cls_rule ? CONTAINER_OF(cls_rule, struct rule, cr) : NULL;
1431 rule_free(struct rule *rule)
1433 free(rule->actions);
1434 free(rule->odp_actions);
1438 /* Destroys 'rule'. If 'rule' is a subrule, also removes it from its
1439 * super-rule's list of subrules. If 'rule' is a super-rule, also iterates
1440 * through all of its subrules and revalidates them, destroying any that no
1441 * longer has a super-rule (which is probably all of them).
1443 * Before calling this function, the caller must make have removed 'rule' from
1444 * the classifier. If 'rule' is an exact-match rule, the caller is also
1445 * responsible for ensuring that it has been uninstalled from the datapath. */
1447 rule_destroy(struct ofproto *ofproto, struct rule *rule)
1450 struct rule *subrule, *next;
1451 LIST_FOR_EACH_SAFE (subrule, next, struct rule, list, &rule->list) {
1452 revalidate_rule(ofproto, subrule);
1455 list_remove(&rule->list);
1461 rule_has_out_port(const struct rule *rule, uint16_t out_port)
1463 const union ofp_action *oa;
1464 struct actions_iterator i;
1466 if (out_port == htons(OFPP_NONE)) {
1469 for (oa = actions_first(&i, rule->actions, rule->n_actions); oa;
1470 oa = actions_next(&i)) {
1471 if (oa->type == htons(OFPAT_OUTPUT) && oa->output.port == out_port) {
1478 /* Executes the actions indicated by 'rule' on 'packet', which is in flow
1479 * 'flow' and is considered to have arrived on ODP port 'in_port'.
1481 * The flow that 'packet' actually contains does not need to actually match
1482 * 'rule'; the actions in 'rule' will be applied to it either way. Likewise,
1483 * the packet and byte counters for 'rule' will be credited for the packet sent
1484 * out whether or not the packet actually matches 'rule'.
1486 * If 'rule' is an exact-match rule and 'flow' actually equals the rule's flow,
1487 * the caller must already have accurately composed ODP actions for it given
1488 * 'packet' using rule_make_actions(). If 'rule' is a wildcard rule, or if
1489 * 'rule' is an exact-match rule but 'flow' is not the rule's flow, then this
1490 * function will compose a set of ODP actions based on 'rule''s OpenFlow
1491 * actions and apply them to 'packet'. */
1493 rule_execute(struct ofproto *ofproto, struct rule *rule,
1494 struct ofpbuf *packet, const flow_t *flow)
1496 const union odp_action *actions;
1498 struct odp_actions a;
1500 /* Grab or compose the ODP actions.
1502 * The special case for an exact-match 'rule' where 'flow' is not the
1503 * rule's flow is important to avoid, e.g., sending a packet out its input
1504 * port simply because the ODP actions were composed for the wrong
1506 if (rule->cr.wc.wildcards || !flow_equal(flow, &rule->cr.flow)) {
1507 struct rule *super = rule->super ? rule->super : rule;
1508 if (xlate_actions(super->actions, super->n_actions, flow, ofproto,
1509 packet, &a, NULL, 0, NULL)) {
1512 actions = a.actions;
1513 n_actions = a.n_actions;
1515 actions = rule->odp_actions;
1516 n_actions = rule->n_odp_actions;
1519 /* Execute the ODP actions. */
1520 if (!dpif_execute(ofproto->dpif, flow->in_port,
1521 actions, n_actions, packet)) {
1522 struct odp_flow_stats stats;
1523 flow_extract_stats(flow, packet, &stats);
1524 update_stats(ofproto, rule, &stats);
1525 rule->used = time_msec();
1526 netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, rule->used);
1531 rule_insert(struct ofproto *p, struct rule *rule, struct ofpbuf *packet,
1534 struct rule *displaced_rule;
1536 /* Insert the rule in the classifier. */
1537 displaced_rule = rule_from_cls_rule(classifier_insert(&p->cls, &rule->cr));
1538 if (!rule->cr.wc.wildcards) {
1539 rule_make_actions(p, rule, packet);
1542 /* Send the packet and credit it to the rule. */
1545 flow_extract(packet, in_port, &flow);
1546 rule_execute(p, rule, packet, &flow);
1549 /* Install the rule in the datapath only after sending the packet, to
1550 * avoid packet reordering. */
1551 if (rule->cr.wc.wildcards) {
1552 COVERAGE_INC(ofproto_add_wc_flow);
1553 p->need_revalidate = true;
1555 rule_install(p, rule, displaced_rule);
1558 /* Free the rule that was displaced, if any. */
1559 if (displaced_rule) {
1560 rule_destroy(p, displaced_rule);
1564 static struct rule *
1565 rule_create_subrule(struct ofproto *ofproto, struct rule *rule,
1568 struct rule *subrule = rule_create(ofproto, rule, NULL, 0,
1569 rule->idle_timeout, rule->hard_timeout,
1571 COVERAGE_INC(ofproto_subrule_create);
1572 cls_rule_from_flow(&subrule->cr, flow, 0,
1573 (rule->cr.priority <= UINT16_MAX ? UINT16_MAX
1574 : rule->cr.priority));
1575 classifier_insert_exact(&ofproto->cls, &subrule->cr);
1581 rule_remove(struct ofproto *ofproto, struct rule *rule)
1583 if (rule->cr.wc.wildcards) {
1584 COVERAGE_INC(ofproto_del_wc_flow);
1585 ofproto->need_revalidate = true;
1587 rule_uninstall(ofproto, rule);
1589 classifier_remove(&ofproto->cls, &rule->cr);
1590 rule_destroy(ofproto, rule);
1593 /* Returns true if the actions changed, false otherwise. */
1595 rule_make_actions(struct ofproto *p, struct rule *rule,
1596 const struct ofpbuf *packet)
1598 const struct rule *super;
1599 struct odp_actions a;
1602 assert(!rule->cr.wc.wildcards);
1604 super = rule->super ? rule->super : rule;
1606 xlate_actions(super->actions, super->n_actions, &rule->cr.flow, p,
1607 packet, &a, &rule->tags, &rule->may_install,
1608 &rule->nf_flow.output_iface);
1610 actions_len = a.n_actions * sizeof *a.actions;
1611 if (rule->n_odp_actions != a.n_actions
1612 || memcmp(rule->odp_actions, a.actions, actions_len)) {
1613 COVERAGE_INC(ofproto_odp_unchanged);
1614 free(rule->odp_actions);
1615 rule->n_odp_actions = a.n_actions;
1616 rule->odp_actions = xmemdup(a.actions, actions_len);
1624 do_put_flow(struct ofproto *ofproto, struct rule *rule, int flags,
1625 struct odp_flow_put *put)
1627 memset(&put->flow.stats, 0, sizeof put->flow.stats);
1628 put->flow.key = rule->cr.flow;
1629 put->flow.actions = rule->odp_actions;
1630 put->flow.n_actions = rule->n_odp_actions;
1632 return dpif_flow_put(ofproto->dpif, put);
1636 rule_install(struct ofproto *p, struct rule *rule, struct rule *displaced_rule)
1638 assert(!rule->cr.wc.wildcards);
1640 if (rule->may_install) {
1641 struct odp_flow_put put;
1642 if (!do_put_flow(p, rule,
1643 ODPPF_CREATE | ODPPF_MODIFY | ODPPF_ZERO_STATS,
1645 rule->installed = true;
1646 if (displaced_rule) {
1647 update_stats(p, displaced_rule, &put.flow.stats);
1648 rule_post_uninstall(p, displaced_rule);
1651 } else if (displaced_rule) {
1652 rule_uninstall(p, displaced_rule);
1657 rule_reinstall(struct ofproto *ofproto, struct rule *rule)
1659 if (rule->installed) {
1660 struct odp_flow_put put;
1661 COVERAGE_INC(ofproto_dp_missed);
1662 do_put_flow(ofproto, rule, ODPPF_CREATE | ODPPF_MODIFY, &put);
1664 rule_install(ofproto, rule, NULL);
1669 rule_update_actions(struct ofproto *ofproto, struct rule *rule)
1671 bool actions_changed;
1672 uint16_t new_out_iface, old_out_iface;
1674 old_out_iface = rule->nf_flow.output_iface;
1675 actions_changed = rule_make_actions(ofproto, rule, NULL);
1677 if (rule->may_install) {
1678 if (rule->installed) {
1679 if (actions_changed) {
1680 struct odp_flow_put put;
1681 do_put_flow(ofproto, rule, ODPPF_CREATE | ODPPF_MODIFY
1682 | ODPPF_ZERO_STATS, &put);
1683 update_stats(ofproto, rule, &put.flow.stats);
1685 /* Temporarily set the old output iface so that NetFlow
1686 * messages have the correct output interface for the old
1688 new_out_iface = rule->nf_flow.output_iface;
1689 rule->nf_flow.output_iface = old_out_iface;
1690 rule_post_uninstall(ofproto, rule);
1691 rule->nf_flow.output_iface = new_out_iface;
1694 rule_install(ofproto, rule, NULL);
1697 rule_uninstall(ofproto, rule);
1702 rule_account(struct ofproto *ofproto, struct rule *rule, uint64_t extra_bytes)
1704 uint64_t total_bytes = rule->byte_count + extra_bytes;
1706 if (ofproto->ofhooks->account_flow_cb
1707 && total_bytes > rule->accounted_bytes)
1709 ofproto->ofhooks->account_flow_cb(
1710 &rule->cr.flow, rule->odp_actions, rule->n_odp_actions,
1711 total_bytes - rule->accounted_bytes, ofproto->aux);
1712 rule->accounted_bytes = total_bytes;
1717 rule_uninstall(struct ofproto *p, struct rule *rule)
1719 assert(!rule->cr.wc.wildcards);
1720 if (rule->installed) {
1721 struct odp_flow odp_flow;
1723 odp_flow.key = rule->cr.flow;
1724 odp_flow.actions = NULL;
1725 odp_flow.n_actions = 0;
1726 if (!dpif_flow_del(p->dpif, &odp_flow)) {
1727 update_stats(p, rule, &odp_flow.stats);
1729 rule->installed = false;
1731 rule_post_uninstall(p, rule);
1736 is_controller_rule(struct rule *rule)
1738 /* If the only action is send to the controller then don't report
1739 * NetFlow expiration messages since it is just part of the control
1740 * logic for the network and not real traffic. */
1742 if (rule && rule->super) {
1743 struct rule *super = rule->super;
1745 return super->n_actions == 1 &&
1746 super->actions[0].type == htons(OFPAT_OUTPUT) &&
1747 super->actions[0].output.port == htons(OFPP_CONTROLLER);
1754 rule_post_uninstall(struct ofproto *ofproto, struct rule *rule)
1756 struct rule *super = rule->super;
1758 rule_account(ofproto, rule, 0);
1760 if (ofproto->netflow && !is_controller_rule(rule)) {
1761 struct ofexpired expired;
1762 expired.flow = rule->cr.flow;
1763 expired.packet_count = rule->packet_count;
1764 expired.byte_count = rule->byte_count;
1765 expired.used = rule->used;
1766 netflow_expire(ofproto->netflow, &rule->nf_flow, &expired);
1769 super->packet_count += rule->packet_count;
1770 super->byte_count += rule->byte_count;
1772 /* Reset counters to prevent double counting if the rule ever gets
1774 rule->packet_count = 0;
1775 rule->byte_count = 0;
1776 rule->accounted_bytes = 0;
1778 netflow_flow_clear(&rule->nf_flow);
1783 queue_tx(struct ofpbuf *msg, const struct ofconn *ofconn,
1784 struct rconn_packet_counter *counter)
1786 update_openflow_length(msg);
1787 if (rconn_send(ofconn->rconn, msg, counter)) {
1793 send_error(const struct ofconn *ofconn, const struct ofp_header *oh,
1794 int error, const void *data, size_t len)
1797 struct ofp_error_msg *oem;
1799 if (!(error >> 16)) {
1800 VLOG_WARN_RL(&rl, "not sending bad error code %d to controller",
1805 COVERAGE_INC(ofproto_error);
1806 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR,
1807 oh ? oh->xid : 0, &buf);
1808 oem->type = htons((unsigned int) error >> 16);
1809 oem->code = htons(error & 0xffff);
1810 memcpy(oem->data, data, len);
1811 queue_tx(buf, ofconn, ofconn->reply_counter);
1815 send_error_oh(const struct ofconn *ofconn, const struct ofp_header *oh,
1818 size_t oh_length = ntohs(oh->length);
1819 send_error(ofconn, oh, error, oh, MIN(oh_length, 64));
1823 hton_ofp_phy_port(struct ofp_phy_port *opp)
1825 opp->port_no = htons(opp->port_no);
1826 opp->config = htonl(opp->config);
1827 opp->state = htonl(opp->state);
1828 opp->curr = htonl(opp->curr);
1829 opp->advertised = htonl(opp->advertised);
1830 opp->supported = htonl(opp->supported);
1831 opp->peer = htonl(opp->peer);
1835 handle_echo_request(struct ofconn *ofconn, struct ofp_header *oh)
1837 struct ofp_header *rq = oh;
1838 queue_tx(make_echo_reply(rq), ofconn, ofconn->reply_counter);
1843 handle_features_request(struct ofproto *p, struct ofconn *ofconn,
1844 struct ofp_header *oh)
1846 struct ofp_switch_features *osf;
1848 unsigned int port_no;
1849 struct ofport *port;
1851 osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, oh->xid, &buf);
1852 osf->datapath_id = htonll(p->datapath_id);
1853 osf->n_buffers = htonl(pktbuf_capacity());
1855 osf->capabilities = htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS |
1856 OFPC_PORT_STATS | OFPC_MULTI_PHY_TX);
1857 osf->actions = htonl((1u << OFPAT_OUTPUT) |
1858 (1u << OFPAT_SET_VLAN_VID) |
1859 (1u << OFPAT_SET_VLAN_PCP) |
1860 (1u << OFPAT_STRIP_VLAN) |
1861 (1u << OFPAT_SET_DL_SRC) |
1862 (1u << OFPAT_SET_DL_DST) |
1863 (1u << OFPAT_SET_NW_SRC) |
1864 (1u << OFPAT_SET_NW_DST) |
1865 (1u << OFPAT_SET_NW_TOS) |
1866 (1u << OFPAT_SET_TP_SRC) |
1867 (1u << OFPAT_SET_TP_DST));
1869 PORT_ARRAY_FOR_EACH (port, &p->ports, port_no) {
1870 hton_ofp_phy_port(ofpbuf_put(buf, &port->opp, sizeof port->opp));
1873 queue_tx(buf, ofconn, ofconn->reply_counter);
1878 handle_get_config_request(struct ofproto *p, struct ofconn *ofconn,
1879 struct ofp_header *oh)
1882 struct ofp_switch_config *osc;
1886 /* Figure out flags. */
1887 dpif_get_drop_frags(p->dpif, &drop_frags);
1888 flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL;
1891 osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf);
1892 osc->flags = htons(flags);
1893 osc->miss_send_len = htons(ofconn->miss_send_len);
1894 queue_tx(buf, ofconn, ofconn->reply_counter);
1900 handle_set_config(struct ofproto *p, struct ofconn *ofconn,
1901 struct ofp_switch_config *osc)
1906 error = check_ofp_message(&osc->header, OFPT_SET_CONFIG, sizeof *osc);
1910 flags = ntohs(osc->flags);
1912 if (ofconn == p->controller) {
1913 switch (flags & OFPC_FRAG_MASK) {
1914 case OFPC_FRAG_NORMAL:
1915 dpif_set_drop_frags(p->dpif, false);
1917 case OFPC_FRAG_DROP:
1918 dpif_set_drop_frags(p->dpif, true);
1921 VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")",
1927 if ((ntohs(osc->miss_send_len) != 0) != (ofconn->miss_send_len != 0)) {
1928 if (ntohs(osc->miss_send_len) != 0) {
1929 ofconn->pktbuf = pktbuf_create();
1931 pktbuf_destroy(ofconn->pktbuf);
1935 ofconn->miss_send_len = ntohs(osc->miss_send_len);
1941 add_output_group_action(struct odp_actions *actions, uint16_t group,
1942 uint16_t *nf_output_iface)
1944 odp_actions_add(actions, ODPAT_OUTPUT_GROUP)->output_group.group = group;
1946 if (group == DP_GROUP_ALL || group == DP_GROUP_FLOOD) {
1947 *nf_output_iface = NF_OUT_FLOOD;
1952 add_controller_action(struct odp_actions *actions,
1953 const struct ofp_action_output *oao)
1955 union odp_action *a = odp_actions_add(actions, ODPAT_CONTROLLER);
1956 a->controller.arg = oao->max_len ? ntohs(oao->max_len) : UINT32_MAX;
1959 struct action_xlate_ctx {
1961 const flow_t *flow; /* Flow to which these actions correspond. */
1962 int recurse; /* Recursion level, via xlate_table_action. */
1963 struct ofproto *ofproto;
1964 const struct ofpbuf *packet; /* The packet corresponding to 'flow', or a
1965 * null pointer if we are revalidating
1966 * without a packet to refer to. */
1969 struct odp_actions *out; /* Datapath actions. */
1970 tag_type *tags; /* Tags associated with OFPP_NORMAL actions. */
1971 bool may_set_up_flow; /* True ordinarily; false if the actions must
1972 * be reassessed for every packet. */
1973 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
1976 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
1977 struct action_xlate_ctx *ctx);
1980 add_output_action(struct action_xlate_ctx *ctx, uint16_t port)
1982 const struct ofport *ofport = port_array_get(&ctx->ofproto->ports, port);
1985 if (ofport->opp.config & OFPPC_NO_FWD) {
1986 /* Forwarding disabled on port. */
1991 * We don't have an ofport record for this port, but it doesn't hurt to
1992 * allow forwarding to it anyhow. Maybe such a port will appear later
1993 * and we're pre-populating the flow table.
1997 odp_actions_add(ctx->out, ODPAT_OUTPUT)->output.port = port;
1998 ctx->nf_output_iface = port;
2001 static struct rule *
2002 lookup_valid_rule(struct ofproto *ofproto, const flow_t *flow)
2005 rule = rule_from_cls_rule(classifier_lookup(&ofproto->cls, flow));
2007 /* The rule we found might not be valid, since we could be in need of
2008 * revalidation. If it is not valid, don't return it. */
2011 && ofproto->need_revalidate
2012 && !revalidate_rule(ofproto, rule)) {
2013 COVERAGE_INC(ofproto_invalidated);
2021 xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
2023 if (!ctx->recurse) {
2028 flow.in_port = in_port;
2030 rule = lookup_valid_rule(ctx->ofproto, &flow);
2037 do_xlate_actions(rule->actions, rule->n_actions, ctx);
2044 xlate_output_action(struct action_xlate_ctx *ctx,
2045 const struct ofp_action_output *oao)
2048 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
2050 ctx->nf_output_iface = NF_OUT_DROP;
2052 switch (ntohs(oao->port)) {
2054 add_output_action(ctx, ctx->flow->in_port);
2057 xlate_table_action(ctx, ctx->flow->in_port);
2060 if (!ctx->ofproto->ofhooks->normal_cb(ctx->flow, ctx->packet,
2061 ctx->out, ctx->tags,
2062 &ctx->nf_output_iface,
2063 ctx->ofproto->aux)) {
2064 COVERAGE_INC(ofproto_uninstallable);
2065 ctx->may_set_up_flow = false;
2069 add_output_group_action(ctx->out, DP_GROUP_FLOOD,
2070 &ctx->nf_output_iface);
2073 add_output_group_action(ctx->out, DP_GROUP_ALL, &ctx->nf_output_iface);
2075 case OFPP_CONTROLLER:
2076 add_controller_action(ctx->out, oao);
2079 add_output_action(ctx, ODPP_LOCAL);
2082 odp_port = ofp_port_to_odp_port(ntohs(oao->port));
2083 if (odp_port != ctx->flow->in_port) {
2084 add_output_action(ctx, odp_port);
2089 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2090 ctx->nf_output_iface = NF_OUT_FLOOD;
2091 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
2092 ctx->nf_output_iface = prev_nf_output_iface;
2093 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2094 ctx->nf_output_iface != NF_OUT_FLOOD) {
2095 ctx->nf_output_iface = NF_OUT_MULTI;
2100 xlate_nicira_action(struct action_xlate_ctx *ctx,
2101 const struct nx_action_header *nah)
2103 const struct nx_action_resubmit *nar;
2104 int subtype = ntohs(nah->subtype);
2106 assert(nah->vendor == htonl(NX_VENDOR_ID));
2108 case NXAST_RESUBMIT:
2109 nar = (const struct nx_action_resubmit *) nah;
2110 xlate_table_action(ctx, ofp_port_to_odp_port(ntohs(nar->in_port)));
2114 VLOG_DBG_RL(&rl, "unknown Nicira action type %"PRIu16, subtype);
2120 do_xlate_actions(const union ofp_action *in, size_t n_in,
2121 struct action_xlate_ctx *ctx)
2123 struct actions_iterator iter;
2124 const union ofp_action *ia;
2125 const struct ofport *port;
2127 port = port_array_get(&ctx->ofproto->ports, ctx->flow->in_port);
2128 if (port && port->opp.config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
2129 port->opp.config & (eth_addr_equals(ctx->flow->dl_dst, stp_eth_addr)
2130 ? OFPPC_NO_RECV_STP : OFPPC_NO_RECV)) {
2131 /* Drop this flow. */
2135 for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
2136 uint16_t type = ntohs(ia->type);
2137 union odp_action *oa;
2141 xlate_output_action(ctx, &ia->output);
2144 case OFPAT_SET_VLAN_VID:
2145 oa = odp_actions_add(ctx->out, ODPAT_SET_VLAN_VID);
2146 oa->vlan_vid.vlan_vid = ia->vlan_vid.vlan_vid;
2149 case OFPAT_SET_VLAN_PCP:
2150 oa = odp_actions_add(ctx->out, ODPAT_SET_VLAN_PCP);
2151 oa->vlan_pcp.vlan_pcp = ia->vlan_pcp.vlan_pcp;
2154 case OFPAT_STRIP_VLAN:
2155 odp_actions_add(ctx->out, ODPAT_STRIP_VLAN);
2158 case OFPAT_SET_DL_SRC:
2159 oa = odp_actions_add(ctx->out, ODPAT_SET_DL_SRC);
2160 memcpy(oa->dl_addr.dl_addr,
2161 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
2164 case OFPAT_SET_DL_DST:
2165 oa = odp_actions_add(ctx->out, ODPAT_SET_DL_DST);
2166 memcpy(oa->dl_addr.dl_addr,
2167 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
2170 case OFPAT_SET_NW_SRC:
2171 oa = odp_actions_add(ctx->out, ODPAT_SET_NW_SRC);
2172 oa->nw_addr.nw_addr = ia->nw_addr.nw_addr;
2175 case OFPAT_SET_NW_DST:
2176 oa = odp_actions_add(ctx->out, ODPAT_SET_NW_DST);
2177 oa->nw_addr.nw_addr = ia->nw_addr.nw_addr;
2179 case OFPAT_SET_NW_TOS:
2180 oa = odp_actions_add(ctx->out, ODPAT_SET_NW_TOS);
2181 oa->nw_tos.nw_tos = ia->nw_tos.nw_tos;
2184 case OFPAT_SET_TP_SRC:
2185 oa = odp_actions_add(ctx->out, ODPAT_SET_TP_SRC);
2186 oa->tp_port.tp_port = ia->tp_port.tp_port;
2189 case OFPAT_SET_TP_DST:
2190 oa = odp_actions_add(ctx->out, ODPAT_SET_TP_DST);
2191 oa->tp_port.tp_port = ia->tp_port.tp_port;
2195 xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
2199 VLOG_DBG_RL(&rl, "unknown action type %"PRIu16, type);
2206 xlate_actions(const union ofp_action *in, size_t n_in,
2207 const flow_t *flow, struct ofproto *ofproto,
2208 const struct ofpbuf *packet,
2209 struct odp_actions *out, tag_type *tags, bool *may_set_up_flow,
2210 uint16_t *nf_output_iface)
2212 tag_type no_tags = 0;
2213 struct action_xlate_ctx ctx;
2214 COVERAGE_INC(ofproto_ofp2odp);
2215 odp_actions_init(out);
2218 ctx.ofproto = ofproto;
2219 ctx.packet = packet;
2221 ctx.tags = tags ? tags : &no_tags;
2222 ctx.may_set_up_flow = true;
2223 ctx.nf_output_iface = NF_OUT_DROP;
2224 do_xlate_actions(in, n_in, &ctx);
2226 /* Check with in-band control to see if we're allowed to set up this
2228 if (!in_band_rule_check(ofproto->in_band, flow, out)) {
2229 ctx.may_set_up_flow = false;
2232 if (may_set_up_flow) {
2233 *may_set_up_flow = ctx.may_set_up_flow;
2235 if (nf_output_iface) {
2236 *nf_output_iface = ctx.nf_output_iface;
2238 if (odp_actions_overflow(out)) {
2239 odp_actions_init(out);
2240 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_TOO_MANY);
2246 handle_packet_out(struct ofproto *p, struct ofconn *ofconn,
2247 struct ofp_header *oh)
2249 struct ofp_packet_out *opo;
2250 struct ofpbuf payload, *buffer;
2251 struct odp_actions actions;
2257 error = check_ofp_packet_out(oh, &payload, &n_actions, p->max_ports);
2261 opo = (struct ofp_packet_out *) oh;
2263 COVERAGE_INC(ofproto_packet_out);
2264 if (opo->buffer_id != htonl(UINT32_MAX)) {
2265 error = pktbuf_retrieve(ofconn->pktbuf, ntohl(opo->buffer_id),
2267 if (error || !buffer) {
2275 flow_extract(&payload, ofp_port_to_odp_port(ntohs(opo->in_port)), &flow);
2276 error = xlate_actions((const union ofp_action *) opo->actions, n_actions,
2277 &flow, p, &payload, &actions, NULL, NULL, NULL);
2282 dpif_execute(p->dpif, flow.in_port, actions.actions, actions.n_actions,
2284 ofpbuf_delete(buffer);
2290 update_port_config(struct ofproto *p, struct ofport *port,
2291 uint32_t config, uint32_t mask)
2293 mask &= config ^ port->opp.config;
2294 if (mask & OFPPC_PORT_DOWN) {
2295 if (config & OFPPC_PORT_DOWN) {
2296 netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
2298 netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
2301 #define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | OFPPC_NO_FWD)
2302 if (mask & REVALIDATE_BITS) {
2303 COVERAGE_INC(ofproto_costly_flags);
2304 port->opp.config ^= mask & REVALIDATE_BITS;
2305 p->need_revalidate = true;
2307 #undef REVALIDATE_BITS
2308 if (mask & OFPPC_NO_FLOOD) {
2309 port->opp.config ^= OFPPC_NO_FLOOD;
2310 refresh_port_group(p, DP_GROUP_FLOOD);
2312 if (mask & OFPPC_NO_PACKET_IN) {
2313 port->opp.config ^= OFPPC_NO_PACKET_IN;
2318 handle_port_mod(struct ofproto *p, struct ofp_header *oh)
2320 const struct ofp_port_mod *opm;
2321 struct ofport *port;
2324 error = check_ofp_message(oh, OFPT_PORT_MOD, sizeof *opm);
2328 opm = (struct ofp_port_mod *) oh;
2330 port = port_array_get(&p->ports,
2331 ofp_port_to_odp_port(ntohs(opm->port_no)));
2333 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT);
2334 } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) {
2335 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR);
2337 update_port_config(p, port, ntohl(opm->config), ntohl(opm->mask));
2338 if (opm->advertise) {
2339 netdev_set_advertisements(port->netdev, ntohl(opm->advertise));
2345 static struct ofpbuf *
2346 make_stats_reply(uint32_t xid, uint16_t type, size_t body_len)
2348 struct ofp_stats_reply *osr;
2351 msg = ofpbuf_new(MIN(sizeof *osr + body_len, UINT16_MAX));
2352 osr = put_openflow_xid(sizeof *osr, OFPT_STATS_REPLY, xid, msg);
2354 osr->flags = htons(0);
2358 static struct ofpbuf *
2359 start_stats_reply(const struct ofp_stats_request *request, size_t body_len)
2361 return make_stats_reply(request->header.xid, request->type, body_len);
2365 append_stats_reply(size_t nbytes, struct ofconn *ofconn, struct ofpbuf **msgp)
2367 struct ofpbuf *msg = *msgp;
2368 assert(nbytes <= UINT16_MAX - sizeof(struct ofp_stats_reply));
2369 if (nbytes + msg->size > UINT16_MAX) {
2370 struct ofp_stats_reply *reply = msg->data;
2371 reply->flags = htons(OFPSF_REPLY_MORE);
2372 *msgp = make_stats_reply(reply->header.xid, reply->type, nbytes);
2373 queue_tx(msg, ofconn, ofconn->reply_counter);
2375 return ofpbuf_put_uninit(*msgp, nbytes);
2379 handle_desc_stats_request(struct ofproto *p, struct ofconn *ofconn,
2380 struct ofp_stats_request *request)
2382 struct ofp_desc_stats *ods;
2385 msg = start_stats_reply(request, sizeof *ods);
2386 ods = append_stats_reply(sizeof *ods, ofconn, &msg);
2387 strncpy(ods->mfr_desc, p->manufacturer, sizeof ods->mfr_desc);
2388 strncpy(ods->hw_desc, p->hardware, sizeof ods->hw_desc);
2389 strncpy(ods->sw_desc, p->software, sizeof ods->sw_desc);
2390 strncpy(ods->serial_num, p->serial, sizeof ods->serial_num);
2391 strncpy(ods->dp_desc, p->dp_desc, sizeof ods->dp_desc);
2392 queue_tx(msg, ofconn, ofconn->reply_counter);
2398 count_subrules(struct cls_rule *cls_rule, void *n_subrules_)
2400 struct rule *rule = rule_from_cls_rule(cls_rule);
2401 int *n_subrules = n_subrules_;
2409 handle_table_stats_request(struct ofproto *p, struct ofconn *ofconn,
2410 struct ofp_stats_request *request)
2412 struct ofp_table_stats *ots;
2414 struct odp_stats dpstats;
2415 int n_exact, n_subrules, n_wild;
2417 msg = start_stats_reply(request, sizeof *ots * 2);
2419 /* Count rules of various kinds. */
2421 classifier_for_each(&p->cls, CLS_INC_EXACT, count_subrules, &n_subrules);
2422 n_exact = classifier_count_exact(&p->cls) - n_subrules;
2423 n_wild = classifier_count(&p->cls) - classifier_count_exact(&p->cls);
2426 dpif_get_dp_stats(p->dpif, &dpstats);
2427 ots = append_stats_reply(sizeof *ots, ofconn, &msg);
2428 memset(ots, 0, sizeof *ots);
2429 ots->table_id = TABLEID_HASH;
2430 strcpy(ots->name, "hash");
2431 ots->wildcards = htonl(0);
2432 ots->max_entries = htonl(dpstats.max_capacity);
2433 ots->active_count = htonl(n_exact);
2434 ots->lookup_count = htonll(dpstats.n_frags + dpstats.n_hit +
2436 ots->matched_count = htonll(dpstats.n_hit); /* XXX */
2438 /* Classifier table. */
2439 ots = append_stats_reply(sizeof *ots, ofconn, &msg);
2440 memset(ots, 0, sizeof *ots);
2441 ots->table_id = TABLEID_CLASSIFIER;
2442 strcpy(ots->name, "classifier");
2443 ots->wildcards = htonl(OFPFW_ALL);
2444 ots->max_entries = htonl(65536);
2445 ots->active_count = htonl(n_wild);
2446 ots->lookup_count = htonll(0); /* XXX */
2447 ots->matched_count = htonll(0); /* XXX */
2449 queue_tx(msg, ofconn, ofconn->reply_counter);
2454 handle_port_stats_request(struct ofproto *p, struct ofconn *ofconn,
2455 struct ofp_stats_request *request)
2457 struct ofp_port_stats *ops;
2459 struct ofport *port;
2460 unsigned int port_no;
2462 msg = start_stats_reply(request, sizeof *ops * 16);
2463 PORT_ARRAY_FOR_EACH (port, &p->ports, port_no) {
2464 struct netdev_stats stats;
2466 /* Intentionally ignore return value, since errors will set 'stats' to
2467 * all-1s, which is correct for OpenFlow, and netdev_get_stats() will
2469 netdev_get_stats(port->netdev, &stats);
2471 ops = append_stats_reply(sizeof *ops, ofconn, &msg);
2472 ops->port_no = htons(odp_port_to_ofp_port(port_no));
2473 memset(ops->pad, 0, sizeof ops->pad);
2474 ops->rx_packets = htonll(stats.rx_packets);
2475 ops->tx_packets = htonll(stats.tx_packets);
2476 ops->rx_bytes = htonll(stats.rx_bytes);
2477 ops->tx_bytes = htonll(stats.tx_bytes);
2478 ops->rx_dropped = htonll(stats.rx_dropped);
2479 ops->tx_dropped = htonll(stats.tx_dropped);
2480 ops->rx_errors = htonll(stats.rx_errors);
2481 ops->tx_errors = htonll(stats.tx_errors);
2482 ops->rx_frame_err = htonll(stats.rx_frame_errors);
2483 ops->rx_over_err = htonll(stats.rx_over_errors);
2484 ops->rx_crc_err = htonll(stats.rx_crc_errors);
2485 ops->collisions = htonll(stats.collisions);
2488 queue_tx(msg, ofconn, ofconn->reply_counter);
2492 struct flow_stats_cbdata {
2493 struct ofproto *ofproto;
2494 struct ofconn *ofconn;
2500 query_stats(struct ofproto *p, struct rule *rule,
2501 uint64_t *packet_countp, uint64_t *byte_countp)
2503 uint64_t packet_count, byte_count;
2504 struct rule *subrule;
2505 struct odp_flow *odp_flows;
2508 packet_count = rule->packet_count;
2509 byte_count = rule->byte_count;
2511 n_odp_flows = rule->cr.wc.wildcards ? list_size(&rule->list) : 1;
2512 odp_flows = xcalloc(1, n_odp_flows * sizeof *odp_flows);
2513 if (rule->cr.wc.wildcards) {
2515 LIST_FOR_EACH (subrule, struct rule, list, &rule->list) {
2516 odp_flows[i++].key = subrule->cr.flow;
2517 packet_count += subrule->packet_count;
2518 byte_count += subrule->byte_count;
2521 odp_flows[0].key = rule->cr.flow;
2524 packet_count = rule->packet_count;
2525 byte_count = rule->byte_count;
2526 if (!dpif_flow_get_multiple(p->dpif, odp_flows, n_odp_flows)) {
2528 for (i = 0; i < n_odp_flows; i++) {
2529 struct odp_flow *odp_flow = &odp_flows[i];
2530 packet_count += odp_flow->stats.n_packets;
2531 byte_count += odp_flow->stats.n_bytes;
2536 *packet_countp = packet_count;
2537 *byte_countp = byte_count;
2541 flow_stats_cb(struct cls_rule *rule_, void *cbdata_)
2543 struct rule *rule = rule_from_cls_rule(rule_);
2544 struct flow_stats_cbdata *cbdata = cbdata_;
2545 struct ofp_flow_stats *ofs;
2546 uint64_t packet_count, byte_count;
2547 size_t act_len, len;
2549 if (rule_is_hidden(rule) || !rule_has_out_port(rule, cbdata->out_port)) {
2553 act_len = sizeof *rule->actions * rule->n_actions;
2554 len = offsetof(struct ofp_flow_stats, actions) + act_len;
2556 query_stats(cbdata->ofproto, rule, &packet_count, &byte_count);
2558 ofs = append_stats_reply(len, cbdata->ofconn, &cbdata->msg);
2559 ofs->length = htons(len);
2560 ofs->table_id = rule->cr.wc.wildcards ? TABLEID_CLASSIFIER : TABLEID_HASH;
2562 flow_to_match(&rule->cr.flow, rule->cr.wc.wildcards, &ofs->match);
2563 ofs->duration = htonl((time_msec() - rule->created) / 1000);
2564 ofs->cookie = rule->flow_cookie;
2565 ofs->priority = htons(rule->cr.priority);
2566 ofs->idle_timeout = htons(rule->idle_timeout);
2567 ofs->hard_timeout = htons(rule->hard_timeout);
2568 memset(ofs->pad2, 0, sizeof ofs->pad2);
2569 ofs->packet_count = htonll(packet_count);
2570 ofs->byte_count = htonll(byte_count);
2571 memcpy(ofs->actions, rule->actions, act_len);
2575 table_id_to_include(uint8_t table_id)
2577 return (table_id == TABLEID_HASH ? CLS_INC_EXACT
2578 : table_id == TABLEID_CLASSIFIER ? CLS_INC_WILD
2579 : table_id == 0xff ? CLS_INC_ALL
2584 handle_flow_stats_request(struct ofproto *p, struct ofconn *ofconn,
2585 const struct ofp_stats_request *osr,
2588 struct ofp_flow_stats_request *fsr;
2589 struct flow_stats_cbdata cbdata;
2590 struct cls_rule target;
2592 if (arg_size != sizeof *fsr) {
2593 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2595 fsr = (struct ofp_flow_stats_request *) osr->body;
2597 COVERAGE_INC(ofproto_flows_req);
2599 cbdata.ofconn = ofconn;
2600 cbdata.out_port = fsr->out_port;
2601 cbdata.msg = start_stats_reply(osr, 1024);
2602 cls_rule_from_match(&target, &fsr->match, 0);
2603 classifier_for_each_match(&p->cls, &target,
2604 table_id_to_include(fsr->table_id),
2605 flow_stats_cb, &cbdata);
2606 queue_tx(cbdata.msg, ofconn, ofconn->reply_counter);
2610 struct flow_stats_ds_cbdata {
2611 struct ofproto *ofproto;
2616 flow_stats_ds_cb(struct cls_rule *rule_, void *cbdata_)
2618 struct rule *rule = rule_from_cls_rule(rule_);
2619 struct flow_stats_ds_cbdata *cbdata = cbdata_;
2620 struct ds *results = cbdata->results;
2621 struct ofp_match match;
2622 uint64_t packet_count, byte_count;
2623 size_t act_len = sizeof *rule->actions * rule->n_actions;
2625 /* Don't report on subrules. */
2626 if (rule->super != NULL) {
2630 query_stats(cbdata->ofproto, rule, &packet_count, &byte_count);
2631 flow_to_ovs_match(&rule->cr.flow, rule->cr.wc.wildcards, &match);
2633 ds_put_format(results, "duration=%llds, ",
2634 (time_msec() - rule->created) / 1000);
2635 ds_put_format(results, "priority=%u, ", rule->cr.priority);
2636 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
2637 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
2638 ofp_print_match(results, &match, true);
2639 ofp_print_actions(results, &rule->actions->header, act_len);
2640 ds_put_cstr(results, "\n");
2643 /* Adds a pretty-printed description of all flows to 'results', including
2644 * those marked hidden by secchan (e.g., by in-band control). */
2646 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
2648 struct ofp_match match;
2649 struct cls_rule target;
2650 struct flow_stats_ds_cbdata cbdata;
2652 memset(&match, 0, sizeof match);
2653 match.wildcards = htonl(OFPFW_ALL);
2656 cbdata.results = results;
2658 cls_rule_from_match(&target, &match, 0);
2659 classifier_for_each_match(&p->cls, &target, CLS_INC_ALL,
2660 flow_stats_ds_cb, &cbdata);
2663 struct aggregate_stats_cbdata {
2664 struct ofproto *ofproto;
2666 uint64_t packet_count;
2667 uint64_t byte_count;
2672 aggregate_stats_cb(struct cls_rule *rule_, void *cbdata_)
2674 struct rule *rule = rule_from_cls_rule(rule_);
2675 struct aggregate_stats_cbdata *cbdata = cbdata_;
2676 uint64_t packet_count, byte_count;
2678 if (rule_is_hidden(rule) || !rule_has_out_port(rule, cbdata->out_port)) {
2682 query_stats(cbdata->ofproto, rule, &packet_count, &byte_count);
2684 cbdata->packet_count += packet_count;
2685 cbdata->byte_count += byte_count;
2690 handle_aggregate_stats_request(struct ofproto *p, struct ofconn *ofconn,
2691 const struct ofp_stats_request *osr,
2694 struct ofp_aggregate_stats_request *asr;
2695 struct ofp_aggregate_stats_reply *reply;
2696 struct aggregate_stats_cbdata cbdata;
2697 struct cls_rule target;
2700 if (arg_size != sizeof *asr) {
2701 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2703 asr = (struct ofp_aggregate_stats_request *) osr->body;
2705 COVERAGE_INC(ofproto_agg_request);
2707 cbdata.out_port = asr->out_port;
2708 cbdata.packet_count = 0;
2709 cbdata.byte_count = 0;
2711 cls_rule_from_match(&target, &asr->match, 0);
2712 classifier_for_each_match(&p->cls, &target,
2713 table_id_to_include(asr->table_id),
2714 aggregate_stats_cb, &cbdata);
2716 msg = start_stats_reply(osr, sizeof *reply);
2717 reply = append_stats_reply(sizeof *reply, ofconn, &msg);
2718 reply->flow_count = htonl(cbdata.n_flows);
2719 reply->packet_count = htonll(cbdata.packet_count);
2720 reply->byte_count = htonll(cbdata.byte_count);
2721 queue_tx(msg, ofconn, ofconn->reply_counter);
2726 handle_stats_request(struct ofproto *p, struct ofconn *ofconn,
2727 struct ofp_header *oh)
2729 struct ofp_stats_request *osr;
2733 error = check_ofp_message_array(oh, OFPT_STATS_REQUEST, sizeof *osr,
2738 osr = (struct ofp_stats_request *) oh;
2740 switch (ntohs(osr->type)) {
2742 return handle_desc_stats_request(p, ofconn, osr);
2745 return handle_flow_stats_request(p, ofconn, osr, arg_size);
2747 case OFPST_AGGREGATE:
2748 return handle_aggregate_stats_request(p, ofconn, osr, arg_size);
2751 return handle_table_stats_request(p, ofconn, osr);
2754 return handle_port_stats_request(p, ofconn, osr);
2757 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
2760 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT);
2764 static long long int
2765 msec_from_nsec(uint64_t sec, uint32_t nsec)
2767 return !sec ? 0 : sec * 1000 + nsec / 1000000;
2771 update_time(struct ofproto *ofproto, struct rule *rule,
2772 const struct odp_flow_stats *stats)
2774 long long int used = msec_from_nsec(stats->used_sec, stats->used_nsec);
2775 if (used > rule->used) {
2777 if (rule->super && used > rule->super->used) {
2778 rule->super->used = used;
2780 netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, used);
2785 update_stats(struct ofproto *ofproto, struct rule *rule,
2786 const struct odp_flow_stats *stats)
2788 if (stats->n_packets) {
2789 update_time(ofproto, rule, stats);
2790 rule->packet_count += stats->n_packets;
2791 rule->byte_count += stats->n_bytes;
2792 netflow_flow_update_flags(&rule->nf_flow, stats->ip_tos,
2798 add_flow(struct ofproto *p, struct ofconn *ofconn,
2799 struct ofp_flow_mod *ofm, size_t n_actions)
2801 struct ofpbuf *packet;
2806 if (ofm->flags & htons(OFPFF_CHECK_OVERLAP)) {
2810 flow_from_match(&flow, &wildcards, &ofm->match);
2811 if (classifier_rule_overlaps(&p->cls, &flow, wildcards,
2812 ntohs(ofm->priority))) {
2813 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP);
2817 rule = rule_create(p, NULL, (const union ofp_action *) ofm->actions,
2818 n_actions, ntohs(ofm->idle_timeout),
2819 ntohs(ofm->hard_timeout), ofm->cookie,
2820 ofm->flags & htons(OFPFF_SEND_FLOW_REM));
2821 cls_rule_from_match(&rule->cr, &ofm->match, ntohs(ofm->priority));
2825 if (ofm->buffer_id != htonl(UINT32_MAX)) {
2826 error = pktbuf_retrieve(ofconn->pktbuf, ntohl(ofm->buffer_id),
2830 rule_insert(p, rule, packet, in_port);
2831 ofpbuf_delete(packet);
2836 modify_flow(struct ofproto *p, const struct ofp_flow_mod *ofm,
2837 size_t n_actions, uint16_t command, struct rule *rule)
2839 if (rule_is_hidden(rule)) {
2843 if (command == OFPFC_DELETE) {
2844 long long int now = time_msec();
2845 send_flow_removed(p, rule, now, OFPRR_DELETE);
2846 rule_remove(p, rule);
2848 size_t actions_len = n_actions * sizeof *rule->actions;
2850 if (n_actions == rule->n_actions
2851 && !memcmp(ofm->actions, rule->actions, actions_len))
2856 free(rule->actions);
2857 rule->actions = xmemdup(ofm->actions, actions_len);
2858 rule->n_actions = n_actions;
2859 rule->flow_cookie = ofm->cookie;
2861 if (rule->cr.wc.wildcards) {
2862 COVERAGE_INC(ofproto_mod_wc_flow);
2863 p->need_revalidate = true;
2865 rule_update_actions(p, rule);
2873 modify_flows_strict(struct ofproto *p, const struct ofp_flow_mod *ofm,
2874 size_t n_actions, uint16_t command)
2880 flow_from_match(&flow, &wildcards, &ofm->match);
2881 rule = rule_from_cls_rule(classifier_find_rule_exactly(
2882 &p->cls, &flow, wildcards,
2883 ntohs(ofm->priority)));
2886 if (command == OFPFC_DELETE
2887 && ofm->out_port != htons(OFPP_NONE)
2888 && !rule_has_out_port(rule, ofm->out_port)) {
2892 modify_flow(p, ofm, n_actions, command, rule);
2897 struct modify_flows_cbdata {
2898 struct ofproto *ofproto;
2899 const struct ofp_flow_mod *ofm;
2906 modify_flows_cb(struct cls_rule *rule_, void *cbdata_)
2908 struct rule *rule = rule_from_cls_rule(rule_);
2909 struct modify_flows_cbdata *cbdata = cbdata_;
2911 if (cbdata->out_port != htons(OFPP_NONE)
2912 && !rule_has_out_port(rule, cbdata->out_port)) {
2916 modify_flow(cbdata->ofproto, cbdata->ofm, cbdata->n_actions,
2917 cbdata->command, rule);
2921 modify_flows_loose(struct ofproto *p, const struct ofp_flow_mod *ofm,
2922 size_t n_actions, uint16_t command)
2924 struct modify_flows_cbdata cbdata;
2925 struct cls_rule target;
2929 cbdata.out_port = (command == OFPFC_DELETE ? ofm->out_port
2930 : htons(OFPP_NONE));
2931 cbdata.n_actions = n_actions;
2932 cbdata.command = command;
2934 cls_rule_from_match(&target, &ofm->match, 0);
2936 classifier_for_each_match(&p->cls, &target, CLS_INC_ALL,
2937 modify_flows_cb, &cbdata);
2942 handle_flow_mod(struct ofproto *p, struct ofconn *ofconn,
2943 struct ofp_flow_mod *ofm)
2948 error = check_ofp_message_array(&ofm->header, OFPT_FLOW_MOD, sizeof *ofm,
2949 sizeof *ofm->actions, &n_actions);
2954 /* We do not support the emergency flow cache. It will hopefully
2955 * get dropped from OpenFlow in the near future. */
2956 if (ofm->flags & htons(OFPFF_EMERG)) {
2957 /* There isn't a good fit for an error code, so just state that the
2958 * flow table is full. */
2959 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL);
2962 normalize_match(&ofm->match);
2963 if (!ofm->match.wildcards) {
2964 ofm->priority = htons(UINT16_MAX);
2967 error = validate_actions((const union ofp_action *) ofm->actions,
2968 n_actions, p->max_ports);
2973 switch (ntohs(ofm->command)) {
2975 return add_flow(p, ofconn, ofm, n_actions);
2978 return modify_flows_loose(p, ofm, n_actions, OFPFC_MODIFY);
2980 case OFPFC_MODIFY_STRICT:
2981 return modify_flows_strict(p, ofm, n_actions, OFPFC_MODIFY);
2984 return modify_flows_loose(p, ofm, n_actions, OFPFC_DELETE);
2986 case OFPFC_DELETE_STRICT:
2987 return modify_flows_strict(p, ofm, n_actions, OFPFC_DELETE);
2990 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND);
2995 send_capability_reply(struct ofproto *p, struct ofconn *ofconn, uint32_t xid)
2997 struct ofmp_capability_reply *ocr;
2999 char capabilities[] = "com.nicira.mgmt.manager=false\n";
3001 ocr = make_openflow_xid(sizeof(*ocr), OFPT_VENDOR, xid, &b);
3002 ocr->header.header.vendor = htonl(NX_VENDOR_ID);
3003 ocr->header.header.subtype = htonl(NXT_MGMT);
3004 ocr->header.type = htons(OFMPT_CAPABILITY_REPLY);
3006 ocr->format = htonl(OFMPCOF_SIMPLE);
3007 ocr->mgmt_id = htonll(p->mgmt_id);
3009 ofpbuf_put(b, capabilities, strlen(capabilities));
3011 queue_tx(b, ofconn, ofconn->reply_counter);
3015 handle_ofmp(struct ofproto *p, struct ofconn *ofconn,
3016 struct ofmp_header *ofmph)
3018 size_t msg_len = ntohs(ofmph->header.header.length);
3019 if (msg_len < sizeof(*ofmph)) {
3020 VLOG_WARN_RL(&rl, "dropping short managment message: %zu\n", msg_len);
3021 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3024 if (ofmph->type == htons(OFMPT_CAPABILITY_REQUEST)) {
3025 struct ofmp_capability_request *ofmpcr;
3027 if (msg_len < sizeof(struct ofmp_capability_request)) {
3028 VLOG_WARN_RL(&rl, "dropping short capability request: %zu\n",
3030 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3033 ofmpcr = (struct ofmp_capability_request *)ofmph;
3034 if (ofmpcr->format != htonl(OFMPCAF_SIMPLE)) {
3035 /* xxx Find a better type than bad subtype */
3036 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE);
3039 send_capability_reply(p, ofconn, ofmph->header.header.xid);
3042 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE);
3047 handle_vendor(struct ofproto *p, struct ofconn *ofconn, void *msg)
3049 struct ofp_vendor_header *ovh = msg;
3050 struct nicira_header *nh;
3052 if (ntohs(ovh->header.length) < sizeof(struct ofp_vendor_header)) {
3053 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3055 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
3056 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
3058 if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) {
3059 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3063 switch (ntohl(nh->subtype)) {
3064 case NXT_STATUS_REQUEST:
3065 return switch_status_handle_request(p->switch_status, ofconn->rconn,
3068 case NXT_ACT_SET_CONFIG:
3069 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE); /* XXX */
3071 case NXT_ACT_GET_CONFIG:
3072 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE); /* XXX */
3074 case NXT_COMMAND_REQUEST:
3076 return executer_handle_request(p->executer, ofconn->rconn, msg);
3081 return handle_ofmp(p, ofconn, msg);
3084 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE);
3088 handle_barrier_request(struct ofconn *ofconn, struct ofp_header *oh)
3090 struct ofp_header *ob;
3093 /* Currently, everything executes synchronously, so we can just
3094 * immediately send the barrier reply. */
3095 ob = make_openflow_xid(sizeof *ob, OFPT_BARRIER_REPLY, oh->xid, &buf);
3096 queue_tx(buf, ofconn, ofconn->reply_counter);
3101 handle_openflow(struct ofconn *ofconn, struct ofproto *p,
3102 struct ofpbuf *ofp_msg)
3104 struct ofp_header *oh = ofp_msg->data;
3107 COVERAGE_INC(ofproto_recv_openflow);
3109 case OFPT_ECHO_REQUEST:
3110 error = handle_echo_request(ofconn, oh);
3113 case OFPT_ECHO_REPLY:
3117 case OFPT_FEATURES_REQUEST:
3118 error = handle_features_request(p, ofconn, oh);
3121 case OFPT_GET_CONFIG_REQUEST:
3122 error = handle_get_config_request(p, ofconn, oh);
3125 case OFPT_SET_CONFIG:
3126 error = handle_set_config(p, ofconn, ofp_msg->data);
3129 case OFPT_PACKET_OUT:
3130 error = handle_packet_out(p, ofconn, ofp_msg->data);
3134 error = handle_port_mod(p, oh);
3138 error = handle_flow_mod(p, ofconn, ofp_msg->data);
3141 case OFPT_STATS_REQUEST:
3142 error = handle_stats_request(p, ofconn, oh);
3146 error = handle_vendor(p, ofconn, ofp_msg->data);
3149 case OFPT_BARRIER_REQUEST:
3150 error = handle_barrier_request(ofconn, oh);
3154 if (VLOG_IS_WARN_ENABLED()) {
3155 char *s = ofp_to_string(oh, ntohs(oh->length), 2);
3156 VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s);
3159 error = ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
3164 send_error_oh(ofconn, ofp_msg->data, error);
3169 handle_odp_msg(struct ofproto *p, struct ofpbuf *packet)
3171 struct odp_msg *msg = packet->data;
3172 uint16_t in_port = odp_port_to_ofp_port(msg->port);
3174 struct ofpbuf payload;
3177 /* Handle controller actions. */
3178 if (msg->type == _ODPL_ACTION_NR) {
3179 COVERAGE_INC(ofproto_ctlr_action);
3180 pinsched_send(p->action_sched, in_port, packet,
3181 send_packet_in_action, p);
3185 payload.data = msg + 1;
3186 payload.size = msg->length - sizeof *msg;
3187 flow_extract(&payload, msg->port, &flow);
3189 /* Check with in-band control to see if this packet should be sent
3190 * to the local port regardless of the flow table. */
3191 if (in_band_msg_in_hook(p->in_band, &flow, &payload)) {
3192 union odp_action action;
3194 memset(&action, 0, sizeof(action));
3195 action.output.type = ODPAT_OUTPUT;
3196 action.output.port = ODPP_LOCAL;
3197 dpif_execute(p->dpif, flow.in_port, &action, 1, &payload);
3200 rule = lookup_valid_rule(p, &flow);
3202 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
3203 struct ofport *port = port_array_get(&p->ports, msg->port);
3205 if (port->opp.config & OFPPC_NO_PACKET_IN) {
3206 COVERAGE_INC(ofproto_no_packet_in);
3207 /* XXX install 'drop' flow entry */
3208 ofpbuf_delete(packet);
3212 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16, msg->port);
3215 COVERAGE_INC(ofproto_packet_in);
3216 pinsched_send(p->miss_sched, in_port, packet, send_packet_in_miss, p);
3220 if (rule->cr.wc.wildcards) {
3221 rule = rule_create_subrule(p, rule, &flow);
3222 rule_make_actions(p, rule, packet);
3224 if (!rule->may_install) {
3225 /* The rule is not installable, that is, we need to process every
3226 * packet, so process the current packet and set its actions into
3228 rule_make_actions(p, rule, packet);
3230 /* XXX revalidate rule if it needs it */
3234 rule_execute(p, rule, &payload, &flow);
3235 rule_reinstall(p, rule);
3237 if (rule->super && rule->super->cr.priority == FAIL_OPEN_PRIORITY
3238 && rconn_is_connected(p->controller->rconn)) {
3240 * Extra-special case for fail-open mode.
3242 * We are in fail-open mode and the packet matched the fail-open rule,
3243 * but we are connected to a controller too. We should send the packet
3244 * up to the controller in the hope that it will try to set up a flow
3245 * and thereby allow us to exit fail-open.
3247 * See the top-level comment in fail-open.c for more information.
3249 pinsched_send(p->miss_sched, in_port, packet, send_packet_in_miss, p);
3251 ofpbuf_delete(packet);
3256 revalidate_cb(struct cls_rule *sub_, void *cbdata_)
3258 struct rule *sub = rule_from_cls_rule(sub_);
3259 struct revalidate_cbdata *cbdata = cbdata_;
3261 if (cbdata->revalidate_all
3262 || (cbdata->revalidate_subrules && sub->super)
3263 || (tag_set_intersects(&cbdata->revalidate_set, sub->tags))) {
3264 revalidate_rule(cbdata->ofproto, sub);
3269 revalidate_rule(struct ofproto *p, struct rule *rule)
3271 const flow_t *flow = &rule->cr.flow;
3273 COVERAGE_INC(ofproto_revalidate_rule);
3276 super = rule_from_cls_rule(classifier_lookup_wild(&p->cls, flow));
3278 rule_remove(p, rule);
3280 } else if (super != rule->super) {
3281 COVERAGE_INC(ofproto_revalidate_moved);
3282 list_remove(&rule->list);
3283 list_push_back(&super->list, &rule->list);
3284 rule->super = super;
3285 rule->hard_timeout = super->hard_timeout;
3286 rule->idle_timeout = super->idle_timeout;
3287 rule->created = super->created;
3292 rule_update_actions(p, rule);
3296 static struct ofpbuf *
3297 compose_flow_removed(const struct rule *rule, long long int now, uint8_t reason)
3299 struct ofp_flow_removed *ofr;
3301 long long int last_used = rule->used ? now - rule->used : 0;
3303 ofr = make_openflow(sizeof *ofr, OFPT_FLOW_REMOVED, &buf);
3304 flow_to_match(&rule->cr.flow, rule->cr.wc.wildcards, &ofr->match);
3305 ofr->cookie = rule->flow_cookie;
3306 ofr->priority = htons(rule->cr.priority);
3307 ofr->reason = reason;
3308 ofr->duration = htonl((now - rule->created - last_used) / 1000);
3309 ofr->idle_timeout = htons(rule->idle_timeout);
3310 ofr->packet_count = htonll(rule->packet_count);
3311 ofr->byte_count = htonll(rule->byte_count);
3317 uninstall_idle_flow(struct ofproto *ofproto, struct rule *rule)
3319 assert(rule->installed);
3320 assert(!rule->cr.wc.wildcards);
3323 rule_remove(ofproto, rule);
3325 rule_uninstall(ofproto, rule);
3329 send_flow_removed(struct ofproto *p, struct rule *rule,
3330 long long int now, uint8_t reason)
3332 struct ofconn *ofconn;
3333 struct ofconn *prev;
3334 struct ofpbuf *buf = NULL;
3336 /* We limit the maximum number of queued flow expirations it by accounting
3337 * them under the counter for replies. That works because preventing
3338 * OpenFlow requests from being processed also prevents new flows from
3339 * being added (and expiring). (It also prevents processing OpenFlow
3340 * requests that would not add new flows, so it is imperfect.) */
3343 LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
3344 if (rule->send_flow_removed && rconn_is_connected(ofconn->rconn)) {
3346 queue_tx(ofpbuf_clone(buf), prev, prev->reply_counter);
3348 buf = compose_flow_removed(rule, now, reason);
3354 queue_tx(buf, prev, prev->reply_counter);
3360 expire_rule(struct cls_rule *cls_rule, void *p_)
3362 struct ofproto *p = p_;
3363 struct rule *rule = rule_from_cls_rule(cls_rule);
3364 long long int hard_expire, idle_expire, expire, now;
3366 hard_expire = (rule->hard_timeout
3367 ? rule->created + rule->hard_timeout * 1000
3369 idle_expire = (rule->idle_timeout
3370 && (rule->super || list_is_empty(&rule->list))
3371 ? rule->used + rule->idle_timeout * 1000
3373 expire = MIN(hard_expire, idle_expire);
3377 if (rule->installed && now >= rule->used + 5000) {
3378 uninstall_idle_flow(p, rule);
3379 } else if (!rule->cr.wc.wildcards) {
3380 active_timeout(p, rule);
3386 COVERAGE_INC(ofproto_expired);
3388 /* Update stats. This code will be a no-op if the rule expired
3389 * due to an idle timeout. */
3390 if (rule->cr.wc.wildcards) {
3391 struct rule *subrule, *next;
3392 LIST_FOR_EACH_SAFE (subrule, next, struct rule, list, &rule->list) {
3393 rule_remove(p, subrule);
3396 rule_uninstall(p, rule);
3399 if (!rule_is_hidden(rule)) {
3400 send_flow_removed(p, rule, now,
3402 ? OFPRR_HARD_TIMEOUT : OFPRR_IDLE_TIMEOUT));
3404 rule_remove(p, rule);
3408 active_timeout(struct ofproto *ofproto, struct rule *rule)
3410 if (ofproto->netflow && !is_controller_rule(rule) &&
3411 netflow_active_timeout_expired(ofproto->netflow, &rule->nf_flow)) {
3412 struct ofexpired expired;
3413 struct odp_flow odp_flow;
3415 /* Get updated flow stats. */
3416 memset(&odp_flow, 0, sizeof odp_flow);
3417 if (rule->installed) {
3418 odp_flow.key = rule->cr.flow;
3419 odp_flow.flags = ODPFF_ZERO_TCP_FLAGS;
3420 dpif_flow_get(ofproto->dpif, &odp_flow);
3422 if (odp_flow.stats.n_packets) {
3423 update_time(ofproto, rule, &odp_flow.stats);
3424 netflow_flow_update_flags(&rule->nf_flow, odp_flow.stats.ip_tos,
3425 odp_flow.stats.tcp_flags);
3429 expired.flow = rule->cr.flow;
3430 expired.packet_count = rule->packet_count +
3431 odp_flow.stats.n_packets;
3432 expired.byte_count = rule->byte_count + odp_flow.stats.n_bytes;
3433 expired.used = rule->used;
3435 netflow_expire(ofproto->netflow, &rule->nf_flow, &expired);
3437 /* Schedule us to send the accumulated records once we have
3438 * collected all of them. */
3439 poll_immediate_wake();
3444 update_used(struct ofproto *p)
3446 struct odp_flow *flows;
3451 error = dpif_flow_list_all(p->dpif, &flows, &n_flows);
3456 for (i = 0; i < n_flows; i++) {
3457 struct odp_flow *f = &flows[i];
3460 rule = rule_from_cls_rule(
3461 classifier_find_rule_exactly(&p->cls, &f->key, 0, UINT16_MAX));
3462 if (!rule || !rule->installed) {
3463 COVERAGE_INC(ofproto_unexpected_rule);
3464 dpif_flow_del(p->dpif, f);
3468 update_time(p, rule, &f->stats);
3469 rule_account(p, rule, f->stats.n_bytes);
3475 do_send_packet_in(struct ofconn *ofconn, uint32_t buffer_id,
3476 const struct ofpbuf *packet, int send_len)
3478 struct odp_msg *msg = packet->data;
3479 struct ofpbuf payload;
3483 /* Extract packet payload from 'msg'. */
3484 payload.data = msg + 1;
3485 payload.size = msg->length - sizeof *msg;
3487 /* Construct ofp_packet_in message. */
3488 reason = msg->type == _ODPL_ACTION_NR ? OFPR_ACTION : OFPR_NO_MATCH;
3489 opi = make_packet_in(buffer_id, odp_port_to_ofp_port(msg->port), reason,
3490 &payload, send_len);
3493 rconn_send_with_limit(ofconn->rconn, opi, ofconn->packet_in_counter, 100);
3497 send_packet_in_action(struct ofpbuf *packet, void *p_)
3499 struct ofproto *p = p_;
3500 struct ofconn *ofconn;
3501 struct odp_msg *msg;
3504 LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
3505 if (ofconn == p->controller || ofconn->miss_send_len) {
3506 do_send_packet_in(ofconn, UINT32_MAX, packet, msg->arg);
3509 ofpbuf_delete(packet);
3513 send_packet_in_miss(struct ofpbuf *packet, void *p_)
3515 struct ofproto *p = p_;
3516 bool in_fail_open = p->fail_open && fail_open_is_active(p->fail_open);
3517 struct ofconn *ofconn;
3518 struct ofpbuf payload;
3519 struct odp_msg *msg;
3522 payload.data = msg + 1;
3523 payload.size = msg->length - sizeof *msg;
3524 LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
3525 if (ofconn->miss_send_len) {
3526 struct pktbuf *pb = ofconn->pktbuf;
3527 uint32_t buffer_id = (in_fail_open
3529 : pktbuf_save(pb, &payload, msg->port));
3530 int send_len = (buffer_id != UINT32_MAX ? ofconn->miss_send_len
3532 do_send_packet_in(ofconn, buffer_id, packet, send_len);
3535 ofpbuf_delete(packet);
3539 pick_datapath_id(const struct ofproto *ofproto)
3541 const struct ofport *port;
3543 port = port_array_get(&ofproto->ports, ODPP_LOCAL);
3545 uint8_t ea[ETH_ADDR_LEN];
3548 error = netdev_get_etheraddr(port->netdev, ea);
3550 return eth_addr_to_uint64(ea);
3552 VLOG_WARN("could not get MAC address for %s (%s)",
3553 netdev_get_name(port->netdev), strerror(error));
3555 return ofproto->fallback_dpid;
3559 pick_fallback_dpid(void)
3561 uint8_t ea[ETH_ADDR_LEN];
3562 eth_addr_random(ea);
3563 ea[0] = 0x00; /* Set Nicira OUI. */
3566 return eth_addr_to_uint64(ea);
3570 default_normal_ofhook_cb(const flow_t *flow, const struct ofpbuf *packet,
3571 struct odp_actions *actions, tag_type *tags,
3572 uint16_t *nf_output_iface, void *ofproto_)
3574 struct ofproto *ofproto = ofproto_;
3577 /* Drop frames for reserved multicast addresses. */
3578 if (eth_addr_is_reserved(flow->dl_dst)) {
3582 /* Learn source MAC (but don't try to learn from revalidation). */
3583 if (packet != NULL) {
3584 tag_type rev_tag = mac_learning_learn(ofproto->ml, flow->dl_src,
3587 /* The log messages here could actually be useful in debugging,
3588 * so keep the rate limit relatively high. */
3589 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
3590 VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on port %"PRIu16,
3591 ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
3592 ofproto_revalidate(ofproto, rev_tag);
3596 /* Determine output port. */
3597 out_port = mac_learning_lookup_tag(ofproto->ml, flow->dl_dst, 0, tags);
3599 add_output_group_action(actions, DP_GROUP_FLOOD, nf_output_iface);
3600 } else if (out_port != flow->in_port) {
3601 odp_actions_add(actions, ODPAT_OUTPUT)->output.port = out_port;
3602 *nf_output_iface = out_port;
3610 static const struct ofhooks default_ofhooks = {
3612 default_normal_ofhook_cb,