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