100-openimp_sync.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. --- a/lib/ipfix.c
  2. +++ b/lib/ipfix.c
  3. @@ -37,6 +37,9 @@ $$LIC$$
  4. #ifdef SCTPSUPPORT
  5. #include <netinet/sctp.h>
  6. #endif
  7. +#ifndef NOTHREADS
  8. +#include <pthread.h>
  9. +#endif
  10. #include <fcntl.h>
  11. #include <netdb.h>
  12. @@ -123,6 +126,18 @@ static uint16_t g_lasttid;
  13. static ipfix_datarecord_t g_data = { NULL, NULL, 0 }; /* ipfix_export */
  14. static ipfix_field_t *g_ipfix_fields;
  15. +#ifndef NOTHREADS
  16. +static pthread_mutex_t g_mutex;
  17. +#define mod_lock() { \
  18. + if ( pthread_mutex_lock( &g_mutex ) !=0 ) \
  19. + mlogf( 0, "[ipfix] mutex_lock() failed: %s\n", \
  20. + strerror( errno ) ); \
  21. + }
  22. +#define mod_unlock() { pthread_mutex_unlock( &g_mutex ); }
  23. +#else
  24. +#define mod_lock()
  25. +#define mod_unlock()
  26. +#endif
  27. /*----- prototypes -------------------------------------------------------*/
  28. @@ -133,6 +148,7 @@ int _ipfix_send_message( ipfix_t *ifh,
  29. ipfix_message_t *message );
  30. int _ipfix_write_msghdr( ipfix_t *ifh, ipfix_message_t *msg, iobuf_t *buf );
  31. void _ipfix_disconnect( ipfix_collector_t *col );
  32. +int _ipfix_export_flush( ipfix_t *ifh );
  33. /* name : do_writeselect
  34. @@ -576,16 +592,18 @@ int ipfix_decode_float( void *in, void *
  35. int ipfix_snprint_float( char *str, size_t size, void *data, size_t len )
  36. {
  37. - float tmp32;
  38. - double tmp64;
  39. + uint32_t tmp32;
  40. + uint64_t tmp64;
  41. switch ( len ) {
  42. case 4:
  43. - ipfix_decode_float( data, &tmp32, 4);
  44. - return snprintf( str, size, "%f", tmp32 );
  45. + memcpy( &tmp32, data, len );
  46. + tmp32 = htonl( tmp32 );
  47. + return snprintf( str, size, "%f", (float)tmp32 );
  48. case 8:
  49. - ipfix_decode_float( data, &tmp64, 8);
  50. - return snprintf( str, size, "%lf", tmp64);
  51. + memcpy( &tmp64, data, len );
  52. + tmp64 = HTONLL( tmp64 );
  53. + return snprintf( str, size, "%lf", (double)tmp64 );
  54. default:
  55. break;
  56. }
  57. @@ -682,12 +700,19 @@ int ipfix_get_eno_ieid( char *field, int
  58. * parameters:
  59. * remarks: init module, read field type info.
  60. */
  61. -int ipfix_init ( void )
  62. +int ipfix_init( void )
  63. {
  64. if ( g_tstart ) {
  65. ipfix_cleanup();
  66. }
  67. +#ifndef NOTHREADS
  68. + if ( pthread_mutex_init( &g_mutex, NULL ) !=0 ) {
  69. + mlogf( 0, "[ipfix] pthread_mutex_init() failed: %s\n",
  70. + strerror(errno) );
  71. + return -1;
  72. + }
  73. +#endif
  74. g_tstart = time(NULL);
  75. signal( SIGPIPE, SIG_IGN );
  76. g_lasttid = 255;
  77. @@ -806,6 +831,9 @@ void ipfix_cleanup ( void )
  78. g_data.maxfields = 0;
  79. g_data.lens = NULL;
  80. g_data.addrs = NULL;
  81. +#ifndef NOTHREADS
  82. + (void)pthread_mutex_destroy( &g_mutex );
  83. +#endif
  84. }
  85. int _ipfix_connect ( ipfix_collector_t *col )
  86. @@ -1465,7 +1493,7 @@ int _ipfix_write_template( ipfix_t
  87. default:
  88. /* check space */
  89. if ( tsize+ifh->offset > IPFIX_DEFAULT_BUFLEN ) {
  90. - if ( ipfix_export_flush( ifh ) < 0 )
  91. + if ( _ipfix_export_flush( ifh ) < 0 )
  92. return -1;
  93. if ( tsize+ifh->offset > IPFIX_DEFAULT_BUFLEN )
  94. return -1;
  95. @@ -1474,6 +1502,8 @@ int _ipfix_write_template( ipfix_t
  96. /* write template prior to data */
  97. if ( ifh->offset > 0 ) {
  98. memmove( ifh->buffer + tsize, ifh->buffer, ifh->offset );
  99. + if ( ifh->cs_tid )
  100. + ifh->cs_header += tsize;
  101. }
  102. buf = ifh->buffer;
  103. @@ -1615,8 +1645,11 @@ int ipfix_open( ipfix_t **ipfixh, int so
  104. return -1;
  105. }
  106. node->ifh = i;
  107. +
  108. + mod_lock();
  109. node->next = g_ipfixlist;
  110. g_ipfixlist = node;
  111. + mod_unlock();
  112. *ipfixh = i;
  113. return 0;
  114. @@ -1633,7 +1666,8 @@ void ipfix_close( ipfix_t *h )
  115. {
  116. ipfix_node_t *l, *n;
  117. - ipfix_export_flush( h );
  118. + mod_lock();
  119. + _ipfix_export_flush( h );
  120. while( h->collectors )
  121. _ipfix_drop_collector( (ipfix_collector_t**)&h->collectors );
  122. @@ -1659,6 +1693,7 @@ void ipfix_close( ipfix_t *h )
  123. #endif
  124. free(h->buffer);
  125. free(h);
  126. + mod_unlock();
  127. }
  128. }
  129. @@ -2156,6 +2191,22 @@ void ipfix_release_template( ipfix_t *if
  130. ipfix_delete_template( ifh, templ );
  131. }
  132. +static void _finish_cs( ipfix_t *ifh )
  133. +{
  134. + size_t buflen;
  135. + uint8_t *buf;
  136. +
  137. + /* finish current dataset */
  138. + if ( (buf=ifh->cs_header) ==NULL )
  139. + return;
  140. + buflen = 0;
  141. + INSERTU16( buf+buflen, buflen, ifh->cs_tid );
  142. + INSERTU16( buf+buflen, buflen, ifh->cs_bytes );
  143. + ifh->cs_bytes = 0;
  144. + ifh->cs_header = NULL;
  145. + ifh->cs_tid = 0;
  146. +}
  147. +
  148. int ipfix_export( ipfix_t *ifh, ipfix_template_t *templ, ... )
  149. {
  150. int i;
  151. @@ -2199,13 +2250,14 @@ int ipfix_export( ipfix_t *ifh, ipfix_te
  152. g_data.addrs, g_data.lens );
  153. }
  154. -int ipfix_export_array( ipfix_t *ifh,
  155. - ipfix_template_t *templ,
  156. - int nfields,
  157. - void **fields,
  158. - uint16_t *lengths )
  159. +static int
  160. +_ipfix_export_array( ipfix_t *ifh,
  161. + ipfix_template_t *templ,
  162. + int nfields,
  163. + void **fields,
  164. + uint16_t *lengths )
  165. {
  166. - int i;
  167. + int i, newset_f=0;
  168. size_t buflen, datasetlen;
  169. uint8_t *p, *buf;
  170. @@ -2249,7 +2301,19 @@ int ipfix_export_array( ipfix_t
  171. /** get size of data set, check space
  172. */
  173. - for ( i=0, datasetlen=4; i<nfields; i++ ) {
  174. + if ( templ->tid == ifh->cs_tid ) {
  175. + newset_f = 0;
  176. + datasetlen = 0;
  177. + }
  178. + else {
  179. + if ( ifh->cs_tid > 0 ) {
  180. + _finish_cs( ifh );
  181. + }
  182. + newset_f = 1;
  183. + datasetlen = 4;
  184. + }
  185. +
  186. + for ( i=0; i<nfields; i++ ) {
  187. if ( templ->fields[i].flength == IPFIX_FT_VARLEN ) {
  188. if ( lengths[i]>254 )
  189. datasetlen += 3;
  190. @@ -2263,21 +2327,29 @@ int ipfix_export_array( ipfix_t
  191. }
  192. datasetlen += lengths[i];
  193. }
  194. - if ( ((ifh->offset + datasetlen) > IPFIX_DEFAULT_BUFLEN )
  195. - && (ipfix_export_flush( ifh ) <0) ) {
  196. - return -1;
  197. +
  198. + if ( (ifh->offset + datasetlen) > IPFIX_DEFAULT_BUFLEN ) {
  199. + if ( ifh->cs_tid )
  200. + _finish_cs( ifh );
  201. + newset_f = 1;
  202. +
  203. + if ( _ipfix_export_flush( ifh ) <0 )
  204. + return -1;
  205. }
  206. - /* fill buffer
  207. - */
  208. + /* fill buffer */
  209. buf = (uint8_t*)(ifh->buffer) + ifh->offset;
  210. buflen = 0;
  211. - /* insert data set
  212. - */
  213. - ifh->nrecords ++;
  214. - INSERTU16( buf+buflen, buflen, templ->tid );
  215. - INSERTU16( buf+buflen, buflen, datasetlen );
  216. + if ( newset_f ) {
  217. + /* insert data set
  218. + */
  219. + ifh->cs_bytes = 0;
  220. + ifh->cs_header = buf;
  221. + ifh->cs_tid = templ->tid;
  222. + INSERTU16( buf+buflen, buflen, templ->tid );
  223. + INSERTU16( buf+buflen, buflen, 4 );
  224. + }
  225. /* insert data record
  226. */
  227. @@ -2303,7 +2375,9 @@ int ipfix_export_array( ipfix_t
  228. buflen += lengths[i];
  229. }
  230. + ifh->nrecords ++;
  231. ifh->offset += buflen;
  232. + ifh->cs_bytes += buflen;
  233. if ( ifh->version == IPFIX_VERSION )
  234. ifh->seqno ++;
  235. return 0;
  236. @@ -2313,7 +2387,7 @@ int ipfix_export_array( ipfix_t
  237. * parameters:
  238. * remarks: rewrite this func!
  239. */
  240. -int ipfix_export_flush( ipfix_t *ifh )
  241. +int _ipfix_export_flush( ipfix_t *ifh )
  242. {
  243. iobuf_t *buf;
  244. ipfix_collector_t *col;
  245. @@ -2322,8 +2396,14 @@ int ipfix_export_flush( ipfix_t *ifh )
  246. if ( (ifh==NULL) || (ifh->offset==0) )
  247. return 0;
  248. - if ( (buf=_ipfix_getbuf()) ==NULL )
  249. + if ( ifh->cs_tid > 0 ) {
  250. + /* finish current dataset */
  251. + _finish_cs( ifh );
  252. + }
  253. +
  254. + if ( (buf=_ipfix_getbuf()) ==NULL ) {
  255. return -1;
  256. + }
  257. #ifdef DEBUG
  258. mlogf( 0, "[ipfix_export_flush] msg has %d records, %d bytes\n",
  259. @@ -2350,3 +2430,30 @@ int ipfix_export_flush( ipfix_t *ifh )
  260. _ipfix_freebuf( buf );
  261. return ret;
  262. }
  263. +
  264. +int ipfix_export_array( ipfix_t *ifh,
  265. + ipfix_template_t *templ,
  266. + int nfields,
  267. + void **fields,
  268. + uint16_t *lengths )
  269. +{
  270. + int ret;
  271. +
  272. + mod_lock();
  273. + ret = _ipfix_export_array( ifh, templ, nfields, fields, lengths );
  274. + mod_unlock();
  275. +
  276. + return ret;
  277. +}
  278. +
  279. +int ipfix_export_flush( ipfix_t *ifh )
  280. +{
  281. + int ret;
  282. +
  283. + mod_lock();
  284. + ret = _ipfix_export_flush( ifh );
  285. + mod_unlock();
  286. +
  287. + return ret;
  288. +}
  289. +
  290. --- a/lib/ipfix.h
  291. +++ b/lib/ipfix.h
  292. @@ -142,6 +142,12 @@ typedef struct
  293. int nrecords; /* no. of records in buffer */
  294. size_t offset; /* output buffer fill level */
  295. uint32_t seqno; /* sequence no. of next message */
  296. +
  297. + /* experimental */
  298. + int cs_tid; /* template id of current dataset */
  299. + int cs_bytes; /* size of current set */
  300. + uint8_t *cs_header; /* start of current set */
  301. +
  302. } ipfix_t;
  303. /** exporter funcs
  304. --- a/lib/ipfix_col.c
  305. +++ b/lib/ipfix_col.c
  306. @@ -907,7 +907,7 @@ int ipfix_decode_datarecord( ipfixt_node
  307. return 0;
  308. }
  309. -static void do_free_datarecord( ipfix_datarecord_t *data )
  310. +void ipfix_free_datarecord( ipfix_datarecord_t *data )
  311. {
  312. if ( data ) {
  313. if ( data->addrs )
  314. @@ -925,6 +925,7 @@ int ipfix_parse_msg( ipfix_input_t *inpu
  315. ipfix_hdr_t hdr; /* ipfix packet header */
  316. ipfixs_node_t *s;
  317. ipfix_datarecord_t data = { NULL, NULL, 0 };
  318. + ipfixe_node_t *e;
  319. uint8_t *buf; /* ipfix payload */
  320. uint16_t setid, setlen; /* set id, set lenght */
  321. int i, nread, offset; /* counter */
  322. @@ -1042,6 +1043,12 @@ int ipfix_parse_msg( ipfix_input_t *inpu
  323. err_flag = 1;
  324. }
  325. else {
  326. + for ( e=g_exporter; e!=NULL; e=e->next ) {
  327. + if ( e->elem->export_dset )
  328. + (void) e->elem->export_dset( t, buf+nread, setlen,
  329. + e->elem->data );
  330. + }
  331. +
  332. /** read data records
  333. */
  334. for ( offset=nread, bytesleft=setlen; bytesleft>4; ) {
  335. @@ -1076,11 +1083,11 @@ int ipfix_parse_msg( ipfix_input_t *inpu
  336. goto errend;
  337. end:
  338. - do_free_datarecord( &data );
  339. + ipfix_free_datarecord( &data );
  340. return nread;
  341. errend:
  342. - do_free_datarecord( &data );
  343. + ipfix_free_datarecord( &data );
  344. return -1;
  345. }
  346. @@ -1093,7 +1100,7 @@ void process_client_tcp( int fd, int mas
  347. tcp_conn_t *tcon = (tcp_conn_t*)data;
  348. char *func = "process_client_tcp";
  349. - mlogf( 3, "[%s] fd %d mask %d called.\n", func, fd, mask );
  350. + mlogf( 4, "[%s] fd %d mask %d called.\n", func, fd, mask );
  351. /** read ipfix header
  352. */
  353. --- a/lib/ipfix_col.h
  354. +++ b/lib/ipfix_col.h
  355. @@ -88,6 +88,7 @@ typedef struct ipfix_col_info
  356. int (*export_newsource)(ipfixs_node_t*,void*);
  357. int (*export_newmsg)(ipfixs_node_t*,ipfix_hdr_t*,void*);
  358. int (*export_trecord)(ipfixs_node_t*,ipfixt_node_t*,void*);
  359. + int (*export_dset)(ipfixt_node_t*,uint8_t*,size_t,void*);
  360. int (*export_drecord)(ipfixs_node_t*,ipfixt_node_t*,
  361. ipfix_datarecord_t*,void*);
  362. void (*export_cleanup)(void*);
  363. --- a/lib/ipfix_col_files.c
  364. +++ b/lib/ipfix_col_files.c
  365. @@ -68,7 +68,7 @@ static int export_newsource_file( ipfixs
  366. return -1;
  367. }
  368. snprintf( s->fname+strlen(s->fname), PATH_MAX-strlen(s->fname),
  369. - "/%u", s->odid );
  370. + "/%u", (unsigned int)s->odid );
  371. if ( (access( s->fname, R_OK ) <0 )
  372. && (mkdir( s->fname, S_IRWXU ) <0) ) {
  373. mlogf( 0, "[%s] cannot access dir '%s': %s\n",