]> git.proxmox.com Git - ovs.git/blame - ofproto/ofproto.c
classifier: Remove OF1.0 special case from classifier_find_rule_exactly().
[ovs.git] / ofproto / ofproto.c
CommitLineData
064af421 1/*
db5ce514 2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
43253595 3 * Copyright (c) 2010 Jean Tourrilhes - HP-Labs.
064af421 4 *
a14bc59f
BP
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
064af421 8 *
a14bc59f
BP
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
064af421
BP
16 */
17
18#include <config.h>
19#include "ofproto.h"
20#include <errno.h>
21#include <inttypes.h>
064af421
BP
22#include <stdbool.h>
23#include <stdlib.h>
10a24935 24#include "byte-order.h"
064af421 25#include "classifier.h"
19a87e36 26#include "connmgr.h"
064af421 27#include "coverage.h"
4f2cad2c 28#include "dynamic-string.h"
ca0f572c
BP
29#include "hash.h"
30#include "hmap.h"
064af421 31#include "netdev.h"
09246b99 32#include "nx-match.h"
064af421 33#include "ofp-print.h"
fa37b408 34#include "ofp-util.h"
064af421
BP
35#include "ofpbuf.h"
36#include "openflow/nicira-ext.h"
37#include "openflow/openflow.h"
064af421
BP
38#include "packets.h"
39#include "pinsched.h"
40#include "pktbuf.h"
41#include "poll-loop.h"
d08a2e92 42#include "private.h"
064af421 43#include "shash.h"
b3c01ed3 44#include "sset.h"
064af421 45#include "timeval.h"
c4617b3c 46#include "unaligned.h"
4f2cad2c 47#include "unixctl.h"
5136ce49 48#include "vlog.h"
064af421 49
d98e6007 50VLOG_DEFINE_THIS_MODULE(ofproto);
064af421 51
d76f09ea 52COVERAGE_DEFINE(ofproto_agg_request);
d76f09ea 53COVERAGE_DEFINE(ofproto_error);
d76f09ea
BP
54COVERAGE_DEFINE(ofproto_flows_req);
55COVERAGE_DEFINE(ofproto_flush);
d76f09ea 56COVERAGE_DEFINE(ofproto_no_packet_in);
d76f09ea
BP
57COVERAGE_DEFINE(ofproto_packet_out);
58COVERAGE_DEFINE(ofproto_queue_req);
59COVERAGE_DEFINE(ofproto_recv_openflow);
60COVERAGE_DEFINE(ofproto_reinit_ports);
d76f09ea
BP
61COVERAGE_DEFINE(ofproto_uninstallable);
62COVERAGE_DEFINE(ofproto_update_port);
63
abe529af
BP
64static void ofport_destroy__(struct ofport *);
65static void ofport_destroy(struct ofport *);
064af421 66
abe529af
BP
67static int rule_create(struct ofproto *, const struct cls_rule *,
68 const union ofp_action *, size_t n_actions,
69 uint16_t idle_timeout, uint16_t hard_timeout,
70 ovs_be64 flow_cookie, bool send_flow_removed,
71 struct rule **rulep);
064af421 72
abe529af
BP
73static uint64_t pick_datapath_id(const struct ofproto *);
74static uint64_t pick_fallback_dpid(void);
f29152ca 75
abe529af
BP
76static void ofproto_destroy__(struct ofproto *);
77static void ofproto_flush_flows__(struct ofproto *);
f29152ca 78
abe529af
BP
79static void ofproto_rule_destroy__(struct rule *);
80static void ofproto_rule_send_removed(struct rule *, uint8_t reason);
81static void ofproto_rule_remove(struct rule *);
7aa697dd 82
abe529af 83static void handle_openflow(struct ofconn *, struct ofpbuf *);
ebe482fd 84
abe529af
BP
85static void update_port(struct ofproto *, const char *devname);
86static int init_ports(struct ofproto *);
87static void reinit_ports(struct ofproto *);
f29152ca 88
abe529af 89static void ofproto_unixctl_init(void);
f29152ca 90
abe529af
BP
91/* All registered ofproto classes, in probe order. */
92static const struct ofproto_class **ofproto_classes;
93static size_t n_ofproto_classes;
94static size_t allocated_ofproto_classes;
f29152ca 95
f797957a 96/* Map from datapath name to struct ofproto, for use by unixctl commands. */
abe529af 97static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos);
f29152ca 98
abe529af 99static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
064af421 100
abe529af
BP
101static void
102ofproto_initialize(void)
103{
104 static bool inited;
bcf84111 105
abe529af
BP
106 if (!inited) {
107 inited = true;
108 ofproto_class_register(&ofproto_dpif_class);
109 }
110}
064af421 111
abe529af
BP
112/* 'type' should be a normalized datapath type, as returned by
113 * ofproto_normalize_type(). Returns the corresponding ofproto_class
114 * structure, or a null pointer if there is none registered for 'type'. */
115static const struct ofproto_class *
116ofproto_class_find__(const char *type)
117{
118 size_t i;
7aa697dd 119
abe529af
BP
120 ofproto_initialize();
121 for (i = 0; i < n_ofproto_classes; i++) {
122 const struct ofproto_class *class = ofproto_classes[i];
123 struct sset types;
124 bool found;
064af421 125
abe529af
BP
126 sset_init(&types);
127 class->enumerate_types(&types);
128 found = sset_contains(&types, type);
129 sset_destroy(&types);
76ce9432 130
abe529af
BP
131 if (found) {
132 return class;
133 }
134 }
135 VLOG_WARN("unknown datapath type %s", type);
136 return NULL;
137}
4a4cdb3b 138
abe529af
BP
139/* Registers a new ofproto class. After successful registration, new ofprotos
140 * of that type can be created using ofproto_create(). */
141int
142ofproto_class_register(const struct ofproto_class *new_class)
143{
144 size_t i;
064af421 145
abe529af
BP
146 for (i = 0; i < n_ofproto_classes; i++) {
147 if (ofproto_classes[i] == new_class) {
148 return EEXIST;
149 }
150 }
064af421 151
abe529af
BP
152 if (n_ofproto_classes >= allocated_ofproto_classes) {
153 ofproto_classes = x2nrealloc(ofproto_classes,
154 &allocated_ofproto_classes,
155 sizeof *ofproto_classes);
156 }
157 ofproto_classes[n_ofproto_classes++] = new_class;
158 return 0;
159}
064af421 160
abe529af
BP
161/* Unregisters a datapath provider. 'type' must have been previously
162 * registered and not currently be in use by any ofprotos. After
163 * unregistration new datapaths of that type cannot be opened using
164 * ofproto_create(). */
165int
166ofproto_class_unregister(const struct ofproto_class *class)
167{
168 size_t i;
fa066f01 169
abe529af
BP
170 for (i = 0; i < n_ofproto_classes; i++) {
171 if (ofproto_classes[i] == class) {
172 for (i++; i < n_ofproto_classes; i++) {
173 ofproto_classes[i - 1] = ofproto_classes[i];
174 }
175 n_ofproto_classes--;
176 return 0;
177 }
178 }
179 VLOG_WARN("attempted to unregister an ofproto class that is not "
180 "registered");
181 return EAFNOSUPPORT;
182}
7aa697dd 183
f79e673f
BP
184/* Clears 'types' and enumerates all registered ofproto types into it. The
185 * caller must first initialize the sset. */
186void
187ofproto_enumerate_types(struct sset *types)
188{
abe529af
BP
189 size_t i;
190
191 ofproto_initialize();
192 for (i = 0; i < n_ofproto_classes; i++) {
193 ofproto_classes[i]->enumerate_types(types);
194 }
f79e673f
BP
195}
196
197/* Returns the fully spelled out name for the given ofproto 'type'.
198 *
199 * Normalized type string can be compared with strcmp(). Unnormalized type
200 * string might be the same even if they have different spellings. */
201const char *
202ofproto_normalize_type(const char *type)
203{
abe529af 204 return type && type[0] ? type : "system";
f79e673f
BP
205}
206
207/* Clears 'names' and enumerates the names of all known created ofprotos with
208 * the given 'type'. The caller must first initialize the sset. Returns 0 if
209 * successful, otherwise a positive errno value.
210 *
211 * Some kinds of datapaths might not be practically enumerable. This is not
212 * considered an error. */
213int
214ofproto_enumerate_names(const char *type, struct sset *names)
215{
abe529af
BP
216 const struct ofproto_class *class = ofproto_class_find__(type);
217 return class ? class->enumerate_names(type, names) : EAFNOSUPPORT;
218 }
f79e673f 219
064af421 220int
abe529af 221ofproto_create(const char *datapath_name, const char *datapath_type,
064af421
BP
222 struct ofproto **ofprotop)
223{
abe529af
BP
224 const struct ofproto_class *class;
225 struct ofproto *ofproto;
064af421
BP
226 int error;
227
228 *ofprotop = NULL;
229
abe529af 230 ofproto_initialize();
7aa697dd
BP
231 ofproto_unixctl_init();
232
abe529af
BP
233 datapath_type = ofproto_normalize_type(datapath_type);
234 class = ofproto_class_find__(datapath_type);
235 if (!class) {
236 VLOG_WARN("could not create datapath %s of unknown type %s",
237 datapath_name, datapath_type);
238 return EAFNOSUPPORT;
064af421
BP
239 }
240
abe529af
BP
241 ofproto = class->alloc();
242 if (!ofproto) {
243 VLOG_ERR("failed to allocate datapath %s of type %s",
244 datapath_name, datapath_type);
245 return ENOMEM;
246 }
247
248 /* Initialize. */
249 memset(ofproto, 0, sizeof *ofproto);
250 ofproto->ofproto_class = class;
251 ofproto->name = xstrdup(datapath_name);
252 ofproto->type = xstrdup(datapath_type);
253 hmap_insert(&all_ofprotos, &ofproto->hmap_node,
254 hash_string(ofproto->name, 0));
255 ofproto->datapath_id = 0;
256 ofproto->fallback_dpid = pick_fallback_dpid();
257 ofproto->mfr_desc = xstrdup(DEFAULT_MFR_DESC);
258 ofproto->hw_desc = xstrdup(DEFAULT_HW_DESC);
259 ofproto->sw_desc = xstrdup(DEFAULT_SW_DESC);
260 ofproto->serial_desc = xstrdup(DEFAULT_SERIAL_DESC);
261 ofproto->dp_desc = xstrdup(DEFAULT_DP_DESC);
262 ofproto->netdev_monitor = netdev_monitor_create();
263 hmap_init(&ofproto->ports);
264 shash_init(&ofproto->port_by_name);
265 classifier_init(&ofproto->cls);
266 ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name);
267
268 error = ofproto->ofproto_class->construct(ofproto);
19a87e36 269 if (error) {
abe529af
BP
270 VLOG_ERR("failed to open datapath %s: %s",
271 datapath_name, strerror(error));
272 ofproto_destroy__(ofproto);
19a87e36
BP
273 return error;
274 }
275
abe529af
BP
276 ofproto->datapath_id = pick_datapath_id(ofproto);
277 VLOG_INFO("using datapath ID %016"PRIx64, ofproto->datapath_id);
278 init_ports(ofproto);
d4cb239a 279
abe529af 280 *ofprotop = ofproto;
064af421
BP
281 return 0;
282}
283
284void
285ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
286{
287 uint64_t old_dpid = p->datapath_id;
fa60c019 288 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
064af421 289 if (p->datapath_id != old_dpid) {
b123cc3c 290 VLOG_INFO("datapath ID changed to %016"PRIx64, p->datapath_id);
76ce9432
BP
291
292 /* Force all active connections to reconnect, since there is no way to
293 * notify a controller that the datapath ID has changed. */
fa05809b 294 ofproto_reconnect_controllers(p);
064af421
BP
295 }
296}
297
76ce9432
BP
298void
299ofproto_set_controllers(struct ofproto *p,
300 const struct ofproto_controller *controllers,
301 size_t n_controllers)
302{
19a87e36 303 connmgr_set_controllers(p->connmgr, controllers, n_controllers);
064af421
BP
304}
305
31681a5d
JP
306void
307ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
308{
19a87e36 309 connmgr_set_fail_mode(p->connmgr, fail_mode);
31681a5d
JP
310}
311
fa05809b
BP
312/* Drops the connections between 'ofproto' and all of its controllers, forcing
313 * them to reconnect. */
314void
315ofproto_reconnect_controllers(struct ofproto *ofproto)
316{
19a87e36 317 connmgr_reconnect(ofproto->connmgr);
917e50e1
BP
318}
319
320/* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
321 * in-band control should guarantee access, in the same way that in-band
322 * control guarantees access to OpenFlow controllers. */
323void
324ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
325 const struct sockaddr_in *extras, size_t n)
326{
19a87e36 327 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
917e50e1
BP
328}
329
b1da6250
BP
330/* Sets the OpenFlow queue used by flows set up by in-band control on
331 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
332 * flows will use the default queue. */
333void
334ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
335{
19a87e36 336 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
b1da6250
BP
337}
338
064af421
BP
339void
340ofproto_set_desc(struct ofproto *p,
5a719c38
JP
341 const char *mfr_desc, const char *hw_desc,
342 const char *sw_desc, const char *serial_desc,
8abc4ed7 343 const char *dp_desc)
064af421 344{
5a719c38
JP
345 struct ofp_desc_stats *ods;
346
347 if (mfr_desc) {
348 if (strlen(mfr_desc) >= sizeof ods->mfr_desc) {
349 VLOG_WARN("truncating mfr_desc, must be less than %zu characters",
350 sizeof ods->mfr_desc);
351 }
352 free(p->mfr_desc);
353 p->mfr_desc = xstrdup(mfr_desc);
064af421 354 }
5a719c38
JP
355 if (hw_desc) {
356 if (strlen(hw_desc) >= sizeof ods->hw_desc) {
357 VLOG_WARN("truncating hw_desc, must be less than %zu characters",
358 sizeof ods->hw_desc);
359 }
360 free(p->hw_desc);
361 p->hw_desc = xstrdup(hw_desc);
064af421 362 }
5a719c38
JP
363 if (sw_desc) {
364 if (strlen(sw_desc) >= sizeof ods->sw_desc) {
365 VLOG_WARN("truncating sw_desc, must be less than %zu characters",
366 sizeof ods->sw_desc);
367 }
368 free(p->sw_desc);
369 p->sw_desc = xstrdup(sw_desc);
370 }
371 if (serial_desc) {
372 if (strlen(serial_desc) >= sizeof ods->serial_num) {
373 VLOG_WARN("truncating serial_desc, must be less than %zu "
374 "characters",
375 sizeof ods->serial_num);
376 }
377 free(p->serial_desc);
378 p->serial_desc = xstrdup(serial_desc);
064af421 379 }
8abc4ed7 380 if (dp_desc) {
5a719c38
JP
381 if (strlen(dp_desc) >= sizeof ods->dp_desc) {
382 VLOG_WARN("truncating dp_desc, must be less than %zu characters",
383 sizeof ods->dp_desc);
384 }
8abc4ed7
JP
385 free(p->dp_desc);
386 p->dp_desc = xstrdup(dp_desc);
387 }
064af421
BP
388}
389
064af421 390int
81e2083f 391ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
064af421 392{
19a87e36 393 return connmgr_set_snoops(ofproto->connmgr, snoops);
064af421
BP
394}
395
396int
0193b2af
JG
397ofproto_set_netflow(struct ofproto *ofproto,
398 const struct netflow_options *nf_options)
064af421 399{
abe529af
BP
400 if (nf_options && sset_is_empty(&nf_options->collectors)) {
401 nf_options = NULL;
402 }
403
404 if (ofproto->ofproto_class->set_netflow) {
405 return ofproto->ofproto_class->set_netflow(ofproto, nf_options);
064af421 406 } else {
abe529af 407 return nf_options ? EOPNOTSUPP : 0;
064af421
BP
408 }
409}
410
abe529af 411int
72b06300
BP
412ofproto_set_sflow(struct ofproto *ofproto,
413 const struct ofproto_sflow_options *oso)
414{
abe529af
BP
415 if (oso && sset_is_empty(&oso->targets)) {
416 oso = NULL;
417 }
72b06300 418
abe529af
BP
419 if (ofproto->ofproto_class->set_sflow) {
420 return ofproto->ofproto_class->set_sflow(ofproto, oso);
72b06300 421 } else {
abe529af 422 return oso ? EOPNOTSUPP : 0;
72b06300
BP
423 }
424}
e7934396
BP
425\f
426/* Connectivity Fault Management configuration. */
427
892815f5 428/* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
e7934396 429void
892815f5 430ofproto_port_clear_cfm(struct ofproto *ofproto, uint16_t ofp_port)
e7934396 431{
abe529af
BP
432 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
433 if (ofport && ofproto->ofproto_class->set_cfm) {
434 ofproto->ofproto_class->set_cfm(ofport, NULL, NULL, 0);
e7934396
BP
435 }
436}
72b06300 437
892815f5 438/* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
e7934396
BP
439 * basic configuration from the configuration members in 'cfm', and the set of
440 * remote maintenance points from the 'n_remote_mps' elements in 'remote_mps'.
441 * Ignores the statistics members of 'cfm'.
442 *
892815f5 443 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
e7934396 444void
892815f5 445ofproto_port_set_cfm(struct ofproto *ofproto, uint16_t ofp_port,
35c33856
BP
446 const struct cfm *cfm,
447 const uint16_t *remote_mps, size_t n_remote_mps)
e7934396
BP
448{
449 struct ofport *ofport;
abe529af 450 int error;
e7934396 451
abe529af 452 ofport = ofproto_get_port(ofproto, ofp_port);
e7934396 453 if (!ofport) {
892815f5
BP
454 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16,
455 ofproto->name, ofp_port);
e7934396
BP
456 return;
457 }
458
abe529af
BP
459 error = (ofproto->ofproto_class->set_cfm
460 ? ofproto->ofproto_class->set_cfm(ofport, cfm,
461 remote_mps, n_remote_mps)
462 : EOPNOTSUPP);
463 if (error) {
464 VLOG_WARN("%s: CFM configuration on port %"PRIu16" (%s) failed (%s)",
465 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
466 strerror(error));
e7934396
BP
467 }
468}
469
892815f5 470/* Returns the connectivity fault management object associated with 'ofp_port'
e7934396 471 * within 'ofproto', or a null pointer if 'ofproto' does not have a port
892815f5
BP
472 * 'ofp_port' or if that port does not have CFM configured. The caller must
473 * not modify or destroy the returned object. */
e7934396 474const struct cfm *
892815f5 475ofproto_port_get_cfm(struct ofproto *ofproto, uint16_t ofp_port)
e7934396 476{
abe529af
BP
477 struct ofport *ofport;
478 const struct cfm *cfm;
479
480 ofport = ofproto_get_port(ofproto, ofp_port);
481 return (ofport
482 && ofproto->ofproto_class->get_cfm
483 && !ofproto->ofproto_class->get_cfm(ofport, &cfm)) ? cfm : NULL;
e7934396 484}
fa066f01
BP
485
486/* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
487 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
488 * 0 if LACP partner information is not current (generally indicating a
489 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
490int
491ofproto_port_is_lacp_current(struct ofproto *ofproto, uint16_t ofp_port)
492{
abe529af
BP
493 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
494 return (ofport && ofproto->ofproto_class->port_is_lacp_current
495 ? ofproto->ofproto_class->port_is_lacp_current(ofport)
fa066f01
BP
496 : -1);
497}
e7934396 498\f
fa066f01
BP
499/* Bundles. */
500
abe529af
BP
501/* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
502 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
503 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
504 * configuration plus, if there is more than one slave, a bonding
505 * configuration.
506 *
507 * If 'aux' is already registered then this function updates its configuration
508 * to 's'. Otherwise, this function registers a new bundle.
509 *
510 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
511 * port. */
512int
513ofproto_bundle_register(struct ofproto *ofproto, void *aux,
514 const struct ofproto_bundle_settings *s)
fa066f01 515{
abe529af
BP
516 return (ofproto->ofproto_class->bundle_set
517 ? ofproto->ofproto_class->bundle_set(ofproto, aux, s)
518 : EOPNOTSUPP);
fa066f01
BP
519}
520
abe529af
BP
521/* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
522 * If no such bundle has been registered, this has no effect. */
523int
524ofproto_bundle_unregister(struct ofproto *ofproto, void *aux)
fa066f01 525{
abe529af 526 return ofproto_bundle_register(ofproto, aux, NULL);
fa066f01
BP
527}
528
abe529af
BP
529\f
530/* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
531 * If 'aux' is already registered then this function updates its configuration
532 * to 's'. Otherwise, this function registers a new mirror.
533 *
534 * Mirrors affect only the treatment of packets output to the OFPP_NORMAL
535 * port. */
536int
537ofproto_mirror_register(struct ofproto *ofproto, void *aux,
538 const struct ofproto_mirror_settings *s)
fa066f01 539{
abe529af
BP
540 return (ofproto->ofproto_class->mirror_set
541 ? ofproto->ofproto_class->mirror_set(ofproto, aux, s)
542 : EOPNOTSUPP);
fa066f01
BP
543}
544
abe529af
BP
545/* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
546 * If no mirror has been registered, this has no effect. */
547int
548ofproto_mirror_unregister(struct ofproto *ofproto, void *aux)
fa066f01 549{
abe529af 550 return ofproto_mirror_register(ofproto, aux, NULL);
fa066f01
BP
551}
552
abe529af
BP
553/* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
554 * which all packets are flooded, instead of using MAC learning. If
555 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
556 *
557 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
558 * port. */
559int
560ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans)
fa066f01 561{
abe529af
BP
562 return (ofproto->ofproto_class->set_flood_vlans
563 ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans)
564 : EOPNOTSUPP);
565}
fa066f01 566
abe529af
BP
567/* Returns true if 'aux' is a registered bundle that is currently in use as the
568 * output for a mirror. */
569bool
570ofproto_is_mirror_output_bundle(struct ofproto *ofproto, void *aux)
571{
572 return (ofproto->ofproto_class->is_mirror_output_bundle
573 ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux)
574 : false);
575}
576\f
577bool
578ofproto_has_snoops(const struct ofproto *ofproto)
579{
580 return connmgr_has_snoops(ofproto->connmgr);
fa066f01
BP
581}
582
583void
abe529af 584ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
fa066f01 585{
abe529af
BP
586 connmgr_get_snoops(ofproto->connmgr, snoops);
587}
fa066f01 588
abe529af
BP
589static void
590ofproto_destroy__(struct ofproto *ofproto)
591{
592 connmgr_destroy(ofproto->connmgr);
fa066f01 593
abe529af
BP
594 hmap_remove(&all_ofprotos, &ofproto->hmap_node);
595 free(ofproto->name);
596 free(ofproto->mfr_desc);
597 free(ofproto->hw_desc);
598 free(ofproto->sw_desc);
599 free(ofproto->serial_desc);
600 free(ofproto->dp_desc);
601 netdev_monitor_destroy(ofproto->netdev_monitor);
602 hmap_destroy(&ofproto->ports);
603 shash_destroy(&ofproto->port_by_name);
604 classifier_destroy(&ofproto->cls);
fa066f01 605
abe529af
BP
606 ofproto->ofproto_class->dealloc(ofproto);
607}
fa066f01 608
abe529af
BP
609void
610ofproto_destroy(struct ofproto *p)
611{
612 struct ofport *ofport, *next_ofport;
fa066f01 613
abe529af 614 if (!p) {
fa066f01
BP
615 return;
616 }
617
abe529af
BP
618 ofproto_flush_flows__(p);
619 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
620 hmap_remove(&p->ports, &ofport->hmap_node);
621 ofport_destroy(ofport);
fa066f01
BP
622 }
623
abe529af
BP
624 p->ofproto_class->destruct(p);
625 ofproto_destroy__(p);
626}
fa066f01 627
abe529af
BP
628/* Destroys the datapath with the respective 'name' and 'type'. With the Linux
629 * kernel datapath, for example, this destroys the datapath in the kernel, and
630 * with the netdev-based datapath, it tears down the data structures that
631 * represent the datapath.
632 *
633 * The datapath should not be currently open as an ofproto. */
634int
635ofproto_delete(const char *name, const char *type)
636{
637 const struct ofproto_class *class = ofproto_class_find__(type);
638 return (!class ? EAFNOSUPPORT
639 : !class->del ? EACCES
640 : class->del(type, name));
641}
fa066f01 642
abe529af
BP
643static void
644process_port_change(struct ofproto *ofproto, int error, char *devname)
645{
646 if (error == ENOBUFS) {
647 reinit_ports(ofproto);
648 } else if (!error) {
649 update_port(ofproto, devname);
650 free(devname);
fa066f01
BP
651 }
652}
653
abe529af
BP
654int
655ofproto_run(struct ofproto *p)
064af421 656{
abe529af
BP
657 char *devname;
658 int error;
fa066f01 659
abe529af
BP
660 error = p->ofproto_class->run(p);
661 if (error == ENODEV) {
662 /* Someone destroyed the datapath behind our back. The caller
663 * better destroy us and give up, because we're just going to
664 * spin from here on out. */
665 static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 5);
666 VLOG_ERR_RL(&rl2, "%s: datapath was destroyed externally",
667 p->name);
668 return ENODEV;
fa066f01
BP
669 }
670
5bf0e941
BP
671 if (p->ofproto_class->port_poll) {
672 while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) {
673 process_port_change(p, error, devname);
674 }
fa066f01 675 }
abe529af
BP
676 while ((error = netdev_monitor_poll(p->netdev_monitor,
677 &devname)) != EAGAIN) {
678 process_port_change(p, error, devname);
fa066f01
BP
679 }
680
abe529af
BP
681 connmgr_run(p->connmgr, handle_openflow);
682
683 return 0;
064af421
BP
684}
685
fa066f01 686void
abe529af 687ofproto_wait(struct ofproto *p)
abdfe474 688{
abe529af 689 p->ofproto_class->wait(p);
5bf0e941
BP
690 if (p->ofproto_class->port_poll_wait) {
691 p->ofproto_class->port_poll_wait(p);
692 }
abe529af
BP
693 netdev_monitor_poll_wait(p->netdev_monitor);
694 connmgr_wait(p->connmgr);
fa066f01
BP
695}
696
abe529af
BP
697bool
698ofproto_is_alive(const struct ofproto *p)
fa066f01 699{
abe529af 700 return connmgr_has_controllers(p->connmgr);
064af421
BP
701}
702
bffc0589 703void
2cdcb898 704ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
bffc0589
AE
705 struct shash *info)
706{
19a87e36 707 connmgr_get_controller_info(ofproto->connmgr, info);
bffc0589
AE
708}
709
710void
711ofproto_free_ofproto_controller_info(struct shash *info)
712{
713 struct shash_node *node;
714
715 SHASH_FOR_EACH (node, info) {
716 struct ofproto_controller_info *cinfo = node->data;
717 while (cinfo->pairs.n) {
718 free((char *) cinfo->pairs.values[--cinfo->pairs.n]);
719 }
720 free(cinfo);
721 }
722 shash_destroy(info);
723}
724
b5827b24
BP
725/* Makes a deep copy of 'old' into 'port'. */
726void
727ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old)
728{
729 port->name = xstrdup(old->name);
730 port->type = xstrdup(old->type);
731 port->ofp_port = old->ofp_port;
732}
733
734/* Frees memory allocated to members of 'ofproto_port'.
735 *
736 * Do not call this function on an ofproto_port obtained from
737 * ofproto_port_dump_next(): that function retains ownership of the data in the
738 * ofproto_port. */
739void
740ofproto_port_destroy(struct ofproto_port *ofproto_port)
741{
742 free(ofproto_port->name);
743 free(ofproto_port->type);
744}
745
b5827b24
BP
746/* Initializes 'dump' to begin dumping the ports in an ofproto.
747 *
748 * This function provides no status indication. An error status for the entire
749 * dump operation is provided when it is completed by calling
750 * ofproto_port_dump_done().
751 */
752void
753ofproto_port_dump_start(struct ofproto_port_dump *dump,
754 const struct ofproto *ofproto)
755{
abe529af
BP
756 dump->ofproto = ofproto;
757 dump->error = ofproto->ofproto_class->port_dump_start(ofproto,
758 &dump->state);
b5827b24
BP
759}
760
761/* Attempts to retrieve another port from 'dump', which must have been created
762 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
763 * 'port' and returns true. On failure, returns false.
764 *
765 * Failure might indicate an actual error or merely that the last port has been
766 * dumped. An error status for the entire dump operation is provided when it
767 * is completed by calling ofproto_port_dump_done().
768 *
769 * The ofproto owns the data stored in 'port'. It will remain valid until at
770 * least the next time 'dump' is passed to ofproto_port_dump_next() or
771 * ofproto_port_dump_done(). */
772bool
773ofproto_port_dump_next(struct ofproto_port_dump *dump,
774 struct ofproto_port *port)
775{
abe529af
BP
776 const struct ofproto *ofproto = dump->ofproto;
777
778 if (dump->error) {
779 return false;
780 }
b5827b24 781
abe529af
BP
782 dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state,
783 port);
784 if (dump->error) {
785 ofproto->ofproto_class->port_dump_done(ofproto, dump->state);
786 return false;
b5827b24 787 }
abe529af 788 return true;
b5827b24
BP
789}
790
791/* Completes port table dump operation 'dump', which must have been created
792 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
793 * error-free, otherwise a positive errno value describing the problem. */
794int
795ofproto_port_dump_done(struct ofproto_port_dump *dump)
796{
abe529af
BP
797 const struct ofproto *ofproto = dump->ofproto;
798 if (!dump->error) {
799 dump->error = ofproto->ofproto_class->port_dump_done(ofproto,
800 dump->state);
801 }
802 return dump->error == EOF ? 0 : dump->error;
b5827b24
BP
803}
804
805/* Attempts to add 'netdev' as a port on 'ofproto'. If successful, returns 0
892815f5 806 * and sets '*ofp_portp' to the new port's OpenFlow port number (if 'ofp_portp'
b5827b24
BP
807 * is non-null). On failure, returns a positive errno value and sets
808 * '*ofp_portp' to OFPP_NONE (if 'ofp_portp' is non-null). */
809int
810ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev,
811 uint16_t *ofp_portp)
812{
abe529af 813 uint16_t ofp_port;
b5827b24
BP
814 int error;
815
abe529af 816 error = ofproto->ofproto_class->port_add(ofproto, netdev, &ofp_port);
1fa24dea
BP
817 if (!error) {
818 update_port(ofproto, netdev_get_name(netdev));
819 }
b5827b24 820 if (ofp_portp) {
abe529af 821 *ofp_portp = error ? OFPP_NONE : ofp_port;
b5827b24
BP
822 }
823 return error;
824}
825
826/* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
827 * initializes '*port' appropriately; on failure, returns a positive errno
828 * value.
829 *
abe529af 830 * The caller owns the data in 'ofproto_port' and must free it with
b5827b24
BP
831 * ofproto_port_destroy() when it is no longer needed. */
832int
833ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname,
834 struct ofproto_port *port)
835{
b5827b24
BP
836 int error;
837
abe529af
BP
838 error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port);
839 if (error) {
840 memset(port, 0, sizeof *port);
b5827b24
BP
841 }
842 return error;
843}
844
845/* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
3a6ccc8c
BP
846 * Returns 0 if successful, otherwise a positive errno. */
847int
b5827b24 848ofproto_port_del(struct ofproto *ofproto, uint16_t ofp_port)
3a6ccc8c 849{
abe529af 850 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1264ec95 851 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
3a6ccc8c
BP
852 int error;
853
abe529af 854 error = ofproto->ofproto_class->port_del(ofproto, ofp_port);
892815f5 855 if (!error && ofport) {
1264ec95
BP
856 /* 'name' is the netdev's name and update_port() is going to close the
857 * netdev. Just in case update_port() refers to 'name' after it
3a6ccc8c
BP
858 * destroys 'ofport', make a copy of it around the update_port()
859 * call. */
860 char *devname = xstrdup(name);
861 update_port(ofproto, devname);
862 free(devname);
863 }
864 return error;
865}
866
fa8b054f
BP
867/* Adds a flow to the OpenFlow flow table in 'p' that matches 'cls_rule' and
868 * performs the 'n_actions' actions in 'actions'. The new flow will not
869 * timeout.
870 *
871 * If cls_rule->priority is in the range of priorities supported by OpenFlow
872 * (0...65535, inclusive) then the flow will be visible to OpenFlow
873 * controllers; otherwise, it will be hidden.
874 *
875 * The caller retains ownership of 'cls_rule' and 'actions'. */
064af421 876void
cf3fad8a 877ofproto_add_flow(struct ofproto *p, const struct cls_rule *cls_rule,
fa8b054f 878 const union ofp_action *actions, size_t n_actions)
064af421
BP
879{
880 struct rule *rule;
abe529af 881 rule_create(p, cls_rule, actions, n_actions, 0, 0, 0, false, &rule);
064af421
BP
882}
883
884void
cf3fad8a 885ofproto_delete_flow(struct ofproto *ofproto, const struct cls_rule *target)
064af421
BP
886{
887 struct rule *rule;
888
889 rule = rule_from_cls_rule(classifier_find_rule_exactly(&ofproto->cls,
cf3fad8a 890 target));
064af421 891 if (rule) {
abe529af 892 ofproto_rule_remove(rule);
064af421
BP
893 }
894}
895
142e1f5c
BP
896static void
897ofproto_flush_flows__(struct ofproto *ofproto)
064af421 898{
5ecc9d81
BP
899 struct rule *rule, *next_rule;
900 struct cls_cursor cursor;
bcf84111 901
064af421 902 COVERAGE_INC(ofproto_flush);
bcf84111 903
abe529af
BP
904 if (ofproto->ofproto_class->flush) {
905 ofproto->ofproto_class->flush(ofproto);
bcf84111 906 }
5ecc9d81
BP
907
908 cls_cursor_init(&cursor, &ofproto->cls, NULL);
909 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
abe529af 910 ofproto_rule_remove(rule);
5ecc9d81 911 }
142e1f5c
BP
912}
913
914void
915ofproto_flush_flows(struct ofproto *ofproto)
916{
917 ofproto_flush_flows__(ofproto);
19a87e36 918 connmgr_flushed(ofproto->connmgr);
064af421
BP
919}
920\f
921static void
922reinit_ports(struct ofproto *p)
923{
abe529af 924 struct ofproto_port_dump dump;
b3c01ed3 925 struct sset devnames;
064af421 926 struct ofport *ofport;
abe529af 927 struct ofproto_port ofproto_port;
b3c01ed3 928 const char *devname;
064af421 929
898bf89d
JP
930 COVERAGE_INC(ofproto_reinit_ports);
931
b3c01ed3 932 sset_init(&devnames);
4e8e4213 933 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1264ec95 934 sset_add(&devnames, netdev_get_name(ofport->netdev));
064af421 935 }
abe529af
BP
936 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
937 sset_add(&devnames, ofproto_port.name);
064af421 938 }
064af421 939
b3c01ed3
BP
940 SSET_FOR_EACH (devname, &devnames) {
941 update_port(p, devname);
064af421 942 }
b3c01ed3 943 sset_destroy(&devnames);
064af421
BP
944}
945
abe529af
BP
946/* Opens and returns a netdev for 'ofproto_port', or a null pointer if the
947 * netdev cannot be opened. On success, also fills in 'opp'. */
b33951b8 948static struct netdev *
abe529af 949ofport_open(const struct ofproto_port *ofproto_port, struct ofp_phy_port *opp)
064af421 950{
9cfcdadf 951 uint32_t curr, advertised, supported, peer;
149f577a 952 struct netdev_options netdev_options;
064af421 953 enum netdev_flags flags;
064af421 954 struct netdev *netdev;
064af421
BP
955 int error;
956
149f577a 957 memset(&netdev_options, 0, sizeof netdev_options);
abe529af
BP
958 netdev_options.name = ofproto_port->name;
959 netdev_options.type = ofproto_port->type;
149f577a 960 netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
149f577a
JG
961
962 error = netdev_open(&netdev_options, &netdev);
064af421
BP
963 if (error) {
964 VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
965 "cannot be opened (%s)",
abe529af
BP
966 ofproto_port->name, ofproto_port->ofp_port,
967 ofproto_port->name, strerror(error));
064af421
BP
968 return NULL;
969 }
970
064af421 971 netdev_get_flags(netdev, &flags);
9cfcdadf 972 netdev_get_features(netdev, &curr, &advertised, &supported, &peer);
064af421 973
abe529af 974 opp->port_no = htons(ofproto_port->ofp_port);
b33951b8 975 netdev_get_etheraddr(netdev, opp->hw_addr);
abe529af 976 ovs_strzcpy(opp->name, ofproto_port->name, sizeof opp->name);
9cfcdadf
BP
977 opp->config = flags & NETDEV_UP ? 0 : htonl(OFPPC_PORT_DOWN);
978 opp->state = netdev_get_carrier(netdev) ? 0 : htonl(OFPPS_LINK_DOWN);
979 opp->curr = htonl(curr);
980 opp->advertised = htonl(advertised);
981 opp->supported = htonl(supported);
982 opp->peer = htonl(peer);
983
b33951b8 984 return netdev;
064af421
BP
985}
986
abe529af
BP
987/* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
988 * port number, and 'config' bits other than OFPPC_PORT_DOWN are
989 * disregarded. */
064af421 990static bool
abe529af 991ofport_equal(const struct ofp_phy_port *a, const struct ofp_phy_port *b)
064af421 992{
064af421 993 BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */
b33951b8 994 return (!memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr)
064af421 995 && a->state == b->state
9cfcdadf 996 && !((a->config ^ b->config) & htonl(OFPPC_PORT_DOWN))
064af421
BP
997 && a->curr == b->curr
998 && a->advertised == b->advertised
999 && a->supported == b->supported
1000 && a->peer == b->peer);
1001}
1002
b33951b8
BP
1003/* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
1004 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
1005 * one with the same name or port number). */
064af421 1006static void
b33951b8
BP
1007ofport_install(struct ofproto *p,
1008 struct netdev *netdev, const struct ofp_phy_port *opp)
064af421 1009{
b33951b8
BP
1010 const char *netdev_name = netdev_get_name(netdev);
1011 struct ofport *ofport;
abe529af 1012 int error;
72b06300 1013
b33951b8 1014 /* Create ofport. */
abe529af
BP
1015 ofport = p->ofproto_class->port_alloc();
1016 if (!ofport) {
1017 error = ENOMEM;
1018 goto error;
1019 }
0f7d71a5 1020 ofport->ofproto = p;
b33951b8
BP
1021 ofport->netdev = netdev;
1022 ofport->opp = *opp;
abe529af 1023 ofport->ofp_port = ntohs(opp->port_no);
b33951b8
BP
1024
1025 /* Add port to 'p'. */
e9e28be3 1026 netdev_monitor_add(p->netdev_monitor, ofport->netdev);
abe529af 1027 hmap_insert(&p->ports, &ofport->hmap_node, hash_int(ofport->ofp_port, 0));
72b06300 1028 shash_add(&p->port_by_name, netdev_name, ofport);
abe529af
BP
1029
1030 /* Let the ofproto_class initialize its private data. */
1031 error = p->ofproto_class->port_construct(ofport);
1032 if (error) {
1033 goto error;
1034 }
1035 connmgr_send_port_status(p->connmgr, opp, OFPPR_ADD);
1036 return;
1037
1038error:
1039 VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)",
1040 p->name, netdev_name, strerror(error));
1041 if (ofport) {
1042 ofport_destroy__(ofport);
1043 } else {
1044 netdev_close(netdev);
72b06300 1045 }
064af421
BP
1046}
1047
b33951b8 1048/* Removes 'ofport' from 'p' and destroys it. */
064af421 1049static void
0f7d71a5 1050ofport_remove(struct ofport *ofport)
064af421 1051{
fa066f01
BP
1052 connmgr_send_port_status(ofport->ofproto->connmgr, &ofport->opp,
1053 OFPPR_DELETE);
abe529af 1054 ofport_destroy(ofport);
b33951b8
BP
1055}
1056
1057/* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
1058 * destroys it. */
1059static void
1060ofport_remove_with_name(struct ofproto *ofproto, const char *name)
1061{
1062 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
1063 if (port) {
0f7d71a5 1064 ofport_remove(port);
b33951b8
BP
1065 }
1066}
1067
1068/* Updates 'port' within 'ofproto' with the new 'netdev' and 'opp'.
1069 *
1070 * Does not handle a name or port number change. The caller must implement
1071 * such a change as a delete followed by an add. */
1072static void
abe529af 1073ofport_modified(struct ofport *port, struct ofp_phy_port *opp)
b33951b8
BP
1074{
1075 memcpy(port->opp.hw_addr, opp->hw_addr, ETH_ADDR_LEN);
9cfcdadf
BP
1076 port->opp.config = ((port->opp.config & ~htonl(OFPPC_PORT_DOWN))
1077 | (opp->config & htonl(OFPPC_PORT_DOWN)));
b33951b8
BP
1078 port->opp.state = opp->state;
1079 port->opp.curr = opp->curr;
1080 port->opp.advertised = opp->advertised;
1081 port->opp.supported = opp->supported;
1082 port->opp.peer = opp->peer;
1083
abe529af 1084 connmgr_send_port_status(port->ofproto->connmgr, &port->opp, OFPPR_MODIFY);
064af421
BP
1085}
1086
abe529af
BP
1087void
1088ofproto_port_unregister(struct ofproto *ofproto, uint16_t ofp_port)
e7934396 1089{
abe529af
BP
1090 struct ofport *port = ofproto_get_port(ofproto, ofp_port);
1091 if (port) {
1092 if (port->ofproto->ofproto_class->set_cfm) {
1093 port->ofproto->ofproto_class->set_cfm(port, NULL, NULL, 0);
1094 }
1095 if (port->ofproto->ofproto_class->bundle_remove) {
1096 port->ofproto->ofproto_class->bundle_remove(port);
e7934396
BP
1097 }
1098 }
1099}
1100
1101static void
abe529af 1102ofport_destroy__(struct ofport *port)
064af421 1103{
abe529af
BP
1104 struct ofproto *ofproto = port->ofproto;
1105 const char *name = netdev_get_name(port->netdev);
fa066f01 1106
abe529af
BP
1107 netdev_monitor_remove(ofproto->netdev_monitor, port->netdev);
1108 hmap_remove(&ofproto->ports, &port->hmap_node);
1109 shash_delete(&ofproto->port_by_name,
1110 shash_find(&ofproto->port_by_name, name));
fa066f01 1111
abe529af
BP
1112 netdev_close(port->netdev);
1113 ofproto->ofproto_class->port_dealloc(port);
fa066f01
BP
1114}
1115
1116static void
abe529af 1117ofport_destroy(struct ofport *port)
fa066f01
BP
1118{
1119 if (port) {
abe529af
BP
1120 port->ofproto->ofproto_class->port_destruct(port);
1121 ofport_destroy__(port);
1122 }
064af421
BP
1123}
1124
abe529af
BP
1125struct ofport *
1126ofproto_get_port(const struct ofproto *ofproto, uint16_t ofp_port)
ca0f572c
BP
1127{
1128 struct ofport *port;
1129
4e8e4213 1130 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node,
abe529af
BP
1131 hash_int(ofp_port, 0), &ofproto->ports) {
1132 if (port->ofp_port == ofp_port) {
ca0f572c
BP
1133 return port;
1134 }
1135 }
1136 return NULL;
1137}
1138
064af421 1139static void
b33951b8 1140update_port(struct ofproto *ofproto, const char *name)
064af421 1141{
abe529af 1142 struct ofproto_port ofproto_port;
b33951b8
BP
1143 struct ofp_phy_port opp;
1144 struct netdev *netdev;
1145 struct ofport *port;
064af421
BP
1146
1147 COVERAGE_INC(ofproto_update_port);
c874dc6d 1148
b33951b8 1149 /* Fetch 'name''s location and properties from the datapath. */
abe529af
BP
1150 netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port)
1151 ? ofport_open(&ofproto_port, &opp)
b33951b8
BP
1152 : NULL);
1153 if (netdev) {
abe529af 1154 port = ofproto_get_port(ofproto, ofproto_port.ofp_port);
b33951b8
BP
1155 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
1156 /* 'name' hasn't changed location. Any properties changed? */
1157 if (!ofport_equal(&port->opp, &opp)) {
abe529af
BP
1158 ofport_modified(port, &opp);
1159 }
1160
1161 /* Install the newly opened netdev in case it has changed. */
1162 netdev_monitor_remove(ofproto->netdev_monitor, port->netdev);
1163 netdev_monitor_add(ofproto->netdev_monitor, netdev);
1164
1165 netdev_close(port->netdev);
1166 port->netdev = netdev;
1167
1168 if (port->ofproto->ofproto_class->port_modified) {
1169 port->ofproto->ofproto_class->port_modified(port);
b33951b8
BP
1170 }
1171 } else {
1172 /* If 'port' is nonnull then its name differs from 'name' and thus
1173 * we should delete it. If we think there's a port named 'name'
1174 * then its port number must be wrong now so delete it too. */
1175 if (port) {
0f7d71a5 1176 ofport_remove(port);
b33951b8
BP
1177 }
1178 ofport_remove_with_name(ofproto, name);
1179 ofport_install(ofproto, netdev, &opp);
c874dc6d 1180 }
b33951b8
BP
1181 } else {
1182 /* Any port named 'name' is gone now. */
1183 ofport_remove_with_name(ofproto, name);
c874dc6d 1184 }
abe529af 1185 ofproto_port_destroy(&ofproto_port);
064af421
BP
1186}
1187
1188static int
1189init_ports(struct ofproto *p)
1190{
abe529af
BP
1191 struct ofproto_port_dump dump;
1192 struct ofproto_port ofproto_port;
1193
1194 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
1195 uint16_t ofp_port = ofproto_port.ofp_port;
1196 if (ofproto_get_port(p, ofp_port)) {
1197 VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
1198 ofp_port);
1199 } else if (shash_find(&p->port_by_name, ofproto_port.name)) {
1200 VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
1201 ofproto_port.name);
1202 } else {
b33951b8
BP
1203 struct ofp_phy_port opp;
1204 struct netdev *netdev;
1205
abe529af 1206 netdev = ofport_open(&ofproto_port, &opp);
b33951b8
BP
1207 if (netdev) {
1208 ofport_install(p, netdev, &opp);
064af421
BP
1209 }
1210 }
1211 }
b0ec0f27 1212
064af421
BP
1213 return 0;
1214}
1215\f
abe529af
BP
1216/* Creates a new rule initialized as specified, inserts it into 'ofproto''s
1217 * flow table, and stores the new rule into '*rulep'. Returns 0 on success,
1218 * otherwise a positive errno value or OpenFlow error code. */
1219static int
1220rule_create(struct ofproto *ofproto, const struct cls_rule *cls_rule,
064af421 1221 const union ofp_action *actions, size_t n_actions,
ca069229 1222 uint16_t idle_timeout, uint16_t hard_timeout,
abe529af
BP
1223 ovs_be64 flow_cookie, bool send_flow_removed,
1224 struct rule **rulep)
064af421 1225{
abe529af
BP
1226 struct rule *rule;
1227 int error;
1228
1229 rule = ofproto->ofproto_class->rule_alloc();
1230 if (!rule) {
1231 error = ENOMEM;
1232 goto error;
1233 }
1234
1235 rule->ofproto = ofproto;
bcf84111 1236 rule->cr = *cls_rule;
abe529af
BP
1237 rule->flow_cookie = flow_cookie;
1238 rule->created = time_msec();
064af421
BP
1239 rule->idle_timeout = idle_timeout;
1240 rule->hard_timeout = hard_timeout;
ca069229 1241 rule->send_flow_removed = send_flow_removed;
3dffcf07 1242 if (n_actions > 0) {
3dffcf07 1243 rule->actions = xmemdup(actions, n_actions * sizeof *actions);
abe529af
BP
1244 } else {
1245 rule->actions = NULL;
3dffcf07 1246 }
abe529af 1247 rule->n_actions = n_actions;
0193b2af 1248
abe529af
BP
1249 error = ofproto->ofproto_class->rule_construct(rule);
1250 if (error) {
1251 ofproto_rule_destroy__(rule);
1252 goto error;
1253 }
064af421 1254
abe529af
BP
1255 *rulep = rule;
1256 return 0;
1257
1258error:
1259 VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)",
1260 ofproto->name, strerror(error));
1261 *rulep = NULL;
1262 return error;
064af421
BP
1263}
1264
1265static void
abe529af 1266ofproto_rule_destroy__(struct rule *rule)
064af421
BP
1267{
1268 free(rule->actions);
abe529af 1269 rule->ofproto->ofproto_class->rule_dealloc(rule);
064af421
BP
1270}
1271
f797957a 1272/* Destroys 'rule' and removes it from the datapath.
064af421 1273 *
bcf84111 1274 * The caller must have already removed 'rule' from the classifier. */
abe529af
BP
1275void
1276ofproto_rule_destroy(struct rule *rule)
064af421 1277{
abe529af
BP
1278 rule->ofproto->ofproto_class->rule_destruct(rule);
1279 ofproto_rule_destroy__(rule);
064af421
BP
1280}
1281
bcf84111
BP
1282/* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
1283 * that outputs to 'out_port' (output to OFPP_FLOOD and OFPP_ALL doesn't
1284 * count). */
064af421 1285static bool
8054fc48 1286rule_has_out_port(const struct rule *rule, ovs_be16 out_port)
064af421
BP
1287{
1288 const union ofp_action *oa;
1289 struct actions_iterator i;
1290
1291 if (out_port == htons(OFPP_NONE)) {
1292 return true;
1293 }
1294 for (oa = actions_first(&i, rule->actions, rule->n_actions); oa;
1295 oa = actions_next(&i)) {
c1c9c9c4 1296 if (action_outputs_to_port(oa, out_port)) {
064af421
BP
1297 return true;
1298 }
1299 }
1300 return false;
1301}
1302
abe529af
BP
1303struct rule *
1304ofproto_rule_lookup(struct ofproto *ofproto, const struct flow *flow)
064af421 1305{
abe529af 1306 return rule_from_cls_rule(classifier_lookup(&ofproto->cls, flow));
bcf84111
BP
1307}
1308
1309/* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s
f797957a
BP
1310 * statistics appropriately. 'packet' must have at least sizeof(struct
1311 * ofp_packet_in) bytes of headroom.
bcf84111
BP
1312 *
1313 * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited
1314 * with statistics for 'packet' either way.
1315 *
1316 * Takes ownership of 'packet'. */
5bf0e941 1317static int
abe529af 1318rule_execute(struct rule *rule, uint16_t in_port, struct ofpbuf *packet)
bcf84111 1319{
bcf84111 1320 struct flow flow;
cdee00fd 1321
abe529af 1322 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
fa066f01 1323
abe529af 1324 flow_extract(packet, 0, in_port, &flow);
5bf0e941 1325 return rule->ofproto->ofproto_class->rule_execute(rule, &flow, packet);
abe529af 1326}
fa066f01 1327
f797957a
BP
1328/* Removes 'rule' from 'ofproto' and frees up the associated memory. Removes
1329 * 'rule' from the classifier. */
abe529af
BP
1330void
1331ofproto_rule_remove(struct rule *rule)
1332{
1333 rule->ofproto->ofproto_class->rule_remove(rule);
1334 ofproto_rule_destroy(rule);
1335}
fa066f01 1336
abe529af
BP
1337/* Returns true if 'rule' should be hidden from the controller.
1338 *
1339 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
1340 * (e.g. by in-band control) and are intentionally hidden from the
1341 * controller. */
1342static bool
1343rule_is_hidden(const struct rule *rule)
1344{
1345 return rule->cr.priority > UINT16_MAX;
fa066f01
BP
1346}
1347\f
064af421
BP
1348static void
1349send_error_oh(const struct ofconn *ofconn, const struct ofp_header *oh,
1350 int error)
1351{
dc4762ed 1352 struct ofpbuf *buf = ofputil_encode_error_msg(error, oh);
26c112c2
BP
1353 if (buf) {
1354 COVERAGE_INC(ofproto_error);
b0421aa2 1355 ofconn_send_reply(ofconn, buf);
26c112c2 1356 }
064af421
BP
1357}
1358
064af421 1359static int
d1e2cf21 1360handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 1361{
b0421aa2 1362 ofconn_send_reply(ofconn, make_echo_reply(oh));
064af421
BP
1363 return 0;
1364}
1365
1366static int
d1e2cf21 1367handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 1368{
64ff1d96 1369 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
064af421
BP
1370 struct ofp_switch_features *osf;
1371 struct ofpbuf *buf;
064af421
BP
1372 struct ofport *port;
1373
1374 osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, oh->xid, &buf);
64ff1d96 1375 osf->datapath_id = htonll(ofproto->datapath_id);
064af421
BP
1376 osf->n_buffers = htonl(pktbuf_capacity());
1377 osf->n_tables = 2;
1378 osf->capabilities = htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS |
0254ae23 1379 OFPC_PORT_STATS | OFPC_ARP_MATCH_IP);
064af421
BP
1380 osf->actions = htonl((1u << OFPAT_OUTPUT) |
1381 (1u << OFPAT_SET_VLAN_VID) |
1382 (1u << OFPAT_SET_VLAN_PCP) |
1383 (1u << OFPAT_STRIP_VLAN) |
1384 (1u << OFPAT_SET_DL_SRC) |
1385 (1u << OFPAT_SET_DL_DST) |
1386 (1u << OFPAT_SET_NW_SRC) |
1387 (1u << OFPAT_SET_NW_DST) |
959a2ecd 1388 (1u << OFPAT_SET_NW_TOS) |
064af421 1389 (1u << OFPAT_SET_TP_SRC) |
c1c9c9c4
BP
1390 (1u << OFPAT_SET_TP_DST) |
1391 (1u << OFPAT_ENQUEUE));
064af421 1392
64ff1d96 1393 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
9cfcdadf 1394 ofpbuf_put(buf, &port->opp, sizeof port->opp);
064af421
BP
1395 }
1396
b0421aa2 1397 ofconn_send_reply(ofconn, buf);
064af421
BP
1398 return 0;
1399}
1400
1401static int
d1e2cf21 1402handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 1403{
64ff1d96 1404 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
064af421
BP
1405 struct ofpbuf *buf;
1406 struct ofp_switch_config *osc;
1407 uint16_t flags;
1408 bool drop_frags;
1409
1410 /* Figure out flags. */
abe529af 1411 drop_frags = ofproto->ofproto_class->get_drop_frags(ofproto);
064af421 1412 flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL;
064af421
BP
1413
1414 /* Send reply. */
1415 osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf);
1416 osc->flags = htons(flags);
810605a2 1417 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
b0421aa2 1418 ofconn_send_reply(ofconn, buf);
064af421
BP
1419
1420 return 0;
1421}
1422
1423static int
d1e2cf21 1424handle_set_config(struct ofconn *ofconn, const struct ofp_switch_config *osc)
064af421 1425{
64ff1d96 1426 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
d1e2cf21 1427 uint16_t flags = ntohs(osc->flags);
064af421 1428
1ce0a5fa
BP
1429 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
1430 && ofconn_get_role(ofconn) != NX_ROLE_SLAVE) {
064af421
BP
1431 switch (flags & OFPC_FRAG_MASK) {
1432 case OFPC_FRAG_NORMAL:
abe529af 1433 ofproto->ofproto_class->set_drop_frags(ofproto, false);
064af421
BP
1434 break;
1435 case OFPC_FRAG_DROP:
abe529af 1436 ofproto->ofproto_class->set_drop_frags(ofproto, true);
064af421
BP
1437 break;
1438 default:
1439 VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")",
1440 osc->flags);
1441 break;
1442 }
1443 }
1444
810605a2 1445 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
064af421
BP
1446
1447 return 0;
1448}
1449
9deba63b
BP
1450/* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
1451 * error message code (composed with ofp_mkerr()) for the caller to propagate
1452 * upward. Otherwise, returns 0.
1453 *
2228b50d 1454 * The log message mentions 'msg_type'. */
9deba63b 1455static int
2228b50d 1456reject_slave_controller(struct ofconn *ofconn, const const char *msg_type)
9deba63b 1457{
1ce0a5fa
BP
1458 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
1459 && ofconn_get_role(ofconn) == NX_ROLE_SLAVE) {
9deba63b 1460 static struct vlog_rate_limit perm_rl = VLOG_RATE_LIMIT_INIT(1, 5);
9deba63b 1461 VLOG_WARN_RL(&perm_rl, "rejecting %s message from slave controller",
2228b50d 1462 msg_type);
9deba63b
BP
1463
1464 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
1465 } else {
1466 return 0;
1467 }
1468}
1469
064af421 1470static int
d1e2cf21 1471handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 1472{
64ff1d96 1473 struct ofproto *p = ofconn_get_ofproto(ofconn);
064af421
BP
1474 struct ofp_packet_out *opo;
1475 struct ofpbuf payload, *buffer;
ac51afaf 1476 union ofp_action *ofp_actions;
ac51afaf 1477 struct ofpbuf request;
ae412e7d 1478 struct flow flow;
ac51afaf 1479 size_t n_ofp_actions;
064af421 1480 uint16_t in_port;
064af421
BP
1481 int error;
1482
ac51afaf
BP
1483 COVERAGE_INC(ofproto_packet_out);
1484
2228b50d 1485 error = reject_slave_controller(ofconn, "OFPT_PACKET_OUT");
9deba63b
BP
1486 if (error) {
1487 return error;
1488 }
1489
ac51afaf 1490 /* Get ofp_packet_out. */
0bc9407d 1491 ofpbuf_use_const(&request, oh, ntohs(oh->length));
bbc32a88 1492 opo = ofpbuf_pull(&request, offsetof(struct ofp_packet_out, actions));
ac51afaf
BP
1493
1494 /* Get actions. */
1495 error = ofputil_pull_actions(&request, ntohs(opo->actions_len),
1496 &ofp_actions, &n_ofp_actions);
064af421
BP
1497 if (error) {
1498 return error;
1499 }
064af421 1500
ac51afaf 1501 /* Get payload. */
064af421 1502 if (opo->buffer_id != htonl(UINT32_MAX)) {
10d0a1d2
BP
1503 error = ofconn_pktbuf_retrieve(ofconn, ntohl(opo->buffer_id),
1504 &buffer, &in_port);
7778bd15 1505 if (error || !buffer) {
064af421
BP
1506 return error;
1507 }
1508 payload = *buffer;
1509 } else {
ac51afaf 1510 payload = request;
064af421
BP
1511 buffer = NULL;
1512 }
1513
abe529af
BP
1514 /* Send out packet. */
1515 flow_extract(&payload, 0, ntohs(opo->in_port), &flow);
1516 error = p->ofproto_class->packet_out(p, &payload, &flow,
1517 ofp_actions, n_ofp_actions);
ac51afaf 1518 ofpbuf_delete(buffer);
abe529af
BP
1519
1520 return error;
064af421
BP
1521}
1522
1523static void
abe529af 1524update_port_config(struct ofport *port, ovs_be32 config, ovs_be32 mask)
064af421 1525{
abe529af
BP
1526 ovs_be32 old_config = port->opp.config;
1527
064af421 1528 mask &= config ^ port->opp.config;
9cfcdadf
BP
1529 if (mask & htonl(OFPPC_PORT_DOWN)) {
1530 if (config & htonl(OFPPC_PORT_DOWN)) {
064af421
BP
1531 netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
1532 } else {
1533 netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
1534 }
1535 }
abe529af
BP
1536
1537 port->opp.config ^= mask & (htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
1538 OFPPC_NO_FLOOD | OFPPC_NO_FWD |
1539 OFPPC_NO_PACKET_IN));
1540 if (port->opp.config != old_config) {
1541 port->ofproto->ofproto_class->port_reconfigured(port, old_config);
064af421
BP
1542 }
1543}
1544
1545static int
d1e2cf21 1546handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 1547{
64ff1d96 1548 struct ofproto *p = ofconn_get_ofproto(ofconn);
d1e2cf21 1549 const struct ofp_port_mod *opm = (const struct ofp_port_mod *) oh;
064af421
BP
1550 struct ofport *port;
1551 int error;
1552
2228b50d 1553 error = reject_slave_controller(ofconn, "OFPT_PORT_MOD");
9deba63b
BP
1554 if (error) {
1555 return error;
1556 }
064af421 1557
abe529af 1558 port = ofproto_get_port(p, ntohs(opm->port_no));
064af421
BP
1559 if (!port) {
1560 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT);
1561 } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) {
1562 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR);
1563 } else {
abe529af 1564 update_port_config(port, opm->config, opm->mask);
064af421
BP
1565 if (opm->advertise) {
1566 netdev_set_advertisements(port->netdev, ntohl(opm->advertise));
1567 }
1568 }
1569 return 0;
1570}
1571
1572static struct ofpbuf *
06a5e131 1573make_ofp_stats_reply(ovs_be32 xid, ovs_be16 type, size_t body_len)
064af421
BP
1574{
1575 struct ofp_stats_reply *osr;
1576 struct ofpbuf *msg;
1577
1578 msg = ofpbuf_new(MIN(sizeof *osr + body_len, UINT16_MAX));
1579 osr = put_openflow_xid(sizeof *osr, OFPT_STATS_REPLY, xid, msg);
1580 osr->type = type;
1581 osr->flags = htons(0);
1582 return msg;
1583}
1584
1585static struct ofpbuf *
d1e2cf21 1586start_ofp_stats_reply(const struct ofp_header *request, size_t body_len)
064af421 1587{
d1e2cf21
BP
1588 const struct ofp_stats_request *osr
1589 = (const struct ofp_stats_request *) request;
1590 return make_ofp_stats_reply(osr->header.xid, osr->type, body_len);
064af421
BP
1591}
1592
1593static void *
06a5e131
BP
1594append_ofp_stats_reply(size_t nbytes, struct ofconn *ofconn,
1595 struct ofpbuf **msgp)
064af421
BP
1596{
1597 struct ofpbuf *msg = *msgp;
1598 assert(nbytes <= UINT16_MAX - sizeof(struct ofp_stats_reply));
1599 if (nbytes + msg->size > UINT16_MAX) {
1600 struct ofp_stats_reply *reply = msg->data;
1601 reply->flags = htons(OFPSF_REPLY_MORE);
06a5e131 1602 *msgp = make_ofp_stats_reply(reply->header.xid, reply->type, nbytes);
b0421aa2 1603 ofconn_send_reply(ofconn, msg);
064af421
BP
1604 }
1605 return ofpbuf_put_uninit(*msgp, nbytes);
1606}
1607
09246b99
BP
1608static struct ofpbuf *
1609make_nxstats_reply(ovs_be32 xid, ovs_be32 subtype, size_t body_len)
1610{
1611 struct nicira_stats_msg *nsm;
1612 struct ofpbuf *msg;
1613
1614 msg = ofpbuf_new(MIN(sizeof *nsm + body_len, UINT16_MAX));
1615 nsm = put_openflow_xid(sizeof *nsm, OFPT_STATS_REPLY, xid, msg);
1616 nsm->type = htons(OFPST_VENDOR);
1617 nsm->flags = htons(0);
1618 nsm->vendor = htonl(NX_VENDOR_ID);
d5f2379b 1619 nsm->subtype = subtype;
09246b99
BP
1620 return msg;
1621}
1622
1623static struct ofpbuf *
1624start_nxstats_reply(const struct nicira_stats_msg *request, size_t body_len)
1625{
1626 return make_nxstats_reply(request->header.xid, request->subtype, body_len);
1627}
1628
1629static void
1630append_nxstats_reply(size_t nbytes, struct ofconn *ofconn,
1631 struct ofpbuf **msgp)
1632{
1633 struct ofpbuf *msg = *msgp;
1634 assert(nbytes <= UINT16_MAX - sizeof(struct nicira_stats_msg));
1635 if (nbytes + msg->size > UINT16_MAX) {
1636 struct nicira_stats_msg *reply = msg->data;
1637 reply->flags = htons(OFPSF_REPLY_MORE);
1638 *msgp = make_nxstats_reply(reply->header.xid, reply->subtype, nbytes);
b0421aa2 1639 ofconn_send_reply(ofconn, msg);
09246b99
BP
1640 }
1641 ofpbuf_prealloc_tailroom(*msgp, nbytes);
1642}
1643
064af421 1644static int
3269c562 1645handle_desc_stats_request(struct ofconn *ofconn,
d1e2cf21 1646 const struct ofp_header *request)
064af421 1647{
64ff1d96 1648 struct ofproto *p = ofconn_get_ofproto(ofconn);
064af421
BP
1649 struct ofp_desc_stats *ods;
1650 struct ofpbuf *msg;
1651
06a5e131
BP
1652 msg = start_ofp_stats_reply(request, sizeof *ods);
1653 ods = append_ofp_stats_reply(sizeof *ods, ofconn, &msg);
5a719c38
JP
1654 memset(ods, 0, sizeof *ods);
1655 ovs_strlcpy(ods->mfr_desc, p->mfr_desc, sizeof ods->mfr_desc);
1656 ovs_strlcpy(ods->hw_desc, p->hw_desc, sizeof ods->hw_desc);
1657 ovs_strlcpy(ods->sw_desc, p->sw_desc, sizeof ods->sw_desc);
1658 ovs_strlcpy(ods->serial_num, p->serial_desc, sizeof ods->serial_num);
1659 ovs_strlcpy(ods->dp_desc, p->dp_desc, sizeof ods->dp_desc);
b0421aa2 1660 ofconn_send_reply(ofconn, msg);
064af421
BP
1661
1662 return 0;
1663}
1664
064af421 1665static int
3269c562 1666handle_table_stats_request(struct ofconn *ofconn,
d1e2cf21 1667 const struct ofp_header *request)
064af421 1668{
64ff1d96 1669 struct ofproto *p = ofconn_get_ofproto(ofconn);
064af421
BP
1670 struct ofp_table_stats *ots;
1671 struct ofpbuf *msg;
064af421 1672
06a5e131 1673 msg = start_ofp_stats_reply(request, sizeof *ots * 2);
064af421 1674
064af421 1675 /* Classifier table. */
06a5e131 1676 ots = append_ofp_stats_reply(sizeof *ots, ofconn, &msg);
064af421 1677 memset(ots, 0, sizeof *ots);
064af421 1678 strcpy(ots->name, "classifier");
f1dd9191 1679 ots->wildcards = (ofconn_get_flow_format(ofconn) == NXFF_OPENFLOW10
f9bfea14 1680 ? htonl(OFPFW_ALL) : htonl(OVSFW_ALL));
ad828225 1681 ots->max_entries = htonl(1024 * 1024); /* An arbitrary big number. */
bcf84111 1682 ots->active_count = htonl(classifier_count(&p->cls));
c4617b3c
BP
1683 put_32aligned_be64(&ots->lookup_count, htonll(0)); /* XXX */
1684 put_32aligned_be64(&ots->matched_count, htonll(0)); /* XXX */
064af421 1685
b0421aa2 1686 ofconn_send_reply(ofconn, msg);
064af421
BP
1687 return 0;
1688}
1689
abaad8cf 1690static void
ca0f572c 1691append_port_stat(struct ofport *port, struct ofconn *ofconn,
a4948b95 1692 struct ofpbuf **msgp)
abaad8cf
JP
1693{
1694 struct netdev_stats stats;
1695 struct ofp_port_stats *ops;
1696
d295e8e9
JP
1697 /* Intentionally ignore return value, since errors will set
1698 * 'stats' to all-1s, which is correct for OpenFlow, and
abaad8cf
JP
1699 * netdev_get_stats() will log errors. */
1700 netdev_get_stats(port->netdev, &stats);
1701
06a5e131 1702 ops = append_ofp_stats_reply(sizeof *ops, ofconn, msgp);
9cfcdadf 1703 ops->port_no = port->opp.port_no;
abaad8cf 1704 memset(ops->pad, 0, sizeof ops->pad);
c4617b3c
BP
1705 put_32aligned_be64(&ops->rx_packets, htonll(stats.rx_packets));
1706 put_32aligned_be64(&ops->tx_packets, htonll(stats.tx_packets));
1707 put_32aligned_be64(&ops->rx_bytes, htonll(stats.rx_bytes));
1708 put_32aligned_be64(&ops->tx_bytes, htonll(stats.tx_bytes));
1709 put_32aligned_be64(&ops->rx_dropped, htonll(stats.rx_dropped));
1710 put_32aligned_be64(&ops->tx_dropped, htonll(stats.tx_dropped));
1711 put_32aligned_be64(&ops->rx_errors, htonll(stats.rx_errors));
1712 put_32aligned_be64(&ops->tx_errors, htonll(stats.tx_errors));
1713 put_32aligned_be64(&ops->rx_frame_err, htonll(stats.rx_frame_errors));
1714 put_32aligned_be64(&ops->rx_over_err, htonll(stats.rx_over_errors));
1715 put_32aligned_be64(&ops->rx_crc_err, htonll(stats.rx_crc_errors));
1716 put_32aligned_be64(&ops->collisions, htonll(stats.collisions));
abaad8cf
JP
1717}
1718
064af421 1719static int
d1e2cf21 1720handle_port_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 1721{
64ff1d96 1722 struct ofproto *p = ofconn_get_ofproto(ofconn);
d1e2cf21 1723 const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
064af421
BP
1724 struct ofp_port_stats *ops;
1725 struct ofpbuf *msg;
1726 struct ofport *port;
064af421 1727
d1e2cf21 1728 msg = start_ofp_stats_reply(oh, sizeof *ops * 16);
abaad8cf 1729 if (psr->port_no != htons(OFPP_NONE)) {
abe529af 1730 port = ofproto_get_port(p, ntohs(psr->port_no));
abaad8cf 1731 if (port) {
ca0f572c 1732 append_port_stat(port, ofconn, &msg);
abaad8cf
JP
1733 }
1734 } else {
4e8e4213 1735 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
ca0f572c 1736 append_port_stat(port, ofconn, &msg);
abaad8cf 1737 }
064af421
BP
1738 }
1739
b0421aa2 1740 ofconn_send_reply(ofconn, msg);
064af421
BP
1741 return 0;
1742}
1743
c6ebb8fb 1744static void
588cd7b5 1745calc_flow_duration__(long long int start, uint32_t *sec, uint32_t *nsec)
c6ebb8fb
BP
1746{
1747 long long int msecs = time_msec() - start;
588cd7b5
BP
1748 *sec = msecs / 1000;
1749 *nsec = (msecs % 1000) * (1000 * 1000);
1750}
1751
1752static void
1753calc_flow_duration(long long int start, ovs_be32 *sec_be, ovs_be32 *nsec_be)
1754{
1755 uint32_t sec, nsec;
1756
1757 calc_flow_duration__(start, &sec, &nsec);
1758 *sec_be = htonl(sec);
1759 *nsec_be = htonl(nsec);
c6ebb8fb
BP
1760}
1761
064af421 1762static void
5ecc9d81
BP
1763put_ofp_flow_stats(struct ofconn *ofconn, struct rule *rule,
1764 ovs_be16 out_port, struct ofpbuf **replyp)
064af421 1765{
abe529af 1766 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
064af421
BP
1767 struct ofp_flow_stats *ofs;
1768 uint64_t packet_count, byte_count;
c4617b3c 1769 ovs_be64 cookie;
064af421
BP
1770 size_t act_len, len;
1771
5ecc9d81 1772 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
064af421
BP
1773 return;
1774 }
1775
1776 act_len = sizeof *rule->actions * rule->n_actions;
1777 len = offsetof(struct ofp_flow_stats, actions) + act_len;
1778
abe529af 1779 ofproto->ofproto_class->rule_get_stats(rule, &packet_count, &byte_count);
064af421 1780
5ecc9d81 1781 ofs = append_ofp_stats_reply(len, ofconn, replyp);
064af421 1782 ofs->length = htons(len);
ad828225 1783 ofs->table_id = 0;
064af421 1784 ofs->pad = 0;
f1dd9191
BP
1785 ofputil_cls_rule_to_match(&rule->cr, ofconn_get_flow_format(ofconn),
1786 &ofs->match, rule->flow_cookie, &cookie);
c4617b3c 1787 put_32aligned_be64(&ofs->cookie, cookie);
c6ebb8fb 1788 calc_flow_duration(rule->created, &ofs->duration_sec, &ofs->duration_nsec);
064af421
BP
1789 ofs->priority = htons(rule->cr.priority);
1790 ofs->idle_timeout = htons(rule->idle_timeout);
1791 ofs->hard_timeout = htons(rule->hard_timeout);
39997502 1792 memset(ofs->pad2, 0, sizeof ofs->pad2);
c4617b3c
BP
1793 put_32aligned_be64(&ofs->packet_count, htonll(packet_count));
1794 put_32aligned_be64(&ofs->byte_count, htonll(byte_count));
3dffcf07
BP
1795 if (rule->n_actions > 0) {
1796 memcpy(ofs->actions, rule->actions, act_len);
1797 }
064af421
BP
1798}
1799
3c4486a5
BP
1800static bool
1801is_valid_table(uint8_t table_id)
064af421 1802{
a02e5331
BP
1803 if (table_id == 0 || table_id == 0xff) {
1804 return true;
1805 } else {
1806 /* It would probably be better to reply with an error but there doesn't
1807 * seem to be any appropriate value, so that might just be
1808 * confusing. */
1809 VLOG_WARN_RL(&rl, "controller asked for invalid table %"PRIu8,
1810 table_id);
1811 return false;
1812 }
064af421
BP
1813}
1814
1815static int
d1e2cf21 1816handle_flow_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 1817{
d1e2cf21 1818 const struct ofp_flow_stats_request *fsr = ofputil_stats_body(oh);
64ff1d96 1819 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5ecc9d81 1820 struct ofpbuf *reply;
064af421 1821
064af421 1822 COVERAGE_INC(ofproto_flows_req);
d1e2cf21 1823 reply = start_ofp_stats_reply(oh, 1024);
3c4486a5 1824 if (is_valid_table(fsr->table_id)) {
5ecc9d81 1825 struct cls_cursor cursor;
3c4486a5 1826 struct cls_rule target;
5ecc9d81 1827 struct rule *rule;
3c4486a5 1828
d8ae4d67
BP
1829 ofputil_cls_rule_from_match(&fsr->match, 0, NXFF_OPENFLOW10, 0,
1830 &target);
64ff1d96 1831 cls_cursor_init(&cursor, &ofproto->cls, &target);
5ecc9d81
BP
1832 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
1833 put_ofp_flow_stats(ofconn, rule, fsr->out_port, &reply);
1834 }
3c4486a5 1835 }
b0421aa2 1836 ofconn_send_reply(ofconn, reply);
3c4486a5 1837
064af421
BP
1838 return 0;
1839}
1840
09246b99 1841static void
5ecc9d81
BP
1842put_nx_flow_stats(struct ofconn *ofconn, struct rule *rule,
1843 ovs_be16 out_port, struct ofpbuf **replyp)
09246b99 1844{
09246b99
BP
1845 struct nx_flow_stats *nfs;
1846 uint64_t packet_count, byte_count;
1847 size_t act_len, start_len;
5ecc9d81 1848 struct ofpbuf *reply;
09246b99 1849
5ecc9d81 1850 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
09246b99
BP
1851 return;
1852 }
1853
abe529af
BP
1854 rule->ofproto->ofproto_class->rule_get_stats(rule,
1855 &packet_count, &byte_count);
09246b99
BP
1856
1857 act_len = sizeof *rule->actions * rule->n_actions;
1858
5ecc9d81 1859 append_nxstats_reply(sizeof *nfs + NXM_MAX_LEN + act_len, ofconn, replyp);
1dfee98d 1860 start_len = (*replyp)->size;
5ecc9d81
BP
1861 reply = *replyp;
1862
1863 nfs = ofpbuf_put_uninit(reply, sizeof *nfs);
09246b99
BP
1864 nfs->table_id = 0;
1865 nfs->pad = 0;
1866 calc_flow_duration(rule->created, &nfs->duration_sec, &nfs->duration_nsec);
1867 nfs->cookie = rule->flow_cookie;
1868 nfs->priority = htons(rule->cr.priority);
1869 nfs->idle_timeout = htons(rule->idle_timeout);
1870 nfs->hard_timeout = htons(rule->hard_timeout);
5ecc9d81 1871 nfs->match_len = htons(nx_put_match(reply, &rule->cr));
09246b99
BP
1872 memset(nfs->pad2, 0, sizeof nfs->pad2);
1873 nfs->packet_count = htonll(packet_count);
1874 nfs->byte_count = htonll(byte_count);
1875 if (rule->n_actions > 0) {
5ecc9d81 1876 ofpbuf_put(reply, rule->actions, act_len);
09246b99 1877 }
5ecc9d81 1878 nfs->length = htons(reply->size - start_len);
09246b99
BP
1879}
1880
1881static int
d1e2cf21 1882handle_nxst_flow(struct ofconn *ofconn, const struct ofp_header *oh)
09246b99 1883{
64ff1d96 1884 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
09246b99 1885 struct nx_flow_stats_request *nfsr;
09246b99 1886 struct cls_rule target;
5ecc9d81 1887 struct ofpbuf *reply;
d1e2cf21 1888 struct ofpbuf b;
09246b99
BP
1889 int error;
1890
0bc9407d 1891 ofpbuf_use_const(&b, oh, ntohs(oh->length));
d1e2cf21 1892
09246b99 1893 /* Dissect the message. */
bbc32a88 1894 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
d1e2cf21 1895 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &target);
09246b99
BP
1896 if (error) {
1897 return error;
1898 }
d1e2cf21
BP
1899 if (b.size) {
1900 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1901 }
09246b99
BP
1902
1903 COVERAGE_INC(ofproto_flows_req);
5ecc9d81 1904 reply = start_nxstats_reply(&nfsr->nsm, 1024);
3c4486a5 1905 if (is_valid_table(nfsr->table_id)) {
5ecc9d81
BP
1906 struct cls_cursor cursor;
1907 struct rule *rule;
1908
64ff1d96 1909 cls_cursor_init(&cursor, &ofproto->cls, &target);
5ecc9d81
BP
1910 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
1911 put_nx_flow_stats(ofconn, rule, nfsr->out_port, &reply);
1912 }
3c4486a5 1913 }
b0421aa2 1914 ofconn_send_reply(ofconn, reply);
5ecc9d81 1915
09246b99
BP
1916 return 0;
1917}
1918
4f2cad2c 1919static void
3394b5b6 1920flow_stats_ds(struct rule *rule, struct ds *results)
4f2cad2c 1921{
4f2cad2c
JP
1922 uint64_t packet_count, byte_count;
1923 size_t act_len = sizeof *rule->actions * rule->n_actions;
1924
abe529af
BP
1925 rule->ofproto->ofproto_class->rule_get_stats(rule,
1926 &packet_count, &byte_count);
4f2cad2c
JP
1927
1928 ds_put_format(results, "duration=%llds, ",
1929 (time_msec() - rule->created) / 1000);
52ae00b3 1930 ds_put_format(results, "priority=%u, ", rule->cr.priority);
4f2cad2c
JP
1931 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
1932 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
cb833cf6 1933 cls_rule_format(&rule->cr, results);
a5df0e72 1934 ds_put_char(results, ',');
3dffcf07
BP
1935 if (act_len > 0) {
1936 ofp_print_actions(results, &rule->actions->header, act_len);
3c8552c1
JP
1937 } else {
1938 ds_put_cstr(results, "drop");
3dffcf07 1939 }
4f2cad2c
JP
1940 ds_put_cstr(results, "\n");
1941}
1942
d295e8e9 1943/* Adds a pretty-printed description of all flows to 'results', including
ee8b231c 1944 * hidden flows (e.g., set up by in-band control). */
4f2cad2c
JP
1945void
1946ofproto_get_all_flows(struct ofproto *p, struct ds *results)
1947{
5ecc9d81
BP
1948 struct cls_cursor cursor;
1949 struct rule *rule;
064af421 1950
5ecc9d81
BP
1951 cls_cursor_init(&cursor, &p->cls, NULL);
1952 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3394b5b6 1953 flow_stats_ds(rule, results);
064af421 1954 }
064af421
BP
1955}
1956
b5827b24
BP
1957/* Obtains the NetFlow engine type and engine ID for 'ofproto' into
1958 * '*engine_type' and '*engine_id', respectively. */
1959void
1960ofproto_get_netflow_ids(const struct ofproto *ofproto,
1961 uint8_t *engine_type, uint8_t *engine_id)
1962{
abe529af 1963 ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id);
b5827b24
BP
1964}
1965
27d34fce
BP
1966static void
1967query_aggregate_stats(struct ofproto *ofproto, struct cls_rule *target,
734bbeb4 1968 ovs_be16 out_port, uint8_t table_id,
27d34fce
BP
1969 struct ofp_aggregate_stats_reply *oasr)
1970{
5ecc9d81
BP
1971 uint64_t total_packets = 0;
1972 uint64_t total_bytes = 0;
1973 int n_flows = 0;
27d34fce
BP
1974
1975 COVERAGE_INC(ofproto_agg_request);
5ecc9d81 1976
3c4486a5 1977 if (is_valid_table(table_id)) {
5ecc9d81
BP
1978 struct cls_cursor cursor;
1979 struct rule *rule;
3c4486a5 1980
5ecc9d81
BP
1981 cls_cursor_init(&cursor, &ofproto->cls, target);
1982 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
1983 if (!rule_is_hidden(rule) && rule_has_out_port(rule, out_port)) {
1984 uint64_t packet_count;
1985 uint64_t byte_count;
1986
abe529af
BP
1987 ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
1988 &byte_count);
5ecc9d81
BP
1989
1990 total_packets += packet_count;
1991 total_bytes += byte_count;
1992 n_flows++;
1993 }
1994 }
3c4486a5 1995 }
27d34fce 1996
5ecc9d81 1997 oasr->flow_count = htonl(n_flows);
c4617b3c
BP
1998 put_32aligned_be64(&oasr->packet_count, htonll(total_packets));
1999 put_32aligned_be64(&oasr->byte_count, htonll(total_bytes));
27d34fce
BP
2000 memset(oasr->pad, 0, sizeof oasr->pad);
2001}
2002
064af421 2003static int
3269c562 2004handle_aggregate_stats_request(struct ofconn *ofconn,
d1e2cf21 2005 const struct ofp_header *oh)
064af421 2006{
d1e2cf21 2007 const struct ofp_aggregate_stats_request *request = ofputil_stats_body(oh);
64ff1d96 2008 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
064af421 2009 struct ofp_aggregate_stats_reply *reply;
064af421
BP
2010 struct cls_rule target;
2011 struct ofpbuf *msg;
2012
d8ae4d67
BP
2013 ofputil_cls_rule_from_match(&request->match, 0, NXFF_OPENFLOW10, 0,
2014 &target);
064af421 2015
d1e2cf21 2016 msg = start_ofp_stats_reply(oh, sizeof *reply);
06a5e131 2017 reply = append_ofp_stats_reply(sizeof *reply, ofconn, &msg);
64ff1d96 2018 query_aggregate_stats(ofproto, &target, request->out_port,
27d34fce 2019 request->table_id, reply);
b0421aa2 2020 ofconn_send_reply(ofconn, msg);
064af421
BP
2021 return 0;
2022}
2023
09246b99 2024static int
d1e2cf21 2025handle_nxst_aggregate(struct ofconn *ofconn, const struct ofp_header *oh)
09246b99 2026{
64ff1d96 2027 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
09246b99
BP
2028 struct nx_aggregate_stats_request *request;
2029 struct ofp_aggregate_stats_reply *reply;
2030 struct cls_rule target;
d1e2cf21 2031 struct ofpbuf b;
09246b99
BP
2032 struct ofpbuf *buf;
2033 int error;
2034
0bc9407d 2035 ofpbuf_use_const(&b, oh, ntohs(oh->length));
d1e2cf21 2036
09246b99 2037 /* Dissect the message. */
bbc32a88 2038 request = ofpbuf_pull(&b, sizeof *request);
d1e2cf21 2039 error = nx_pull_match(&b, ntohs(request->match_len), 0, &target);
09246b99
BP
2040 if (error) {
2041 return error;
2042 }
d1e2cf21
BP
2043 if (b.size) {
2044 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2045 }
09246b99
BP
2046
2047 /* Reply. */
2048 COVERAGE_INC(ofproto_flows_req);
2049 buf = start_nxstats_reply(&request->nsm, sizeof *reply);
2050 reply = ofpbuf_put_uninit(buf, sizeof *reply);
64ff1d96 2051 query_aggregate_stats(ofproto, &target, request->out_port,
09246b99 2052 request->table_id, reply);
b0421aa2 2053 ofconn_send_reply(ofconn, buf);
09246b99
BP
2054
2055 return 0;
2056}
2057
c1c9c9c4
BP
2058struct queue_stats_cbdata {
2059 struct ofconn *ofconn;
ca0f572c 2060 struct ofport *ofport;
c1c9c9c4 2061 struct ofpbuf *msg;
c1c9c9c4
BP
2062};
2063
2064static void
db9220c3 2065put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
c1c9c9c4
BP
2066 const struct netdev_queue_stats *stats)
2067{
2068 struct ofp_queue_stats *reply;
2069
06a5e131 2070 reply = append_ofp_stats_reply(sizeof *reply, cbdata->ofconn, &cbdata->msg);
9cfcdadf 2071 reply->port_no = cbdata->ofport->opp.port_no;
c1c9c9c4
BP
2072 memset(reply->pad, 0, sizeof reply->pad);
2073 reply->queue_id = htonl(queue_id);
c4617b3c
BP
2074 put_32aligned_be64(&reply->tx_bytes, htonll(stats->tx_bytes));
2075 put_32aligned_be64(&reply->tx_packets, htonll(stats->tx_packets));
2076 put_32aligned_be64(&reply->tx_errors, htonll(stats->tx_errors));
c1c9c9c4
BP
2077}
2078
2079static void
db9220c3 2080handle_queue_stats_dump_cb(uint32_t queue_id,
c1c9c9c4
BP
2081 struct netdev_queue_stats *stats,
2082 void *cbdata_)
2083{
2084 struct queue_stats_cbdata *cbdata = cbdata_;
2085
2086 put_queue_stats(cbdata, queue_id, stats);
2087}
2088
2089static void
ca0f572c 2090handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
c1c9c9c4
BP
2091 struct queue_stats_cbdata *cbdata)
2092{
ca0f572c 2093 cbdata->ofport = port;
c1c9c9c4
BP
2094 if (queue_id == OFPQ_ALL) {
2095 netdev_dump_queue_stats(port->netdev,
2096 handle_queue_stats_dump_cb, cbdata);
2097 } else {
2098 struct netdev_queue_stats stats;
2099
1ac788f6
BP
2100 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
2101 put_queue_stats(cbdata, queue_id, &stats);
2102 }
c1c9c9c4
BP
2103 }
2104}
2105
2106static int
d1e2cf21 2107handle_queue_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
c1c9c9c4 2108{
64ff1d96 2109 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
d1e2cf21 2110 const struct ofp_queue_stats_request *qsr;
c1c9c9c4
BP
2111 struct queue_stats_cbdata cbdata;
2112 struct ofport *port;
2113 unsigned int port_no;
2114 uint32_t queue_id;
2115
d1e2cf21
BP
2116 qsr = ofputil_stats_body(oh);
2117 if (!qsr) {
c1c9c9c4
BP
2118 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2119 }
c1c9c9c4
BP
2120
2121 COVERAGE_INC(ofproto_queue_req);
2122
2123 cbdata.ofconn = ofconn;
d1e2cf21 2124 cbdata.msg = start_ofp_stats_reply(oh, 128);
c1c9c9c4
BP
2125
2126 port_no = ntohs(qsr->port_no);
2127 queue_id = ntohl(qsr->queue_id);
2128 if (port_no == OFPP_ALL) {
4e8e4213 2129 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
ca0f572c 2130 handle_queue_stats_for_port(port, queue_id, &cbdata);
c1c9c9c4 2131 }
abe529af
BP
2132 } else if (port_no < OFPP_MAX) {
2133 port = ofproto_get_port(ofproto, port_no);
c1c9c9c4 2134 if (port) {
ca0f572c 2135 handle_queue_stats_for_port(port, queue_id, &cbdata);
c1c9c9c4
BP
2136 }
2137 } else {
2138 ofpbuf_delete(cbdata.msg);
2139 return ofp_mkerr(OFPET_QUEUE_OP_FAILED, OFPQOFC_BAD_PORT);
2140 }
b0421aa2 2141 ofconn_send_reply(ofconn, cbdata.msg);
c1c9c9c4
BP
2142
2143 return 0;
2144}
2145
79eee1eb
BP
2146/* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
2147 * in which no matching flow already exists in the flow table.
2148 *
2149 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
64ff1d96 2150 * ofp_actions, to the ofproto's flow table. Returns 0 on success or an
3269c562 2151 * OpenFlow error code as encoded by ofp_mkerr() on failure.
79eee1eb
BP
2152 *
2153 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
2154 * if any. */
064af421 2155static int
3052b0c5 2156add_flow(struct ofconn *ofconn, struct flow_mod *fm)
064af421 2157{
64ff1d96 2158 struct ofproto *p = ofconn_get_ofproto(ofconn);
064af421
BP
2159 struct ofpbuf *packet;
2160 struct rule *rule;
2161 uint16_t in_port;
abe529af 2162 int buf_err;
064af421
BP
2163 int error;
2164
3052b0c5
BP
2165 if (fm->flags & OFPFF_CHECK_OVERLAP
2166 && classifier_rule_overlaps(&p->cls, &fm->cr)) {
2167 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP);
49bdc010 2168 }
abe529af
BP
2169
2170 buf_err = ofconn_pktbuf_retrieve(ofconn, fm->buffer_id, &packet, &in_port);
2171 error = rule_create(p, &fm->cr, fm->actions, fm->n_actions,
2172 fm->idle_timeout, fm->hard_timeout, fm->cookie,
2173 fm->flags & OFPFF_SEND_FLOW_REM, &rule);
2174 if (error) {
2175 ofpbuf_delete(packet);
2176 return error;
064af421
BP
2177 }
2178
afe75089 2179 if (packet) {
5bf0e941
BP
2180 assert(!buf_err);
2181 return rule_execute(rule, in_port, packet);
afe75089 2182 }
abe529af 2183 return buf_err;
064af421
BP
2184}
2185
79eee1eb 2186static struct rule *
3052b0c5 2187find_flow_strict(struct ofproto *p, const struct flow_mod *fm)
064af421 2188{
3052b0c5 2189 return rule_from_cls_rule(classifier_find_rule_exactly(&p->cls, &fm->cr));
79eee1eb 2190}
064af421 2191
79eee1eb 2192static int
3269c562 2193send_buffered_packet(struct ofconn *ofconn,
3052b0c5 2194 struct rule *rule, uint32_t buffer_id)
79eee1eb
BP
2195{
2196 struct ofpbuf *packet;
2197 uint16_t in_port;
79eee1eb 2198 int error;
064af421 2199
3052b0c5 2200 if (buffer_id == UINT32_MAX) {
79eee1eb 2201 return 0;
064af421 2202 }
79eee1eb 2203
10d0a1d2 2204 error = ofconn_pktbuf_retrieve(ofconn, buffer_id, &packet, &in_port);
79eee1eb
BP
2205 if (error) {
2206 return error;
2207 }
2208
5bf0e941 2209 return rule_execute(rule, in_port, packet);
064af421 2210}
79eee1eb
BP
2211\f
2212/* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
064af421
BP
2213
2214struct modify_flows_cbdata {
2215 struct ofproto *ofproto;
3052b0c5 2216 const struct flow_mod *fm;
79eee1eb 2217 struct rule *match;
064af421
BP
2218};
2219
abe529af 2220static int modify_flow(const struct flow_mod *, struct rule *);
79eee1eb
BP
2221
2222/* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code as
2223 * encoded by ofp_mkerr() on failure.
2224 *
2225 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
2226 * if any. */
2227static int
3052b0c5 2228modify_flows_loose(struct ofconn *ofconn, struct flow_mod *fm)
79eee1eb 2229{
64ff1d96 2230 struct ofproto *p = ofconn_get_ofproto(ofconn);
5ecc9d81
BP
2231 struct rule *match = NULL;
2232 struct cls_cursor cursor;
2233 struct rule *rule;
abe529af 2234 int error;
79eee1eb 2235
abe529af 2236 error = 0;
5ecc9d81
BP
2237 cls_cursor_init(&cursor, &p->cls, &fm->cr);
2238 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2239 if (!rule_is_hidden(rule)) {
abe529af
BP
2240 int retval = modify_flow(fm, rule);
2241 if (!retval) {
2242 match = rule;
2243 } else {
2244 error = retval;
2245 }
5ecc9d81
BP
2246 }
2247 }
79eee1eb 2248
abe529af
BP
2249 if (error) {
2250 return error;
2251 } else if (match) {
d6302b0f
BP
2252 /* This credits the packet to whichever flow happened to match last.
2253 * That's weird. Maybe we should do a lookup for the flow that
2254 * actually matches the packet? Who knows. */
5ecc9d81 2255 send_buffered_packet(ofconn, match, fm->buffer_id);
79eee1eb
BP
2256 return 0;
2257 } else {
3052b0c5 2258 return add_flow(ofconn, fm);
79eee1eb
BP
2259 }
2260}
2261
2262/* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
2263 * code as encoded by ofp_mkerr() on failure.
2264 *
2265 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
2266 * if any. */
2267static int
3052b0c5 2268modify_flow_strict(struct ofconn *ofconn, struct flow_mod *fm)
79eee1eb 2269{
64ff1d96 2270 struct ofproto *p = ofconn_get_ofproto(ofconn);
3052b0c5 2271 struct rule *rule = find_flow_strict(p, fm);
79eee1eb 2272 if (rule && !rule_is_hidden(rule)) {
abe529af
BP
2273 int error = modify_flow(fm, rule);
2274 if (!error) {
2275 error = send_buffered_packet(ofconn, rule, fm->buffer_id);
2276 }
2277 return error;
79eee1eb 2278 } else {
3052b0c5 2279 return add_flow(ofconn, fm);
79eee1eb
BP
2280 }
2281}
2282
79eee1eb 2283/* Implements core of OFPFC_MODIFY and OFPFC_MODIFY_STRICT where 'rule' has
abe529af
BP
2284 * been identified as a flow to be modified, by changing the rule's actions to
2285 * match those in 'ofm' (which is followed by 'n_actions' ofp_action[]
2286 * structures). */
064af421 2287static int
abe529af 2288modify_flow(const struct flow_mod *fm, struct rule *rule)
064af421 2289{
3052b0c5 2290 size_t actions_len = fm->n_actions * sizeof *rule->actions;
abe529af 2291 int error;
79eee1eb 2292
3052b0c5
BP
2293 if (fm->n_actions == rule->n_actions
2294 && (!fm->n_actions
2295 || !memcmp(fm->actions, rule->actions, actions_len))) {
abe529af
BP
2296 error = 0;
2297 } else {
2298 error = rule->ofproto->ofproto_class->rule_modify_actions(
2299 rule, fm->actions, fm->n_actions);
2300 if (!error) {
2301 free(rule->actions);
2302 rule->actions = (fm->n_actions
2303 ? xmemdup(fm->actions, actions_len)
2304 : NULL);
2305 rule->n_actions = fm->n_actions;
2306 }
79eee1eb
BP
2307 }
2308
abe529af
BP
2309 if (!error) {
2310 rule->flow_cookie = fm->cookie;
2311 }
79eee1eb 2312
abe529af 2313 return error;
79eee1eb
BP
2314}
2315\f
2316/* OFPFC_DELETE implementation. */
2317
abe529af 2318static void delete_flow(struct rule *, ovs_be16 out_port);
79eee1eb
BP
2319
2320/* Implements OFPFC_DELETE. */
2321static void
3052b0c5 2322delete_flows_loose(struct ofproto *p, const struct flow_mod *fm)
79eee1eb 2323{
5ecc9d81
BP
2324 struct rule *rule, *next_rule;
2325 struct cls_cursor cursor;
064af421 2326
5ecc9d81
BP
2327 cls_cursor_init(&cursor, &p->cls, &fm->cr);
2328 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
abe529af 2329 delete_flow(rule, htons(fm->out_port));
5ecc9d81 2330 }
064af421
BP
2331}
2332
79eee1eb
BP
2333/* Implements OFPFC_DELETE_STRICT. */
2334static void
3052b0c5 2335delete_flow_strict(struct ofproto *p, struct flow_mod *fm)
79eee1eb 2336{
3052b0c5 2337 struct rule *rule = find_flow_strict(p, fm);
79eee1eb 2338 if (rule) {
abe529af 2339 delete_flow(rule, htons(fm->out_port));
79eee1eb
BP
2340 }
2341}
2342
79eee1eb
BP
2343/* Implements core of OFPFC_DELETE and OFPFC_DELETE_STRICT where 'rule' has
2344 * been identified as a flow to delete from 'p''s flow table, by deleting the
2345 * flow and sending out a OFPT_FLOW_REMOVED message to any interested
2346 * controller.
2347 *
2348 * Will not delete 'rule' if it is hidden. Will delete 'rule' only if
2349 * 'out_port' is htons(OFPP_NONE) or if 'rule' actually outputs to the
2350 * specified 'out_port'. */
2351static void
abe529af 2352delete_flow(struct rule *rule, ovs_be16 out_port)
79eee1eb
BP
2353{
2354 if (rule_is_hidden(rule)) {
2355 return;
2356 }
2357
2358 if (out_port != htons(OFPP_NONE) && !rule_has_out_port(rule, out_port)) {
2359 return;
2360 }
2361
abe529af
BP
2362 ofproto_rule_send_removed(rule, OFPRR_DELETE);
2363 ofproto_rule_remove(rule);
2364}
2365
2366static void
2367ofproto_rule_send_removed(struct rule *rule, uint8_t reason)
2368{
2369 struct ofputil_flow_removed fr;
2370
2371 if (rule_is_hidden(rule) || !rule->send_flow_removed) {
2372 return;
2373 }
2374
2375 fr.rule = rule->cr;
2376 fr.cookie = rule->flow_cookie;
2377 fr.reason = reason;
2378 calc_flow_duration__(rule->created, &fr.duration_sec, &fr.duration_nsec);
2379 fr.idle_timeout = rule->idle_timeout;
2380 rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
2381 &fr.byte_count);
2382
2383 connmgr_send_flow_removed(rule->ofproto->connmgr, &fr);
2384}
2385
2386/* Sends an OpenFlow "flow removed" message with the given 'reason' (either
2387 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
2388 * ofproto.
2389 *
2390 * ofproto implementation ->run() functions should use this function to expire
2391 * OpenFlow flows. */
2392void
2393ofproto_rule_expire(struct rule *rule, uint8_t reason)
2394{
2395 assert(reason == OFPRR_HARD_TIMEOUT || reason == OFPRR_IDLE_TIMEOUT);
2396 ofproto_rule_send_removed(rule, reason);
2397 ofproto_rule_remove(rule);
79eee1eb
BP
2398}
2399\f
064af421 2400static int
2e4f5fcf 2401handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 2402{
64ff1d96 2403 struct ofproto *p = ofconn_get_ofproto(ofconn);
2e4f5fcf 2404 struct flow_mod fm;
064af421
BP
2405 int error;
2406
3052b0c5 2407 error = reject_slave_controller(ofconn, "flow_mod");
9deba63b
BP
2408 if (error) {
2409 return error;
2410 }
3052b0c5 2411
f1dd9191 2412 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_flow_format(ofconn));
064af421
BP
2413 if (error) {
2414 return error;
2415 }
2416
2e4f5fcf
BP
2417 /* We do not support the emergency flow cache. It will hopefully get
2418 * dropped from OpenFlow in the near future. */
2419 if (fm.flags & OFPFF_EMERG) {
49bdc010
JP
2420 /* There isn't a good fit for an error code, so just state that the
2421 * flow table is full. */
2422 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL);
2423 }
2424
2e4f5fcf 2425 switch (fm.command) {
3052b0c5 2426 case OFPFC_ADD:
2e4f5fcf 2427 return add_flow(ofconn, &fm);
3052b0c5
BP
2428
2429 case OFPFC_MODIFY:
2e4f5fcf 2430 return modify_flows_loose(ofconn, &fm);
3052b0c5
BP
2431
2432 case OFPFC_MODIFY_STRICT:
2e4f5fcf 2433 return modify_flow_strict(ofconn, &fm);
3052b0c5
BP
2434
2435 case OFPFC_DELETE:
2e4f5fcf 2436 delete_flows_loose(p, &fm);
3052b0c5
BP
2437 return 0;
2438
2439 case OFPFC_DELETE_STRICT:
2e4f5fcf 2440 delete_flow_strict(p, &fm);
3052b0c5
BP
2441 return 0;
2442
2443 default:
2444 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND);
2445 }
2446}
2447
659586ef 2448static int
d1e2cf21 2449handle_tun_id_from_cookie(struct ofconn *ofconn, const struct ofp_header *oh)
659586ef 2450{
d1e2cf21
BP
2451 const struct nxt_tun_id_cookie *msg
2452 = (const struct nxt_tun_id_cookie *) oh;
f1dd9191
BP
2453 enum nx_flow_format flow_format;
2454
2455 flow_format = msg->set ? NXFF_TUN_ID_FROM_COOKIE : NXFF_OPENFLOW10;
2456 ofconn_set_flow_format(ofconn, flow_format);
659586ef 2457
659586ef
JG
2458 return 0;
2459}
2460
9deba63b 2461static int
d1e2cf21 2462handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
9deba63b 2463{
d1e2cf21 2464 struct nx_role_request *nrr = (struct nx_role_request *) oh;
9deba63b
BP
2465 struct nx_role_request *reply;
2466 struct ofpbuf *buf;
2467 uint32_t role;
2468
1ce0a5fa 2469 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY) {
19a87e36 2470 VLOG_WARN_RL(&rl, "ignoring role request on service connection");
9deba63b
BP
2471 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
2472 }
2473
2474 role = ntohl(nrr->role);
2475 if (role != NX_ROLE_OTHER && role != NX_ROLE_MASTER
2476 && role != NX_ROLE_SLAVE) {
2477 VLOG_WARN_RL(&rl, "received request for unknown role %"PRIu32, role);
2478
2479 /* There's no good error code for this. */
2480 return ofp_mkerr(OFPET_BAD_REQUEST, -1);
2481 }
2482
1ce0a5fa 2483 ofconn_set_role(ofconn, role);
9deba63b 2484
d1e2cf21 2485 reply = make_nxmsg_xid(sizeof *reply, NXT_ROLE_REPLY, oh->xid, &buf);
9deba63b 2486 reply->role = htonl(role);
b0421aa2 2487 ofconn_send_reply(ofconn, buf);
9deba63b
BP
2488
2489 return 0;
2490}
2491
09246b99 2492static int
d1e2cf21 2493handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
09246b99 2494{
d1e2cf21
BP
2495 const struct nxt_set_flow_format *msg
2496 = (const struct nxt_set_flow_format *) oh;
09246b99 2497 uint32_t format;
09246b99
BP
2498
2499 format = ntohl(msg->format);
2500 if (format == NXFF_OPENFLOW10
2501 || format == NXFF_TUN_ID_FROM_COOKIE
2502 || format == NXFF_NXM) {
f1dd9191 2503 ofconn_set_flow_format(ofconn, format);
09246b99
BP
2504 return 0;
2505 } else {
2506 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
2507 }
2508}
2509
064af421 2510static int
d1e2cf21 2511handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
246e61ea
JP
2512{
2513 struct ofp_header *ob;
2514 struct ofpbuf *buf;
2515
2516 /* Currently, everything executes synchronously, so we can just
2517 * immediately send the barrier reply. */
2518 ob = make_openflow_xid(sizeof *ob, OFPT_BARRIER_REPLY, oh->xid, &buf);
b0421aa2 2519 ofconn_send_reply(ofconn, buf);
246e61ea
JP
2520 return 0;
2521}
2522
d1e2cf21
BP
2523static int
2524handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
064af421 2525{
d1e2cf21
BP
2526 const struct ofp_header *oh = msg->data;
2527 const struct ofputil_msg_type *type;
064af421
BP
2528 int error;
2529
d1e2cf21
BP
2530 error = ofputil_decode_msg_type(oh, &type);
2531 if (error) {
2532 return error;
2533 }
064af421 2534
d1e2cf21
BP
2535 switch (ofputil_msg_type_code(type)) {
2536 /* OpenFlow requests. */
2537 case OFPUTIL_OFPT_ECHO_REQUEST:
2538 return handle_echo_request(ofconn, oh);
064af421 2539
d1e2cf21
BP
2540 case OFPUTIL_OFPT_FEATURES_REQUEST:
2541 return handle_features_request(ofconn, oh);
064af421 2542
d1e2cf21
BP
2543 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
2544 return handle_get_config_request(ofconn, oh);
064af421 2545
d1e2cf21
BP
2546 case OFPUTIL_OFPT_SET_CONFIG:
2547 return handle_set_config(ofconn, msg->data);
064af421 2548
d1e2cf21
BP
2549 case OFPUTIL_OFPT_PACKET_OUT:
2550 return handle_packet_out(ofconn, oh);
064af421 2551
d1e2cf21
BP
2552 case OFPUTIL_OFPT_PORT_MOD:
2553 return handle_port_mod(ofconn, oh);
064af421 2554
d1e2cf21 2555 case OFPUTIL_OFPT_FLOW_MOD:
2e4f5fcf 2556 return handle_flow_mod(ofconn, oh);
064af421 2557
d1e2cf21
BP
2558 case OFPUTIL_OFPT_BARRIER_REQUEST:
2559 return handle_barrier_request(ofconn, oh);
064af421 2560
d1e2cf21
BP
2561 /* OpenFlow replies. */
2562 case OFPUTIL_OFPT_ECHO_REPLY:
2563 return 0;
246e61ea 2564
d1e2cf21 2565 /* Nicira extension requests. */
d1e2cf21
BP
2566 case OFPUTIL_NXT_TUN_ID_FROM_COOKIE:
2567 return handle_tun_id_from_cookie(ofconn, oh);
2568
2569 case OFPUTIL_NXT_ROLE_REQUEST:
2570 return handle_role_request(ofconn, oh);
2571
2572 case OFPUTIL_NXT_SET_FLOW_FORMAT:
2573 return handle_nxt_set_flow_format(ofconn, oh);
2574
2575 case OFPUTIL_NXT_FLOW_MOD:
2e4f5fcf 2576 return handle_flow_mod(ofconn, oh);
d1e2cf21
BP
2577
2578 /* OpenFlow statistics requests. */
2579 case OFPUTIL_OFPST_DESC_REQUEST:
2580 return handle_desc_stats_request(ofconn, oh);
2581
2582 case OFPUTIL_OFPST_FLOW_REQUEST:
2583 return handle_flow_stats_request(ofconn, oh);
2584
2585 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
2586 return handle_aggregate_stats_request(ofconn, oh);
2587
2588 case OFPUTIL_OFPST_TABLE_REQUEST:
2589 return handle_table_stats_request(ofconn, oh);
2590
2591 case OFPUTIL_OFPST_PORT_REQUEST:
2592 return handle_port_stats_request(ofconn, oh);
2593
2594 case OFPUTIL_OFPST_QUEUE_REQUEST:
2595 return handle_queue_stats_request(ofconn, oh);
2596
2597 /* Nicira extension statistics requests. */
2598 case OFPUTIL_NXST_FLOW_REQUEST:
2599 return handle_nxst_flow(ofconn, oh);
2600
2601 case OFPUTIL_NXST_AGGREGATE_REQUEST:
2602 return handle_nxst_aggregate(ofconn, oh);
2603
2604 case OFPUTIL_INVALID:
2605 case OFPUTIL_OFPT_HELLO:
2606 case OFPUTIL_OFPT_ERROR:
2607 case OFPUTIL_OFPT_FEATURES_REPLY:
2608 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
2609 case OFPUTIL_OFPT_PACKET_IN:
2610 case OFPUTIL_OFPT_FLOW_REMOVED:
2611 case OFPUTIL_OFPT_PORT_STATUS:
2612 case OFPUTIL_OFPT_BARRIER_REPLY:
2613 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
2614 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
2615 case OFPUTIL_OFPST_DESC_REPLY:
2616 case OFPUTIL_OFPST_FLOW_REPLY:
2617 case OFPUTIL_OFPST_QUEUE_REPLY:
2618 case OFPUTIL_OFPST_PORT_REPLY:
2619 case OFPUTIL_OFPST_TABLE_REPLY:
2620 case OFPUTIL_OFPST_AGGREGATE_REPLY:
d1e2cf21
BP
2621 case OFPUTIL_NXT_ROLE_REPLY:
2622 case OFPUTIL_NXT_FLOW_REMOVED:
2623 case OFPUTIL_NXST_FLOW_REPLY:
2624 case OFPUTIL_NXST_AGGREGATE_REPLY:
064af421
BP
2625 default:
2626 if (VLOG_IS_WARN_ENABLED()) {
2627 char *s = ofp_to_string(oh, ntohs(oh->length), 2);
2628 VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s);
2629 free(s);
2630 }
d1e2cf21
BP
2631 if (oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY) {
2632 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT);
2633 } else {
2634 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
2635 }
064af421 2636 }
d1e2cf21 2637}
064af421 2638
d1e2cf21
BP
2639static void
2640handle_openflow(struct ofconn *ofconn, struct ofpbuf *ofp_msg)
2641{
2642 int error = handle_openflow__(ofconn, ofp_msg);
064af421
BP
2643 if (error) {
2644 send_error_oh(ofconn, ofp_msg->data, error);
2645 }
d1e2cf21 2646 COVERAGE_INC(ofproto_recv_openflow);
064af421
BP
2647}
2648\f
064af421 2649static uint64_t
fa60c019 2650pick_datapath_id(const struct ofproto *ofproto)
064af421 2651{
fa60c019 2652 const struct ofport *port;
064af421 2653
abe529af 2654 port = ofproto_get_port(ofproto, OFPP_LOCAL);
fa60c019
BP
2655 if (port) {
2656 uint8_t ea[ETH_ADDR_LEN];
2657 int error;
2658
2659 error = netdev_get_etheraddr(port->netdev, ea);
064af421
BP
2660 if (!error) {
2661 return eth_addr_to_uint64(ea);
2662 }
2663 VLOG_WARN("could not get MAC address for %s (%s)",
fa60c019 2664 netdev_get_name(port->netdev), strerror(error));
064af421 2665 }
fa60c019 2666 return ofproto->fallback_dpid;
064af421
BP
2667}
2668
2669static uint64_t
2670pick_fallback_dpid(void)
2671{
2672 uint8_t ea[ETH_ADDR_LEN];
70150daf 2673 eth_addr_nicira_random(ea);
064af421
BP
2674 return eth_addr_to_uint64(ea);
2675}
2676\f
abe529af
BP
2677/* unixctl commands. */
2678
2679struct ofproto *
2ac6bedd
BP
2680ofproto_lookup(const char *name)
2681{
2682 struct ofproto *ofproto;
2683
2684 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
2685 &all_ofprotos) {
2686 if (!strcmp(ofproto->name, name)) {
2687 return ofproto;
2688 }
2689 }
2690 return NULL;
2691}
2692
7aa697dd
BP
2693static void
2694ofproto_unixctl_list(struct unixctl_conn *conn, const char *arg OVS_UNUSED,
2695 void *aux OVS_UNUSED)
2696{
2ac6bedd 2697 struct ofproto *ofproto;
7aa697dd
BP
2698 struct ds results;
2699
2700 ds_init(&results);
2ac6bedd
BP
2701 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
2702 ds_put_format(&results, "%s\n", ofproto->name);
7aa697dd
BP
2703 }
2704 unixctl_command_reply(conn, 200, ds_cstr(&results));
2705 ds_destroy(&results);
2706}
2707
7aa697dd
BP
2708static void
2709ofproto_unixctl_init(void)
2710{
2711 static bool registered;
2712 if (registered) {
2713 return;
2714 }
2715 registered = true;
2716
2717 unixctl_command_register("ofproto/list", ofproto_unixctl_list, NULL);
7aa697dd 2718}