ofproto: Rename "private.h" to "ofproto->provider.h".
[sliver-openvswitch.git] / ofproto / ofproto-provider.h
1 /*
2  * Copyright (c) 2009, 2010, 2011 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef OFPROTO_OFPROTO_PROVIDER_H
18 #define OFPROTO_OFPROTO_PROVIDER_H 1
19
20 /* Definitions for use within ofproto. */
21
22 #include "ofproto/ofproto.h"
23 #include "classifier.h"
24 #include "list.h"
25 #include "shash.h"
26 #include "timeval.h"
27
28 /* An OpenFlow switch.
29  *
30  * With few exceptions, ofproto implementations may look at these fields but
31  * should not modify them. */
32 struct ofproto {
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. */
37
38     /* Settings. */
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. */
46
47     /* Datapath. */
48     struct hmap ports;          /* Contains "struct ofport"s. */
49     struct shash port_by_name;
50
51     /* Flow tables. */
52     struct classifier *tables;  /* Each classifier contains "struct rule"s. */
53     int n_tables;
54
55     /* OpenFlow connections. */
56     struct connmgr *connmgr;
57
58     /* Flow table operation tracking. */
59     int state;                  /* Internal state. */
60     struct list pending;        /* List of "struct ofopgroup"s. */
61     struct hmap deletions;      /* All OFOPERATION_DELETE "ofoperation"s. */
62 };
63
64 struct ofproto *ofproto_lookup(const char *name);
65 struct ofport *ofproto_get_port(const struct ofproto *, uint16_t ofp_port);
66
67 /* An OpenFlow port within a "struct ofproto".
68  *
69  * With few exceptions, ofproto implementations may look at these fields but
70  * should not modify them. */
71 struct ofport {
72     struct ofproto *ofproto;    /* The ofproto that contains this port. */
73     struct hmap_node hmap_node; /* In struct ofproto's "ports" hmap. */
74     struct netdev *netdev;
75     struct ofp_phy_port opp;
76     uint16_t ofp_port;          /* OpenFlow port number. */
77     unsigned int change_seq;
78 };
79
80 /* An OpenFlow flow within a "struct ofproto".
81  *
82  * With few exceptions, ofproto implementations may look at these fields but
83  * should not modify them. */
84 struct rule {
85     struct ofproto *ofproto;     /* The ofproto that contains this rule. */
86     struct list ofproto_node;    /* Owned by ofproto base code. */
87     struct cls_rule cr;          /* In owning ofproto's classifier. */
88
89     struct ofoperation *pending; /* Operation now in progress, if nonnull. */
90
91     ovs_be64 flow_cookie;        /* Controller-issued identifier. */
92
93     long long int created;       /* Creation time. */
94     uint16_t idle_timeout;       /* In seconds from time of last use. */
95     uint16_t hard_timeout;       /* In seconds from time of creation. */
96     uint8_t table_id;            /* Index in ofproto's 'tables' array. */
97     bool send_flow_removed;      /* Send a flow removed message? */
98
99     union ofp_action *actions;   /* OpenFlow actions. */
100     int n_actions;               /* Number of elements in actions[]. */
101 };
102
103 static inline struct rule *
104 rule_from_cls_rule(const struct cls_rule *cls_rule)
105 {
106     return cls_rule ? CONTAINER_OF(cls_rule, struct rule, cr) : NULL;
107 }
108
109 void ofproto_rule_expire(struct rule *, uint8_t reason);
110 void ofproto_rule_destroy(struct rule *);
111
112 void ofoperation_complete(struct ofoperation *, int status);
113 struct rule *ofoperation_get_victim(struct ofoperation *);
114
115 /* ofproto class structure, to be defined by each ofproto implementation.
116  *
117  *
118  * Data Structures
119  * ===============
120  *
121  * These functions work primarily with three different kinds of data
122  * structures:
123  *
124  *   - "struct ofproto", which represents an OpenFlow switch.
125  *
126  *   - "struct ofport", which represents a port within an ofproto.
127  *
128  *   - "struct rule", which represents an OpenFlow flow within an ofproto.
129  *
130  * Each of these data structures contains all of the implementation-independent
131  * generic state for the respective concept, called the "base" state.  None of
132  * them contains any extra space for ofproto implementations to use.  Instead,
133  * each implementation is expected to declare its own data structure that
134  * contains an instance of the generic data structure plus additional
135  * implementation-specific members, called the "derived" state.  The
136  * implementation can use casts or (preferably) the CONTAINER_OF macro to
137  * obtain access to derived state given only a pointer to the embedded generic
138  * data structure.
139  *
140  *
141  * Life Cycle
142  * ==========
143  *
144  * Four stylized functions accompany each of these data structures:
145  *
146  *            "alloc"       "construct"       "destruct"       "dealloc"
147  *            ------------  ----------------  ---------------  --------------
148  *   ofproto  ->alloc       ->construct       ->destruct       ->dealloc
149  *   ofport   ->port_alloc  ->port_construct  ->port_destruct  ->port_dealloc
150  *   rule     ->rule_alloc  ->rule_construct  ->rule_destruct  ->rule_dealloc
151  *
152  * Any instance of a given data structure goes through the following life
153  * cycle:
154  *
155  *   1. The client calls the "alloc" function to obtain raw memory.  If "alloc"
156  *      fails, skip all the other steps.
157  *
158  *   2. The client initializes all of the data structure's base state.  If this
159  *      fails, skip to step 7.
160  *
161  *   3. The client calls the "construct" function.  The implementation
162  *      initializes derived state.  It may refer to the already-initialized
163  *      base state.  If "construct" fails, skip to step 6.
164  *
165  *   4. The data structure is now initialized and in use.
166  *
167  *   5. When the data structure is no longer needed, the client calls the
168  *      "destruct" function.  The implementation uninitializes derived state.
169  *      The base state has not been uninitialized yet, so the implementation
170  *      may still refer to it.
171  *
172  *   6. The client uninitializes all of the data structure's base state.
173  *
174  *   7. The client calls the "dealloc" to free the raw memory.  The
175  *      implementation must not refer to base or derived state in the data
176  *      structure, because it has already been uninitialized.
177  *
178  * Each "alloc" function allocates and returns a new instance of the respective
179  * data structure.  The "alloc" function is not given any information about the
180  * use of the new data structure, so it cannot perform much initialization.
181  * Its purpose is just to ensure that the new data structure has enough room
182  * for base and derived state.  It may return a null pointer if memory is not
183  * available, in which case none of the other functions is called.
184  *
185  * Each "construct" function initializes derived state in its respective data
186  * structure.  When "construct" is called, all of the base state has already
187  * been initialized, so the "construct" function may refer to it.  The
188  * "construct" function is allowed to fail, in which case the client calls the
189  * "dealloc" function (but not the "destruct" function).
190  *
191  * Each "destruct" function uninitializes and frees derived state in its
192  * respective data structure.  When "destruct" is called, the base state has
193  * not yet been uninitialized, so the "destruct" function may refer to it.  The
194  * "destruct" function is not allowed to fail.
195  *
196  * Each "dealloc" function frees raw memory that was allocated by the the
197  * "alloc" function.  The memory's base and derived members might not have ever
198  * been initialized (but if "construct" returned successfully, then it has been
199  * "destruct"ed already).  The "dealloc" function is not allowed to fail.
200  *
201  *
202  * Conventions
203  * ===========
204  *
205  * Most of these functions return 0 if they are successful or a positive error
206  * code on failure.  Depending on the function, valid error codes are either
207  * errno values or OpenFlow error codes constructed with ofp_mkerr().
208  *
209  * Most of these functions are expected to execute synchronously, that is, to
210  * block as necessary to obtain a result.  Thus, these functions may return
211  * EAGAIN (or EWOULDBLOCK or EINPROGRESS) only where the function descriptions
212  * explicitly say those errors are a possibility.  We may relax this
213  * requirement in the future if and when we encounter performance problems. */
214 struct ofproto_class {
215 /* ## ----------------- ## */
216 /* ## Factory Functions ## */
217 /* ## ----------------- ## */
218
219     /* Enumerates the types of all support ofproto types into 'types'.  The
220      * caller has already initialized 'types' and other ofproto classes might
221      * already have added names to it. */
222     void (*enumerate_types)(struct sset *types);
223
224     /* Enumerates the names of all existing datapath of the specified 'type'
225      * into 'names' 'all_dps'.  The caller has already initialized 'names' as
226      * an empty sset.
227      *
228      * 'type' is one of the types enumerated by ->enumerate_types().
229      *
230      * Returns 0 if successful, otherwise a positive errno value.
231      */
232     int (*enumerate_names)(const char *type, struct sset *names);
233
234     /* Deletes the datapath with the specified 'type' and 'name'.  The caller
235      * should have closed any open ofproto with this 'type' and 'name'; this
236      * function is allowed to fail if that is not the case.
237      *
238      * 'type' is one of the types enumerated by ->enumerate_types().
239      * 'name' is one of the names enumerated by ->enumerate_names() for 'type'.
240      *
241      * Returns 0 if successful, otherwise a positive errno value.
242      */
243     int (*del)(const char *type, const char *name);
244
245 /* ## --------------------------- ## */
246 /* ## Top-Level ofproto Functions ## */
247 /* ## --------------------------- ## */
248
249     /* Life-cycle functions for an "ofproto" (see "Life Cycle" above).
250      *
251      *
252      * Construction
253      * ============
254      *
255      * ->construct() should not modify most base members of the ofproto.  In
256      * particular, the client will initialize the ofproto's 'ports' member
257      * after construction is complete.
258      *
259      * ->construct() should initialize the base 'n_tables' member to the number
260      * of flow tables supported by the datapath (between 1 and 255, inclusive),
261      * initialize the base 'tables' member with space for one classifier per
262      * table, and initialize each classifier with classifier_init.  Each flow
263      * table should be initially empty, so ->construct() should delete flows
264      * from the underlying datapath, if necessary, rather than populating the
265      * tables.
266      *
267      * Only one ofproto instance needs to be supported for any given datapath.
268      * If a datapath is already open as part of one "ofproto", then another
269      * attempt to "construct" the same datapath as part of another ofproto is
270      * allowed to fail with an error.
271      *
272      * ->construct() returns 0 if successful, otherwise a positive errno
273      * value.
274      *
275      *
276      * Destruction
277      * ===========
278      *
279      * ->destruct() must do at least the following:
280      *
281      *   - If 'ofproto' has any pending asynchronous operations, ->destruct()
282      *     must complete all of them by calling ofoperation_complete().
283      *
284      *   - If 'ofproto' has any rules left in any of its flow tables, ->
285      */
286     struct ofproto *(*alloc)(void);
287     int (*construct)(struct ofproto *ofproto);
288     void (*destruct)(struct ofproto *ofproto);
289     void (*dealloc)(struct ofproto *ofproto);
290
291     /* Performs any periodic activity required by 'ofproto'.  It should:
292      *
293      *   - Call connmgr_send_packet_in() for each received packet that missed
294      *     in the OpenFlow flow table or that had a OFPP_CONTROLLER output
295      *     action.
296      *
297      *   - Call ofproto_rule_expire() for each OpenFlow flow that has reached
298      *     its hard_timeout or idle_timeout, to expire the flow.
299      *
300      * Returns 0 if successful, otherwise a positive errno value.  The ENODEV
301      * return value specifically means that the datapath underlying 'ofproto'
302      * has been destroyed (externally, e.g. by an admin running ovs-dpctl).
303      */
304     int (*run)(struct ofproto *ofproto);
305
306     /* Causes the poll loop to wake up when 'ofproto''s 'run' function needs to
307      * be called, e.g. by calling the timer or fd waiting functions in
308      * poll-loop.h.  */
309     void (*wait)(struct ofproto *ofproto);
310
311     /* Every "struct rule" in 'ofproto' is about to be deleted, one by one.
312      * This function may prepare for that, for example by clearing state in
313      * advance.  It should *not* actually delete any "struct rule"s from
314      * 'ofproto', only prepare for it.
315      *
316      * This function is optional; it's really just for optimization in case
317      * it's cheaper to delete all the flows from your hardware in a single pass
318      * than to do it one by one. */
319     void (*flush)(struct ofproto *ofproto);
320
321     /* Helper for the OpenFlow OFPT_FEATURES_REQUEST request.
322      *
323      * The implementation should store true in '*arp_match_ip' if the switch
324      * supports matching IP addresses inside ARP requests and replies, false
325      * otherwise.
326      *
327      * The implementation should store in '*actions' a bitmap of the supported
328      * OpenFlow actions: the bit with value (1 << n) should be set to 1 if the
329      * implementation supports the action with value 'n', and to 0 otherwise.
330      * For example, if the implementation supports the OFPAT_OUTPUT and
331      * OFPAT_ENQUEUE actions, but no others, it would set '*actions' to (1 <<
332      * OFPAT_OUTPUT) | (1 << OFPAT_ENQUEUE).  Vendor actions are not included
333      * in '*actions'. */
334     void (*get_features)(struct ofproto *ofproto,
335                          bool *arp_match_ip, uint32_t *actions);
336
337     /* Helper for the OpenFlow OFPST_TABLE statistics request.
338      *
339      * The 'ots' array contains 'ofproto->n_tables' elements.  Each element is
340      * initialized as:
341      *
342      *   - 'table_id' to the array index.
343      *
344      *   - 'name' to "table#" where # is the table ID.
345      *
346      *   - 'wildcards' to OFPFW_ALL.
347      *
348      *   - 'max_entries' to 1,000,000.
349      *
350      *   - 'active_count' to the classifier_count() for the table.
351      *
352      *   - 'lookup_count' and 'matched_count' to 0.
353      *
354      * The implementation should update any members in each element for which
355      * it has better values:
356      *
357      *   - 'name' to a more meaningful name.
358      *
359      *   - 'wildcards' to the set of wildcards actually supported by the table
360      *     (if it doesn't support all OpenFlow wildcards).
361      *
362      *   - 'max_entries' to the maximum number of flows actually supported by
363      *     the hardware.
364      *
365      *   - 'lookup_count' to the number of packets looked up in this flow table
366      *     so far.
367      *
368      *   - 'matched_count' to the number of packets looked up in this flow
369      *     table so far that matched one of the flow entries.
370      *
371      * Keep in mind that all of the members of struct ofp_table_stats are in
372      * network byte order.
373      */
374     void (*get_tables)(struct ofproto *ofproto, struct ofp_table_stats *ots);
375
376 /* ## ---------------- ## */
377 /* ## ofport Functions ## */
378 /* ## ---------------- ## */
379
380     /* Life-cycle functions for a "struct ofport" (see "Life Cycle" above).
381      *
382      * ->port_construct() should not modify any base members of the ofport.
383      *
384      * ofports are managed by the base ofproto code.  The ofproto
385      * implementation should only create and destroy them in response to calls
386      * to these functions.  The base ofproto code will create and destroy
387      * ofports in the following situations:
388      *
389      *   - Just after the ->construct() function is called, the base ofproto
390      *     iterates over all of the implementation's ports, using
391      *     ->port_dump_start() and related functions, and constructs an ofport
392      *     for each dumped port.
393      *
394      *   - If ->port_poll() reports that a specific port has changed, then the
395      *     base ofproto will query that port with ->port_query_by_name() and
396      *     construct or destruct ofports as necessary to reflect the updated
397      *     set of ports.
398      *
399      *   - If ->port_poll() returns ENOBUFS to report an unspecified port set
400      *     change, then the base ofproto will iterate over all of the
401      *     implementation's ports, in the same way as at ofproto
402      *     initialization, and construct and destruct ofports to reflect all of
403      *     the changes.
404      *
405      * ->port_construct() returns 0 if successful, otherwise a positive errno
406      * value.
407      */
408     struct ofport *(*port_alloc)(void);
409     int (*port_construct)(struct ofport *ofport);
410     void (*port_destruct)(struct ofport *ofport);
411     void (*port_dealloc)(struct ofport *ofport);
412
413     /* Called after 'ofport->netdev' is replaced by a new netdev object.  If
414      * the ofproto implementation uses the ofport's netdev internally, then it
415      * should switch to using the new one.  The old one has been closed.
416      *
417      * An ofproto implementation that doesn't need to do anything in this
418      * function may use a null pointer. */
419     void (*port_modified)(struct ofport *ofport);
420
421     /* Called after an OpenFlow OFPT_PORT_MOD request changes a port's
422      * configuration.  'ofport->opp.config' contains the new configuration.
423      * 'old_config' contains the previous configuration.
424      *
425      * The caller implements OFPPC_PORT_DOWN using netdev functions to turn
426      * NETDEV_UP on and off, so this function doesn't have to do anything for
427      * that bit (and it won't be called if that is the only bit that
428      * changes). */
429     void (*port_reconfigured)(struct ofport *ofport, ovs_be32 old_config);
430
431     /* Looks up a port named 'devname' in 'ofproto'.  On success, initializes
432      * '*port' appropriately.
433      *
434      * The caller owns the data in 'port' and must free it with
435      * ofproto_port_destroy() when it is no longer needed. */
436     int (*port_query_by_name)(const struct ofproto *ofproto,
437                               const char *devname, struct ofproto_port *port);
438
439     /* Attempts to add 'netdev' as a port on 'ofproto'.  Returns 0 if
440      * successful, otherwise a positive errno value.  If successful, sets
441      * '*ofp_portp' to the new port's port number.
442      *
443      * It doesn't matter whether the new port will be returned by a later call
444      * to ->port_poll(); the implementation may do whatever is more
445      * convenient. */
446     int (*port_add)(struct ofproto *ofproto, struct netdev *netdev,
447                     uint16_t *ofp_portp);
448
449     /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.  Returns
450      * 0 if successful, otherwise a positive errno value.
451      *
452      * It doesn't matter whether the new port will be returned by a later call
453      * to ->port_poll(); the implementation may do whatever is more
454      * convenient. */
455     int (*port_del)(struct ofproto *ofproto, uint16_t ofp_port);
456
457     /* Port iteration functions.
458      *
459      * The client might not be entirely in control of the ports within an
460      * ofproto.  Some hardware implementations, for example, might have a fixed
461      * set of ports in a datapath, and the Linux datapath allows the system
462      * administrator to externally add and remove ports with ovs-dpctl.  For
463      * this reason, the client needs a way to iterate through all the ports
464      * that are actually in a datapath.  These functions provide that
465      * functionality.
466      *
467      * The 'state' pointer provides the implementation a place to
468      * keep track of its position.  Its format is opaque to the caller.
469      *
470      * The ofproto provider retains ownership of the data that it stores into
471      * ->port_dump_next()'s 'port' argument.  The data must remain valid until
472      * at least the next call to ->port_dump_next() or ->port_dump_done() for
473      * 'state'.  The caller will not modify or free it.
474      *
475      * Details
476      * =======
477      *
478      * ->port_dump_start() attempts to begin dumping the ports in 'ofproto'.
479      * On success, it should return 0 and initialize '*statep' with any data
480      * needed for iteration.  On failure, returns a positive errno value, and
481      * the client will not call ->port_dump_next() or ->port_dump_done().
482      *
483      * ->port_dump_next() attempts to retrieve another port from 'ofproto' for
484      * 'state'.  If there is another port, it should store the port's
485      * information into 'port' and return 0.  It should return EOF if all ports
486      * have already been iterated.  Otherwise, on error, it should return a
487      * positive errno value.  This function will not be called again once it
488      * returns nonzero once for a given iteration (but the 'port_dump_done'
489      * function will be called afterward).
490      *
491      * ->port_dump_done() allows the implementation to release resources used
492      * for iteration.  The caller might decide to stop iteration in the middle
493      * by calling this function before ->port_dump_next() returns nonzero.
494      *
495      * Usage Example
496      * =============
497      *
498      * int error;
499      * void *state;
500      *
501      * error = ofproto->ofproto_class->port_dump_start(ofproto, &state);
502      * if (!error) {
503      *     for (;;) {
504      *         struct ofproto_port port;
505      *
506      *         error = ofproto->ofproto_class->port_dump_next(
507      *                     ofproto, state, &port);
508      *         if (error) {
509      *             break;
510      *         }
511      *         // Do something with 'port' here (without modifying or freeing
512      *         // any of its data).
513      *     }
514      *     ofproto->ofproto_class->port_dump_done(ofproto, state);
515      * }
516      * // 'error' is now EOF (success) or a positive errno value (failure).
517      */
518     int (*port_dump_start)(const struct ofproto *ofproto, void **statep);
519     int (*port_dump_next)(const struct ofproto *ofproto, void *state,
520                           struct ofproto_port *port);
521     int (*port_dump_done)(const struct ofproto *ofproto, void *state);
522
523     /* Polls for changes in the set of ports in 'ofproto'.  If the set of ports
524      * in 'ofproto' has changed, then this function should do one of the
525      * following:
526      *
527      * - Preferably: store the name of the device that was added to or deleted
528      *   from 'ofproto' in '*devnamep' and return 0.  The caller is responsible
529      *   for freeing '*devnamep' (with free()) when it no longer needs it.
530      *
531      * - Alternatively: return ENOBUFS, without indicating the device that was
532      *   added or deleted.
533      *
534      * Occasional 'false positives', in which the function returns 0 while
535      * indicating a device that was not actually added or deleted or returns
536      * ENOBUFS without any change, are acceptable.
537      *
538      * The purpose of 'port_poll' is to let 'ofproto' know about changes made
539      * externally to the 'ofproto' object, e.g. by a system administrator via
540      * ovs-dpctl.  Therefore, it's OK, and even preferable, for port_poll() to
541      * not report changes made through calls to 'port_add' or 'port_del' on the
542      * same 'ofproto' object.  (But it's OK for it to report them too, just
543      * slightly less efficient.)
544      *
545      * If the set of ports in 'ofproto' has not changed, returns EAGAIN.  May
546      * also return other positive errno values to indicate that something has
547      * gone wrong.
548      *
549      * If the set of ports in a datapath is fixed, or if the only way that the
550      * set of ports in a datapath can change is through ->port_add() and
551      * ->port_del(), then this function may be a null pointer.
552      */
553     int (*port_poll)(const struct ofproto *ofproto, char **devnamep);
554
555     /* Arranges for the poll loop to wake up when ->port_poll() will return a
556      * value other than EAGAIN.
557      *
558      * If the set of ports in a datapath is fixed, or if the only way that the
559      * set of ports in a datapath can change is through ->port_add() and
560      * ->port_del(), or if the poll loop will always wake up anyway when
561      * ->port_poll() will return a value other than EAGAIN, then this function
562      * may be a null pointer.
563      */
564     void (*port_poll_wait)(const struct ofproto *ofproto);
565
566     /* Checks the status of LACP negotiation for 'port'.  Returns 1 if LACP
567      * partner information for 'port' is up-to-date, 0 if LACP partner
568      * information is not current (generally indicating a connectivity
569      * problem), or -1 if LACP is not enabled on 'port'.
570      *
571      * This function may be a null pointer if the ofproto implementation does
572      * not support LACP. */
573     int (*port_is_lacp_current)(const struct ofport *port);
574
575 /* ## ----------------------- ## */
576 /* ## OpenFlow Rule Functions ## */
577 /* ## ----------------------- ## */
578
579
580
581     /* Chooses an appropriate table for 'cls_rule' within 'ofproto'.  On
582      * success, stores the table ID into '*table_idp' and returns 0.  On
583      * failure, returns an OpenFlow error code (as returned by ofp_mkerr()).
584      *
585      * The choice of table should be a function of 'cls_rule' and 'ofproto''s
586      * datapath capabilities.  It should not depend on the flows already in
587      * 'ofproto''s flow tables.  Failure implies that an OpenFlow rule with
588      * 'cls_rule' as its matching condition can never be inserted into
589      * 'ofproto', even starting from an empty flow table.
590      *
591      * If multiple tables are candidates for inserting the flow, the function
592      * should choose one arbitrarily (but deterministically).
593      *
594      * This function will never be called for an ofproto that has only one
595      * table, so it may be NULL in that case. */
596     int (*rule_choose_table)(const struct ofproto *ofproto,
597                              const struct cls_rule *cls_rule,
598                              uint8_t *table_idp);
599
600     /* Life-cycle functions for a "struct rule" (see "Life Cycle" above).
601      *
602      *
603      * Asynchronous Operation Support
604      * ==============================
605      *
606      * The life-cycle operations on rules can operate asynchronously, meaning
607      * that ->rule_construct() and ->rule_destruct() only need to initiate
608      * their respective operations and do not need to wait for them to complete
609      * before they return.  ->rule_modify_actions() also operates
610      * asynchronously.
611      *
612      * An ofproto implementation reports the success or failure of an
613      * asynchronous operation on a rule using the rule's 'pending' member,
614      * which points to a opaque "struct ofoperation" that represents the
615      * ongoing opreation.  When the operation completes, the ofproto
616      * implementation calls ofoperation_complete(), passing the ofoperation and
617      * an error indication.
618      *
619      * Only the following contexts may call ofoperation_complete():
620      *
621      *   - The function called to initiate the operation,
622      *     e.g. ->rule_construct() or ->rule_destruct().  This is the best
623      *     choice if the operation completes quickly.
624      *
625      *   - The implementation's ->run() function.
626      *
627      *   - The implementation's ->destruct() function.
628      *
629      * The ofproto base code updates the flow table optimistically, assuming
630      * that the operation will probably succeed:
631      *
632      *   - ofproto adds or replaces the rule in the flow table before calling
633      *     ->rule_construct().
634      *
635      *   - ofproto updates the rule's actions before calling
636      *     ->rule_modify_actions().
637      *
638      *   - ofproto removes the rule before calling ->rule_destruct().
639      *
640      * With one exception, when an asynchronous operation completes with an
641      * error, ofoperation_complete() backs out the already applied changes:
642      *
643      *   - If adding or replacing a rule in the flow table fails, ofproto
644      *     removes the new rule or restores the original rule.
645      *
646      *   - If modifying a rule's actions fails, ofproto restores the original
647      *     actions.
648      *
649      *   - Removing a rule is not allowed to fail.  It must always succeed.
650      *
651      * The ofproto base code serializes operations: if any operation is in
652      * progress on a given rule, ofproto postpones initiating any new operation
653      * on that rule until the pending operation completes.  Therefore, every
654      * operation must eventually complete through a call to
655      * ofoperation_complete() to avoid delaying new operations indefinitely
656      * (including any OpenFlow request that affects the rule in question, even
657      * just to query its statistics).
658      *
659      *
660      * Construction
661      * ============
662      *
663      * When ->rule_construct() is called, the caller has already inserted
664      * 'rule' into 'rule->ofproto''s flow table numbered 'rule->table_id'.
665      * There are two cases:
666      *
667      *   - 'rule' is a new rule in its flow table.  In this case,
668      *     ofoperation_get_victim(rule) returns NULL.
669      *
670      *   - 'rule' is replacing an existing rule in its flow table that had the
671      *     same matching criteria and priority.  In this case,
672      *     ofoperation_get_victim(rule) returns the rule being replaced.
673      *
674      * ->rule_construct() should set the following in motion:
675      *
676      *   - Validate that the matching rule in 'rule->cr' is supported by the
677      *     datapath.  For example, if the rule's table does not support
678      *     registers, then it is an error if 'rule->cr' does not wildcard all
679      *     registers.
680      *
681      *   - Validate that 'rule->actions' and 'rule->n_actions' are well-formed
682      *     OpenFlow actions that the datapath can correctly implement.  The
683      *     validate_actions() function (in ofp-util.c) can be useful as a model
684      *     for action validation, but it accepts all of the OpenFlow actions
685      *     that OVS understands.  If your ofproto implementation only
686      *     implements a subset of those, then you should implement your own
687      *     action validation.
688      *
689      *   - If the rule is valid, update the datapath flow table, adding the new
690      *     rule or replacing the existing one.
691      *
692      * (On failure, the ofproto code will roll back the insertion from the flow
693      * table, either removing 'rule' or replacing it by the flow that was
694      * originally in its place.)
695      *
696      * ->rule_construct() must act in one of the following ways:
697      *
698      *   - If it succeeds, it must call ofoperation_complete() and return 0.
699      *
700      *   - If it fails, it must act in one of the following ways:
701      *
702      *       * Call ofoperation_complete() and return 0.
703      *
704      *       * Return an OpenFlow error code (as returned by ofp_mkerr()).  (Do
705      *         not call ofoperation_complete() in this case.)
706      *
707      *     In the former case, ->rule_destruct() will be called; in the latter
708      *     case, it will not.  ->rule_dealloc() will be called in either case.
709      *
710      *   - If the operation is only partially complete, then it must return 0.
711      *     Later, when the operation is complete, the ->run() or ->destruct()
712      *     function must call ofoperation_complete() to report success or
713      *     failure.
714      *
715      * ->rule_construct() should not modify any base members of struct rule.
716      *
717      *
718      * Destruction
719      * ===========
720      *
721      * When ->rule_destruct() is called, the caller has already removed 'rule'
722      * from 'rule->ofproto''s flow table.  ->rule_destruct() should set in
723      * motion removing 'rule' from the datapath flow table.  If removal
724      * completes synchronously, it should call ofoperation_complete().
725      * Otherwise, the ->run() or ->destruct() function must later call
726      * ofoperation_complete() after the operation completes.
727      *
728      * Rule destruction must not fail. */
729     struct rule *(*rule_alloc)(void);
730     int (*rule_construct)(struct rule *rule);
731     void (*rule_destruct)(struct rule *rule);
732     void (*rule_dealloc)(struct rule *rule);
733
734     /* Obtains statistics for 'rule', storing the number of packets that have
735      * matched it in '*packet_count' and the number of bytes in those packets
736      * in '*byte_count'.  UINT64_MAX indicates that the packet count or byte
737      * count is unknown. */
738     void (*rule_get_stats)(struct rule *rule, uint64_t *packet_count,
739                            uint64_t *byte_count);
740
741     /* Applies the actions in 'rule' to 'packet'.  (This implements sending
742      * buffered packets for OpenFlow OFPT_FLOW_MOD commands.)
743      *
744      * Takes ownership of 'packet' (so it should eventually free it, with
745      * ofpbuf_delete()).
746      *
747      * 'flow' reflects the flow information for 'packet'.  All of the
748      * information in 'flow' is extracted from 'packet', except for
749      * flow->tun_id and flow->in_port, which are assigned the correct values
750      * for the incoming packet.  The register values are zeroed.
751      *
752      * The statistics for 'packet' should be included in 'rule'.
753      *
754      * Returns 0 if successful, otherwise an OpenFlow error code (as returned
755      * by ofp_mkerr()). */
756     int (*rule_execute)(struct rule *rule, struct flow *flow,
757                         struct ofpbuf *packet);
758
759     /* When ->rule_modify_actions() is called, the caller has already replaced
760      * the OpenFlow actions in 'rule' by a new set.  (The original actions are
761      * in rule->pending->actions.)
762      *
763      * ->rule_modify_actions() should set the following in motion:
764      *
765      *   - Validate that the actions now in 'rule' are well-formed OpenFlow
766      *     actions that the datapath can correctly implement.
767      *
768      *   - Update the datapath flow table with the new actions.
769      *
770      * If the operation synchronously completes, ->rule_modify_actions() may
771      * call ofoperation_complete() before it returns.  Otherwise, ->run()
772      * should call ofoperation_complete() later, after the operation does
773      * complete.
774      *
775      * If the operation fails, then the base ofproto code will restore the
776      * original 'actions' and 'n_actions' of 'rule'.
777      *
778      * ->rule_modify_actions() should not modify any base members of struct
779      * rule. */
780     void (*rule_modify_actions)(struct rule *rule);
781
782     /* These functions implement the OpenFlow IP fragment handling policy.  By
783      * default ('drop_frags' == false), an OpenFlow switch should treat IP
784      * fragments the same way as other packets (although TCP and UDP port
785      * numbers cannot be determined).  With 'drop_frags' == true, the switch
786      * should drop all IP fragments without passing them through the flow
787      * table. */
788     bool (*get_drop_frags)(struct ofproto *ofproto);
789     void (*set_drop_frags)(struct ofproto *ofproto, bool drop_frags);
790
791     /* Implements the OpenFlow OFPT_PACKET_OUT command.  The datapath should
792      * execute the 'n_actions' in the 'actions' array on 'packet'.
793      *
794      * The caller retains ownership of 'packet', so ->packet_out() should not
795      * modify or free it.
796      *
797      * This function must validate that the 'n_actions' elements in 'actions'
798      * are well-formed OpenFlow actions that can be correctly implemented by
799      * the datapath.  If not, then it should return an OpenFlow error code (as
800      * returned by ofp_mkerr()).
801      *
802      * 'flow' reflects the flow information for 'packet'.  All of the
803      * information in 'flow' is extracted from 'packet', except for
804      * flow->in_port, which is taken from the OFPT_PACKET_OUT message.
805      * flow->tun_id and its register values are zeroed.
806      *
807      * 'packet' is not matched against the OpenFlow flow table, so its
808      * statistics should not be included in OpenFlow flow statistics.
809      *
810      * Returns 0 if successful, otherwise an OpenFlow error code (as returned
811      * by ofp_mkerr()). */
812     int (*packet_out)(struct ofproto *ofproto, struct ofpbuf *packet,
813                       const struct flow *flow,
814                       const union ofp_action *actions,
815                       size_t n_actions);
816
817 /* ## ------------------------- ## */
818 /* ## OFPP_NORMAL configuration ## */
819 /* ## ------------------------- ## */
820
821     /* Configures NetFlow on 'ofproto' according to the options in
822      * 'netflow_options', or turns off NetFlow if 'netflow_options' is NULL.
823      *
824      * EOPNOTSUPP as a return value indicates that 'ofproto' does not support
825      * NetFlow, as does a null pointer. */
826     int (*set_netflow)(struct ofproto *ofproto,
827                        const struct netflow_options *netflow_options);
828
829     void (*get_netflow_ids)(const struct ofproto *ofproto,
830                             uint8_t *engine_type, uint8_t *engine_id);
831
832     /* Configures sFlow on 'ofproto' according to the options in
833      * 'sflow_options', or turns off sFlow if 'sflow_options' is NULL.
834      *
835      * EOPNOTSUPP as a return value indicates that 'ofproto' does not support
836      * sFlow, as does a null pointer. */
837     int (*set_sflow)(struct ofproto *ofproto,
838                      const struct ofproto_sflow_options *sflow_options);
839
840     /* Configures connectivity fault management on 'ofport'.
841      *
842      * If 'cfm_settings' is nonnull, configures CFM according to its members.
843      *
844      * If 'cfm_settings' is null, removes any connectivity fault management
845      * configuration from 'ofport'.
846      *
847      * EOPNOTSUPP as a return value indicates that this ofproto_class does not
848      * support CFM, as does a null pointer. */
849     int (*set_cfm)(struct ofport *ofport, const struct cfm_settings *s);
850
851     /* Checks the fault status of CFM configured on 'ofport'.  Returns 1 if CFM
852      * is faulted (generally indicating a connectivity problem), 0 if CFM is
853      * not faulted, or -1 if CFM is not enabled on 'port'
854      *
855      * This function may be a null pointer if the ofproto implementation does
856      * not support CFM. */
857     int (*get_cfm_fault)(const struct ofport *ofport);
858
859     /* If 's' is nonnull, this function registers a "bundle" associated with
860      * client data pointer 'aux' in 'ofproto'.  A bundle is the same concept as
861      * a Port in OVSDB, that is, it consists of one or more "slave" devices
862      * (Interfaces, in OVSDB) along with VLAN and LACP configuration and, if
863      * there is more than one slave, a bonding configuration.  If 'aux' is
864      * already registered then this function updates its configuration to 's'.
865      * Otherwise, this function registers a new bundle.
866      *
867      * If 's' is NULL, this function unregisters the bundle registered on
868      * 'ofproto' associated with client data pointer 'aux'.  If no such bundle
869      * has been registered, this has no effect.
870      *
871      * This function affects only the behavior of the NXAST_AUTOPATH action and
872      * output to the OFPP_NORMAL port.  An implementation that does not support
873      * it at all may set it to NULL or return EOPNOTSUPP.  An implementation
874      * that supports only a subset of the functionality should implement what
875      * it can and return 0. */
876     int (*bundle_set)(struct ofproto *ofproto, void *aux,
877                       const struct ofproto_bundle_settings *s);
878
879     /* If 'port' is part of any bundle, removes it from that bundle.  If the
880      * bundle now has no ports, deletes the bundle.  If the bundle now has only
881      * one port, deconfigures the bundle's bonding configuration. */
882     void (*bundle_remove)(struct ofport *ofport);
883
884     /* If 's' is nonnull, this function registers a mirror associated with
885      * client data pointer 'aux' in 'ofproto'.  A mirror is the same concept as
886      * a Mirror in OVSDB.  If 'aux' is already registered then this function
887      * updates its configuration to 's'.  Otherwise, this function registers a
888      * new mirror.
889      *
890      * If 's' is NULL, this function unregisters the mirror registered on
891      * 'ofproto' associated with client data pointer 'aux'.  If no such mirror
892      * has been registered, this has no effect.
893      *
894      * This function affects only the behavior of the OFPP_NORMAL action.  An
895      * implementation that does not support it at all may set it to NULL or
896      * return EOPNOTSUPP.  An implementation that supports only a subset of the
897      * functionality should implement what it can and return 0. */
898     int (*mirror_set)(struct ofproto *ofproto, void *aux,
899                       const struct ofproto_mirror_settings *s);
900
901     /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs
902      * on which all packets are flooded, instead of using MAC learning.  If
903      * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
904      *
905      * This function affects only the behavior of the OFPP_NORMAL action.  An
906      * implementation that does not support it may set it to NULL or return
907      * EOPNOTSUPP. */
908     int (*set_flood_vlans)(struct ofproto *ofproto,
909                            unsigned long *flood_vlans);
910
911     /* Returns true if 'aux' is a registered bundle that is currently in use as
912      * the output for a mirror. */
913     bool (*is_mirror_output_bundle)(struct ofproto *ofproto, void *aux);
914 };
915
916 extern const struct ofproto_class ofproto_dpif_class;
917
918 int ofproto_class_register(const struct ofproto_class *);
919 int ofproto_class_unregister(const struct ofproto_class *);
920
921 void ofproto_add_flow(struct ofproto *, const struct cls_rule *,
922                       const union ofp_action *, size_t n_actions);
923 bool ofproto_delete_flow(struct ofproto *, const struct cls_rule *);
924 void ofproto_flush_flows(struct ofproto *);
925
926 #endif /* ofproto/ofproto-provider.h */