1 /*
2  *  Copyright (c) 2012-2020, Peter Haag
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions are met:
6  *
7  *   * Redistributions of source code must retain the above copyright notice,
8  *     this list of conditions and the following disclaimer.
9  *   * Redistributions in binary form must reproduce the above copyright notice,
10  *     this list of conditions and the following disclaimer in the documentation
11  *     and/or other materials provided with the distribution.
12  *   * Neither the name of the author nor the names of its contributors may be
13  *     used to endorse or promote products derived from this software without
14  *     specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef _EXPORTER_H
31 #define _EXPORTER_H 1
32 
33 #include "config.h"
34 
35 #include <sys/types.h>
36 #ifdef HAVE_STDINT_H
37 #include <stdint.h>
38 #endif
39 
40 #include "nffile.h"
41 
42 typedef struct optionTag_s {
43 	uint16_t offset;
44 	uint16_t length;
45 } optionTag_t;
46 
47 /*
48  *
49  * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
50  * |  - |	     0     |      1       |      2       |      3       |      4       |      5       |      6       |      7       |
51  * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
52  * |  0 |       record type == 7      |             size            |                          version                          |
53  * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
54  * |  1 |                                                                                                                       |
55  * +----+--------------+--------------+--------------+----------  ip   ------------+--------------+--------------+--------------+
56  * |  2 |                                                                                                                       |
57  * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
58  * |  3 |          sa_family          |            sysid            |                             id                            |
59  * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
60  */
61 typedef struct exporter_info_record_s {
62 	record_header_t	header;
63 
64 	// exporter version
65 	uint32_t 	version;
66 #define SFLOW_VERSION  9999
67 
68 	// IP address
69 	ip_addr_t	ip;
70 	uint16_t	sa_family;
71 
72 	// internal assigned ID
73 	uint16_t	sysid;
74 
75 	// exporter ID/Domain ID/Observation Domain ID assigned by the device
76 	uint32_t	id;
77 
78 } exporter_info_record_t;
79 
80 /*
81  * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
82  * |  - |	     0     |      1       |      2       |      3       |      4       |      5       |      6       |      7       |
83  * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
84  * |  0 |       record type == 8      |             size            |                         stat_count                        |
85  * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
86  * |  1 |                           sysid[0]                        |                      sequence_failure[0]                  |
87  * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
88  * |  2 |                                                        packets[0]                                                     |
89  * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
90  * |  3 |                                                         flows[0]                                                      |
91  * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
92  * ... more stat records [x], one for each exporter
93  * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
94  */
95 typedef struct exporter_stats_record_s {
96 	record_header_t	header;
97 
98 	uint32_t	stat_count;		// number of stat records
99 
100 	struct exporter_stat_s {
101 		uint32_t	sysid;				// identifies the exporter
102 		uint32_t	sequence_failure;	// number of sequence failues
103 		uint64_t	packets;			// number of packets sent by this exporter
104 		uint64_t	flows;				// number of flow records sent by this exporter
105 	} stat[1];
106 
107 } exporter_stats_record_t;
108 
109 /*
110  * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
111  * |  - |	     0     |      1       |      2       |      3       |      4       |      5       |      6       |      7       |
112  * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
113  * |  0 |       record type == 9      |             size            |                             id                            |
114  * +----+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
115  * |  1 |                          interval                         |             mode            |       exporter_sysid        |
116  * +----+--------------+--------------+--------------+-----------------------------+--------------+--------------+--------------+
117  */
118 typedef struct sampler_info_record_s {
119 	record_header_t	header;
120 
121 	// sampler data
122 	int32_t		id;				// id assigned by the exporting device
123 	uint32_t	interval;		// sampling interval
124 	uint16_t	mode;			// sampling mode
125 	uint16_t	exporter_sysid; // internal reference to exporter
126 
127 } sampler_info_record_t;
128 
129 typedef struct sampler_s {
130 	struct sampler_s *next;
131 	sampler_info_record_t	info;	// sampler record nffile
132 } sampler_t;
133 
134 typedef struct exporter_s {
135 	// linked chain
136 	struct exporter_s *next;
137 
138 	// exporter information
139 	exporter_info_record_t info;	// exporter record nffile
140 
141 	uint64_t	packets;			// number of packets sent by this exporter
142 	uint64_t	flows;				// number of flow records sent by this exporter
143 	uint32_t	sequence_failure;	// number of sequence failues
144 	uint32_t	padding_errors;		// number of sequence failues
145 
146 	sampler_t *sampler;				// list of samplers associated with this exporter
147 
148 } exporter_t;
149 
150 typedef struct samplerOption_s {
151 	struct samplerOption_s *next;
152 	uint32_t	tableID;	// table id
153 #define STDSAMPLING34	1
154 #define STDSAMPLING35	2
155 #define STDMASK			0x3
156 #define STDFLAGS		0x3
157 
158 #define SAMPLER302		4
159 #define SAMPLER304		8
160 #define SAMPLER305		16
161 #define SAMPLERMASK		0x1C
162 #define SAMPLERFLAGS	0x1C
163 
164 	uint32_t	flags;		// info about this map
165 
166 	// sampling offset/length values
167 	optionTag_t id;
168 	optionTag_t mode;
169 	optionTag_t interval;
170 
171 } samplerOption_t;
172 
173 
174 int InitExporterList(void);
175 
176 int AddExporterInfo(exporter_info_record_t *exporter_record);
177 
178 int AddSamplerInfo(sampler_info_record_t *sampler_record);
179 
180 int AddExporterStat(exporter_stats_record_t *stat_record);
181 
182 void ExportExporterList( nffile_t *nffile );
183 
184 void PrintExporters(char *filename);
185 
186 #endif //_EXPORTER_H
187 
188