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