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