xref: /mOS-networking-stack/core/src/debug.c (revision 76404edc)
1*76404edcSAsim Jamshed #include <stdio.h>
2*76404edcSAsim Jamshed #include <unistd.h>
3*76404edcSAsim Jamshed #include <string.h>
4*76404edcSAsim Jamshed #include <stdint.h>
5*76404edcSAsim Jamshed #include <stdarg.h>
6*76404edcSAsim Jamshed #include "debug.h"
7*76404edcSAsim Jamshed #include "tcp_in.h"
8*76404edcSAsim Jamshed #include "logger.h"
9*76404edcSAsim Jamshed #include "ip_in.h"
10*76404edcSAsim Jamshed 
11*76404edcSAsim Jamshed /*----------------------------------------------------------------------------*/
12*76404edcSAsim Jamshed void flush_log_data(mtcp_manager_t mtcp)
13*76404edcSAsim Jamshed {
14*76404edcSAsim Jamshed 	int ret = 0;
15*76404edcSAsim Jamshed 	if (mtcp->w_buffer) {
16*76404edcSAsim Jamshed 		EnqueueJobBuffer(mtcp->logger, mtcp->w_buffer);
17*76404edcSAsim Jamshed 		ret = write(mtcp->sp_fd, "A", 1);
18*76404edcSAsim Jamshed 		if (ret != 1) {
19*76404edcSAsim Jamshed 			TRACE_INFO("Failed to flush logs in the buffer.\n");
20*76404edcSAsim Jamshed 			perror("write() for pipe");
21*76404edcSAsim Jamshed 		}
22*76404edcSAsim Jamshed 	}
23*76404edcSAsim Jamshed }
24*76404edcSAsim Jamshed /*----------------------------------------------------------------------------*/
25*76404edcSAsim Jamshed void
26*76404edcSAsim Jamshed thread_printf(mtcp_manager_t mtcp, FILE* f_idx, const char* _Format, ...)
27*76404edcSAsim Jamshed {
28*76404edcSAsim Jamshed 	va_list argptr;
29*76404edcSAsim Jamshed 	va_start(argptr, _Format);
30*76404edcSAsim Jamshed 
31*76404edcSAsim Jamshed 	#define PRINT_LIMIT 4096
32*76404edcSAsim Jamshed 	int len;
33*76404edcSAsim Jamshed 	log_buff *wbuf;
34*76404edcSAsim Jamshed 
35*76404edcSAsim Jamshed 	assert(f_idx != NULL);
36*76404edcSAsim Jamshed 
37*76404edcSAsim Jamshed 	pthread_mutex_lock(&mtcp->logger->mutex);
38*76404edcSAsim Jamshed 	wbuf = mtcp->w_buffer;
39*76404edcSAsim Jamshed 	if (wbuf && (wbuf->buff_len + PRINT_LIMIT > LOG_BUFF_SIZE)) {
40*76404edcSAsim Jamshed 		flush_log_data(mtcp);
41*76404edcSAsim Jamshed 		wbuf = NULL;
42*76404edcSAsim Jamshed 	}
43*76404edcSAsim Jamshed 
44*76404edcSAsim Jamshed 	if (!wbuf) {
45*76404edcSAsim Jamshed 		wbuf = DequeueFreeBuffer(mtcp->logger);
46*76404edcSAsim Jamshed 		assert(wbuf);
47*76404edcSAsim Jamshed 		wbuf->buff_len = 0;
48*76404edcSAsim Jamshed 		wbuf->tid = mtcp->ctx->cpu;
49*76404edcSAsim Jamshed 		wbuf->fid = f_idx;
50*76404edcSAsim Jamshed 		mtcp->w_buffer = wbuf;
51*76404edcSAsim Jamshed 	}
52*76404edcSAsim Jamshed 
53*76404edcSAsim Jamshed 	len = vsnprintf(wbuf->buff + wbuf->buff_len, PRINT_LIMIT, _Format, argptr);
54*76404edcSAsim Jamshed 	wbuf->buff_len += len;
55*76404edcSAsim Jamshed 	pthread_mutex_unlock(&mtcp->logger->mutex);
56*76404edcSAsim Jamshed 
57*76404edcSAsim Jamshed 	va_end(argptr);
58*76404edcSAsim Jamshed 
59*76404edcSAsim Jamshed }
60*76404edcSAsim Jamshed /*----------------------------------------------------------------------------*/
61*76404edcSAsim Jamshed void
62*76404edcSAsim Jamshed DumpPacket(mtcp_manager_t mtcp, char *buf, int len, char *step, int ifindex)
63*76404edcSAsim Jamshed {
64*76404edcSAsim Jamshed 	struct ethhdr *ethh;
65*76404edcSAsim Jamshed 	struct iphdr *iph;
66*76404edcSAsim Jamshed 	struct udphdr *udph;
67*76404edcSAsim Jamshed 	struct tcphdr *tcph;
68*76404edcSAsim Jamshed 	uint8_t *t;
69*76404edcSAsim Jamshed 
70*76404edcSAsim Jamshed 	if (ifindex >= 0)
71*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "%s %d %u", step, ifindex, mtcp->cur_ts);
72*76404edcSAsim Jamshed 	else
73*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "%s ? %u", step, mtcp->cur_ts);
74*76404edcSAsim Jamshed 
75*76404edcSAsim Jamshed 	ethh = (struct ethhdr *)buf;
76*76404edcSAsim Jamshed //	if (ntohs(ethh->h_proto) != ETH_P_IP) {
77*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "%02X:%02X:%02X:%02X:%02X:%02X -> %02X:%02X:%02X:%02X:%02X:%02X ",
78*76404edcSAsim Jamshed 				ethh->h_source[0],
79*76404edcSAsim Jamshed 				ethh->h_source[1],
80*76404edcSAsim Jamshed 				ethh->h_source[2],
81*76404edcSAsim Jamshed 				ethh->h_source[3],
82*76404edcSAsim Jamshed 				ethh->h_source[4],
83*76404edcSAsim Jamshed 				ethh->h_source[5],
84*76404edcSAsim Jamshed 				ethh->h_dest[0],
85*76404edcSAsim Jamshed 				ethh->h_dest[1],
86*76404edcSAsim Jamshed 				ethh->h_dest[2],
87*76404edcSAsim Jamshed 				ethh->h_dest[3],
88*76404edcSAsim Jamshed 				ethh->h_dest[4],
89*76404edcSAsim Jamshed 				ethh->h_dest[5]);
90*76404edcSAsim Jamshed 
91*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "protocol %04hx  ", ntohs(ethh->h_proto));
92*76404edcSAsim Jamshed //		goto done;
93*76404edcSAsim Jamshed //	}
94*76404edcSAsim Jamshed 
95*76404edcSAsim Jamshed 	thread_printf(mtcp, mtcp->log_fp, " ");
96*76404edcSAsim Jamshed 
97*76404edcSAsim Jamshed 	iph = (struct iphdr *)(ethh + 1);
98*76404edcSAsim Jamshed 	udph = (struct udphdr *)((uint32_t *)iph + iph->ihl);
99*76404edcSAsim Jamshed 	tcph = (struct tcphdr *)((uint32_t *)iph + iph->ihl);
100*76404edcSAsim Jamshed 
101*76404edcSAsim Jamshed 	t = (uint8_t *)&iph->saddr;
102*76404edcSAsim Jamshed 	thread_printf(mtcp, mtcp->log_fp, "%u.%u.%u.%u", t[0], t[1], t[2], t[3]);
103*76404edcSAsim Jamshed 	if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP)
104*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "(%d)", ntohs(udph->source));
105*76404edcSAsim Jamshed 
106*76404edcSAsim Jamshed 	thread_printf(mtcp, mtcp->log_fp, " -> ");
107*76404edcSAsim Jamshed 
108*76404edcSAsim Jamshed 	t = (uint8_t *)&iph->daddr;
109*76404edcSAsim Jamshed 	thread_printf(mtcp, mtcp->log_fp, "%u.%u.%u.%u", t[0], t[1], t[2], t[3]);
110*76404edcSAsim Jamshed 	if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP)
111*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "(%d)", ntohs(udph->dest));
112*76404edcSAsim Jamshed 
113*76404edcSAsim Jamshed 	thread_printf(mtcp, mtcp->log_fp, " IP_ID=%d", ntohs(iph->id));
114*76404edcSAsim Jamshed 	thread_printf(mtcp, mtcp->log_fp, " TTL=%d ", iph->ttl);
115*76404edcSAsim Jamshed 
116*76404edcSAsim Jamshed 	if (ip_fast_csum(iph, iph->ihl)) {
117*76404edcSAsim Jamshed 		__sum16 org_csum, correct_csum;
118*76404edcSAsim Jamshed 
119*76404edcSAsim Jamshed 		org_csum = iph->check;
120*76404edcSAsim Jamshed 		iph->check = 0;
121*76404edcSAsim Jamshed 		correct_csum = ip_fast_csum(iph, iph->ihl);
122*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "(bad checksum %04x should be %04x) ",
123*76404edcSAsim Jamshed 				ntohs(org_csum), ntohs(correct_csum));
124*76404edcSAsim Jamshed 		iph->check = org_csum;
125*76404edcSAsim Jamshed 	}
126*76404edcSAsim Jamshed 
127*76404edcSAsim Jamshed 	switch (iph->protocol) {
128*76404edcSAsim Jamshed 	case IPPROTO_TCP:
129*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "TCP ");
130*76404edcSAsim Jamshed 
131*76404edcSAsim Jamshed 		if (tcph->syn)
132*76404edcSAsim Jamshed 			thread_printf(mtcp, mtcp->log_fp, "S ");
133*76404edcSAsim Jamshed 		if (tcph->fin)
134*76404edcSAsim Jamshed 			thread_printf(mtcp, mtcp->log_fp, "F ");
135*76404edcSAsim Jamshed 		if (tcph->ack)
136*76404edcSAsim Jamshed 			thread_printf(mtcp, mtcp->log_fp, "A ");
137*76404edcSAsim Jamshed 		if (tcph->rst)
138*76404edcSAsim Jamshed 			thread_printf(mtcp, mtcp->log_fp, "R ");
139*76404edcSAsim Jamshed 
140*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "seq %u ", ntohl(tcph->seq));
141*76404edcSAsim Jamshed 		if (tcph->ack)
142*76404edcSAsim Jamshed 			thread_printf(mtcp, mtcp->log_fp, "ack %u ", ntohl(tcph->ack_seq));
143*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "WDW=%u ", ntohs(tcph->window));
144*76404edcSAsim Jamshed 		break;
145*76404edcSAsim Jamshed 	case IPPROTO_UDP:
146*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "UDP ");
147*76404edcSAsim Jamshed 		break;
148*76404edcSAsim Jamshed 	default:
149*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "protocol %d ", iph->protocol);
150*76404edcSAsim Jamshed 		goto done;
151*76404edcSAsim Jamshed 	}
152*76404edcSAsim Jamshed done:
153*76404edcSAsim Jamshed 	thread_printf(mtcp, mtcp->log_fp, "len=%d\n", len);
154*76404edcSAsim Jamshed }
155*76404edcSAsim Jamshed /*----------------------------------------------------------------------------*/
156*76404edcSAsim Jamshed void
157*76404edcSAsim Jamshed DumpIPPacket(mtcp_manager_t mtcp, const struct iphdr *iph, int len)
158*76404edcSAsim Jamshed {
159*76404edcSAsim Jamshed 	struct udphdr *udph;
160*76404edcSAsim Jamshed 	struct tcphdr *tcph;
161*76404edcSAsim Jamshed 	uint8_t *t;
162*76404edcSAsim Jamshed 
163*76404edcSAsim Jamshed 	udph = (struct udphdr *)((uint32_t *)iph + iph->ihl);
164*76404edcSAsim Jamshed 	tcph = (struct tcphdr *)((uint32_t *)iph + iph->ihl);
165*76404edcSAsim Jamshed 
166*76404edcSAsim Jamshed 	t = (uint8_t *)&iph->saddr;
167*76404edcSAsim Jamshed 	thread_printf(mtcp, mtcp->log_fp, "%u.%u.%u.%u", t[0], t[1], t[2], t[3]);
168*76404edcSAsim Jamshed 	if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP)
169*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "(%d)", ntohs(udph->source));
170*76404edcSAsim Jamshed 
171*76404edcSAsim Jamshed 	thread_printf(mtcp, mtcp->log_fp, " -> ");
172*76404edcSAsim Jamshed 
173*76404edcSAsim Jamshed 	t = (uint8_t *)&iph->daddr;
174*76404edcSAsim Jamshed 	thread_printf(mtcp, mtcp->log_fp, "%u.%u.%u.%u", t[0], t[1], t[2], t[3]);
175*76404edcSAsim Jamshed 	if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP)
176*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "(%d)", ntohs(udph->dest));
177*76404edcSAsim Jamshed 
178*76404edcSAsim Jamshed 	thread_printf(mtcp, mtcp->log_fp, " IP_ID=%d", ntohs(iph->id));
179*76404edcSAsim Jamshed 	thread_printf(mtcp, mtcp->log_fp, " TTL=%d ", iph->ttl);
180*76404edcSAsim Jamshed 
181*76404edcSAsim Jamshed 	if (ip_fast_csum(iph, iph->ihl)) {
182*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "(bad checksum) ");
183*76404edcSAsim Jamshed 	}
184*76404edcSAsim Jamshed 
185*76404edcSAsim Jamshed 	switch (iph->protocol) {
186*76404edcSAsim Jamshed 	case IPPROTO_TCP:
187*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "TCP ");
188*76404edcSAsim Jamshed 
189*76404edcSAsim Jamshed 		if (tcph->syn)
190*76404edcSAsim Jamshed 			thread_printf(mtcp, mtcp->log_fp, "S ");
191*76404edcSAsim Jamshed 		if (tcph->fin)
192*76404edcSAsim Jamshed 			thread_printf(mtcp, mtcp->log_fp, "F ");
193*76404edcSAsim Jamshed 		if (tcph->ack)
194*76404edcSAsim Jamshed 			thread_printf(mtcp, mtcp->log_fp, "A ");
195*76404edcSAsim Jamshed 		if (tcph->rst)
196*76404edcSAsim Jamshed 			thread_printf(mtcp, mtcp->log_fp, "R ");
197*76404edcSAsim Jamshed 
198*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "seq %u ", ntohl(tcph->seq));
199*76404edcSAsim Jamshed 		if (tcph->ack)
200*76404edcSAsim Jamshed 			thread_printf(mtcp, mtcp->log_fp, "ack %u ", ntohl(tcph->ack_seq));
201*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "WDW=%u ", ntohs(tcph->window));
202*76404edcSAsim Jamshed 		break;
203*76404edcSAsim Jamshed 	case IPPROTO_UDP:
204*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "UDP ");
205*76404edcSAsim Jamshed 		break;
206*76404edcSAsim Jamshed 	default:
207*76404edcSAsim Jamshed 		thread_printf(mtcp, mtcp->log_fp, "protocol %d ", iph->protocol);
208*76404edcSAsim Jamshed 		goto done;
209*76404edcSAsim Jamshed 	}
210*76404edcSAsim Jamshed done:
211*76404edcSAsim Jamshed 	thread_printf(mtcp, mtcp->log_fp, "len=%d\n", len);
212*76404edcSAsim Jamshed }
213*76404edcSAsim Jamshed /*----------------------------------------------------------------------------*/
214*76404edcSAsim Jamshed void
215*76404edcSAsim Jamshed DumpIPPacketToFile(FILE *fout, const struct iphdr *iph, int len)
216*76404edcSAsim Jamshed {
217*76404edcSAsim Jamshed 	struct udphdr *udph;
218*76404edcSAsim Jamshed 	struct tcphdr *tcph;
219*76404edcSAsim Jamshed 	uint8_t *t;
220*76404edcSAsim Jamshed 
221*76404edcSAsim Jamshed 	udph = (struct udphdr *)((uint32_t *)iph + iph->ihl);
222*76404edcSAsim Jamshed 	tcph = (struct tcphdr *)((uint32_t *)iph + iph->ihl);
223*76404edcSAsim Jamshed 
224*76404edcSAsim Jamshed 	t = (uint8_t *)&iph->saddr;
225*76404edcSAsim Jamshed 	fprintf(fout, "%u.%u.%u.%u", t[0], t[1], t[2], t[3]);
226*76404edcSAsim Jamshed 	if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP)
227*76404edcSAsim Jamshed 		fprintf(fout, "(%d)", ntohs(udph->source));
228*76404edcSAsim Jamshed 
229*76404edcSAsim Jamshed 	fprintf(fout, " -> ");
230*76404edcSAsim Jamshed 
231*76404edcSAsim Jamshed 	t = (uint8_t *)&iph->daddr;
232*76404edcSAsim Jamshed 	fprintf(fout, "%u.%u.%u.%u", t[0], t[1], t[2], t[3]);
233*76404edcSAsim Jamshed 	if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP)
234*76404edcSAsim Jamshed 		fprintf(fout, "(%d)", ntohs(udph->dest));
235*76404edcSAsim Jamshed 
236*76404edcSAsim Jamshed 	fprintf(fout, " IP_ID=%d", ntohs(iph->id));
237*76404edcSAsim Jamshed 	fprintf(fout, " TTL=%d ", iph->ttl);
238*76404edcSAsim Jamshed 
239*76404edcSAsim Jamshed 	if (ip_fast_csum(iph, iph->ihl)) {
240*76404edcSAsim Jamshed 		fprintf(fout, "(bad checksum) ");
241*76404edcSAsim Jamshed 	}
242*76404edcSAsim Jamshed 
243*76404edcSAsim Jamshed 	switch (iph->protocol) {
244*76404edcSAsim Jamshed 	case IPPROTO_TCP:
245*76404edcSAsim Jamshed 		fprintf(fout, "TCP ");
246*76404edcSAsim Jamshed 
247*76404edcSAsim Jamshed 		if (tcph->syn)
248*76404edcSAsim Jamshed 			fprintf(fout, "S ");
249*76404edcSAsim Jamshed 		if (tcph->fin)
250*76404edcSAsim Jamshed 			fprintf(fout, "F ");
251*76404edcSAsim Jamshed 		if (tcph->ack)
252*76404edcSAsim Jamshed 			fprintf(fout, "A ");
253*76404edcSAsim Jamshed 		if (tcph->rst)
254*76404edcSAsim Jamshed 			fprintf(fout, "R ");
255*76404edcSAsim Jamshed 
256*76404edcSAsim Jamshed 		fprintf(fout, "seq %u ", ntohl(tcph->seq));
257*76404edcSAsim Jamshed 		if (tcph->ack)
258*76404edcSAsim Jamshed 			fprintf(fout, "ack %u ", ntohl(tcph->ack_seq));
259*76404edcSAsim Jamshed 		fprintf(fout, "WDW=%u ", ntohs(tcph->window));
260*76404edcSAsim Jamshed 		break;
261*76404edcSAsim Jamshed 	case IPPROTO_UDP:
262*76404edcSAsim Jamshed 		fprintf(fout, "UDP ");
263*76404edcSAsim Jamshed 		break;
264*76404edcSAsim Jamshed 	default:
265*76404edcSAsim Jamshed 		fprintf(fout, "protocol %d ", iph->protocol);
266*76404edcSAsim Jamshed 		goto done;
267*76404edcSAsim Jamshed 	}
268*76404edcSAsim Jamshed done:
269*76404edcSAsim Jamshed 	fprintf(fout, "len=%d\n", len);
270*76404edcSAsim Jamshed }
271