iPXE
ring.h
Go to the documentation of this file.
00001 /******************************************************************************
00002  * ring.h
00003  *
00004  * Shared producer-consumer ring macros.
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a copy
00007  * of this software and associated documentation files (the "Software"), to
00008  * deal in the Software without restriction, including without limitation the
00009  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
00010  * sell copies of the Software, and to permit persons to whom the Software is
00011  * furnished to do so, subject to the following conditions:
00012  *
00013  * The above copyright notice and this permission notice shall be included in
00014  * all copies or substantial portions of the Software.
00015  *
00016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00021  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00022  * DEALINGS IN THE SOFTWARE.
00023  *
00024  * Tim Deegan and Andrew Warfield November 2004.
00025  */
00026 
00027 #ifndef __XEN_PUBLIC_IO_RING_H__
00028 #define __XEN_PUBLIC_IO_RING_H__
00029 
00030 FILE_LICENCE ( MIT );
00031 
00032 #include "../xen-compat.h"
00033 
00034 #if __XEN_INTERFACE_VERSION__ < 0x00030208
00035 #define xen_mb()  mb()
00036 #define xen_rmb() rmb()
00037 #define xen_wmb() wmb()
00038 #endif
00039 
00040 typedef unsigned int RING_IDX;
00041 
00042 /* Round a 32-bit unsigned constant down to the nearest power of two. */
00043 #define __RD2(_x)  (((_x) & 0x00000002) ? 0x2                  : ((_x) & 0x1))
00044 #define __RD4(_x)  (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2    : __RD2(_x))
00045 #define __RD8(_x)  (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4    : __RD4(_x))
00046 #define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8    : __RD8(_x))
00047 #define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
00048 
00049 /*
00050  * Calculate size of a shared ring, given the total available space for the
00051  * ring and indexes (_sz), and the name tag of the request/response structure.
00052  * A ring contains as many entries as will fit, rounded down to the nearest
00053  * power of two (so we can mask with (size-1) to loop around).
00054  */
00055 #define __CONST_RING_SIZE(_s, _sz) \
00056     (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \
00057             sizeof(((struct _s##_sring *)0)->ring[0])))
00058 /*
00059  * The same for passing in an actual pointer instead of a name tag.
00060  */
00061 #define __RING_SIZE(_s, _sz) \
00062     (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
00063 
00064 /*
00065  * Macros to make the correct C datatypes for a new kind of ring.
00066  *
00067  * To make a new ring datatype, you need to have two message structures,
00068  * let's say request_t, and response_t already defined.
00069  *
00070  * In a header where you want the ring datatype declared, you then do:
00071  *
00072  *     DEFINE_RING_TYPES(mytag, request_t, response_t);
00073  *
00074  * These expand out to give you a set of types, as you can see below.
00075  * The most important of these are:
00076  *
00077  *     mytag_sring_t      - The shared ring.
00078  *     mytag_front_ring_t - The 'front' half of the ring.
00079  *     mytag_back_ring_t  - The 'back' half of the ring.
00080  *
00081  * To initialize a ring in your code you need to know the location and size
00082  * of the shared memory area (PAGE_SIZE, for instance). To initialise
00083  * the front half:
00084  *
00085  *     mytag_front_ring_t front_ring;
00086  *     SHARED_RING_INIT((mytag_sring_t *)shared_page);
00087  *     FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
00088  *
00089  * Initializing the back follows similarly (note that only the front
00090  * initializes the shared ring):
00091  *
00092  *     mytag_back_ring_t back_ring;
00093  *     BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
00094  */
00095 
00096 #define DEFINE_RING_TYPES(__name, __req_t, __rsp_t)                     \
00097                                                                         \
00098 /* Shared ring entry */                                                 \
00099 union __name##_sring_entry {                                            \
00100     __req_t req;                                                        \
00101     __rsp_t rsp;                                                        \
00102 };                                                                      \
00103                                                                         \
00104 /* Shared ring page */                                                  \
00105 struct __name##_sring {                                                 \
00106     RING_IDX req_prod, req_event;                                       \
00107     RING_IDX rsp_prod, rsp_event;                                       \
00108     union {                                                             \
00109         struct {                                                        \
00110             uint8_t smartpoll_active;                                   \
00111         } netif;                                                        \
00112         struct {                                                        \
00113             uint8_t msg;                                                \
00114         } tapif_user;                                                   \
00115         uint8_t pvt_pad[4];                                             \
00116     } private;                                                          \
00117     uint8_t __pad[44];                                                  \
00118     union __name##_sring_entry ring[1]; /* variable-length */           \
00119 };                                                                      \
00120                                                                         \
00121 /* "Front" end's private variables */                                   \
00122 struct __name##_front_ring {                                            \
00123     RING_IDX req_prod_pvt;                                              \
00124     RING_IDX rsp_cons;                                                  \
00125     unsigned int nr_ents;                                               \
00126     struct __name##_sring *sring;                                       \
00127 };                                                                      \
00128                                                                         \
00129 /* "Back" end's private variables */                                    \
00130 struct __name##_back_ring {                                             \
00131     RING_IDX rsp_prod_pvt;                                              \
00132     RING_IDX req_cons;                                                  \
00133     unsigned int nr_ents;                                               \
00134     struct __name##_sring *sring;                                       \
00135 };                                                                      \
00136                                                                         \
00137 /* Syntactic sugar */                                                   \
00138 typedef struct __name##_sring __name##_sring_t;                         \
00139 typedef struct __name##_front_ring __name##_front_ring_t;               \
00140 typedef struct __name##_back_ring __name##_back_ring_t
00141 
00142 /*
00143  * Macros for manipulating rings.
00144  *
00145  * FRONT_RING_whatever works on the "front end" of a ring: here
00146  * requests are pushed on to the ring and responses taken off it.
00147  *
00148  * BACK_RING_whatever works on the "back end" of a ring: here
00149  * requests are taken off the ring and responses put on.
00150  *
00151  * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
00152  * This is OK in 1-for-1 request-response situations where the
00153  * requestor (front end) never has more than RING_SIZE()-1
00154  * outstanding requests.
00155  */
00156 
00157 /* Initialising empty rings */
00158 #define SHARED_RING_INIT(_s) do {                                       \
00159     (_s)->req_prod  = (_s)->rsp_prod  = 0;                              \
00160     (_s)->req_event = (_s)->rsp_event = 1;                              \
00161     (void)memset((_s)->private.pvt_pad, 0, sizeof((_s)->private.pvt_pad)); \
00162     (void)memset((_s)->__pad, 0, sizeof((_s)->__pad));                  \
00163 } while(0)
00164 
00165 #define FRONT_RING_INIT(_r, _s, __size) do {                            \
00166     (_r)->req_prod_pvt = 0;                                             \
00167     (_r)->rsp_cons = 0;                                                 \
00168     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
00169     (_r)->sring = (_s);                                                 \
00170 } while (0)
00171 
00172 #define BACK_RING_INIT(_r, _s, __size) do {                             \
00173     (_r)->rsp_prod_pvt = 0;                                             \
00174     (_r)->req_cons = 0;                                                 \
00175     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
00176     (_r)->sring = (_s);                                                 \
00177 } while (0)
00178 
00179 /* How big is this ring? */
00180 #define RING_SIZE(_r)                                                   \
00181     ((_r)->nr_ents)
00182 
00183 /* Number of free requests (for use on front side only). */
00184 #define RING_FREE_REQUESTS(_r)                                          \
00185     (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
00186 
00187 /* Test if there is an empty slot available on the front ring.
00188  * (This is only meaningful from the front. )
00189  */
00190 #define RING_FULL(_r)                                                   \
00191     (RING_FREE_REQUESTS(_r) == 0)
00192 
00193 /* Test if there are outstanding messages to be processed on a ring. */
00194 #define RING_HAS_UNCONSUMED_RESPONSES(_r)                               \
00195     ((_r)->sring->rsp_prod - (_r)->rsp_cons)
00196 
00197 #ifdef __GNUC__
00198 #define RING_HAS_UNCONSUMED_REQUESTS(_r) ({                             \
00199     unsigned int req = (_r)->sring->req_prod - (_r)->req_cons;          \
00200     unsigned int rsp = RING_SIZE(_r) -                                  \
00201         ((_r)->req_cons - (_r)->rsp_prod_pvt);                          \
00202     req < rsp ? req : rsp;                                              \
00203 })
00204 #else
00205 /* Same as above, but without the nice GCC ({ ... }) syntax. */
00206 #define RING_HAS_UNCONSUMED_REQUESTS(_r)                                \
00207     ((((_r)->sring->req_prod - (_r)->req_cons) <                        \
00208       (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ?        \
00209      ((_r)->sring->req_prod - (_r)->req_cons) :                         \
00210      (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
00211 #endif
00212 
00213 /* Direct access to individual ring elements, by index. */
00214 #define RING_GET_REQUEST(_r, _idx)                                      \
00215     (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
00216 
00217 #define RING_GET_RESPONSE(_r, _idx)                                     \
00218     (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
00219 
00220 /* Loop termination condition: Would the specified index overflow the ring? */
00221 #define RING_REQUEST_CONS_OVERFLOW(_r, _cons)                           \
00222     (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
00223 
00224 /* Ill-behaved frontend determination: Can there be this many requests? */
00225 #define RING_REQUEST_PROD_OVERFLOW(_r, _prod)                           \
00226     (((_prod) - (_r)->rsp_prod_pvt) > RING_SIZE(_r))
00227 
00228 #define RING_PUSH_REQUESTS(_r) do {                                     \
00229     xen_wmb(); /* back sees requests /before/ updated producer index */ \
00230     (_r)->sring->req_prod = (_r)->req_prod_pvt;                         \
00231 } while (0)
00232 
00233 #define RING_PUSH_RESPONSES(_r) do {                                    \
00234     xen_wmb(); /* front sees resps /before/ updated producer index */   \
00235     (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt;                         \
00236 } while (0)
00237 
00238 /*
00239  * Notification hold-off (req_event and rsp_event):
00240  *
00241  * When queueing requests or responses on a shared ring, it may not always be
00242  * necessary to notify the remote end. For example, if requests are in flight
00243  * in a backend, the front may be able to queue further requests without
00244  * notifying the back (if the back checks for new requests when it queues
00245  * responses).
00246  *
00247  * When enqueuing requests or responses:
00248  *
00249  *  Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
00250  *  is a boolean return value. True indicates that the receiver requires an
00251  *  asynchronous notification.
00252  *
00253  * After dequeuing requests or responses (before sleeping the connection):
00254  *
00255  *  Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
00256  *  The second argument is a boolean return value. True indicates that there
00257  *  are pending messages on the ring (i.e., the connection should not be put
00258  *  to sleep).
00259  *
00260  *  These macros will set the req_event/rsp_event field to trigger a
00261  *  notification on the very next message that is enqueued. If you want to
00262  *  create batches of work (i.e., only receive a notification after several
00263  *  messages have been enqueued) then you will need to create a customised
00264  *  version of the FINAL_CHECK macro in your own code, which sets the event
00265  *  field appropriately.
00266  */
00267 
00268 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do {           \
00269     RING_IDX __old = (_r)->sring->req_prod;                             \
00270     RING_IDX __new = (_r)->req_prod_pvt;                                \
00271     xen_wmb(); /* back sees requests /before/ updated producer index */ \
00272     (_r)->sring->req_prod = __new;                                      \
00273     xen_mb(); /* back sees new requests /before/ we check req_event */  \
00274     (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) <           \
00275                  (RING_IDX)(__new - __old));                            \
00276 } while (0)
00277 
00278 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do {          \
00279     RING_IDX __old = (_r)->sring->rsp_prod;                             \
00280     RING_IDX __new = (_r)->rsp_prod_pvt;                                \
00281     xen_wmb(); /* front sees resps /before/ updated producer index */   \
00282     (_r)->sring->rsp_prod = __new;                                      \
00283     xen_mb(); /* front sees new resps /before/ we check rsp_event */    \
00284     (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) <           \
00285                  (RING_IDX)(__new - __old));                            \
00286 } while (0)
00287 
00288 #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do {             \
00289     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
00290     if (_work_to_do) break;                                             \
00291     (_r)->sring->req_event = (_r)->req_cons + 1;                        \
00292     xen_mb();                                                           \
00293     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
00294 } while (0)
00295 
00296 #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do {            \
00297     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
00298     if (_work_to_do) break;                                             \
00299     (_r)->sring->rsp_event = (_r)->rsp_cons + 1;                        \
00300     xen_mb();                                                           \
00301     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
00302 } while (0)
00303 
00304 #endif /* __XEN_PUBLIC_IO_RING_H__ */
00305 
00306 /*
00307  * Local variables:
00308  * mode: C
00309  * c-file-style: "BSD"
00310  * c-basic-offset: 4
00311  * tab-width: 4
00312  * indent-tabs-mode: nil
00313  * End:
00314  */