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"
30 #include "fail-open.h"
32 #include "mac-learning.h"
36 #include "ofp-print.h"
37 #include "ofproto-sflow.h"
39 #include "openflow/nicira-ext.h"
40 #include "openflow/openflow.h"
41 #include "openvswitch/datapath-protocol.h"
45 #include "poll-loop.h"
46 #include "port-array.h"
51 #include "stream-ssl.h"
59 #define THIS_MODULE VLM_ofproto
62 #include "sflow_api.h"
66 TABLEID_CLASSIFIER = 1
70 struct netdev *netdev;
71 struct ofp_phy_port opp; /* In host byte order. */
74 static void ofport_free(struct ofport *);
75 static void hton_ofp_phy_port(struct ofp_phy_port *);
77 static int xlate_actions(const union ofp_action *in, size_t n_in,
78 const flow_t *flow, struct ofproto *ofproto,
79 const struct ofpbuf *packet,
80 struct odp_actions *out, tag_type *tags,
81 bool *may_set_up_flow, uint16_t *nf_output_iface);
86 uint16_t idle_timeout; /* In seconds from time of last use. */
87 uint16_t hard_timeout; /* In seconds from time of creation. */
88 long long int used; /* Last-used time (0 if never used). */
89 long long int created; /* Creation time. */
90 uint64_t packet_count; /* Number of packets received. */
91 uint64_t byte_count; /* Number of bytes received. */
92 uint64_t accounted_bytes; /* Number of bytes passed to account_cb. */
93 tag_type tags; /* Tags (set only by hooks). */
94 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
96 /* If 'super' is non-NULL, this rule is a subrule, that is, it is an
97 * exact-match rule (having cr.wc.wildcards of 0) generated from the
98 * wildcard rule 'super'. In this case, 'list' is an element of the
101 * If 'super' is NULL, this rule is a super-rule, and 'list' is the head of
102 * a list of subrules. A super-rule with no wildcards (where
103 * cr.wc.wildcards is 0) will never have any subrules. */
109 * A subrule has no actions (it uses the super-rule's actions). */
111 union ofp_action *actions;
115 * A super-rule with wildcard fields never has ODP actions (since the
116 * datapath only supports exact-match flows). */
117 bool installed; /* Installed in datapath? */
118 bool may_install; /* True ordinarily; false if actions must
119 * be reassessed for every packet. */
121 union odp_action *odp_actions;
125 rule_is_hidden(const struct rule *rule)
127 /* Subrules are merely an implementation detail, so hide them from the
129 if (rule->super != NULL) {
133 /* Rules with priority higher than UINT16_MAX are set up by ofproto itself
134 * (e.g. by in-band control) and are intentionally hidden from the
136 if (rule->cr.priority > UINT16_MAX) {
143 static struct rule *rule_create(struct ofproto *, struct rule *super,
144 const union ofp_action *, size_t n_actions,
145 uint16_t idle_timeout, uint16_t hard_timeout);
146 static void rule_free(struct rule *);
147 static void rule_destroy(struct ofproto *, struct rule *);
148 static struct rule *rule_from_cls_rule(const struct cls_rule *);
149 static void rule_insert(struct ofproto *, struct rule *,
150 struct ofpbuf *packet, uint16_t in_port);
151 static void rule_remove(struct ofproto *, struct rule *);
152 static bool rule_make_actions(struct ofproto *, struct rule *,
153 const struct ofpbuf *packet);
154 static void rule_install(struct ofproto *, struct rule *,
155 struct rule *displaced_rule);
156 static void rule_uninstall(struct ofproto *, struct rule *);
157 static void rule_post_uninstall(struct ofproto *, struct rule *);
162 struct pktbuf *pktbuf;
166 struct rconn_packet_counter *packet_in_counter;
168 /* Number of OpenFlow messages queued as replies to OpenFlow requests, and
169 * the maximum number before we stop reading OpenFlow requests. */
170 #define OFCONN_REPLY_MAX 100
171 struct rconn_packet_counter *reply_counter;
174 static struct ofconn *ofconn_create(struct ofproto *, struct rconn *);
175 static void ofconn_destroy(struct ofconn *);
176 static void ofconn_run(struct ofconn *, struct ofproto *);
177 static void ofconn_wait(struct ofconn *);
178 static void queue_tx(struct ofpbuf *msg, const struct ofconn *ofconn,
179 struct rconn_packet_counter *counter);
183 uint64_t datapath_id; /* Datapath ID. */
184 uint64_t fallback_dpid; /* Datapath ID if no better choice found. */
185 char *manufacturer; /* Manufacturer. */
186 char *hardware; /* Hardware. */
187 char *software; /* Software version. */
188 char *serial; /* Serial number. */
192 struct netdev_monitor *netdev_monitor;
193 struct port_array ports; /* Index is ODP port nr; ofport->opp.port_no is
195 struct shash port_by_name;
199 struct switch_status *switch_status;
200 struct status_category *ss_cat;
201 struct in_band *in_band;
202 struct discovery *discovery;
203 struct fail_open *fail_open;
204 struct pinsched *miss_sched, *action_sched;
205 struct netflow *netflow;
206 struct ofproto_sflow *sflow;
209 struct classifier cls;
210 bool need_revalidate;
211 long long int next_expiration;
212 struct tag_set revalidate_set;
214 /* OpenFlow connections. */
215 struct list all_conns;
216 struct ofconn *controller;
217 struct pvconn **listeners;
219 struct pvconn **snoops;
222 /* Hooks for ovs-vswitchd. */
223 const struct ofhooks *ofhooks;
226 /* Used by default ofhooks. */
227 struct mac_learning *ml;
230 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
232 static const struct ofhooks default_ofhooks;
234 static uint64_t pick_datapath_id(const struct ofproto *);
235 static uint64_t pick_fallback_dpid(void);
236 static void send_packet_in_miss(struct ofpbuf *, void *ofproto);
237 static void send_packet_in_action(struct ofpbuf *, void *ofproto);
238 static void update_used(struct ofproto *);
239 static void update_stats(struct ofproto *, struct rule *,
240 const struct odp_flow_stats *);
241 static void expire_rule(struct cls_rule *, void *ofproto);
242 static void active_timeout(struct ofproto *ofproto, struct rule *rule);
243 static bool revalidate_rule(struct ofproto *p, struct rule *rule);
244 static void revalidate_cb(struct cls_rule *rule_, void *p_);
246 static void handle_odp_msg(struct ofproto *, struct ofpbuf *);
248 static void handle_openflow(struct ofconn *, struct ofproto *,
251 static void refresh_port_groups(struct ofproto *);
253 static void update_port(struct ofproto *, const char *devname);
254 static int init_ports(struct ofproto *);
255 static void reinit_ports(struct ofproto *);
258 ofproto_create(const char *datapath, const char *datapath_type,
259 const struct ofhooks *ofhooks, void *aux,
260 struct ofproto **ofprotop)
262 struct odp_stats stats;
269 /* Connect to datapath and start listening for messages. */
270 error = dpif_open(datapath, datapath_type, &dpif);
272 VLOG_ERR("failed to open datapath %s: %s", datapath, strerror(error));
275 error = dpif_get_dp_stats(dpif, &stats);
277 VLOG_ERR("failed to obtain stats for datapath %s: %s",
278 datapath, strerror(error));
282 error = dpif_recv_set_mask(dpif, ODPL_MISS | ODPL_ACTION | ODPL_SFLOW);
284 VLOG_ERR("failed to listen on datapath %s: %s",
285 datapath, strerror(error));
289 dpif_flow_flush(dpif);
290 dpif_recv_purge(dpif);
292 /* Initialize settings. */
293 p = xzalloc(sizeof *p);
294 p->fallback_dpid = pick_fallback_dpid();
295 p->datapath_id = p->fallback_dpid;
296 p->manufacturer = xstrdup("Nicira Networks, Inc.");
297 p->hardware = xstrdup("Reference Implementation");
298 p->software = xstrdup(VERSION BUILDNR);
299 p->serial = xstrdup("None");
301 /* Initialize datapath. */
303 p->netdev_monitor = netdev_monitor_create();
304 port_array_init(&p->ports);
305 shash_init(&p->port_by_name);
306 p->max_ports = stats.max_ports;
308 /* Initialize submodules. */
309 p->switch_status = switch_status_create(p);
313 p->miss_sched = p->action_sched = NULL;
317 /* Initialize flow table. */
318 classifier_init(&p->cls);
319 p->need_revalidate = false;
320 p->next_expiration = time_msec() + 1000;
321 tag_set_init(&p->revalidate_set);
323 /* Initialize OpenFlow connections. */
324 list_init(&p->all_conns);
325 p->controller = ofconn_create(p, rconn_create(5, 8));
326 p->controller->pktbuf = pktbuf_create();
327 p->controller->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
333 /* Initialize hooks. */
335 p->ofhooks = ofhooks;
339 p->ofhooks = &default_ofhooks;
341 p->ml = mac_learning_create();
344 /* Register switch status category. */
345 p->ss_cat = switch_status_register(p->switch_status, "remote",
346 rconn_status_cb, p->controller->rconn);
348 /* Pick final datapath ID. */
349 p->datapath_id = pick_datapath_id(p);
350 VLOG_INFO("using datapath ID %012"PRIx64, p->datapath_id);
357 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
359 uint64_t old_dpid = p->datapath_id;
360 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
361 if (p->datapath_id != old_dpid) {
362 VLOG_INFO("datapath ID changed to %012"PRIx64, p->datapath_id);
363 rconn_reconnect(p->controller->rconn);
368 ofproto_set_probe_interval(struct ofproto *p, int probe_interval)
370 probe_interval = probe_interval ? MAX(probe_interval, 5) : 0;
371 rconn_set_probe_interval(p->controller->rconn, probe_interval);
373 int trigger_duration = probe_interval ? probe_interval * 3 : 15;
374 fail_open_set_trigger_duration(p->fail_open, trigger_duration);
379 ofproto_set_max_backoff(struct ofproto *p, int max_backoff)
381 rconn_set_max_backoff(p->controller->rconn, max_backoff);
385 ofproto_set_desc(struct ofproto *p,
386 const char *manufacturer, const char *hardware,
387 const char *software, const char *serial)
390 free(p->manufacturer);
391 p->manufacturer = xstrdup(manufacturer);
395 p->hardware = xstrdup(hardware);
399 p->software = xstrdup(software);
403 p->serial = xstrdup(serial);
408 ofproto_set_in_band(struct ofproto *p, bool in_band)
410 if (in_band != (p->in_band != NULL)) {
412 return in_band_create(p, p->dpif, p->switch_status,
413 p->controller->rconn, &p->in_band);
415 ofproto_set_discovery(p, false, NULL, true);
416 in_band_destroy(p->in_band);
419 rconn_reconnect(p->controller->rconn);
425 ofproto_set_discovery(struct ofproto *p, bool discovery,
426 const char *re, bool update_resolv_conf)
428 if (discovery != (p->discovery != NULL)) {
430 int error = ofproto_set_in_band(p, true);
434 error = discovery_create(re, update_resolv_conf,
435 p->dpif, p->switch_status,
441 discovery_destroy(p->discovery);
444 rconn_disconnect(p->controller->rconn);
445 } else if (discovery) {
446 discovery_set_update_resolv_conf(p->discovery, update_resolv_conf);
447 return discovery_set_accept_controller_re(p->discovery, re);
453 ofproto_set_controller(struct ofproto *ofproto, const char *controller)
455 if (ofproto->discovery) {
457 } else if (controller) {
458 if (strcmp(rconn_get_name(ofproto->controller->rconn), controller)) {
459 return rconn_connect(ofproto->controller->rconn, controller);
464 rconn_disconnect(ofproto->controller->rconn);
470 set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
471 const struct svec *svec)
473 struct pvconn **pvconns = *pvconnsp;
474 size_t n_pvconns = *n_pvconnsp;
478 for (i = 0; i < n_pvconns; i++) {
479 pvconn_close(pvconns[i]);
483 pvconns = xmalloc(svec->n * sizeof *pvconns);
485 for (i = 0; i < svec->n; i++) {
486 const char *name = svec->names[i];
487 struct pvconn *pvconn;
490 error = pvconn_open(name, &pvconn);
492 pvconns[n_pvconns++] = pvconn;
494 VLOG_ERR("failed to listen on %s: %s", name, strerror(error));
502 *n_pvconnsp = n_pvconns;
508 ofproto_set_listeners(struct ofproto *ofproto, const struct svec *listeners)
510 return set_pvconns(&ofproto->listeners, &ofproto->n_listeners, listeners);
514 ofproto_set_snoops(struct ofproto *ofproto, const struct svec *snoops)
516 return set_pvconns(&ofproto->snoops, &ofproto->n_snoops, snoops);
520 ofproto_set_netflow(struct ofproto *ofproto,
521 const struct netflow_options *nf_options)
523 if (nf_options && nf_options->collectors.n) {
524 if (!ofproto->netflow) {
525 ofproto->netflow = netflow_create();
527 return netflow_set_options(ofproto->netflow, nf_options);
529 netflow_destroy(ofproto->netflow);
530 ofproto->netflow = NULL;
536 ofproto_set_sflow(struct ofproto *ofproto,
537 const struct ofproto_sflow_options *oso)
539 struct ofproto_sflow *os = ofproto->sflow;
542 struct ofport *ofport;
543 unsigned int odp_port;
545 os = ofproto->sflow = ofproto_sflow_create(ofproto->dpif);
546 refresh_port_groups(ofproto);
547 PORT_ARRAY_FOR_EACH (ofport, &ofproto->ports, odp_port) {
548 ofproto_sflow_add_port(os, odp_port,
549 netdev_get_name(ofport->netdev));
552 ofproto_sflow_set_options(os, oso);
554 ofproto_sflow_destroy(os);
555 ofproto->sflow = NULL;
560 ofproto_set_failure(struct ofproto *ofproto, bool fail_open)
563 struct rconn *rconn = ofproto->controller->rconn;
564 int trigger_duration = rconn_get_probe_interval(rconn) * 3;
565 if (!ofproto->fail_open) {
566 ofproto->fail_open = fail_open_create(ofproto, trigger_duration,
567 ofproto->switch_status,
570 fail_open_set_trigger_duration(ofproto->fail_open,
574 fail_open_destroy(ofproto->fail_open);
575 ofproto->fail_open = NULL;
580 ofproto_set_rate_limit(struct ofproto *ofproto,
581 int rate_limit, int burst_limit)
583 if (rate_limit > 0) {
584 if (!ofproto->miss_sched) {
585 ofproto->miss_sched = pinsched_create(rate_limit, burst_limit,
586 ofproto->switch_status);
587 ofproto->action_sched = pinsched_create(rate_limit, burst_limit,
590 pinsched_set_limits(ofproto->miss_sched, rate_limit, burst_limit);
591 pinsched_set_limits(ofproto->action_sched,
592 rate_limit, burst_limit);
595 pinsched_destroy(ofproto->miss_sched);
596 ofproto->miss_sched = NULL;
597 pinsched_destroy(ofproto->action_sched);
598 ofproto->action_sched = NULL;
603 ofproto_set_stp(struct ofproto *ofproto OVS_UNUSED, bool enable_stp)
607 VLOG_WARN("STP is not yet implemented");
615 ofproto_get_datapath_id(const struct ofproto *ofproto)
617 return ofproto->datapath_id;
621 ofproto_get_probe_interval(const struct ofproto *ofproto)
623 return rconn_get_probe_interval(ofproto->controller->rconn);
627 ofproto_get_max_backoff(const struct ofproto *ofproto)
629 return rconn_get_max_backoff(ofproto->controller->rconn);
633 ofproto_get_in_band(const struct ofproto *ofproto)
635 return ofproto->in_band != NULL;
639 ofproto_get_discovery(const struct ofproto *ofproto)
641 return ofproto->discovery != NULL;
645 ofproto_get_controller(const struct ofproto *ofproto)
647 return rconn_get_name(ofproto->controller->rconn);
651 ofproto_get_listeners(const struct ofproto *ofproto, struct svec *listeners)
655 for (i = 0; i < ofproto->n_listeners; i++) {
656 svec_add(listeners, pvconn_get_name(ofproto->listeners[i]));
661 ofproto_get_snoops(const struct ofproto *ofproto, struct svec *snoops)
665 for (i = 0; i < ofproto->n_snoops; i++) {
666 svec_add(snoops, pvconn_get_name(ofproto->snoops[i]));
671 ofproto_destroy(struct ofproto *p)
673 struct ofconn *ofconn, *next_ofconn;
674 struct ofport *ofport;
675 unsigned int port_no;
682 /* Destroy fail-open early, because it touches the classifier. */
683 ofproto_set_failure(p, false);
685 ofproto_flush_flows(p);
686 classifier_destroy(&p->cls);
688 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, struct ofconn, node,
690 ofconn_destroy(ofconn);
694 netdev_monitor_destroy(p->netdev_monitor);
695 PORT_ARRAY_FOR_EACH (ofport, &p->ports, port_no) {
698 shash_destroy(&p->port_by_name);
700 switch_status_destroy(p->switch_status);
701 in_band_destroy(p->in_band);
702 discovery_destroy(p->discovery);
703 pinsched_destroy(p->miss_sched);
704 pinsched_destroy(p->action_sched);
705 netflow_destroy(p->netflow);
706 ofproto_sflow_destroy(p->sflow);
708 switch_status_unregister(p->ss_cat);
710 for (i = 0; i < p->n_listeners; i++) {
711 pvconn_close(p->listeners[i]);
715 for (i = 0; i < p->n_snoops; i++) {
716 pvconn_close(p->snoops[i]);
720 mac_learning_destroy(p->ml);
726 ofproto_run(struct ofproto *p)
728 int error = ofproto_run1(p);
730 error = ofproto_run2(p, false);
736 process_port_change(struct ofproto *ofproto, int error, char *devname)
738 if (error == ENOBUFS) {
739 reinit_ports(ofproto);
741 update_port(ofproto, devname);
747 ofproto_run1(struct ofproto *p)
749 struct ofconn *ofconn, *next_ofconn;
754 if (shash_is_empty(&p->port_by_name)) {
758 for (i = 0; i < 50; i++) {
762 error = dpif_recv(p->dpif, &buf);
764 if (error == ENODEV) {
765 /* Someone destroyed the datapath behind our back. The caller
766 * better destroy us and give up, because we're just going to
767 * spin from here on out. */
768 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
769 VLOG_ERR_RL(&rl, "%s: datapath was destroyed externally",
776 handle_odp_msg(p, buf);
779 while ((error = dpif_port_poll(p->dpif, &devname)) != EAGAIN) {
780 process_port_change(p, error, devname);
782 while ((error = netdev_monitor_poll(p->netdev_monitor,
783 &devname)) != EAGAIN) {
784 process_port_change(p, error, devname);
788 in_band_run(p->in_band);
791 char *controller_name;
792 if (rconn_is_connectivity_questionable(p->controller->rconn)) {
793 discovery_question_connectivity(p->discovery);
795 if (discovery_run(p->discovery, &controller_name)) {
796 if (controller_name) {
797 rconn_connect(p->controller->rconn, controller_name);
799 rconn_disconnect(p->controller->rconn);
803 pinsched_run(p->miss_sched, send_packet_in_miss, p);
804 pinsched_run(p->action_sched, send_packet_in_action, p);
806 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, struct ofconn, node,
808 ofconn_run(ofconn, p);
811 /* Fail-open maintenance. Do this after processing the ofconns since
812 * fail-open checks the status of the controller rconn. */
814 fail_open_run(p->fail_open);
817 for (i = 0; i < p->n_listeners; i++) {
821 retval = pvconn_accept(p->listeners[i], OFP_VERSION, &vconn);
823 ofconn_create(p, rconn_new_from_vconn("passive", vconn));
824 } else if (retval != EAGAIN) {
825 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
829 for (i = 0; i < p->n_snoops; i++) {
833 retval = pvconn_accept(p->snoops[i], OFP_VERSION, &vconn);
835 rconn_add_monitor(p->controller->rconn, vconn);
836 } else if (retval != EAGAIN) {
837 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
841 if (time_msec() >= p->next_expiration) {
842 COVERAGE_INC(ofproto_expiration);
843 p->next_expiration = time_msec() + 1000;
846 classifier_for_each(&p->cls, CLS_INC_ALL, expire_rule, p);
848 /* Let the hook know that we're at a stable point: all outstanding data
849 * in existing flows has been accounted to the account_cb. Thus, the
850 * hook can now reasonably do operations that depend on having accurate
851 * flow volume accounting (currently, that's just bond rebalancing). */
852 if (p->ofhooks->account_checkpoint_cb) {
853 p->ofhooks->account_checkpoint_cb(p->aux);
858 netflow_run(p->netflow);
861 ofproto_sflow_run(p->sflow);
867 struct revalidate_cbdata {
868 struct ofproto *ofproto;
869 bool revalidate_all; /* Revalidate all exact-match rules? */
870 bool revalidate_subrules; /* Revalidate all exact-match subrules? */
871 struct tag_set revalidate_set; /* Set of tags to revalidate. */
875 ofproto_run2(struct ofproto *p, bool revalidate_all)
877 if (p->need_revalidate || revalidate_all
878 || !tag_set_is_empty(&p->revalidate_set)) {
879 struct revalidate_cbdata cbdata;
881 cbdata.revalidate_all = revalidate_all;
882 cbdata.revalidate_subrules = p->need_revalidate;
883 cbdata.revalidate_set = p->revalidate_set;
884 tag_set_init(&p->revalidate_set);
885 COVERAGE_INC(ofproto_revalidate);
886 classifier_for_each(&p->cls, CLS_INC_EXACT, revalidate_cb, &cbdata);
887 p->need_revalidate = false;
894 ofproto_wait(struct ofproto *p)
896 struct ofconn *ofconn;
899 dpif_recv_wait(p->dpif);
900 dpif_port_poll_wait(p->dpif);
901 netdev_monitor_poll_wait(p->netdev_monitor);
902 LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
906 in_band_wait(p->in_band);
909 discovery_wait(p->discovery);
912 fail_open_wait(p->fail_open);
914 pinsched_wait(p->miss_sched);
915 pinsched_wait(p->action_sched);
917 ofproto_sflow_wait(p->sflow);
919 if (!tag_set_is_empty(&p->revalidate_set)) {
920 poll_immediate_wake();
922 if (p->need_revalidate) {
923 /* Shouldn't happen, but if it does just go around again. */
924 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
925 poll_immediate_wake();
926 } else if (p->next_expiration != LLONG_MAX) {
927 poll_timer_wait(p->next_expiration - time_msec());
929 for (i = 0; i < p->n_listeners; i++) {
930 pvconn_wait(p->listeners[i]);
932 for (i = 0; i < p->n_snoops; i++) {
933 pvconn_wait(p->snoops[i]);
938 ofproto_revalidate(struct ofproto *ofproto, tag_type tag)
940 tag_set_add(&ofproto->revalidate_set, tag);
944 ofproto_get_revalidate_set(struct ofproto *ofproto)
946 return &ofproto->revalidate_set;
950 ofproto_is_alive(const struct ofproto *p)
952 return p->discovery || rconn_is_alive(p->controller->rconn);
956 ofproto_send_packet(struct ofproto *p, const flow_t *flow,
957 const union ofp_action *actions, size_t n_actions,
958 const struct ofpbuf *packet)
960 struct odp_actions odp_actions;
963 error = xlate_actions(actions, n_actions, flow, p, packet, &odp_actions,
969 /* XXX Should we translate the dpif_execute() errno value into an OpenFlow
971 dpif_execute(p->dpif, flow->in_port, odp_actions.actions,
972 odp_actions.n_actions, packet);
977 ofproto_add_flow(struct ofproto *p,
978 const flow_t *flow, uint32_t wildcards, unsigned int priority,
979 const union ofp_action *actions, size_t n_actions,
983 rule = rule_create(p, NULL, actions, n_actions,
984 idle_timeout >= 0 ? idle_timeout : 5 /* XXX */, 0);
985 cls_rule_from_flow(&rule->cr, flow, wildcards, priority);
986 rule_insert(p, rule, NULL, 0);
990 ofproto_delete_flow(struct ofproto *ofproto, const flow_t *flow,
991 uint32_t wildcards, unsigned int priority)
995 rule = rule_from_cls_rule(classifier_find_rule_exactly(&ofproto->cls,
999 rule_remove(ofproto, rule);
1004 destroy_rule(struct cls_rule *rule_, void *ofproto_)
1006 struct rule *rule = rule_from_cls_rule(rule_);
1007 struct ofproto *ofproto = ofproto_;
1009 /* Mark the flow as not installed, even though it might really be
1010 * installed, so that rule_remove() doesn't bother trying to uninstall it.
1011 * There is no point in uninstalling it individually since we are about to
1012 * blow away all the flows with dpif_flow_flush(). */
1013 rule->installed = false;
1015 rule_remove(ofproto, rule);
1019 ofproto_flush_flows(struct ofproto *ofproto)
1021 COVERAGE_INC(ofproto_flush);
1022 classifier_for_each(&ofproto->cls, CLS_INC_ALL, destroy_rule, ofproto);
1023 dpif_flow_flush(ofproto->dpif);
1024 if (ofproto->in_band) {
1025 in_band_flushed(ofproto->in_band);
1027 if (ofproto->fail_open) {
1028 fail_open_flushed(ofproto->fail_open);
1033 reinit_ports(struct ofproto *p)
1035 struct svec devnames;
1036 struct ofport *ofport;
1037 unsigned int port_no;
1038 struct odp_port *odp_ports;
1042 svec_init(&devnames);
1043 PORT_ARRAY_FOR_EACH (ofport, &p->ports, port_no) {
1044 svec_add (&devnames, (char *) ofport->opp.name);
1046 dpif_port_list(p->dpif, &odp_ports, &n_odp_ports);
1047 for (i = 0; i < n_odp_ports; i++) {
1048 svec_add (&devnames, odp_ports[i].devname);
1052 svec_sort_unique(&devnames);
1053 for (i = 0; i < devnames.n; i++) {
1054 update_port(p, devnames.names[i]);
1056 svec_destroy(&devnames);
1060 refresh_port_group(struct ofproto *p, unsigned int group)
1064 struct ofport *port;
1065 unsigned int port_no;
1067 assert(group == DP_GROUP_ALL || group == DP_GROUP_FLOOD);
1069 ports = xmalloc(port_array_count(&p->ports) * sizeof *ports);
1071 PORT_ARRAY_FOR_EACH (port, &p->ports, port_no) {
1072 if (group == DP_GROUP_ALL || !(port->opp.config & OFPPC_NO_FLOOD)) {
1073 ports[n_ports++] = port_no;
1076 dpif_port_group_set(p->dpif, group, ports, n_ports);
1083 refresh_port_groups(struct ofproto *p)
1085 size_t n_flood = refresh_port_group(p, DP_GROUP_FLOOD);
1086 size_t n_all = refresh_port_group(p, DP_GROUP_ALL);
1088 ofproto_sflow_set_group_sizes(p->sflow, n_flood, n_all);
1092 static struct ofport *
1093 make_ofport(const struct odp_port *odp_port)
1095 struct netdev_options netdev_options;
1096 enum netdev_flags flags;
1097 struct ofport *ofport;
1098 struct netdev *netdev;
1102 memset(&netdev_options, 0, sizeof netdev_options);
1103 netdev_options.name = odp_port->devname;
1104 netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
1105 netdev_options.may_open = true;
1107 error = netdev_open(&netdev_options, &netdev);
1109 VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
1110 "cannot be opened (%s)",
1111 odp_port->devname, odp_port->port,
1112 odp_port->devname, strerror(error));
1116 ofport = xmalloc(sizeof *ofport);
1117 ofport->netdev = netdev;
1118 ofport->opp.port_no = odp_port_to_ofp_port(odp_port->port);
1119 netdev_get_etheraddr(netdev, ofport->opp.hw_addr);
1120 memcpy(ofport->opp.name, odp_port->devname,
1121 MIN(sizeof ofport->opp.name, sizeof odp_port->devname));
1122 ofport->opp.name[sizeof ofport->opp.name - 1] = '\0';
1124 netdev_get_flags(netdev, &flags);
1125 ofport->opp.config = flags & NETDEV_UP ? 0 : OFPPC_PORT_DOWN;
1127 netdev_get_carrier(netdev, &carrier);
1128 ofport->opp.state = carrier ? 0 : OFPPS_LINK_DOWN;
1130 netdev_get_features(netdev,
1131 &ofport->opp.curr, &ofport->opp.advertised,
1132 &ofport->opp.supported, &ofport->opp.peer);
1137 ofport_conflicts(const struct ofproto *p, const struct odp_port *odp_port)
1139 if (port_array_get(&p->ports, odp_port->port)) {
1140 VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
1143 } else if (shash_find(&p->port_by_name, odp_port->devname)) {
1144 VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
1153 ofport_equal(const struct ofport *a_, const struct ofport *b_)
1155 const struct ofp_phy_port *a = &a_->opp;
1156 const struct ofp_phy_port *b = &b_->opp;
1158 BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */
1159 return (a->port_no == b->port_no
1160 && !memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr)
1161 && !strcmp((char *) a->name, (char *) b->name)
1162 && a->state == b->state
1163 && a->config == b->config
1164 && a->curr == b->curr
1165 && a->advertised == b->advertised
1166 && a->supported == b->supported
1167 && a->peer == b->peer);
1171 send_port_status(struct ofproto *p, const struct ofport *ofport,
1174 /* XXX Should limit the number of queued port status change messages. */
1175 struct ofconn *ofconn;
1176 LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
1177 struct ofp_port_status *ops;
1180 ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &b);
1181 ops->reason = reason;
1182 ops->desc = ofport->opp;
1183 hton_ofp_phy_port(&ops->desc);
1184 queue_tx(b, ofconn, NULL);
1186 if (p->ofhooks->port_changed_cb) {
1187 p->ofhooks->port_changed_cb(reason, &ofport->opp, p->aux);
1192 ofport_install(struct ofproto *p, struct ofport *ofport)
1194 uint16_t odp_port = ofp_port_to_odp_port(ofport->opp.port_no);
1195 const char *netdev_name = (const char *) ofport->opp.name;
1197 netdev_monitor_add(p->netdev_monitor, ofport->netdev);
1198 port_array_set(&p->ports, odp_port, ofport);
1199 shash_add(&p->port_by_name, netdev_name, ofport);
1201 ofproto_sflow_add_port(p->sflow, odp_port, netdev_name);
1206 ofport_remove(struct ofproto *p, struct ofport *ofport)
1208 uint16_t odp_port = ofp_port_to_odp_port(ofport->opp.port_no);
1210 netdev_monitor_remove(p->netdev_monitor, ofport->netdev);
1211 port_array_set(&p->ports, odp_port, NULL);
1212 shash_delete(&p->port_by_name,
1213 shash_find(&p->port_by_name, (char *) ofport->opp.name));
1215 ofproto_sflow_del_port(p->sflow, odp_port);
1220 ofport_free(struct ofport *ofport)
1223 netdev_close(ofport->netdev);
1229 update_port(struct ofproto *p, const char *devname)
1231 struct odp_port odp_port;
1232 struct ofport *old_ofport;
1233 struct ofport *new_ofport;
1236 COVERAGE_INC(ofproto_update_port);
1238 /* Query the datapath for port information. */
1239 error = dpif_port_query_by_name(p->dpif, devname, &odp_port);
1241 /* Find the old ofport. */
1242 old_ofport = shash_find_data(&p->port_by_name, devname);
1245 /* There's no port named 'devname' but there might be a port with
1246 * the same port number. This could happen if a port is deleted
1247 * and then a new one added in its place very quickly, or if a port
1248 * is renamed. In the former case we want to send an OFPPR_DELETE
1249 * and an OFPPR_ADD, and in the latter case we want to send a
1250 * single OFPPR_MODIFY. We can distinguish the cases by comparing
1251 * the old port's ifindex against the new port, or perhaps less
1252 * reliably but more portably by comparing the old port's MAC
1253 * against the new port's MAC. However, this code isn't that smart
1254 * and always sends an OFPPR_MODIFY (XXX). */
1255 old_ofport = port_array_get(&p->ports, odp_port.port);
1257 } else if (error != ENOENT && error != ENODEV) {
1258 VLOG_WARN_RL(&rl, "dpif_port_query_by_name returned unexpected error "
1259 "%s", strerror(error));
1263 /* Create a new ofport. */
1264 new_ofport = !error ? make_ofport(&odp_port) : NULL;
1266 /* Eliminate a few pathological cases. */
1267 if (!old_ofport && !new_ofport) {
1269 } else if (old_ofport && new_ofport) {
1270 /* Most of the 'config' bits are OpenFlow soft state, but
1271 * OFPPC_PORT_DOWN is maintained the kernel. So transfer the OpenFlow
1272 * bits from old_ofport. (make_ofport() only sets OFPPC_PORT_DOWN and
1273 * leaves the other bits 0.) */
1274 new_ofport->opp.config |= old_ofport->opp.config & ~OFPPC_PORT_DOWN;
1276 if (ofport_equal(old_ofport, new_ofport)) {
1277 /* False alarm--no change. */
1278 ofport_free(new_ofport);
1283 /* Now deal with the normal cases. */
1285 ofport_remove(p, old_ofport);
1288 ofport_install(p, new_ofport);
1290 send_port_status(p, new_ofport ? new_ofport : old_ofport,
1291 (!old_ofport ? OFPPR_ADD
1292 : !new_ofport ? OFPPR_DELETE
1294 ofport_free(old_ofport);
1296 /* Update port groups. */
1297 refresh_port_groups(p);
1301 init_ports(struct ofproto *p)
1303 struct odp_port *ports;
1308 error = dpif_port_list(p->dpif, &ports, &n_ports);
1313 for (i = 0; i < n_ports; i++) {
1314 const struct odp_port *odp_port = &ports[i];
1315 if (!ofport_conflicts(p, odp_port)) {
1316 struct ofport *ofport = make_ofport(odp_port);
1318 ofport_install(p, ofport);
1323 refresh_port_groups(p);
1327 static struct ofconn *
1328 ofconn_create(struct ofproto *p, struct rconn *rconn)
1330 struct ofconn *ofconn = xmalloc(sizeof *ofconn);
1331 list_push_back(&p->all_conns, &ofconn->node);
1332 ofconn->rconn = rconn;
1333 ofconn->pktbuf = NULL;
1334 ofconn->send_flow_exp = false;
1335 ofconn->miss_send_len = 0;
1336 ofconn->packet_in_counter = rconn_packet_counter_create ();
1337 ofconn->reply_counter = rconn_packet_counter_create ();
1342 ofconn_destroy(struct ofconn *ofconn)
1344 list_remove(&ofconn->node);
1345 rconn_destroy(ofconn->rconn);
1346 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1347 rconn_packet_counter_destroy(ofconn->reply_counter);
1348 pktbuf_destroy(ofconn->pktbuf);
1353 ofconn_run(struct ofconn *ofconn, struct ofproto *p)
1357 rconn_run(ofconn->rconn);
1359 if (rconn_packet_counter_read (ofconn->reply_counter) < OFCONN_REPLY_MAX) {
1360 /* Limit the number of iterations to prevent other tasks from
1362 for (iteration = 0; iteration < 50; iteration++) {
1363 struct ofpbuf *of_msg = rconn_recv(ofconn->rconn);
1368 fail_open_maybe_recover(p->fail_open);
1370 handle_openflow(ofconn, p, of_msg);
1371 ofpbuf_delete(of_msg);
1375 if (ofconn != p->controller && !rconn_is_alive(ofconn->rconn)) {
1376 ofconn_destroy(ofconn);
1381 ofconn_wait(struct ofconn *ofconn)
1383 rconn_run_wait(ofconn->rconn);
1384 if (rconn_packet_counter_read (ofconn->reply_counter) < OFCONN_REPLY_MAX) {
1385 rconn_recv_wait(ofconn->rconn);
1387 COVERAGE_INC(ofproto_ofconn_stuck);
1391 /* Caller is responsible for initializing the 'cr' member of the returned
1393 static struct rule *
1394 rule_create(struct ofproto *ofproto, struct rule *super,
1395 const union ofp_action *actions, size_t n_actions,
1396 uint16_t idle_timeout, uint16_t hard_timeout)
1398 struct rule *rule = xzalloc(sizeof *rule);
1399 rule->idle_timeout = idle_timeout;
1400 rule->hard_timeout = hard_timeout;
1401 rule->used = rule->created = time_msec();
1402 rule->super = super;
1404 list_push_back(&super->list, &rule->list);
1406 list_init(&rule->list);
1408 rule->n_actions = n_actions;
1409 rule->actions = xmemdup(actions, n_actions * sizeof *actions);
1410 netflow_flow_clear(&rule->nf_flow);
1411 netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, rule->created);
1416 static struct rule *
1417 rule_from_cls_rule(const struct cls_rule *cls_rule)
1419 return cls_rule ? CONTAINER_OF(cls_rule, struct rule, cr) : NULL;
1423 rule_free(struct rule *rule)
1425 free(rule->actions);
1426 free(rule->odp_actions);
1430 /* Destroys 'rule'. If 'rule' is a subrule, also removes it from its
1431 * super-rule's list of subrules. If 'rule' is a super-rule, also iterates
1432 * through all of its subrules and revalidates them, destroying any that no
1433 * longer has a super-rule (which is probably all of them).
1435 * Before calling this function, the caller must make have removed 'rule' from
1436 * the classifier. If 'rule' is an exact-match rule, the caller is also
1437 * responsible for ensuring that it has been uninstalled from the datapath. */
1439 rule_destroy(struct ofproto *ofproto, struct rule *rule)
1442 struct rule *subrule, *next;
1443 LIST_FOR_EACH_SAFE (subrule, next, struct rule, list, &rule->list) {
1444 revalidate_rule(ofproto, subrule);
1447 list_remove(&rule->list);
1453 rule_has_out_port(const struct rule *rule, uint16_t out_port)
1455 const union ofp_action *oa;
1456 struct actions_iterator i;
1458 if (out_port == htons(OFPP_NONE)) {
1461 for (oa = actions_first(&i, rule->actions, rule->n_actions); oa;
1462 oa = actions_next(&i)) {
1463 if (oa->type == htons(OFPAT_OUTPUT) && oa->output.port == out_port) {
1470 /* Executes the actions indicated by 'rule' on 'packet', which is in flow
1471 * 'flow' and is considered to have arrived on ODP port 'in_port'.
1473 * The flow that 'packet' actually contains does not need to actually match
1474 * 'rule'; the actions in 'rule' will be applied to it either way. Likewise,
1475 * the packet and byte counters for 'rule' will be credited for the packet sent
1476 * out whether or not the packet actually matches 'rule'.
1478 * If 'rule' is an exact-match rule and 'flow' actually equals the rule's flow,
1479 * the caller must already have accurately composed ODP actions for it given
1480 * 'packet' using rule_make_actions(). If 'rule' is a wildcard rule, or if
1481 * 'rule' is an exact-match rule but 'flow' is not the rule's flow, then this
1482 * function will compose a set of ODP actions based on 'rule''s OpenFlow
1483 * actions and apply them to 'packet'. */
1485 rule_execute(struct ofproto *ofproto, struct rule *rule,
1486 struct ofpbuf *packet, const flow_t *flow)
1488 const union odp_action *actions;
1490 struct odp_actions a;
1492 /* Grab or compose the ODP actions.
1494 * The special case for an exact-match 'rule' where 'flow' is not the
1495 * rule's flow is important to avoid, e.g., sending a packet out its input
1496 * port simply because the ODP actions were composed for the wrong
1498 if (rule->cr.wc.wildcards || !flow_equal(flow, &rule->cr.flow)) {
1499 struct rule *super = rule->super ? rule->super : rule;
1500 if (xlate_actions(super->actions, super->n_actions, flow, ofproto,
1501 packet, &a, NULL, 0, NULL)) {
1504 actions = a.actions;
1505 n_actions = a.n_actions;
1507 actions = rule->odp_actions;
1508 n_actions = rule->n_odp_actions;
1511 /* Execute the ODP actions. */
1512 if (!dpif_execute(ofproto->dpif, flow->in_port,
1513 actions, n_actions, packet)) {
1514 struct odp_flow_stats stats;
1515 flow_extract_stats(flow, packet, &stats);
1516 update_stats(ofproto, rule, &stats);
1517 rule->used = time_msec();
1518 netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, rule->used);
1523 rule_insert(struct ofproto *p, struct rule *rule, struct ofpbuf *packet,
1526 struct rule *displaced_rule;
1528 /* Insert the rule in the classifier. */
1529 displaced_rule = rule_from_cls_rule(classifier_insert(&p->cls, &rule->cr));
1530 if (!rule->cr.wc.wildcards) {
1531 rule_make_actions(p, rule, packet);
1534 /* Send the packet and credit it to the rule. */
1537 flow_extract(packet, in_port, &flow);
1538 rule_execute(p, rule, packet, &flow);
1541 /* Install the rule in the datapath only after sending the packet, to
1542 * avoid packet reordering. */
1543 if (rule->cr.wc.wildcards) {
1544 COVERAGE_INC(ofproto_add_wc_flow);
1545 p->need_revalidate = true;
1547 rule_install(p, rule, displaced_rule);
1550 /* Free the rule that was displaced, if any. */
1551 if (displaced_rule) {
1552 rule_destroy(p, displaced_rule);
1556 static struct rule *
1557 rule_create_subrule(struct ofproto *ofproto, struct rule *rule,
1560 struct rule *subrule = rule_create(ofproto, rule, NULL, 0,
1561 rule->idle_timeout, rule->hard_timeout);
1562 COVERAGE_INC(ofproto_subrule_create);
1563 cls_rule_from_flow(&subrule->cr, flow, 0,
1564 (rule->cr.priority <= UINT16_MAX ? UINT16_MAX
1565 : rule->cr.priority));
1566 classifier_insert_exact(&ofproto->cls, &subrule->cr);
1572 rule_remove(struct ofproto *ofproto, struct rule *rule)
1574 if (rule->cr.wc.wildcards) {
1575 COVERAGE_INC(ofproto_del_wc_flow);
1576 ofproto->need_revalidate = true;
1578 rule_uninstall(ofproto, rule);
1580 classifier_remove(&ofproto->cls, &rule->cr);
1581 rule_destroy(ofproto, rule);
1584 /* Returns true if the actions changed, false otherwise. */
1586 rule_make_actions(struct ofproto *p, struct rule *rule,
1587 const struct ofpbuf *packet)
1589 const struct rule *super;
1590 struct odp_actions a;
1593 assert(!rule->cr.wc.wildcards);
1595 super = rule->super ? rule->super : rule;
1597 xlate_actions(super->actions, super->n_actions, &rule->cr.flow, p,
1598 packet, &a, &rule->tags, &rule->may_install,
1599 &rule->nf_flow.output_iface);
1601 actions_len = a.n_actions * sizeof *a.actions;
1602 if (rule->n_odp_actions != a.n_actions
1603 || memcmp(rule->odp_actions, a.actions, actions_len)) {
1604 COVERAGE_INC(ofproto_odp_unchanged);
1605 free(rule->odp_actions);
1606 rule->n_odp_actions = a.n_actions;
1607 rule->odp_actions = xmemdup(a.actions, actions_len);
1615 do_put_flow(struct ofproto *ofproto, struct rule *rule, int flags,
1616 struct odp_flow_put *put)
1618 memset(&put->flow.stats, 0, sizeof put->flow.stats);
1619 put->flow.key = rule->cr.flow;
1620 put->flow.actions = rule->odp_actions;
1621 put->flow.n_actions = rule->n_odp_actions;
1623 return dpif_flow_put(ofproto->dpif, put);
1627 rule_install(struct ofproto *p, struct rule *rule, struct rule *displaced_rule)
1629 assert(!rule->cr.wc.wildcards);
1631 if (rule->may_install) {
1632 struct odp_flow_put put;
1633 if (!do_put_flow(p, rule,
1634 ODPPF_CREATE | ODPPF_MODIFY | ODPPF_ZERO_STATS,
1636 rule->installed = true;
1637 if (displaced_rule) {
1638 update_stats(p, displaced_rule, &put.flow.stats);
1639 rule_post_uninstall(p, displaced_rule);
1642 } else if (displaced_rule) {
1643 rule_uninstall(p, displaced_rule);
1648 rule_reinstall(struct ofproto *ofproto, struct rule *rule)
1650 if (rule->installed) {
1651 struct odp_flow_put put;
1652 COVERAGE_INC(ofproto_dp_missed);
1653 do_put_flow(ofproto, rule, ODPPF_CREATE | ODPPF_MODIFY, &put);
1655 rule_install(ofproto, rule, NULL);
1660 rule_update_actions(struct ofproto *ofproto, struct rule *rule)
1662 bool actions_changed;
1663 uint16_t new_out_iface, old_out_iface;
1665 old_out_iface = rule->nf_flow.output_iface;
1666 actions_changed = rule_make_actions(ofproto, rule, NULL);
1668 if (rule->may_install) {
1669 if (rule->installed) {
1670 if (actions_changed) {
1671 struct odp_flow_put put;
1672 do_put_flow(ofproto, rule, ODPPF_CREATE | ODPPF_MODIFY
1673 | ODPPF_ZERO_STATS, &put);
1674 update_stats(ofproto, rule, &put.flow.stats);
1676 /* Temporarily set the old output iface so that NetFlow
1677 * messages have the correct output interface for the old
1679 new_out_iface = rule->nf_flow.output_iface;
1680 rule->nf_flow.output_iface = old_out_iface;
1681 rule_post_uninstall(ofproto, rule);
1682 rule->nf_flow.output_iface = new_out_iface;
1685 rule_install(ofproto, rule, NULL);
1688 rule_uninstall(ofproto, rule);
1693 rule_account(struct ofproto *ofproto, struct rule *rule, uint64_t extra_bytes)
1695 uint64_t total_bytes = rule->byte_count + extra_bytes;
1697 if (ofproto->ofhooks->account_flow_cb
1698 && total_bytes > rule->accounted_bytes)
1700 ofproto->ofhooks->account_flow_cb(
1701 &rule->cr.flow, rule->odp_actions, rule->n_odp_actions,
1702 total_bytes - rule->accounted_bytes, ofproto->aux);
1703 rule->accounted_bytes = total_bytes;
1708 rule_uninstall(struct ofproto *p, struct rule *rule)
1710 assert(!rule->cr.wc.wildcards);
1711 if (rule->installed) {
1712 struct odp_flow odp_flow;
1714 odp_flow.key = rule->cr.flow;
1715 odp_flow.actions = NULL;
1716 odp_flow.n_actions = 0;
1717 if (!dpif_flow_del(p->dpif, &odp_flow)) {
1718 update_stats(p, rule, &odp_flow.stats);
1720 rule->installed = false;
1722 rule_post_uninstall(p, rule);
1727 is_controller_rule(struct rule *rule)
1729 /* If the only action is send to the controller then don't report
1730 * NetFlow expiration messages since it is just part of the control
1731 * logic for the network and not real traffic. */
1733 if (rule && rule->super) {
1734 struct rule *super = rule->super;
1736 return super->n_actions == 1 &&
1737 super->actions[0].type == htons(OFPAT_OUTPUT) &&
1738 super->actions[0].output.port == htons(OFPP_CONTROLLER);
1745 rule_post_uninstall(struct ofproto *ofproto, struct rule *rule)
1747 struct rule *super = rule->super;
1749 rule_account(ofproto, rule, 0);
1751 if (ofproto->netflow && !is_controller_rule(rule)) {
1752 struct ofexpired expired;
1753 expired.flow = rule->cr.flow;
1754 expired.packet_count = rule->packet_count;
1755 expired.byte_count = rule->byte_count;
1756 expired.used = rule->used;
1757 netflow_expire(ofproto->netflow, &rule->nf_flow, &expired);
1760 super->packet_count += rule->packet_count;
1761 super->byte_count += rule->byte_count;
1763 /* Reset counters to prevent double counting if the rule ever gets
1765 rule->packet_count = 0;
1766 rule->byte_count = 0;
1767 rule->accounted_bytes = 0;
1769 netflow_flow_clear(&rule->nf_flow);
1774 queue_tx(struct ofpbuf *msg, const struct ofconn *ofconn,
1775 struct rconn_packet_counter *counter)
1777 update_openflow_length(msg);
1778 if (rconn_send(ofconn->rconn, msg, counter)) {
1784 send_error(const struct ofconn *ofconn, const struct ofp_header *oh,
1785 int error, const void *data, size_t len)
1788 struct ofp_error_msg *oem;
1790 if (!(error >> 16)) {
1791 VLOG_WARN_RL(&rl, "not sending bad error code %d to controller",
1796 COVERAGE_INC(ofproto_error);
1797 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR,
1798 oh ? oh->xid : 0, &buf);
1799 oem->type = htons((unsigned int) error >> 16);
1800 oem->code = htons(error & 0xffff);
1801 memcpy(oem->data, data, len);
1802 queue_tx(buf, ofconn, ofconn->reply_counter);
1806 send_error_oh(const struct ofconn *ofconn, const struct ofp_header *oh,
1809 size_t oh_length = ntohs(oh->length);
1810 send_error(ofconn, oh, error, oh, MIN(oh_length, 64));
1814 hton_ofp_phy_port(struct ofp_phy_port *opp)
1816 opp->port_no = htons(opp->port_no);
1817 opp->config = htonl(opp->config);
1818 opp->state = htonl(opp->state);
1819 opp->curr = htonl(opp->curr);
1820 opp->advertised = htonl(opp->advertised);
1821 opp->supported = htonl(opp->supported);
1822 opp->peer = htonl(opp->peer);
1826 handle_echo_request(struct ofconn *ofconn, struct ofp_header *oh)
1828 struct ofp_header *rq = oh;
1829 queue_tx(make_echo_reply(rq), ofconn, ofconn->reply_counter);
1834 handle_features_request(struct ofproto *p, struct ofconn *ofconn,
1835 struct ofp_header *oh)
1837 struct ofp_switch_features *osf;
1839 unsigned int port_no;
1840 struct ofport *port;
1842 osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, oh->xid, &buf);
1843 osf->datapath_id = htonll(p->datapath_id);
1844 osf->n_buffers = htonl(pktbuf_capacity());
1846 osf->capabilities = htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS |
1847 OFPC_PORT_STATS | OFPC_MULTI_PHY_TX);
1848 osf->actions = htonl((1u << OFPAT_OUTPUT) |
1849 (1u << OFPAT_SET_VLAN_VID) |
1850 (1u << OFPAT_SET_VLAN_PCP) |
1851 (1u << OFPAT_STRIP_VLAN) |
1852 (1u << OFPAT_SET_DL_SRC) |
1853 (1u << OFPAT_SET_DL_DST) |
1854 (1u << OFPAT_SET_NW_SRC) |
1855 (1u << OFPAT_SET_NW_DST) |
1856 (1u << OFPAT_SET_NW_TOS) |
1857 (1u << OFPAT_SET_TP_SRC) |
1858 (1u << OFPAT_SET_TP_DST));
1860 PORT_ARRAY_FOR_EACH (port, &p->ports, port_no) {
1861 hton_ofp_phy_port(ofpbuf_put(buf, &port->opp, sizeof port->opp));
1864 queue_tx(buf, ofconn, ofconn->reply_counter);
1869 handle_get_config_request(struct ofproto *p, struct ofconn *ofconn,
1870 struct ofp_header *oh)
1873 struct ofp_switch_config *osc;
1877 /* Figure out flags. */
1878 dpif_get_drop_frags(p->dpif, &drop_frags);
1879 flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL;
1880 if (ofconn->send_flow_exp) {
1881 flags |= OFPC_SEND_FLOW_EXP;
1885 osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf);
1886 osc->flags = htons(flags);
1887 osc->miss_send_len = htons(ofconn->miss_send_len);
1888 queue_tx(buf, ofconn, ofconn->reply_counter);
1894 handle_set_config(struct ofproto *p, struct ofconn *ofconn,
1895 struct ofp_switch_config *osc)
1900 error = check_ofp_message(&osc->header, OFPT_SET_CONFIG, sizeof *osc);
1904 flags = ntohs(osc->flags);
1906 ofconn->send_flow_exp = (flags & OFPC_SEND_FLOW_EXP) != 0;
1908 if (ofconn == p->controller) {
1909 switch (flags & OFPC_FRAG_MASK) {
1910 case OFPC_FRAG_NORMAL:
1911 dpif_set_drop_frags(p->dpif, false);
1913 case OFPC_FRAG_DROP:
1914 dpif_set_drop_frags(p->dpif, true);
1917 VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")",
1923 if ((ntohs(osc->miss_send_len) != 0) != (ofconn->miss_send_len != 0)) {
1924 if (ntohs(osc->miss_send_len) != 0) {
1925 ofconn->pktbuf = pktbuf_create();
1927 pktbuf_destroy(ofconn->pktbuf);
1931 ofconn->miss_send_len = ntohs(osc->miss_send_len);
1937 add_output_group_action(struct odp_actions *actions, uint16_t group,
1938 uint16_t *nf_output_iface)
1940 odp_actions_add(actions, ODPAT_OUTPUT_GROUP)->output_group.group = group;
1942 if (group == DP_GROUP_ALL || group == DP_GROUP_FLOOD) {
1943 *nf_output_iface = NF_OUT_FLOOD;
1948 add_controller_action(struct odp_actions *actions,
1949 const struct ofp_action_output *oao)
1951 union odp_action *a = odp_actions_add(actions, ODPAT_CONTROLLER);
1952 a->controller.arg = oao->max_len ? ntohs(oao->max_len) : UINT32_MAX;
1955 struct action_xlate_ctx {
1957 const flow_t *flow; /* Flow to which these actions correspond. */
1958 int recurse; /* Recursion level, via xlate_table_action. */
1959 struct ofproto *ofproto;
1960 const struct ofpbuf *packet; /* The packet corresponding to 'flow', or a
1961 * null pointer if we are revalidating
1962 * without a packet to refer to. */
1965 struct odp_actions *out; /* Datapath actions. */
1966 tag_type *tags; /* Tags associated with OFPP_NORMAL actions. */
1967 bool may_set_up_flow; /* True ordinarily; false if the actions must
1968 * be reassessed for every packet. */
1969 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
1972 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
1973 struct action_xlate_ctx *ctx);
1976 add_output_action(struct action_xlate_ctx *ctx, uint16_t port)
1978 const struct ofport *ofport = port_array_get(&ctx->ofproto->ports, port);
1981 if (ofport->opp.config & OFPPC_NO_FWD) {
1982 /* Forwarding disabled on port. */
1987 * We don't have an ofport record for this port, but it doesn't hurt to
1988 * allow forwarding to it anyhow. Maybe such a port will appear later
1989 * and we're pre-populating the flow table.
1993 odp_actions_add(ctx->out, ODPAT_OUTPUT)->output.port = port;
1994 ctx->nf_output_iface = port;
1997 static struct rule *
1998 lookup_valid_rule(struct ofproto *ofproto, const flow_t *flow)
2001 rule = rule_from_cls_rule(classifier_lookup(&ofproto->cls, flow));
2003 /* The rule we found might not be valid, since we could be in need of
2004 * revalidation. If it is not valid, don't return it. */
2007 && ofproto->need_revalidate
2008 && !revalidate_rule(ofproto, rule)) {
2009 COVERAGE_INC(ofproto_invalidated);
2017 xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
2019 if (!ctx->recurse) {
2024 flow.in_port = in_port;
2026 rule = lookup_valid_rule(ctx->ofproto, &flow);
2033 do_xlate_actions(rule->actions, rule->n_actions, ctx);
2040 xlate_output_action(struct action_xlate_ctx *ctx,
2041 const struct ofp_action_output *oao)
2044 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
2046 ctx->nf_output_iface = NF_OUT_DROP;
2048 switch (ntohs(oao->port)) {
2050 add_output_action(ctx, ctx->flow->in_port);
2053 xlate_table_action(ctx, ctx->flow->in_port);
2056 if (!ctx->ofproto->ofhooks->normal_cb(ctx->flow, ctx->packet,
2057 ctx->out, ctx->tags,
2058 &ctx->nf_output_iface,
2059 ctx->ofproto->aux)) {
2060 COVERAGE_INC(ofproto_uninstallable);
2061 ctx->may_set_up_flow = false;
2065 add_output_group_action(ctx->out, DP_GROUP_FLOOD,
2066 &ctx->nf_output_iface);
2069 add_output_group_action(ctx->out, DP_GROUP_ALL, &ctx->nf_output_iface);
2071 case OFPP_CONTROLLER:
2072 add_controller_action(ctx->out, oao);
2075 add_output_action(ctx, ODPP_LOCAL);
2078 odp_port = ofp_port_to_odp_port(ntohs(oao->port));
2079 if (odp_port != ctx->flow->in_port) {
2080 add_output_action(ctx, odp_port);
2085 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2086 ctx->nf_output_iface = NF_OUT_FLOOD;
2087 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
2088 ctx->nf_output_iface = prev_nf_output_iface;
2089 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2090 ctx->nf_output_iface != NF_OUT_FLOOD) {
2091 ctx->nf_output_iface = NF_OUT_MULTI;
2096 xlate_nicira_action(struct action_xlate_ctx *ctx,
2097 const struct nx_action_header *nah)
2099 const struct nx_action_resubmit *nar;
2100 int subtype = ntohs(nah->subtype);
2102 assert(nah->vendor == htonl(NX_VENDOR_ID));
2104 case NXAST_RESUBMIT:
2105 nar = (const struct nx_action_resubmit *) nah;
2106 xlate_table_action(ctx, ofp_port_to_odp_port(ntohs(nar->in_port)));
2110 VLOG_DBG_RL(&rl, "unknown Nicira action type %"PRIu16, subtype);
2116 do_xlate_actions(const union ofp_action *in, size_t n_in,
2117 struct action_xlate_ctx *ctx)
2119 struct actions_iterator iter;
2120 const union ofp_action *ia;
2121 const struct ofport *port;
2123 port = port_array_get(&ctx->ofproto->ports, ctx->flow->in_port);
2124 if (port && port->opp.config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
2125 port->opp.config & (eth_addr_equals(ctx->flow->dl_dst, stp_eth_addr)
2126 ? OFPPC_NO_RECV_STP : OFPPC_NO_RECV)) {
2127 /* Drop this flow. */
2131 for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
2132 uint16_t type = ntohs(ia->type);
2133 union odp_action *oa;
2137 xlate_output_action(ctx, &ia->output);
2140 case OFPAT_SET_VLAN_VID:
2141 oa = odp_actions_add(ctx->out, ODPAT_SET_VLAN_VID);
2142 oa->vlan_vid.vlan_vid = ia->vlan_vid.vlan_vid;
2145 case OFPAT_SET_VLAN_PCP:
2146 oa = odp_actions_add(ctx->out, ODPAT_SET_VLAN_PCP);
2147 oa->vlan_pcp.vlan_pcp = ia->vlan_pcp.vlan_pcp;
2150 case OFPAT_STRIP_VLAN:
2151 odp_actions_add(ctx->out, ODPAT_STRIP_VLAN);
2154 case OFPAT_SET_DL_SRC:
2155 oa = odp_actions_add(ctx->out, ODPAT_SET_DL_SRC);
2156 memcpy(oa->dl_addr.dl_addr,
2157 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
2160 case OFPAT_SET_DL_DST:
2161 oa = odp_actions_add(ctx->out, ODPAT_SET_DL_DST);
2162 memcpy(oa->dl_addr.dl_addr,
2163 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
2166 case OFPAT_SET_NW_SRC:
2167 oa = odp_actions_add(ctx->out, ODPAT_SET_NW_SRC);
2168 oa->nw_addr.nw_addr = ia->nw_addr.nw_addr;
2171 case OFPAT_SET_NW_DST:
2172 oa = odp_actions_add(ctx->out, ODPAT_SET_NW_DST);
2173 oa->nw_addr.nw_addr = ia->nw_addr.nw_addr;
2175 case OFPAT_SET_NW_TOS:
2176 oa = odp_actions_add(ctx->out, ODPAT_SET_NW_TOS);
2177 oa->nw_tos.nw_tos = ia->nw_tos.nw_tos;
2180 case OFPAT_SET_TP_SRC:
2181 oa = odp_actions_add(ctx->out, ODPAT_SET_TP_SRC);
2182 oa->tp_port.tp_port = ia->tp_port.tp_port;
2185 case OFPAT_SET_TP_DST:
2186 oa = odp_actions_add(ctx->out, ODPAT_SET_TP_DST);
2187 oa->tp_port.tp_port = ia->tp_port.tp_port;
2191 xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
2195 VLOG_DBG_RL(&rl, "unknown action type %"PRIu16, type);
2202 xlate_actions(const union ofp_action *in, size_t n_in,
2203 const flow_t *flow, struct ofproto *ofproto,
2204 const struct ofpbuf *packet,
2205 struct odp_actions *out, tag_type *tags, bool *may_set_up_flow,
2206 uint16_t *nf_output_iface)
2208 tag_type no_tags = 0;
2209 struct action_xlate_ctx ctx;
2210 COVERAGE_INC(ofproto_ofp2odp);
2211 odp_actions_init(out);
2214 ctx.ofproto = ofproto;
2215 ctx.packet = packet;
2217 ctx.tags = tags ? tags : &no_tags;
2218 ctx.may_set_up_flow = true;
2219 ctx.nf_output_iface = NF_OUT_DROP;
2220 do_xlate_actions(in, n_in, &ctx);
2222 /* Check with in-band control to see if we're allowed to set up this
2224 if (!in_band_rule_check(ofproto->in_band, flow, out)) {
2225 ctx.may_set_up_flow = false;
2228 if (may_set_up_flow) {
2229 *may_set_up_flow = ctx.may_set_up_flow;
2231 if (nf_output_iface) {
2232 *nf_output_iface = ctx.nf_output_iface;
2234 if (odp_actions_overflow(out)) {
2235 odp_actions_init(out);
2236 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_TOO_MANY);
2242 handle_packet_out(struct ofproto *p, struct ofconn *ofconn,
2243 struct ofp_header *oh)
2245 struct ofp_packet_out *opo;
2246 struct ofpbuf payload, *buffer;
2247 struct odp_actions actions;
2253 error = check_ofp_packet_out(oh, &payload, &n_actions, p->max_ports);
2257 opo = (struct ofp_packet_out *) oh;
2259 COVERAGE_INC(ofproto_packet_out);
2260 if (opo->buffer_id != htonl(UINT32_MAX)) {
2261 error = pktbuf_retrieve(ofconn->pktbuf, ntohl(opo->buffer_id),
2263 if (error || !buffer) {
2271 flow_extract(&payload, ofp_port_to_odp_port(ntohs(opo->in_port)), &flow);
2272 error = xlate_actions((const union ofp_action *) opo->actions, n_actions,
2273 &flow, p, &payload, &actions, NULL, NULL, NULL);
2278 dpif_execute(p->dpif, flow.in_port, actions.actions, actions.n_actions,
2280 ofpbuf_delete(buffer);
2286 update_port_config(struct ofproto *p, struct ofport *port,
2287 uint32_t config, uint32_t mask)
2289 mask &= config ^ port->opp.config;
2290 if (mask & OFPPC_PORT_DOWN) {
2291 if (config & OFPPC_PORT_DOWN) {
2292 netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
2294 netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
2297 #define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | OFPPC_NO_FWD)
2298 if (mask & REVALIDATE_BITS) {
2299 COVERAGE_INC(ofproto_costly_flags);
2300 port->opp.config ^= mask & REVALIDATE_BITS;
2301 p->need_revalidate = true;
2303 #undef REVALIDATE_BITS
2304 if (mask & OFPPC_NO_FLOOD) {
2305 port->opp.config ^= OFPPC_NO_FLOOD;
2306 refresh_port_groups(p);
2308 if (mask & OFPPC_NO_PACKET_IN) {
2309 port->opp.config ^= OFPPC_NO_PACKET_IN;
2314 handle_port_mod(struct ofproto *p, struct ofp_header *oh)
2316 const struct ofp_port_mod *opm;
2317 struct ofport *port;
2320 error = check_ofp_message(oh, OFPT_PORT_MOD, sizeof *opm);
2324 opm = (struct ofp_port_mod *) oh;
2326 port = port_array_get(&p->ports,
2327 ofp_port_to_odp_port(ntohs(opm->port_no)));
2329 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT);
2330 } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) {
2331 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR);
2333 update_port_config(p, port, ntohl(opm->config), ntohl(opm->mask));
2334 if (opm->advertise) {
2335 netdev_set_advertisements(port->netdev, ntohl(opm->advertise));
2341 static struct ofpbuf *
2342 make_stats_reply(uint32_t xid, uint16_t type, size_t body_len)
2344 struct ofp_stats_reply *osr;
2347 msg = ofpbuf_new(MIN(sizeof *osr + body_len, UINT16_MAX));
2348 osr = put_openflow_xid(sizeof *osr, OFPT_STATS_REPLY, xid, msg);
2350 osr->flags = htons(0);
2354 static struct ofpbuf *
2355 start_stats_reply(const struct ofp_stats_request *request, size_t body_len)
2357 return make_stats_reply(request->header.xid, request->type, body_len);
2361 append_stats_reply(size_t nbytes, struct ofconn *ofconn, struct ofpbuf **msgp)
2363 struct ofpbuf *msg = *msgp;
2364 assert(nbytes <= UINT16_MAX - sizeof(struct ofp_stats_reply));
2365 if (nbytes + msg->size > UINT16_MAX) {
2366 struct ofp_stats_reply *reply = msg->data;
2367 reply->flags = htons(OFPSF_REPLY_MORE);
2368 *msgp = make_stats_reply(reply->header.xid, reply->type, nbytes);
2369 queue_tx(msg, ofconn, ofconn->reply_counter);
2371 return ofpbuf_put_uninit(*msgp, nbytes);
2375 handle_desc_stats_request(struct ofproto *p, struct ofconn *ofconn,
2376 struct ofp_stats_request *request)
2378 struct ofp_desc_stats *ods;
2381 msg = start_stats_reply(request, sizeof *ods);
2382 ods = append_stats_reply(sizeof *ods, ofconn, &msg);
2383 strncpy(ods->mfr_desc, p->manufacturer, sizeof ods->mfr_desc);
2384 strncpy(ods->hw_desc, p->hardware, sizeof ods->hw_desc);
2385 strncpy(ods->sw_desc, p->software, sizeof ods->sw_desc);
2386 strncpy(ods->serial_num, p->serial, sizeof ods->serial_num);
2387 queue_tx(msg, ofconn, ofconn->reply_counter);
2393 count_subrules(struct cls_rule *cls_rule, void *n_subrules_)
2395 struct rule *rule = rule_from_cls_rule(cls_rule);
2396 int *n_subrules = n_subrules_;
2404 handle_table_stats_request(struct ofproto *p, struct ofconn *ofconn,
2405 struct ofp_stats_request *request)
2407 struct ofp_table_stats *ots;
2409 struct odp_stats dpstats;
2410 int n_exact, n_subrules, n_wild;
2412 msg = start_stats_reply(request, sizeof *ots * 2);
2414 /* Count rules of various kinds. */
2416 classifier_for_each(&p->cls, CLS_INC_EXACT, count_subrules, &n_subrules);
2417 n_exact = classifier_count_exact(&p->cls) - n_subrules;
2418 n_wild = classifier_count(&p->cls) - classifier_count_exact(&p->cls);
2421 dpif_get_dp_stats(p->dpif, &dpstats);
2422 ots = append_stats_reply(sizeof *ots, ofconn, &msg);
2423 memset(ots, 0, sizeof *ots);
2424 ots->table_id = TABLEID_HASH;
2425 strcpy(ots->name, "hash");
2426 ots->wildcards = htonl(0);
2427 ots->max_entries = htonl(dpstats.max_capacity);
2428 ots->active_count = htonl(n_exact);
2429 ots->lookup_count = htonll(dpstats.n_frags + dpstats.n_hit +
2431 ots->matched_count = htonll(dpstats.n_hit); /* XXX */
2433 /* Classifier table. */
2434 ots = append_stats_reply(sizeof *ots, ofconn, &msg);
2435 memset(ots, 0, sizeof *ots);
2436 ots->table_id = TABLEID_CLASSIFIER;
2437 strcpy(ots->name, "classifier");
2438 ots->wildcards = htonl(OFPFW_ALL);
2439 ots->max_entries = htonl(65536);
2440 ots->active_count = htonl(n_wild);
2441 ots->lookup_count = htonll(0); /* XXX */
2442 ots->matched_count = htonll(0); /* XXX */
2444 queue_tx(msg, ofconn, ofconn->reply_counter);
2449 handle_port_stats_request(struct ofproto *p, struct ofconn *ofconn,
2450 struct ofp_stats_request *request)
2452 struct ofp_port_stats *ops;
2454 struct ofport *port;
2455 unsigned int port_no;
2457 msg = start_stats_reply(request, sizeof *ops * 16);
2458 PORT_ARRAY_FOR_EACH (port, &p->ports, port_no) {
2459 struct netdev_stats stats;
2461 /* Intentionally ignore return value, since errors will set 'stats' to
2462 * all-1s, which is correct for OpenFlow, and netdev_get_stats() will
2464 netdev_get_stats(port->netdev, &stats);
2466 ops = append_stats_reply(sizeof *ops, ofconn, &msg);
2467 ops->port_no = htons(odp_port_to_ofp_port(port_no));
2468 memset(ops->pad, 0, sizeof ops->pad);
2469 ops->rx_packets = htonll(stats.rx_packets);
2470 ops->tx_packets = htonll(stats.tx_packets);
2471 ops->rx_bytes = htonll(stats.rx_bytes);
2472 ops->tx_bytes = htonll(stats.tx_bytes);
2473 ops->rx_dropped = htonll(stats.rx_dropped);
2474 ops->tx_dropped = htonll(stats.tx_dropped);
2475 ops->rx_errors = htonll(stats.rx_errors);
2476 ops->tx_errors = htonll(stats.tx_errors);
2477 ops->rx_frame_err = htonll(stats.rx_frame_errors);
2478 ops->rx_over_err = htonll(stats.rx_over_errors);
2479 ops->rx_crc_err = htonll(stats.rx_crc_errors);
2480 ops->collisions = htonll(stats.collisions);
2483 queue_tx(msg, ofconn, ofconn->reply_counter);
2487 struct flow_stats_cbdata {
2488 struct ofproto *ofproto;
2489 struct ofconn *ofconn;
2495 query_stats(struct ofproto *p, struct rule *rule,
2496 uint64_t *packet_countp, uint64_t *byte_countp)
2498 uint64_t packet_count, byte_count;
2499 struct rule *subrule;
2500 struct odp_flow *odp_flows;
2503 packet_count = rule->packet_count;
2504 byte_count = rule->byte_count;
2506 n_odp_flows = rule->cr.wc.wildcards ? list_size(&rule->list) : 1;
2507 odp_flows = xzalloc(n_odp_flows * sizeof *odp_flows);
2508 if (rule->cr.wc.wildcards) {
2510 LIST_FOR_EACH (subrule, struct rule, list, &rule->list) {
2511 odp_flows[i++].key = subrule->cr.flow;
2512 packet_count += subrule->packet_count;
2513 byte_count += subrule->byte_count;
2516 odp_flows[0].key = rule->cr.flow;
2519 packet_count = rule->packet_count;
2520 byte_count = rule->byte_count;
2521 if (!dpif_flow_get_multiple(p->dpif, odp_flows, n_odp_flows)) {
2523 for (i = 0; i < n_odp_flows; i++) {
2524 struct odp_flow *odp_flow = &odp_flows[i];
2525 packet_count += odp_flow->stats.n_packets;
2526 byte_count += odp_flow->stats.n_bytes;
2531 *packet_countp = packet_count;
2532 *byte_countp = byte_count;
2536 flow_stats_cb(struct cls_rule *rule_, void *cbdata_)
2538 struct rule *rule = rule_from_cls_rule(rule_);
2539 struct flow_stats_cbdata *cbdata = cbdata_;
2540 struct ofp_flow_stats *ofs;
2541 uint64_t packet_count, byte_count;
2542 size_t act_len, len;
2544 if (rule_is_hidden(rule) || !rule_has_out_port(rule, cbdata->out_port)) {
2548 act_len = sizeof *rule->actions * rule->n_actions;
2549 len = offsetof(struct ofp_flow_stats, actions) + act_len;
2551 query_stats(cbdata->ofproto, rule, &packet_count, &byte_count);
2553 ofs = append_stats_reply(len, cbdata->ofconn, &cbdata->msg);
2554 ofs->length = htons(len);
2555 ofs->table_id = rule->cr.wc.wildcards ? TABLEID_CLASSIFIER : TABLEID_HASH;
2557 flow_to_match(&rule->cr.flow, rule->cr.wc.wildcards, &ofs->match);
2558 ofs->duration = htonl((time_msec() - rule->created) / 1000);
2559 ofs->priority = htons(rule->cr.priority);
2560 ofs->idle_timeout = htons(rule->idle_timeout);
2561 ofs->hard_timeout = htons(rule->hard_timeout);
2563 ofs->packet_count = htonll(packet_count);
2564 ofs->byte_count = htonll(byte_count);
2565 memcpy(ofs->actions, rule->actions, act_len);
2569 table_id_to_include(uint8_t table_id)
2571 return (table_id == TABLEID_HASH ? CLS_INC_EXACT
2572 : table_id == TABLEID_CLASSIFIER ? CLS_INC_WILD
2573 : table_id == 0xff ? CLS_INC_ALL
2578 handle_flow_stats_request(struct ofproto *p, struct ofconn *ofconn,
2579 const struct ofp_stats_request *osr,
2582 struct ofp_flow_stats_request *fsr;
2583 struct flow_stats_cbdata cbdata;
2584 struct cls_rule target;
2586 if (arg_size != sizeof *fsr) {
2587 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2589 fsr = (struct ofp_flow_stats_request *) osr->body;
2591 COVERAGE_INC(ofproto_flows_req);
2593 cbdata.ofconn = ofconn;
2594 cbdata.out_port = fsr->out_port;
2595 cbdata.msg = start_stats_reply(osr, 1024);
2596 cls_rule_from_match(&target, &fsr->match, 0);
2597 classifier_for_each_match(&p->cls, &target,
2598 table_id_to_include(fsr->table_id),
2599 flow_stats_cb, &cbdata);
2600 queue_tx(cbdata.msg, ofconn, ofconn->reply_counter);
2604 struct flow_stats_ds_cbdata {
2605 struct ofproto *ofproto;
2610 flow_stats_ds_cb(struct cls_rule *rule_, void *cbdata_)
2612 struct rule *rule = rule_from_cls_rule(rule_);
2613 struct flow_stats_ds_cbdata *cbdata = cbdata_;
2614 struct ds *results = cbdata->results;
2615 struct ofp_match match;
2616 uint64_t packet_count, byte_count;
2617 size_t act_len = sizeof *rule->actions * rule->n_actions;
2619 /* Don't report on subrules. */
2620 if (rule->super != NULL) {
2624 query_stats(cbdata->ofproto, rule, &packet_count, &byte_count);
2625 flow_to_ovs_match(&rule->cr.flow, rule->cr.wc.wildcards, &match);
2627 ds_put_format(results, "duration=%llds, ",
2628 (time_msec() - rule->created) / 1000);
2629 ds_put_format(results, "priority=%u, ", rule->cr.priority);
2630 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
2631 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
2632 ofp_print_match(results, &match, true);
2633 ofp_print_actions(results, &rule->actions->header, act_len);
2634 ds_put_cstr(results, "\n");
2637 /* Adds a pretty-printed description of all flows to 'results', including
2638 * those marked hidden by secchan (e.g., by in-band control). */
2640 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
2642 struct ofp_match match;
2643 struct cls_rule target;
2644 struct flow_stats_ds_cbdata cbdata;
2646 memset(&match, 0, sizeof match);
2647 match.wildcards = htonl(OFPFW_ALL);
2650 cbdata.results = results;
2652 cls_rule_from_match(&target, &match, 0);
2653 classifier_for_each_match(&p->cls, &target, CLS_INC_ALL,
2654 flow_stats_ds_cb, &cbdata);
2657 struct aggregate_stats_cbdata {
2658 struct ofproto *ofproto;
2660 uint64_t packet_count;
2661 uint64_t byte_count;
2666 aggregate_stats_cb(struct cls_rule *rule_, void *cbdata_)
2668 struct rule *rule = rule_from_cls_rule(rule_);
2669 struct aggregate_stats_cbdata *cbdata = cbdata_;
2670 uint64_t packet_count, byte_count;
2672 if (rule_is_hidden(rule) || !rule_has_out_port(rule, cbdata->out_port)) {
2676 query_stats(cbdata->ofproto, rule, &packet_count, &byte_count);
2678 cbdata->packet_count += packet_count;
2679 cbdata->byte_count += byte_count;
2684 handle_aggregate_stats_request(struct ofproto *p, struct ofconn *ofconn,
2685 const struct ofp_stats_request *osr,
2688 struct ofp_aggregate_stats_request *asr;
2689 struct ofp_aggregate_stats_reply *reply;
2690 struct aggregate_stats_cbdata cbdata;
2691 struct cls_rule target;
2694 if (arg_size != sizeof *asr) {
2695 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2697 asr = (struct ofp_aggregate_stats_request *) osr->body;
2699 COVERAGE_INC(ofproto_agg_request);
2701 cbdata.out_port = asr->out_port;
2702 cbdata.packet_count = 0;
2703 cbdata.byte_count = 0;
2705 cls_rule_from_match(&target, &asr->match, 0);
2706 classifier_for_each_match(&p->cls, &target,
2707 table_id_to_include(asr->table_id),
2708 aggregate_stats_cb, &cbdata);
2710 msg = start_stats_reply(osr, sizeof *reply);
2711 reply = append_stats_reply(sizeof *reply, ofconn, &msg);
2712 reply->flow_count = htonl(cbdata.n_flows);
2713 reply->packet_count = htonll(cbdata.packet_count);
2714 reply->byte_count = htonll(cbdata.byte_count);
2715 queue_tx(msg, ofconn, ofconn->reply_counter);
2720 handle_stats_request(struct ofproto *p, struct ofconn *ofconn,
2721 struct ofp_header *oh)
2723 struct ofp_stats_request *osr;
2727 error = check_ofp_message_array(oh, OFPT_STATS_REQUEST, sizeof *osr,
2732 osr = (struct ofp_stats_request *) oh;
2734 switch (ntohs(osr->type)) {
2736 return handle_desc_stats_request(p, ofconn, osr);
2739 return handle_flow_stats_request(p, ofconn, osr, arg_size);
2741 case OFPST_AGGREGATE:
2742 return handle_aggregate_stats_request(p, ofconn, osr, arg_size);
2745 return handle_table_stats_request(p, ofconn, osr);
2748 return handle_port_stats_request(p, ofconn, osr);
2751 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
2754 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT);
2758 static long long int
2759 msec_from_nsec(uint64_t sec, uint32_t nsec)
2761 return !sec ? 0 : sec * 1000 + nsec / 1000000;
2765 update_time(struct ofproto *ofproto, struct rule *rule,
2766 const struct odp_flow_stats *stats)
2768 long long int used = msec_from_nsec(stats->used_sec, stats->used_nsec);
2769 if (used > rule->used) {
2771 if (rule->super && used > rule->super->used) {
2772 rule->super->used = used;
2774 netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, used);
2779 update_stats(struct ofproto *ofproto, struct rule *rule,
2780 const struct odp_flow_stats *stats)
2782 if (stats->n_packets) {
2783 update_time(ofproto, rule, stats);
2784 rule->packet_count += stats->n_packets;
2785 rule->byte_count += stats->n_bytes;
2786 netflow_flow_update_flags(&rule->nf_flow, stats->ip_tos,
2792 add_flow(struct ofproto *p, struct ofconn *ofconn,
2793 struct ofp_flow_mod *ofm, size_t n_actions)
2795 struct ofpbuf *packet;
2800 if (ofm->flags & htons(OFPFF_CHECK_OVERLAP)) {
2804 flow_from_match(&flow, &wildcards, &ofm->match);
2805 if (classifier_rule_overlaps(&p->cls, &flow, wildcards,
2806 ntohs(ofm->priority))) {
2807 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP);
2811 rule = rule_create(p, NULL, (const union ofp_action *) ofm->actions,
2812 n_actions, ntohs(ofm->idle_timeout),
2813 ntohs(ofm->hard_timeout));
2814 cls_rule_from_match(&rule->cr, &ofm->match, ntohs(ofm->priority));
2817 if (ofm->buffer_id != htonl(UINT32_MAX)) {
2818 error = pktbuf_retrieve(ofconn->pktbuf, ntohl(ofm->buffer_id),
2822 in_port = UINT16_MAX;
2825 rule_insert(p, rule, packet, in_port);
2826 ofpbuf_delete(packet);
2831 modify_flow(struct ofproto *p, const struct ofp_flow_mod *ofm,
2832 size_t n_actions, uint16_t command, struct rule *rule)
2834 if (rule_is_hidden(rule)) {
2838 if (command == OFPFC_DELETE) {
2839 rule_remove(p, rule);
2841 size_t actions_len = n_actions * sizeof *rule->actions;
2843 if (n_actions == rule->n_actions
2844 && !memcmp(ofm->actions, rule->actions, actions_len))
2849 free(rule->actions);
2850 rule->actions = xmemdup(ofm->actions, actions_len);
2851 rule->n_actions = n_actions;
2853 if (rule->cr.wc.wildcards) {
2854 COVERAGE_INC(ofproto_mod_wc_flow);
2855 p->need_revalidate = true;
2857 rule_update_actions(p, rule);
2865 modify_flows_strict(struct ofproto *p, const struct ofp_flow_mod *ofm,
2866 size_t n_actions, uint16_t command)
2872 flow_from_match(&flow, &wildcards, &ofm->match);
2873 rule = rule_from_cls_rule(classifier_find_rule_exactly(
2874 &p->cls, &flow, wildcards,
2875 ntohs(ofm->priority)));
2878 if (command == OFPFC_DELETE
2879 && ofm->out_port != htons(OFPP_NONE)
2880 && !rule_has_out_port(rule, ofm->out_port)) {
2884 modify_flow(p, ofm, n_actions, command, rule);
2889 struct modify_flows_cbdata {
2890 struct ofproto *ofproto;
2891 const struct ofp_flow_mod *ofm;
2898 modify_flows_cb(struct cls_rule *rule_, void *cbdata_)
2900 struct rule *rule = rule_from_cls_rule(rule_);
2901 struct modify_flows_cbdata *cbdata = cbdata_;
2903 if (cbdata->out_port != htons(OFPP_NONE)
2904 && !rule_has_out_port(rule, cbdata->out_port)) {
2908 modify_flow(cbdata->ofproto, cbdata->ofm, cbdata->n_actions,
2909 cbdata->command, rule);
2913 modify_flows_loose(struct ofproto *p, const struct ofp_flow_mod *ofm,
2914 size_t n_actions, uint16_t command)
2916 struct modify_flows_cbdata cbdata;
2917 struct cls_rule target;
2921 cbdata.out_port = (command == OFPFC_DELETE ? ofm->out_port
2922 : htons(OFPP_NONE));
2923 cbdata.n_actions = n_actions;
2924 cbdata.command = command;
2926 cls_rule_from_match(&target, &ofm->match, 0);
2928 classifier_for_each_match(&p->cls, &target, CLS_INC_ALL,
2929 modify_flows_cb, &cbdata);
2934 handle_flow_mod(struct ofproto *p, struct ofconn *ofconn,
2935 struct ofp_flow_mod *ofm)
2940 error = check_ofp_message_array(&ofm->header, OFPT_FLOW_MOD, sizeof *ofm,
2941 sizeof *ofm->actions, &n_actions);
2946 /* We do not support the emergency flow cache. It will hopefully
2947 * get dropped from OpenFlow in the near future. */
2948 if (ofm->flags & htons(OFPFF_EMERG)) {
2949 /* There isn't a good fit for an error code, so just state that the
2950 * flow table is full. */
2951 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL);
2954 normalize_match(&ofm->match);
2955 if (!ofm->match.wildcards) {
2956 ofm->priority = htons(UINT16_MAX);
2959 error = validate_actions((const union ofp_action *) ofm->actions,
2960 n_actions, p->max_ports);
2965 switch (ntohs(ofm->command)) {
2967 return add_flow(p, ofconn, ofm, n_actions);
2970 return modify_flows_loose(p, ofm, n_actions, OFPFC_MODIFY);
2972 case OFPFC_MODIFY_STRICT:
2973 return modify_flows_strict(p, ofm, n_actions, OFPFC_MODIFY);
2976 return modify_flows_loose(p, ofm, n_actions, OFPFC_DELETE);
2978 case OFPFC_DELETE_STRICT:
2979 return modify_flows_strict(p, ofm, n_actions, OFPFC_DELETE);
2982 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND);
2987 handle_vendor(struct ofproto *p, struct ofconn *ofconn, void *msg)
2989 struct ofp_vendor_header *ovh = msg;
2990 struct nicira_header *nh;
2992 if (ntohs(ovh->header.length) < sizeof(struct ofp_vendor_header)) {
2993 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2995 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
2996 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
2998 if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) {
2999 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
3003 switch (ntohl(nh->subtype)) {
3004 case NXT_STATUS_REQUEST:
3005 return switch_status_handle_request(p->switch_status, ofconn->rconn,
3009 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE);
3013 handle_openflow(struct ofconn *ofconn, struct ofproto *p,
3014 struct ofpbuf *ofp_msg)
3016 struct ofp_header *oh = ofp_msg->data;
3019 COVERAGE_INC(ofproto_recv_openflow);
3021 case OFPT_ECHO_REQUEST:
3022 error = handle_echo_request(ofconn, oh);
3025 case OFPT_ECHO_REPLY:
3029 case OFPT_FEATURES_REQUEST:
3030 error = handle_features_request(p, ofconn, oh);
3033 case OFPT_GET_CONFIG_REQUEST:
3034 error = handle_get_config_request(p, ofconn, oh);
3037 case OFPT_SET_CONFIG:
3038 error = handle_set_config(p, ofconn, ofp_msg->data);
3041 case OFPT_PACKET_OUT:
3042 error = handle_packet_out(p, ofconn, ofp_msg->data);
3046 error = handle_port_mod(p, oh);
3050 error = handle_flow_mod(p, ofconn, ofp_msg->data);
3053 case OFPT_STATS_REQUEST:
3054 error = handle_stats_request(p, ofconn, oh);
3058 error = handle_vendor(p, ofconn, ofp_msg->data);
3062 if (VLOG_IS_WARN_ENABLED()) {
3063 char *s = ofp_to_string(oh, ntohs(oh->length), 2);
3064 VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s);
3067 error = ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
3072 send_error_oh(ofconn, ofp_msg->data, error);
3077 handle_odp_miss_msg(struct ofproto *p, struct ofpbuf *packet)
3079 struct odp_msg *msg = packet->data;
3080 uint16_t in_port = odp_port_to_ofp_port(msg->port);
3082 struct ofpbuf payload;
3085 payload.data = msg + 1;
3086 payload.size = msg->length - sizeof *msg;
3087 flow_extract(&payload, msg->port, &flow);
3089 /* Check with in-band control to see if this packet should be sent
3090 * to the local port regardless of the flow table. */
3091 if (in_band_msg_in_hook(p->in_band, &flow, &payload)) {
3092 union odp_action action;
3094 memset(&action, 0, sizeof(action));
3095 action.output.type = ODPAT_OUTPUT;
3096 action.output.port = ODPP_LOCAL;
3097 dpif_execute(p->dpif, flow.in_port, &action, 1, &payload);
3100 rule = lookup_valid_rule(p, &flow);
3102 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
3103 struct ofport *port = port_array_get(&p->ports, msg->port);
3105 if (port->opp.config & OFPPC_NO_PACKET_IN) {
3106 COVERAGE_INC(ofproto_no_packet_in);
3107 /* XXX install 'drop' flow entry */
3108 ofpbuf_delete(packet);
3112 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16, msg->port);
3115 COVERAGE_INC(ofproto_packet_in);
3116 pinsched_send(p->miss_sched, in_port, packet, send_packet_in_miss, p);
3120 if (rule->cr.wc.wildcards) {
3121 rule = rule_create_subrule(p, rule, &flow);
3122 rule_make_actions(p, rule, packet);
3124 if (!rule->may_install) {
3125 /* The rule is not installable, that is, we need to process every
3126 * packet, so process the current packet and set its actions into
3128 rule_make_actions(p, rule, packet);
3130 /* XXX revalidate rule if it needs it */
3134 rule_execute(p, rule, &payload, &flow);
3135 rule_reinstall(p, rule);
3137 if (rule->super && rule->super->cr.priority == FAIL_OPEN_PRIORITY
3138 && rconn_is_connected(p->controller->rconn)) {
3140 * Extra-special case for fail-open mode.
3142 * We are in fail-open mode and the packet matched the fail-open rule,
3143 * but we are connected to a controller too. We should send the packet
3144 * up to the controller in the hope that it will try to set up a flow
3145 * and thereby allow us to exit fail-open.
3147 * See the top-level comment in fail-open.c for more information.
3149 pinsched_send(p->miss_sched, in_port, packet, send_packet_in_miss, p);
3151 ofpbuf_delete(packet);
3156 handle_odp_msg(struct ofproto *p, struct ofpbuf *packet)
3158 struct odp_msg *msg = packet->data;
3160 switch (msg->type) {
3161 case _ODPL_ACTION_NR:
3162 COVERAGE_INC(ofproto_ctlr_action);
3163 pinsched_send(p->action_sched, odp_port_to_ofp_port(msg->port), packet,
3164 send_packet_in_action, p);
3167 case _ODPL_SFLOW_NR:
3169 ofproto_sflow_received(p->sflow, msg);
3171 ofpbuf_delete(packet);
3175 handle_odp_miss_msg(p, packet);
3179 VLOG_WARN_RL(&rl, "received ODP message of unexpected type %"PRIu32,
3186 revalidate_cb(struct cls_rule *sub_, void *cbdata_)
3188 struct rule *sub = rule_from_cls_rule(sub_);
3189 struct revalidate_cbdata *cbdata = cbdata_;
3191 if (cbdata->revalidate_all
3192 || (cbdata->revalidate_subrules && sub->super)
3193 || (tag_set_intersects(&cbdata->revalidate_set, sub->tags))) {
3194 revalidate_rule(cbdata->ofproto, sub);
3199 revalidate_rule(struct ofproto *p, struct rule *rule)
3201 const flow_t *flow = &rule->cr.flow;
3203 COVERAGE_INC(ofproto_revalidate_rule);
3206 super = rule_from_cls_rule(classifier_lookup_wild(&p->cls, flow));
3208 rule_remove(p, rule);
3210 } else if (super != rule->super) {
3211 COVERAGE_INC(ofproto_revalidate_moved);
3212 list_remove(&rule->list);
3213 list_push_back(&super->list, &rule->list);
3214 rule->super = super;
3215 rule->hard_timeout = super->hard_timeout;
3216 rule->idle_timeout = super->idle_timeout;
3217 rule->created = super->created;
3222 rule_update_actions(p, rule);
3226 static struct ofpbuf *
3227 compose_flow_exp(const struct rule *rule, long long int now, uint8_t reason)
3229 struct ofp_flow_expired *ofe;
3232 ofe = make_openflow(sizeof *ofe, OFPT_FLOW_EXPIRED, &buf);
3233 flow_to_match(&rule->cr.flow, rule->cr.wc.wildcards, &ofe->match);
3234 ofe->priority = htons(rule->cr.priority);
3235 ofe->reason = reason;
3236 ofe->duration = htonl((now - rule->created) / 1000);
3237 ofe->packet_count = htonll(rule->packet_count);
3238 ofe->byte_count = htonll(rule->byte_count);
3244 send_flow_exp(struct ofproto *p, struct rule *rule,
3245 long long int now, uint8_t reason)
3247 struct ofconn *ofconn;
3248 struct ofconn *prev;
3249 struct ofpbuf *buf = NULL;
3251 /* We limit the maximum number of queued flow expirations it by accounting
3252 * them under the counter for replies. That works because preventing
3253 * OpenFlow requests from being processed also prevents new flows from
3254 * being added (and expiring). (It also prevents processing OpenFlow
3255 * requests that would not add new flows, so it is imperfect.) */
3258 LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
3259 if (ofconn->send_flow_exp && rconn_is_connected(ofconn->rconn)) {
3261 queue_tx(ofpbuf_clone(buf), prev, prev->reply_counter);
3263 buf = compose_flow_exp(rule, now, reason);
3269 queue_tx(buf, prev, prev->reply_counter);
3274 uninstall_idle_flow(struct ofproto *ofproto, struct rule *rule)
3276 assert(rule->installed);
3277 assert(!rule->cr.wc.wildcards);
3280 rule_remove(ofproto, rule);
3282 rule_uninstall(ofproto, rule);
3287 expire_rule(struct cls_rule *cls_rule, void *p_)
3289 struct ofproto *p = p_;
3290 struct rule *rule = rule_from_cls_rule(cls_rule);
3291 long long int hard_expire, idle_expire, expire, now;
3293 hard_expire = (rule->hard_timeout
3294 ? rule->created + rule->hard_timeout * 1000
3296 idle_expire = (rule->idle_timeout
3297 && (rule->super || list_is_empty(&rule->list))
3298 ? rule->used + rule->idle_timeout * 1000
3300 expire = MIN(hard_expire, idle_expire);
3304 if (rule->installed && now >= rule->used + 5000) {
3305 uninstall_idle_flow(p, rule);
3306 } else if (!rule->cr.wc.wildcards) {
3307 active_timeout(p, rule);
3313 COVERAGE_INC(ofproto_expired);
3315 /* Update stats. This code will be a no-op if the rule expired
3316 * due to an idle timeout. */
3317 if (rule->cr.wc.wildcards) {
3318 struct rule *subrule, *next;
3319 LIST_FOR_EACH_SAFE (subrule, next, struct rule, list, &rule->list) {
3320 rule_remove(p, subrule);
3323 rule_uninstall(p, rule);
3326 if (!rule_is_hidden(rule)) {
3327 send_flow_exp(p, rule, now,
3329 ? OFPER_HARD_TIMEOUT : OFPER_IDLE_TIMEOUT));
3331 rule_remove(p, rule);
3335 active_timeout(struct ofproto *ofproto, struct rule *rule)
3337 if (ofproto->netflow && !is_controller_rule(rule) &&
3338 netflow_active_timeout_expired(ofproto->netflow, &rule->nf_flow)) {
3339 struct ofexpired expired;
3340 struct odp_flow odp_flow;
3342 /* Get updated flow stats. */
3343 memset(&odp_flow, 0, sizeof odp_flow);
3344 if (rule->installed) {
3345 odp_flow.key = rule->cr.flow;
3346 odp_flow.flags = ODPFF_ZERO_TCP_FLAGS;
3347 dpif_flow_get(ofproto->dpif, &odp_flow);
3349 if (odp_flow.stats.n_packets) {
3350 update_time(ofproto, rule, &odp_flow.stats);
3351 netflow_flow_update_flags(&rule->nf_flow, odp_flow.stats.ip_tos,
3352 odp_flow.stats.tcp_flags);
3356 expired.flow = rule->cr.flow;
3357 expired.packet_count = rule->packet_count +
3358 odp_flow.stats.n_packets;
3359 expired.byte_count = rule->byte_count + odp_flow.stats.n_bytes;
3360 expired.used = rule->used;
3362 netflow_expire(ofproto->netflow, &rule->nf_flow, &expired);
3364 /* Schedule us to send the accumulated records once we have
3365 * collected all of them. */
3366 poll_immediate_wake();
3371 update_used(struct ofproto *p)
3373 struct odp_flow *flows;
3378 error = dpif_flow_list_all(p->dpif, &flows, &n_flows);
3383 for (i = 0; i < n_flows; i++) {
3384 struct odp_flow *f = &flows[i];
3387 rule = rule_from_cls_rule(
3388 classifier_find_rule_exactly(&p->cls, &f->key, 0, UINT16_MAX));
3389 if (!rule || !rule->installed) {
3390 COVERAGE_INC(ofproto_unexpected_rule);
3391 dpif_flow_del(p->dpif, f);
3395 update_time(p, rule, &f->stats);
3396 rule_account(p, rule, f->stats.n_bytes);
3402 do_send_packet_in(struct ofconn *ofconn, uint32_t buffer_id,
3403 const struct ofpbuf *packet, int send_len)
3405 struct odp_msg *msg = packet->data;
3406 struct ofpbuf payload;
3410 /* Extract packet payload from 'msg'. */
3411 payload.data = msg + 1;
3412 payload.size = msg->length - sizeof *msg;
3414 /* Construct ofp_packet_in message. */
3415 reason = msg->type == _ODPL_ACTION_NR ? OFPR_ACTION : OFPR_NO_MATCH;
3416 opi = make_packet_in(buffer_id, odp_port_to_ofp_port(msg->port), reason,
3417 &payload, send_len);
3420 rconn_send_with_limit(ofconn->rconn, opi, ofconn->packet_in_counter, 100);
3424 send_packet_in_action(struct ofpbuf *packet, void *p_)
3426 struct ofproto *p = p_;
3427 struct ofconn *ofconn;
3428 struct odp_msg *msg;
3431 LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
3432 if (ofconn == p->controller || ofconn->miss_send_len) {
3433 do_send_packet_in(ofconn, UINT32_MAX, packet, msg->arg);
3436 ofpbuf_delete(packet);
3440 send_packet_in_miss(struct ofpbuf *packet, void *p_)
3442 struct ofproto *p = p_;
3443 bool in_fail_open = p->fail_open && fail_open_is_active(p->fail_open);
3444 struct ofconn *ofconn;
3445 struct ofpbuf payload;
3446 struct odp_msg *msg;
3449 payload.data = msg + 1;
3450 payload.size = msg->length - sizeof *msg;
3451 LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
3452 if (ofconn->miss_send_len) {
3453 struct pktbuf *pb = ofconn->pktbuf;
3454 uint32_t buffer_id = (in_fail_open
3456 : pktbuf_save(pb, &payload, msg->port));
3457 int send_len = (buffer_id != UINT32_MAX ? ofconn->miss_send_len
3459 do_send_packet_in(ofconn, buffer_id, packet, send_len);
3462 ofpbuf_delete(packet);
3466 pick_datapath_id(const struct ofproto *ofproto)
3468 const struct ofport *port;
3470 port = port_array_get(&ofproto->ports, ODPP_LOCAL);
3472 uint8_t ea[ETH_ADDR_LEN];
3475 error = netdev_get_etheraddr(port->netdev, ea);
3477 return eth_addr_to_uint64(ea);
3479 VLOG_WARN("could not get MAC address for %s (%s)",
3480 netdev_get_name(port->netdev), strerror(error));
3482 return ofproto->fallback_dpid;
3486 pick_fallback_dpid(void)
3488 uint8_t ea[ETH_ADDR_LEN];
3489 eth_addr_nicira_random(ea);
3490 return eth_addr_to_uint64(ea);
3494 default_normal_ofhook_cb(const flow_t *flow, const struct ofpbuf *packet,
3495 struct odp_actions *actions, tag_type *tags,
3496 uint16_t *nf_output_iface, void *ofproto_)
3498 struct ofproto *ofproto = ofproto_;
3501 /* Drop frames for reserved multicast addresses. */
3502 if (eth_addr_is_reserved(flow->dl_dst)) {
3506 /* Learn source MAC (but don't try to learn from revalidation). */
3507 if (packet != NULL) {
3508 tag_type rev_tag = mac_learning_learn(ofproto->ml, flow->dl_src,
3511 /* The log messages here could actually be useful in debugging,
3512 * so keep the rate limit relatively high. */
3513 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
3514 VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on port %"PRIu16,
3515 ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
3516 ofproto_revalidate(ofproto, rev_tag);
3520 /* Determine output port. */
3521 out_port = mac_learning_lookup_tag(ofproto->ml, flow->dl_dst, 0, tags);
3523 add_output_group_action(actions, DP_GROUP_FLOOD, nf_output_iface);
3524 } else if (out_port != flow->in_port) {
3525 odp_actions_add(actions, ODPAT_OUTPUT)->output.port = out_port;
3526 *nf_output_iface = out_port;
3534 static const struct ofhooks default_ofhooks = {
3536 default_normal_ofhook_cb,