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