blob: c8f9861b933a6460006b5f13e6f1654fff32fd2c [file] [log] [blame]
Thierry FOURNIER4aec0a42018-02-23 11:42:57 +01001/* Main SPOA server includes
2 *
3 * Copyright 2016 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
4 * Copyright 2018 OZON / Thierry Fournier <thierry.fournier@ozon.io>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11#ifndef __SPOA_H__
12#define __SPOA_H__
13
Thierry FOURNIER880d7e12018-02-25 10:54:56 +010014#include <pthread.h>
Thierry FOURNIER4aec0a42018-02-23 11:42:57 +010015#include <stdbool.h>
16#include <stdint.h>
17#include <netinet/in.h>
Thierry FOURNIER880d7e12018-02-25 10:54:56 +010018#include <sys/time.h>
Thierry FOURNIER4aec0a42018-02-23 11:42:57 +010019
20#define MAX_FRAME_SIZE 16384
21#define SPOP_VERSION "1.0"
22#define SPOA_CAPABILITIES ""
23
24/* All supported data types */
25enum spoe_data_type {
26 SPOE_DATA_T_NULL = 0,
27 SPOE_DATA_T_BOOL,
28 SPOE_DATA_T_INT32,
29 SPOE_DATA_T_UINT32,
30 SPOE_DATA_T_INT64,
31 SPOE_DATA_T_UINT64,
32 SPOE_DATA_T_IPV4,
33 SPOE_DATA_T_IPV6,
34 SPOE_DATA_T_STR,
35 SPOE_DATA_T_BIN,
36 SPOE_DATA_TYPES
37};
38
39/* Scopes used for variables set by agents. It is a way to be agnotic to vars
40 * scope. */
41enum spoe_vars_scope {
42 SPOE_SCOPE_PROC = 0, /* <=> SCOPE_PROC */
43 SPOE_SCOPE_SESS, /* <=> SCOPE_SESS */
44 SPOE_SCOPE_TXN, /* <=> SCOPE_TXN */
45 SPOE_SCOPE_REQ, /* <=> SCOPE_REQ */
46 SPOE_SCOPE_RES, /* <=> SCOPE_RES */
47};
48
49struct worker {
50 unsigned int id;
51 char buf[MAX_FRAME_SIZE];
52 unsigned int len;
53 unsigned int size;
54 int status_code;
55 unsigned int stream_id;
56 unsigned int frame_id;
57 bool healthcheck;
58 int ip_score; /* -1 if unset, else between 0 and 100 */
59};
60
61struct chunk {
62 char *str; /* beginning of the string itself. Might not be 0-terminated */
63 int len; /* current size of the string from first to last char */
64};
65
66union spoe_value {
67 bool boolean; /* use for boolean */
68 int32_t sint32; /* used for signed 32bits integers */
69 uint32_t uint32; /* used for signed 32bits integers */
70 int32_t sint64; /* used for signed 64bits integers */
71 uint32_t uint64; /* used for signed 64bits integers */
72 struct in_addr ipv4; /* used for ipv4 addresses */
73 struct in6_addr ipv6; /* used for ipv6 addresses */
74 struct chunk buffer; /* used for char strings or buffers */
75};
76
77/* Used to store sample constant */
78struct spoe_data {
79 enum spoe_data_type type; /* SPOE_DATA_T_* */
80 union spoe_value u; /* spoe data value */
81};
82
Thierry FOURNIER64eaa332018-02-23 14:58:40 +010083struct spoe_kv {
84 struct chunk name;
85 struct spoe_data value;
86};
87
88struct ps {
89 struct ps *next;
90 char *ext;
91 int (*init_worker)(struct worker *w);
92};
93
Thierry FOURNIER880d7e12018-02-25 10:54:56 +010094extern bool debug;
95extern pthread_key_t worker_id;
96
Thierry FOURNIER64eaa332018-02-23 14:58:40 +010097void ps_register(struct ps *ps);
98
Thierry FOURNIER880d7e12018-02-25 10:54:56 +010099#define LOG(fmt, args...) \
100 do { \
101 struct timeval now; \
102 int wid = *((int*)pthread_getspecific(worker_id)); \
103 \
104 gettimeofday(&now, NULL); \
105 fprintf(stderr, "%ld.%06ld [%02d] " fmt "\n", \
106 now.tv_sec, now.tv_usec, wid, ##args); \
107 } while (0)
108
109#define DEBUG(x...) \
110 do { \
111 if (debug) \
112 LOG(x); \
113 } while (0)
114
Thierry FOURNIER4aec0a42018-02-23 11:42:57 +0100115#endif /* __SPOA_H__ */