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