Whamcloud - gitweb
- merge 2 weeks of b1_4 fixes onto HEAD
[fs/lustre-release.git] / lustre / portals / include / linux / kpr.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #ifndef _KPR_H
5 #define _KPR_H
6
7 # include <portals/lib-types.h> /* for ptl_hdr_t */
8
9 /******************************************************************************/
10 /* Kernel Portals Router interface */
11
12 typedef void (*kpr_fwd_callback_t)(void *arg, int error); // completion callback
13
14 /* space for routing targets to stash "stuff" in a forwarded packet */
15 typedef union {
16         long long        _alignment;
17         void            *_space[16];            /* scale with CPU arch */
18 } kprfd_scratch_t;
19
20 /* Kernel Portals Routing Forwarded message Descriptor */
21 typedef struct {
22         struct list_head     kprfd_list;        /* stash in queues (routing target can use) */
23         ptl_nid_t            kprfd_target_nid;  /* final destination NID */
24         ptl_nid_t            kprfd_gateway_nid; /* gateway NID */
25         ptl_hdr_t           *kprfd_hdr;         /* header in wire byte order */
26         int                  kprfd_nob;         /* # payload bytes */
27         int                  kprfd_niov;        /* # payload frags */
28         ptl_kiov_t          *kprfd_kiov;        /* payload fragments */
29         void                *kprfd_router_arg;  /* originating NAL's router arg */
30         kpr_fwd_callback_t   kprfd_callback;    /* completion callback */
31         void                *kprfd_callback_arg; /* completion callback arg */
32         kprfd_scratch_t      kprfd_scratch;     /* scratchpad for routing targets */
33 } kpr_fwd_desc_t;
34
35 typedef void  (*kpr_fwd_t)(void *arg, kpr_fwd_desc_t *fwd);
36 typedef void  (*kpr_notify_t)(void *arg, ptl_nid_t peer, int alive);
37
38 /* NAL's routing interface (Kernel Portals Routing Nal Interface) */
39 typedef const struct {
40         int             kprni_nalid;    /* NAL's id */
41         void           *kprni_arg;      /* Arg to pass when calling into NAL */
42         kpr_fwd_t       kprni_fwd;      /* NAL's forwarding entrypoint */
43         kpr_notify_t    kprni_notify;   /* NAL's notification entrypoint */
44 } kpr_nal_interface_t;
45
46 /* Router's routing interface (Kernel Portals Routing Router Interface) */
47 typedef const struct {
48         /* register the calling NAL with the router and get back the handle for
49          * subsequent calls */
50         int     (*kprri_register) (kpr_nal_interface_t *nal_interface,
51                                    void **router_arg);
52
53         /* ask the router to find a gateway that forwards to 'nid' and is a
54          * peer of the calling NAL; assume caller will send 'nob' bytes of
55          * payload there */
56         int     (*kprri_lookup) (void *router_arg, ptl_nid_t nid, int nob,
57                                  ptl_nid_t *gateway_nid);
58
59         /* hand a packet over to the router for forwarding */
60         kpr_fwd_t kprri_fwd_start;
61
62         /* hand a packet back to the router for completion */
63         void    (*kprri_fwd_done) (void *router_arg, kpr_fwd_desc_t *fwd,
64                                    int error);
65
66         /* notify the router about peer state */
67         void    (*kprri_notify) (void *router_arg, ptl_nid_t peer,
68                                  int alive, time_t when);
69
70         /* the calling NAL is shutting down */
71         void    (*kprri_shutdown) (void *router_arg);
72
73         /* deregister the calling NAL with the router */
74         void    (*kprri_deregister) (void *router_arg);
75
76 } kpr_router_interface_t;
77
78 /* Convenient struct for NAL to stash router interface/args */
79 typedef struct {
80         kpr_router_interface_t  *kpr_interface;
81         void                    *kpr_arg;
82 } kpr_router_t;
83
84 extern kpr_router_interface_t   kpr_router_interface;
85
86 static inline int
87 kpr_register (kpr_router_t *router, kpr_nal_interface_t *nalif)
88 {
89         int    rc;
90
91         router->kpr_interface = PORTAL_SYMBOL_GET (kpr_router_interface);
92         if (router->kpr_interface == NULL)
93                 return (-ENOENT);
94
95         rc = (router->kpr_interface)->kprri_register (nalif, &router->kpr_arg);
96         if (rc != 0)
97                 router->kpr_interface = NULL;
98
99         PORTAL_SYMBOL_PUT (kpr_router_interface);
100         return (rc);
101 }
102
103 static inline int
104 kpr_routing (kpr_router_t *router)
105 {
106         return (router->kpr_interface != NULL);
107 }
108
109 static inline int
110 kpr_lookup (kpr_router_t *router, ptl_nid_t nid, int nob, ptl_nid_t *gateway_nid)
111 {
112         if (!kpr_routing (router))
113                 return (-ENETUNREACH);
114
115         return (router->kpr_interface->kprri_lookup(router->kpr_arg, nid, nob,
116                                                     gateway_nid));
117 }
118
119 static inline void
120 kpr_fwd_init (kpr_fwd_desc_t *fwd, ptl_nid_t nid, ptl_hdr_t *hdr,
121               int nob, int niov, ptl_kiov_t *kiov,
122               kpr_fwd_callback_t callback, void *callback_arg)
123 {
124         fwd->kprfd_target_nid   = nid;
125         fwd->kprfd_gateway_nid  = nid;
126         fwd->kprfd_hdr          = hdr;
127         fwd->kprfd_nob          = nob;
128         fwd->kprfd_niov         = niov;
129         fwd->kprfd_kiov         = kiov;
130         fwd->kprfd_callback     = callback;
131         fwd->kprfd_callback_arg = callback_arg;
132 }
133
134 static inline void
135 kpr_fwd_start (kpr_router_t *router, kpr_fwd_desc_t *fwd)
136 {
137         if (!kpr_routing (router))
138                 fwd->kprfd_callback (fwd->kprfd_callback_arg, -ENETUNREACH);
139         else
140                 router->kpr_interface->kprri_fwd_start (router->kpr_arg, fwd);
141 }
142
143 static inline void
144 kpr_fwd_done (kpr_router_t *router, kpr_fwd_desc_t *fwd, int error)
145 {
146         LASSERT (kpr_routing (router));
147         router->kpr_interface->kprri_fwd_done (router->kpr_arg, fwd, error);
148 }
149
150 static inline void
151 kpr_notify (kpr_router_t *router,
152             ptl_nid_t peer, int alive, time_t when)
153 {
154         if (!kpr_routing (router))
155                 return;
156
157         router->kpr_interface->kprri_notify(router->kpr_arg, peer, alive, when);
158 }
159
160 static inline void
161 kpr_shutdown (kpr_router_t *router)
162 {
163         if (kpr_routing (router))
164                 router->kpr_interface->kprri_shutdown (router->kpr_arg);
165 }
166
167 static inline void
168 kpr_deregister (kpr_router_t *router)
169 {
170         if (!kpr_routing (router))
171                 return;
172         router->kpr_interface->kprri_deregister (router->kpr_arg);
173         router->kpr_interface = NULL;
174 }
175
176 #endif /* _KPR_H */