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