Belle II Software  release-08-01-10
nsmsys2.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 /*
9  nsmsys2.h
10 
11  This file may be included by both C and C++ programs.
12 */
13 
14 #ifndef __nsmsys2_h__
15 #define __nsmsys2_h__
16 
17 #include <netinet/in.h> /* for struct sockaddr_in, sockaddr */
18 
19 #define NSM2_PORT 8120 /* chosen as it corresponds to 0x2012
20  (2012 is the year started writing NSM2) */
21 #define NSM2_MEMSIZ (4096*1024)
22 
23 #define NSMENV_HOST "NSM2_HOST"
24 #define NSMENV_PORT "NSM2_PORT"
25 #define NSMENV_SHMKEY "NSM2_SHMKEY"
26 
27 #define NSMDENV_USER "NSMD2_USER"
28 #define NSMDENV_GROUP "NSMD2_GROUP"
29 #define NSMDENV_DEBUG "NSMD2_DEBUG"
30 #define NSMDENV_LOGDIR "NSMD2_LOGDIR"
31 #define NSMDENV_PRIORITY "NSMD2_PRIORITY"
32 #define NSMDENV_TCPBUF "NSMD2_TCPBUF"
33 
34 #ifndef __nsm2_typedef_nonstdint__
35 #define __nsm2_typedef_nonstdint__
36 typedef unsigned char byte8;
37 typedef unsigned short uint16;
38 typedef unsigned int uint32;
39 typedef unsigned long long uint64;
40 typedef signed short int16;
41 typedef signed int int32;
42 typedef signed long long int64;
43 #endif /* nsm2_typedef_nonstdint */
44 
45 #ifndef __nsm2_typedef_sockad__
46 #define __nsm2_typedef_sockad__
47 typedef struct sockaddr SOCKAD;
48 typedef struct sockaddr_in SOCKAD_IN;
49 #endif /* nsms2_typedef */
50 
51 #define NSMSYS_NAME_SIZ 31
52 #define NSMSYS_MAX_NOD 1024
53 #define NSMSYS_MAX_REF 4096
54 #define NSMSYS_MAX_REG 8192
55 #define NSMSYS_MAX_DAT 1024
56 #define NSMSYS_MAX_CON 1024
57 #define NSMSYS_MAX_REQ 1024
58 #define NSMSYS_MAX_HASH 2039 /* prime number, about twice of MAX_NOD etc */
59 #define NSMSYS_MAX_SCH 256
60 #define NSMSYS_DNAM_SIZ 31
61 #define NSMSYS_DFMT_SIZ 63
62 #define NSMSYS_LOGDIR_SIZ 200
63 #define NSMSYS_LOGNAME_SIZ 256
64 
65 /*
66  udp message size is limited by MTU,
67  tcp message size is limited by length in uint16_t and npar in byte8
68 */
69 #define NSM_MTUSIZ 1500
70 #define NSM_IPHEADSIZ 20 /* optionally 24 */
71 #define NSM_UDPHEADSIZ 8
72 #define NSM_UDPMSGSIZ 1472
73 #define NSM_UDPDATSIZ 1464 /* header part */
74 
75 #define NSM_TCPHEADSIZ 16 /* header */
76 #define NSM_TCPPARMAX 256 /* par in uint32_t */
77 #define NSM_TCPDATSIZ 65536 /* max size by uint16_t */
78 #define NSM_TCPBUFSIZ (256*4 + NSM_TCPDATSIZ) /* par + data */
79 #define NSM_TCPMSGSIZ (16 + NSM_TCPBUFSIZ) /* header + par + data */
80 #define NSM_TCPTHRESHOLD (16 + 256*4) /* at least header + par */
81 
82 #define NSM_DATCHUNKSIZ 1464 /* MTU - UDPHEAD - udpbuf.head */
83 #define NSM_DATMAXCHUNK 44
84 #define NSM_DATMAXSIZ (NSM_DATCHUNKSIZ*NSM_DATMAXCHUNK) /* 64416 in uint16_t */
85 
86 #define NSMD_MAXINVFMT 32
87 #define NSMD_TIMINVFMT 3600
88 
89 /* NSMudpbuf (in network byte order) */
90 typedef struct {
91  uint8_t req;
92  uint8_t npar; /* num of parameters of int32_t in dat area */
93  uint16_t seq;
94  uint16_t par1;
95  uint16_t par2;
96  char dat[NSM_UDPDATSIZ];
97 } NSMudpbuf;
98 
99 /* NSMtcphead (in network byte order) */
100 typedef struct NSMtcphead_struct {
101  uint16_t req;
102  uint16_t seq;
103  uint16_t src;
104  uint16_t dest;
105  uint8_t opt;
106  uint8_t npar;
107  uint16_t len;
108  uint32_t from; // source IP, only when the message has to be forwarded
109 } NSMtcphead;
110 
111 /* NSMDmsg (in host byte order) */
112 typedef struct {
113  uint16_t req;
114  uint16_t seq;
115  uint16_t src; // source NSM nodeid, or (-1) if not an NSM client
116  uint16_t dest; // destination NSM nodeid, or (-1) if not an NSM client
117  uint8_t opt;
118  uint8_t npar;
119  uint16_t len;
120  uint32_t from; // source IP address in network byte order
121  uint32_t pars[256];
122  const char* datap;
123 } NSMdmsg;
124 
125 /* NSM messsage header is designed to minimize the header size
126  max message length for UDP is 1468 byte = 367 32-bit-word */
127 
128 /* The list of NSMDCMD_xxxx below should be in the same order as nsmd_reqlist
129  which is implemented as an array instead of linear search list */
130 
131 #define NSMCMD_FIRST (0xff00)
132 #define NSMCMD_LAST (0xff1c) /* last of UDP */
133 
134 #define NSMCMD_NEWCLIENT (NSMCMD_FIRST + 0x00)
135 #define NSMCMD_NEWCLIENTOB (NSMCMD_FIRST + 0x01)
136 #define NSMCMD_NEWMASTER (NSMCMD_FIRST + 0x02)
137 #define NSMCMD_DELCLIENT (NSMCMD_FIRST + 0x03)
138 #define NSMCMD_ALLOCMEM (NSMCMD_FIRST + 0x04)
139 #define NSMCMD_FREEMEM (NSMCMD_FIRST + 0x05)
140 #define NSMCMD_FLUSHMEM (NSMCMD_FIRST + 0x06)
141 #define NSMCMD_REQCPYMEM (NSMCMD_FIRST + 0x07)
142 #define NSMCMD_SYSCPYMEM (NSMCMD_FIRST + 0x08)
143 #define NSMCMD_OPENMEM (NSMCMD_FIRST + 0x09)
144 #define NSMCMD_CLOSEMEM (NSMCMD_FIRST + 0x0a)
145 #define NSMCMD_DUMPNODE (NSMCMD_FIRST + 0x0b)
146 #define NSMCMD_DUMPALLOC (NSMCMD_FIRST + 0x0c)
147 #define NSMCMD_DUMPDATA (NSMCMD_FIRST + 0x0d)
148 #define NSMCMD_DUMPCONN (NSMCMD_FIRST + 0x0e)
149 #define NSMCMD_DUMPINFO (NSMCMD_FIRST + 0x0f)
150 #define NSMCMD_DEBUGFLAG (NSMCMD_FIRST + 0x10)
151 #define NSMCMD_KILLDAEMON (NSMCMD_FIRST + 0x11)
152 #define NSMCMD_CLEANUP (NSMCMD_FIRST + 0x12)
153 #define NSMCMD_TOUCHMEM (NSMCMD_FIRST + 0x13)
154 #define NSMCMD_PING (NSMCMD_FIRST + 0x14)
155 #define NSMCMD_PONG (NSMCMD_FIRST + 0x15)
156 #define NSMCMD_READY (NSMCMD_FIRST + 0x16)
157 #define NSMCMD_NOP (NSMCMD_FIRST + 0x17)
158 #define NSMCMD_NEWREQ (NSMCMD_FIRST + 0x18)
159 #define NSMCMD_DELREQ (NSMCMD_FIRST + 0x19)
160 
161 #define NSMCMD_UDPOFFSET (0xff00)
162 #define NSMCMD_NEWDAEMON (NSMCMD_FIRST + 0x1a) /* for UDP */
163 #define NSMCMD_ACKDAEMON (NSMCMD_FIRST + 0x1b) /* for UDP */
164 #define NSMCMD_USRCPYMEM (NSMCMD_FIRST + 0x1c) /* for UDP */
165 
166 
167 typedef struct {
168  char name[NSMSYS_NAME_SIZ + 1]; /* 32 byte, *name == 0 if unavailable */
169  uint16_t libver; /* library version, from nsm2-1997 */
170  int16_t noddat; /* sys.dat index or -1 if unavailable */
171  int16_t nodref; /* sys.ref index or -1 if unavailable */
172  int16_t nodreg; /* sys.reg index or -1 if unavailable */
173  int32_t rsvi32;
174  int32_t ipaddr; /* ip address or -1 if unavailable */
175  int32_t nodpid; /* process id */
176  int32_t noduid; /* uid */
177  int64_t nodtim; /* creation time */
178 } NSMnod; /* total 56 byte */
179 
180 typedef struct {
181  char name[NSMSYS_NAME_SIZ + 1]; /* 32 byte, *name == 0 if unavailable */
182  int16_t code;
183  int16_t regid;
184 } NSMreq; /* total 40 byte */
185 
186 typedef struct {
187  int16_t regnod;
188  int16_t regreq;
189  int16_t nodnext;
190  int16_t reqnext;
191 } NSMreg; /* total 8 byte */
192 
193 typedef struct {
194  int32_t refrsv;
195  int16_t refnod; /* sys.nod index */
196  int16_t refdat; /* sys.dat index */
197  int64_t reftim; /* creation time */
198 } NSMref; /* total 16 byte */
199 
200 typedef struct {
201  char dtnam[NSMSYS_DNAM_SIZ + 1]; /* 32 byte */
202  char dtfmt[NSMSYS_DFMT_SIZ + 1]; /* 64 byte */
203  int16_t dtrev; /* data revision */
204  int16_t owner;
205  int16_t anext; /* next sys.dat index or -1 if last */
206  /* anext linked list is an address ascending list which is needed to
207  find a free space for a new allocation */
208  int16_t nnext; /* next sys.dat index of the same node, or -1 if last */
209  /* nnext linked list is needed to find all the data belong to the same
210  node */
211  uint16_t dtsiz; /* size up to 65120 (=1480*44), or 0 if unavailable */
212  uint16_t dtref; /* number of reference count */
213  uint16_t dtcyc; /* update cycle in 10msec unit, up to about 10 min */
214 
215  int16_t dtrsv;
216  int32_t dtpos; /* mem.mem index (up to NSM2_MEMSIZ), or -1 if unavailable */
217  int32_t rsv32;
218  int64_t dttim; /* creation time in 10msec unit */
219 } NSMdat; /* total 128 byte */
220 
221 typedef struct {
222  int16_t disid;
223  int16_t disnod;
224  int32_t discnt; /* update counter */
225  int64_t distim; /* time to distribute in 10msec unit */
226 } NSMdat_snd;
227 
228 typedef struct {
229  int32_t rcvcnt;
230  int32_t rcvdt1; /* delta time between newest update and dttim */
231  int16_t dtddt[44]; /* delta time between newest and each chunk */
232 } NSMdat_rcv;
233 
234 
235 
236 #define NSMCON_NON (-1)
237 #define NSMCON_UDP 0
238 #define NSMCON_TCP 1
239 #define NSMCON_OUT 2
240 
241 typedef struct {
242  int32_t sock;
243  int32_t pid;
244  int32_t nid;
245  int16_t status; /* flag for invalid connection: 0=OK, 1=WAITING, 2=NG */
246  int16_t ready; /* flag for ready to become the master */
247  /*
248  (&udpcon = sys.con[NSMCON_UDP], &tcpcon = sys.con[NSMCON_TCP])
249  udpcon.timstart for the start time of the daemon
250  tcpcon.timstart for the start time of the NSM network
251  udpcon.timevent when master/deputy/priority/generation modified
252  tcpcon.timevent when nod/ref/dat modified
253  */
254  int64_t timstart; /* start time reported from remote */
255  int64_t timevent; /* when status changed */
256  SOCKAD_IN sockad; /* 16 bytes */
257  int32_t icnt; /* how many times recv */
258  int32_t ocnt; /* how many times sent */
259  int64_t isiz; /* how many bytes recv */
260  int64_t osiz; /* how many bytes sent */
261  int64_t otim; /* how much time (in us) select had to wait */
262  int32_t osel; /* how many times select had to wait */
263  uint16_t version; /* nsmd2 version or library version */
264  int16_t rsv16;
265  uint8_t sigobs; /* sigobs=1 if SIGRTMIN is not defined */
266  uint8_t priority; /* between 0-100 */
267  uint8_t rsv;
268  char name[13]; /* (only first 12 bytes, total 80 bytes */
269 } NSMcon;
270 
271 typedef struct {
272  int64_t when; /* time_t */
273  int32_t opt; /* option */
274  int16_t conid; /* conid, to remove when connection closed */
275  int16_t funcid; /* function to call */
276 } NSMsch;
277 
278 typedef struct NSMmem_struct {
279  /* first 4 words are common to both NSMmem and NSMsys */
280  int32_t ipaddr;
281  int32_t pid;
282  int64_t timstart;
283  int64_t timevent;
284  char mem[NSM2_MEMSIZ];
285 } NSMmem;
286 
287 typedef struct NSMsys_struct {
288  /* first 4 words are common to both NSMmem and NSMsys */
289  int32_t ipaddr;
290  int32_t pid;
291  int64_t timstart; /* creation time of NSMsys (may not by me) */
292  int64_t timevent; /* last update of NSMsys */
293  uint32_t netmask;
294  int16_t sock_updated; /* socket updated, but fdset not updated yet */
295  int16_t afirst; /* set and distributed by master */
296  int16_t nnod; /* set and distributed by master */
297  int16_t nref; /* set and distributed by master */
298  int16_t ndat; /* set and distributed by master */
299  int16_t nreq; /* set and distributed by master */
300  NSMnod nod[NSMSYS_MAX_NOD]; /* set and distributed by master */
301  NSMref ref[NSMSYS_MAX_REF]; /* set and distributed by master */
302  NSMdat dat[NSMSYS_MAX_DAT]; /* set and distributed by master */
303  NSMreq req[NSMSYS_MAX_REQ]; /* set and distributed by master */
304  NSMreg reg[NSMSYS_MAX_REG]; /* set and distributed by master */
305 
306  /* hash tables (+1 for alignment as MAX_HASH is not even */
307  int32_t nodhash[NSMSYS_MAX_HASH + 1]; /* set and distributed by master */
308  int32_t dathash[NSMSYS_MAX_HASH + 1]; /* set and distributed by master */
309  int32_t reqhash[NSMSYS_MAX_HASH + 1]; /* set and distributed by master */
310 
311  NSMcon con[NSMSYS_MAX_CON];
312  NSMsch sch[NSMSYS_MAX_SCH];
313  int32_t ncon;
314  int32_t nsch;
315  int16_t conid[NSMSYS_MAX_NOD]; /* nod => con mapping */
316  int16_t ireg_free; /* cache to make it easier to find new reg slot */
317  int16_t nsnd;
318  int16_t rsv16[2];
319  NSMdat_snd snd[NSMSYS_MAX_DAT];
320  NSMdat_rcv rcv[NSMSYS_MAX_DAT];
321 
322  uint16_t generation;
323  int16_t master;
324  int16_t deputy;
325  int16_t ready;
326 
327  /* copy of global variables for reference (not for use by nsmd) */
328  uint16_t version; /* nsmd version */
329  uint16_t required; /* required version */
330  int16_t priority;
331  int16_t debug;
332  char logfile[NSMSYS_LOGNAME_SIZ];
333 } NSMsys;
334 
335 /* NSMDtcpq for queuing of tcp-write messages */
336 typedef struct nsmd_tcpq {
337  int conid;
338  int req; /* host-byte-order: redundant info also in buf */
339  int npar; /* host-byte-order: redundant info also in buf */
340  int msglen; /* host-byte-order: redundant info also in buf */
341  struct nsmd_tcpq* prevp;
342  struct nsmd_tcpq* nextp;
343  int pos;
344  const char* datap;
345  char buf[NSM_TCPTHRESHOLD]; /* network-byte-order */
346 } NSMDtcpq;
347 
348 #endif /* __nsmsys2_h__ */
349 
350 /* -- (emacs outline mode setup) ------------------------------------- */
351 /*
352 // Local Variables: ***
353 // mode:outline-minor ***
354 // outline-regexp:"^/\\* --[+ ]" ***
355 // End: ***
356 */