Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[fs/lustre-release.git] / lnet / 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-nal.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 /* Router's control interface (Kernel Portals Routing Control Interface) */
85 typedef const struct {
86         int     (*kprci_add_route)(int gateway_nal, ptl_nid_t gateway_nid,
87                                    ptl_nid_t lo_nid, ptl_nid_t hi_nid);
88         int     (*kprci_del_route)(int gateway_nal, ptl_nid_t gateway_nid,
89                                    ptl_nid_t lo_nid, ptl_nid_t hi_nid);
90         int     (*kprci_get_route)(int index, int *gateway_nal,
91                                    ptl_nid_t *gateway,
92                                    ptl_nid_t *lo_nid, ptl_nid_t *hi_nid,
93                                    int *alive);
94         int     (*kprci_notify)(int gateway_nal, ptl_nid_t gateway_nid,
95                                 int alive, time_t when);
96 } kpr_control_interface_t;
97
98 extern kpr_control_interface_t  kpr_control_interface;
99 extern kpr_router_interface_t   kpr_router_interface;
100
101 static inline int
102 kpr_register (kpr_router_t *router, kpr_nal_interface_t *nalif)
103 {
104         int    rc;
105
106         router->kpr_interface = PORTAL_SYMBOL_GET (kpr_router_interface);
107         if (router->kpr_interface == NULL)
108                 return (-ENOENT);
109
110         rc = (router->kpr_interface)->kprri_register (nalif, &router->kpr_arg);
111         if (rc != 0)
112                 router->kpr_interface = NULL;
113
114         PORTAL_SYMBOL_PUT (kpr_router_interface);
115         return (rc);
116 }
117
118 static inline int
119 kpr_routing (kpr_router_t *router)
120 {
121         return (router->kpr_interface != NULL);
122 }
123
124 static inline int
125 kpr_lookup (kpr_router_t *router, ptl_nid_t nid, int nob, ptl_nid_t *gateway_nid)
126 {
127         if (!kpr_routing (router))
128                 return (-ENETUNREACH);
129
130         return (router->kpr_interface->kprri_lookup(router->kpr_arg, nid, nob,
131                                                     gateway_nid));
132 }
133
134 static inline void
135 kpr_fwd_init (kpr_fwd_desc_t *fwd, ptl_nid_t nid, ptl_hdr_t *hdr,
136               int nob, int niov, ptl_kiov_t *kiov,
137               kpr_fwd_callback_t callback, void *callback_arg)
138 {
139         fwd->kprfd_target_nid   = nid;
140         fwd->kprfd_gateway_nid  = nid;
141         fwd->kprfd_hdr          = hdr;
142         fwd->kprfd_nob          = nob;
143         fwd->kprfd_niov         = niov;
144         fwd->kprfd_kiov         = kiov;
145         fwd->kprfd_callback     = callback;
146         fwd->kprfd_callback_arg = callback_arg;
147 }
148
149 static inline void
150 kpr_fwd_start (kpr_router_t *router, kpr_fwd_desc_t *fwd)
151 {
152         if (!kpr_routing (router))
153                 fwd->kprfd_callback (fwd->kprfd_callback_arg, -ENETUNREACH);
154         else
155                 router->kpr_interface->kprri_fwd_start (router->kpr_arg, fwd);
156 }
157
158 static inline void
159 kpr_fwd_done (kpr_router_t *router, kpr_fwd_desc_t *fwd, int error)
160 {
161         LASSERT (kpr_routing (router));
162         router->kpr_interface->kprri_fwd_done (router->kpr_arg, fwd, error);
163 }
164
165 static inline void
166 kpr_notify (kpr_router_t *router,
167             ptl_nid_t peer, int alive, time_t when)
168 {
169         if (!kpr_routing (router))
170                 return;
171
172         router->kpr_interface->kprri_notify(router->kpr_arg, peer, alive, when);
173 }
174
175 static inline void
176 kpr_shutdown (kpr_router_t *router)
177 {
178         if (kpr_routing (router))
179                 router->kpr_interface->kprri_shutdown (router->kpr_arg);
180 }
181
182 static inline void
183 kpr_deregister (kpr_router_t *router)
184 {
185         if (!kpr_routing (router))
186                 return;
187         router->kpr_interface->kprri_deregister (router->kpr_arg);
188         router->kpr_interface = NULL;
189 }
190
191 #endif /* _KPR_H */