2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef OFPROTO_PRIVATE_H
18 #define OFPROTO_PRIVATE_H 1
20 /* Definitions for use within ofproto. */
22 #include "ofproto/ofproto.h"
23 #include "classifier.h"
28 /* An OpenFlow switch.
30 * With few exceptions, ofproto implementations may look at these fields but
31 * should not modify them. */
33 const struct ofproto_class *ofproto_class;
34 char *type; /* Datapath type. */
35 char *name; /* Datapath name. */
36 struct hmap_node hmap_node; /* In global 'all_ofprotos' hmap. */
39 uint64_t fallback_dpid; /* Datapath ID if no better choice found. */
40 uint64_t datapath_id; /* Datapath ID. */
41 char *mfr_desc; /* Manufacturer. */
42 char *hw_desc; /* Hardware. */
43 char *sw_desc; /* Software version. */
44 char *serial_desc; /* Serial number. */
45 char *dp_desc; /* Datapath description. */
48 struct netdev_monitor *netdev_monitor;
49 struct hmap ports; /* Contains "struct ofport"s. */
50 struct shash port_by_name;
53 struct classifier cls; /* Contains "struct rule"s. */
55 /* OpenFlow connections. */
56 struct connmgr *connmgr;
59 struct ofproto *ofproto_lookup(const char *name);
60 struct ofport *ofproto_get_port(const struct ofproto *, uint16_t ofp_port);
62 /* An OpenFlow port within a "struct ofproto".
64 * With few exceptions, ofproto implementations may look at these fields but
65 * should not modify them. */
67 struct ofproto *ofproto; /* The ofproto that contains this port. */
68 struct hmap_node hmap_node; /* In struct ofproto's "ports" hmap. */
69 struct netdev *netdev;
70 struct ofp_phy_port opp;
71 uint16_t ofp_port; /* OpenFlow port number. */
74 /* An OpenFlow flow within a "struct ofproto".
76 * With few exceptions, ofproto implementations may look at these fields but
77 * should not modify them. */
79 struct ofproto *ofproto; /* The ofproto that contains this rule. */
80 struct cls_rule cr; /* In owning ofproto's classifier. */
82 ovs_be64 flow_cookie; /* Controller-issued identifier. */
84 long long int created; /* Creation time. */
85 uint16_t idle_timeout; /* In seconds from time of last use. */
86 uint16_t hard_timeout; /* In seconds from time of creation. */
87 bool send_flow_removed; /* Send a flow removed message? */
89 union ofp_action *actions; /* OpenFlow actions. */
90 int n_actions; /* Number of elements in actions[]. */
93 static inline struct rule *
94 rule_from_cls_rule(const struct cls_rule *cls_rule)
96 return cls_rule ? CONTAINER_OF(cls_rule, struct rule, cr) : NULL;
99 struct rule *ofproto_rule_lookup(struct ofproto *, const struct flow *);
100 void ofproto_rule_expire(struct rule *, uint8_t reason);
101 void ofproto_rule_destroy(struct rule *);
103 /* ofproto class structure, to be defined by each ofproto implementation.
109 * These functions work primarily with three different kinds of data
112 * - "struct ofproto", which represents an OpenFlow switch.
114 * - "struct ofport", which represents a port within an ofproto.
116 * - "struct rule", which represents an OpenFlow flow within an ofproto.
118 * Each of these data structures contains all of the implementation-independent
119 * generic state for the respective concept, called the "base" state. None of
120 * them contains any extra space for ofproto implementations to use. Instead,
121 * each implementation is expected to declare its own data structure that
122 * contains an instance of the generic data structure plus additional
123 * implementation-specific members, called the "derived" state. The
124 * implementation can use casts or (preferably) the CONTAINER_OF macro to
125 * obtain access to derived state given only a pointer to the embedded generic
132 * Four stylized functions accompany each of these data structures:
134 * "alloc" "construct" "destruct" "dealloc"
135 * ------------ ---------------- --------------- --------------
136 * ofproto ->alloc ->construct ->destruct ->dealloc
137 * ofport ->port_alloc ->port_construct ->port_destruct ->port_dealloc
138 * rule ->rule_alloc ->rule_construct ->rule_destruct ->rule_dealloc
140 * Any instance of a given data structure goes through the following life
143 * 1. The client calls the "alloc" function to obtain raw memory. If "alloc"
144 * fails, skip all the other steps.
146 * 2. The client initializes all of the data structure's base state. If this
147 * fails, skip to step 7.
149 * 3. The client calls the "construct" function. The implementation
150 * initializes derived state. It may refer to the already-initialized
151 * base state. If "construct" fails, skip to step 6.
153 * 4. The data structure is now initialized and in use.
155 * 5. When the data structure is no longer needed, the client calls the
156 * "destruct" function. The implementation uninitializes derived state.
157 * The base state has not been uninitialized yet, so the implementation
158 * may still refer to it.
160 * 6. The client uninitializes all of the data structure's base state.
162 * 7. The client calls the "dealloc" to free the raw memory. The
163 * implementation must not refer to base or derived state in the data
164 * structure, because it has already been uninitialized.
166 * Each "alloc" function allocates and returns a new instance of the respective
167 * data structure. The "alloc" function is not given any information about the
168 * use of the new data structure, so it cannot perform much initialization.
169 * Its purpose is just to ensure that the new data structure has enough room
170 * for base and derived state. It may return a null pointer if memory is not
171 * available, in which case none of the other functions is called.
173 * Each "construct" function initializes derived state in its respective data
174 * structure. When "construct" is called, all of the base state has already
175 * been initialized, so the "construct" function may refer to it. The
176 * "construct" function is allowed to fail, in which case the client calls the
177 * "dealloc" function (but not the "destruct" function).
179 * Each "destruct" function uninitializes and frees derived state in its
180 * respective data structure. When "destruct" is called, the base state has
181 * not yet been uninitialized, so the "destruct" function may refer to it. The
182 * "destruct" function is not allowed to fail.
184 * Each "dealloc" function frees raw memory that was allocated by the the
185 * "alloc" function. The memory's base and derived members might not have ever
186 * been initialized (but if "construct" returned successfully, then it has been
187 * "destruct"ed already). The "dealloc" function is not allowed to fail.
193 * Most of these functions return 0 if they are successful or a positive error
194 * code on failure. Depending on the function, valid error codes are either
195 * errno values or OpenFlow error codes constructed with ofp_mkerr().
197 * Most of these functions are expected to execute synchronously, that is, to
198 * block as necessary to obtain a result. Thus, these functions may return
199 * EAGAIN (or EWOULDBLOCK or EINPROGRESS) only where the function descriptions
200 * explicitly say those errors are a possibility. We may relax this
201 * requirement in the future if and when we encounter performance problems. */
202 struct ofproto_class {
203 /* ## ----------------- ## */
204 /* ## Factory Functions ## */
205 /* ## ----------------- ## */
207 void (*enumerate_types)(struct sset *types);
208 int (*enumerate_names)(const char *type, struct sset *names);
209 int (*del)(const char *type, const char *name);
211 /* ## --------------------------- ## */
212 /* ## Top-Level ofproto Functions ## */
213 /* ## --------------------------- ## */
215 /* Life-cycle functions for an "ofproto" (see "Life Cycle" above).
217 * ->construct() should not modify any base members of the ofproto, even
218 * though it may be tempting in a few cases. In particular, the client
219 * will initialize the ofproto's 'ports' member after construction is
220 * complete. An ofproto's flow table should be initially empty, so
221 * ->construct() should delete flows from the underlying datapath, if
222 * necessary, rather than populating the ofproto's 'cls'.
224 * Only one ofproto instance needs to be supported for any given datapath.
225 * If a datapath is already open as part of one "ofproto", then another
226 * attempt to "construct" the same datapath as part of another ofproto is
227 * allowed to fail with an error. */
228 struct ofproto *(*alloc)(void);
229 int (*construct)(struct ofproto *ofproto);
230 void (*destruct)(struct ofproto *ofproto);
231 void (*dealloc)(struct ofproto *ofproto);
233 /* Performs any periodic activity required by 'ofproto'. It should:
235 * - Call connmgr_send_packet_in() for each received packet that missed
236 * in the OpenFlow flow table or that had a OFPP_CONTROLLER output
239 * - Call ofproto_rule_expire() for each OpenFlow flow that has reached
240 * its hard_timeout or idle_timeout, to expire the flow.
242 int (*run)(struct ofproto *ofproto);
244 /* Causes the poll loop to wake up when 'ofproto''s 'run' function needs to
245 * be called, e.g. by calling the timer or fd waiting functions in
247 void (*wait)(struct ofproto *ofproto);
249 /* Every "struct rule" in 'ofproto' is about to be deleted, one by one.
250 * This function may prepare for that, for example by clearing state in
251 * advance. It should *not* actually delete any "struct rule"s from
252 * 'ofproto', only prepare for it.
254 * This function is optional; it's really just for optimization in case
255 * it's cheaper to delete all the flows from your hardware in a single pass
256 * than to do it one by one. */
257 void (*flush)(struct ofproto *ofproto);
259 /* ## ---------------- ## */
260 /* ## ofport Functions ## */
261 /* ## ---------------- ## */
263 /* Life-cycle functions for a "struct ofport" (see "Life Cycle" above).
265 * ->port_construct() should not modify any base members of the ofport.
267 * ofports are managed by the base ofproto code. The ofproto
268 * implementation should only create and destroy them in response to calls
269 * to these functions. The base ofproto code will create and destroy
270 * ofports in the following situations:
272 * - Just after the ->construct() function is called, the base ofproto
273 * iterates over all of the implementation's ports, using
274 * ->port_dump_start() and related functions, and constructs an ofport
275 * for each dumped port.
277 * - If ->port_poll() reports that a specific port has changed, then the
278 * base ofproto will query that port with ->port_query_by_name() and
279 * construct or destruct ofports as necessary to reflect the updated
282 * - If ->port_poll() returns ENOBUFS to report an unspecified port set
283 * change, then the base ofproto will iterate over all of the
284 * implementation's ports, in the same way as at ofproto
285 * initialization, and construct and destruct ofports to reflect all of
288 struct ofport *(*port_alloc)(void);
289 int (*port_construct)(struct ofport *ofport);
290 void (*port_destruct)(struct ofport *ofport);
291 void (*port_dealloc)(struct ofport *ofport);
293 /* Called after 'ofport->netdev' is replaced by a new netdev object. If
294 * the ofproto implementation uses the ofport's netdev internally, then it
295 * should switch to using the new one. The old one has been closed.
297 * An ofproto implementation that doesn't need to do anything in this
298 * function may use a null pointer. */
299 void (*port_modified)(struct ofport *ofport);
301 /* Called after an OpenFlow OFPT_PORT_MOD request changes a port's
302 * configuration. 'ofport->opp.config' contains the new configuration.
303 * 'old_config' contains the previous configuration.
305 * The caller implements OFPPC_PORT_DOWN using netdev functions to turn
306 * NETDEV_UP on and off, so this function doesn't have to do anything for
307 * that bit (and it won't be called if that is the only bit that
309 void (*port_reconfigured)(struct ofport *ofport, ovs_be32 old_config);
311 /* Looks up a port named 'devname' in 'ofproto'. On success, initializes
312 * '*port' appropriately.
314 * The caller owns the data in 'port' and must free it with
315 * ofproto_port_destroy() when it is no longer needed. */
316 int (*port_query_by_name)(const struct ofproto *ofproto,
317 const char *devname, struct ofproto_port *port);
319 /* Attempts to add 'netdev' as a port on 'ofproto'. If successful, sets
320 * '*ofp_portp' to the new port's port number. */
321 int (*port_add)(struct ofproto *ofproto, struct netdev *netdev,
322 uint16_t *ofp_portp);
324 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'. */
325 int (*port_del)(struct ofproto *ofproto, uint16_t ofp_port);
327 /* Attempts to begin dumping the ports in 'ofproto'. On success, returns 0
328 * and initializes '*statep' with any data needed for iteration. On
329 * failure, returns a positive errno value. */
330 int (*port_dump_start)(const struct ofproto *ofproto, void **statep);
332 /* Attempts to retrieve another port from 'ofproto' for 'state', which was
333 * initialized by a successful call to the 'port_dump_start' function for
334 * 'ofproto'. On success, stores a new ofproto_port into 'port' and
335 * returns 0. Returns EOF if the end of the port table has been reached,
336 * or a positive errno value on error. This function will not be called
337 * again once it returns nonzero once for a given iteration (but the
338 * 'port_dump_done' function will be called afterward).
340 * The ofproto provider retains ownership of the data stored in 'port'. It
341 * must remain valid until at least the next call to 'port_dump_next' or
342 * 'port_dump_done' for 'state'. */
343 int (*port_dump_next)(const struct ofproto *ofproto, void *state,
344 struct ofproto_port *port);
346 /* Releases resources from 'ofproto' for 'state', which was initialized by
347 * a successful call to the 'port_dump_start' function for 'ofproto'. */
348 int (*port_dump_done)(const struct ofproto *ofproto, void *state);
350 /* Polls for changes in the set of ports in 'ofproto'. If the set of ports
351 * in 'ofproto' has changed, then this function should do one of the
354 * - Preferably: store the name of the device that was added to or deleted
355 * from 'ofproto' in '*devnamep' and return 0. The caller is responsible
356 * for freeing '*devnamep' (with free()) when it no longer needs it.
358 * - Alternatively: return ENOBUFS, without indicating the device that was
361 * Occasional 'false positives', in which the function returns 0 while
362 * indicating a device that was not actually added or deleted or returns
363 * ENOBUFS without any change, are acceptable.
365 * The purpose of 'port_poll' is to let 'ofproto' know about changes made
366 * externally to the 'ofproto' object, e.g. by a system administrator via
367 * ovs-dpctl. Therefore, it's OK, and even preferable, for port_poll() to
368 * not report changes made through calls to 'port_add' or 'port_del' on the
369 * same 'ofproto' object. (But it's OK for it to report them too, just
370 * slightly less efficient.)
372 * If the set of ports in 'ofproto' has not changed, returns EAGAIN. May
373 * also return other positive errno values to indicate that something has
375 int (*port_poll)(const struct ofproto *ofproto, char **devnamep);
377 /* Arranges for the poll loop to wake up when 'port_poll' will return a
378 * value other than EAGAIN. */
379 void (*port_poll_wait)(const struct ofproto *ofproto);
381 int (*port_is_lacp_current)(const struct ofport *port);
383 struct rule *(*rule_alloc)(void);
384 int (*rule_construct)(struct rule *rule);
385 void (*rule_destruct)(struct rule *rule);
386 void (*rule_dealloc)(struct rule *rule);
388 void (*rule_remove)(struct rule *rule);
390 void (*rule_get_stats)(struct rule *rule, uint64_t *packet_count,
391 uint64_t *byte_count);
393 void (*rule_execute)(struct rule *rule, struct flow *flow,
394 struct ofpbuf *packet);
396 int (*rule_modify_actions)(struct rule *rule,
397 const union ofp_action *actions, size_t n);
399 bool (*get_drop_frags)(struct ofproto *ofproto);
400 void (*set_drop_frags)(struct ofproto *ofproto, bool drop_frags);
402 int (*packet_out)(struct ofproto *ofproto, struct ofpbuf *packet,
403 const struct flow *flow,
404 const union ofp_action *actions,
407 /* Configures NetFlow on 'ofproto' according to the options in
408 * 'netflow_options', or turns off NetFlow if 'netflow_options' is NULL.
410 * EOPNOTSUPP as a return value indicates that 'ofproto' does not support
411 * sFlow, as does a null pointer. */
412 int (*set_netflow)(struct ofproto *ofproto,
413 const struct netflow_options *netflow_options);
415 void (*get_netflow_ids)(const struct ofproto *ofproto,
416 uint8_t *engine_type, uint8_t *engine_id);
418 /* Configures sFlow on 'ofproto' according to the options in
419 * 'sflow_options', or turns off sFlow if 'sflow_options' is NULL.
421 * EOPNOTSUPP as a return value indicates that 'ofproto' does not support
422 * sFlow, as does a null pointer. */
423 int (*set_sflow)(struct ofproto *ofproto,
424 const struct ofproto_sflow_options *sflow_options);
426 /* Configures connectivity fault management on 'ofport'.
428 * If 'cfm' is nonnull, takes basic configuration from the configuration
429 * members in 'cfm', and the set of remote maintenance points from the
430 * 'n_remote_mps' elements in 'remote_mps'. Ignores the statistics members
433 * If 'cfm' is null, removes any connectivity fault management
434 * configuration from 'ofport'.
436 * EOPNOTSUPP as a return value indicates that this ofproto_class does not
437 * support CFM, as does a null pointer. */
438 int (*set_cfm)(struct ofport *ofport, const struct cfm *cfm,
439 const uint16_t *remote_mps, size_t n_remote_mps);
441 /* Stores the connectivity fault management object associated with 'ofport'
442 * in '*cfmp'. Stores a null pointer in '*cfmp' if CFM is not configured
443 * on 'ofport'. The caller must not modify or destroy the returned object.
445 * This function may be NULL if this ofproto_class does not support CFM. */
446 int (*get_cfm)(struct ofport *ofport, const struct cfm **cfmp);
448 /* If 's' is nonnull, this function registers a "bundle" associated with
449 * client data pointer 'aux' in 'ofproto'. A bundle is the same concept as
450 * a Port in OVSDB, that is, it consists of one or more "slave" devices
451 * (Interfaces, in OVSDB) along with VLAN and LACP configuration and, if
452 * there is more than one slave, a bonding configuration. If 'aux' is
453 * already registered then this function updates its configuration to 's'.
454 * Otherwise, this function registers a new bundle.
456 * If 's' is NULL, this function unregisters the bundle registered on
457 * 'ofproto' associated with client data pointer 'aux'. If no such bundle
458 * has been registered, this has no effect.
460 * This function affects only the behavior of the NXAST_AUTOPATH action and
461 * output to the OFPP_NORMAL port. An implementation that does not support
462 * it at all may set it to NULL or return EOPNOTSUPP. An implementation
463 * that supports only a subset of the functionality should implement what
464 * it can and return 0. */
465 int (*bundle_set)(struct ofproto *ofproto, void *aux,
466 const struct ofproto_bundle_settings *s);
468 /* If 'port' is part of any bundle, removes it from that bundle. If the
469 * bundle now has no ports, deletes the bundle. If the bundle now has only
470 * one port, deconfigures the bundle's bonding configuration. */
471 void (*bundle_remove)(struct ofport *ofport);
473 /* If 's' is nonnull, this function registers a mirror associated with
474 * client data pointer 'aux' in 'ofproto'. A mirror is the same concept as
475 * a Mirror in OVSDB. If 'aux' is already registered then this function
476 * updates its configuration to 's'. Otherwise, this function registers a
479 * If 's' is NULL, this function unregisters the mirror registered on
480 * 'ofproto' associated with client data pointer 'aux'. If no such mirror
481 * has been registered, this has no effect.
483 * This function affects only the behavior of the OFPP_NORMAL action. An
484 * implementation that does not support it at all may set it to NULL or
485 * return EOPNOTSUPP. An implementation that supports only a subset of the
486 * functionality should implement what it can and return 0. */
487 int (*mirror_set)(struct ofproto *ofproto, void *aux,
488 const struct ofproto_mirror_settings *s);
490 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs
491 * on which all packets are flooded, instead of using MAC learning. If
492 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
494 * This function affects only the behavior of the OFPP_NORMAL action. An
495 * implementation that does not support it may set it to NULL or return
497 int (*set_flood_vlans)(struct ofproto *ofproto,
498 unsigned long *flood_vlans);
500 /* Returns true if 'aux' is a registered bundle that is currently in use as
501 * the output for a mirror. */
502 bool (*is_mirror_output_bundle)(struct ofproto *ofproto, void *aux);
505 extern const struct ofproto_class ofproto_dpif_class;
507 int ofproto_class_register(const struct ofproto_class *);
508 int ofproto_class_unregister(const struct ofproto_class *);
510 void ofproto_add_flow(struct ofproto *, const struct cls_rule *,
511 const union ofp_action *, size_t n_actions);
512 void ofproto_delete_flow(struct ofproto *, const struct cls_rule *);
513 void ofproto_flush_flows(struct ofproto *);
515 #endif /* ofproto/private.h */