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