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