Skip to content

Commit abec020

Browse files
authored
Merge pull request sonic-net#1 from YonatanPitz/sonic_bm_tor_cli
Sonic bm tor cli
2 parents 9b12354 + 813082d commit abec020

23 files changed

Lines changed: 504 additions & 253 deletions

orchagent/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ orchagent_SOURCES = \
3838
pfcwdorch.cpp \
3939
pfcactionhandler.cpp \
4040
bmt_common.cpp \
41-
bmt_cache_evacuator.cpp \
41+
bmt_cache_debug.cpp \
4242
bmt_cache_inserter.cpp \
4343
bmtorcacheorch.cpp \
4444
acltable.h \
@@ -63,7 +63,7 @@ orchagent_SOURCES = \
6363
switchorch.h \
6464
swssnet.h \
6565
tunneldecaporch.h \
66-
bmt_cache_evacuator.h \
66+
bmt_cache_debug.h \
6767
bmt_cache_inserter.h \
6868
bmt_common.h \
6969
bmt_orch_constants.h \

orchagent/bmt_cache_debug.cpp

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <sys/socket.h>
5+
#include <pthread.h>
6+
#include <unistd.h>
7+
#include <netinet/in.h>
8+
#include <iostream>
9+
#include <sstream>
10+
#include <map>
11+
12+
#include "bmt_common.h"
13+
#include "logger.h"
14+
#include "bmt_common.h"
15+
16+
extern global_config_t g;
17+
pthread_t debug_thread;
18+
int sock = 0;
19+
20+
/**
21+
* Simple command mapping:
22+
* INPUT CFG OUTPUT
23+
* exit
24+
* scan
25+
* flush
26+
*
27+
*/
28+
void dispatch(std::string &input, global_config_t* cfg, std::ostringstream &stream) {
29+
(void)cfg;
30+
if (!input.compare("evac-stop")) {
31+
cfg->exitFlag = true;
32+
stream << "Exiting evacuator thread";
33+
}
34+
else if (!input.compare("insert-stop")) {
35+
cfg->scanDpdkPort = true;
36+
stream << "Exiting inserter thread";
37+
}
38+
else if (!input.compare("flush")) {
39+
cfg->flushCache = true;
40+
stream << "Flushing the cache";
41+
}
42+
else if (!input.compare("pause")) {
43+
cfg->pauseCacheInsertion = true;
44+
stream << "Insertion paused";
45+
}
46+
else if (!input.compare("resume")) {
47+
cfg->pauseCacheInsertion = false;
48+
stream << "Insertion resumed";
49+
}
50+
else if (!input.compare("status") || !input.compare("s")) {
51+
stream << "sampler init status " << cfg->sampler_init_status << std::endl;
52+
stream << "inserter is " << (cfg->pauseCacheInsertion ? "paused" : "running") << std::endl;
53+
stream << "cacheInsertCount " << cfg->cacheInsertCount << std::endl;
54+
stream << "cacheInsertSkip " << cfg->cacheInsertSkip << std::endl;
55+
stream << "cacheRemoveCount " << cfg->cacheRemoveCount << std::endl;
56+
stream << "flushCache " << cfg->flushCache << std::endl;
57+
stream << "exitFlag " << cfg->exitFlag << std::endl;
58+
stream << "scanDpdkPort " << cfg->scanDpdkPort << std::endl;
59+
}
60+
else {
61+
stream << input << "??? Try - status, flush, pause, resume, evac-stop, insert-stop";
62+
}
63+
}
64+
65+
void* debug_listener(void * data)
66+
{
67+
static const int BUFLEN = 2000;
68+
char buf[BUFLEN+1];
69+
struct sockaddr_in si_other;
70+
int slen = sizeof(si_other);
71+
ssize_t recv_len;
72+
global_config_t* cfg = (global_config_t*) data;
73+
std::string raw_input;
74+
printf("starting BMT debug listener thread\n");
75+
76+
while(1)
77+
{
78+
fflush(stdout);
79+
memset(buf,'\0', BUFLEN);
80+
if ((recv_len = recvfrom(sock, buf, BUFLEN, 0, (struct sockaddr *) &si_other, (socklen_t *)&slen)) == -1) {
81+
SWSS_LOG_ERROR("BMT debug server recvfrom error");
82+
}
83+
printf("Received BMT command: \"%s\"\n" , buf);
84+
raw_input.clear();
85+
raw_input.assign(buf);
86+
std::ostringstream stream;
87+
dispatch(raw_input, cfg, stream);
88+
std::string str = stream.str();
89+
int len = (int)str.length()+1;
90+
if (len >= BUFLEN) {
91+
len = BUFLEN;
92+
memcpy(buf, str.c_str(), len-1);
93+
buf[BUFLEN] = '\0';
94+
}
95+
else {
96+
memcpy(buf, str.c_str(), len);
97+
}
98+
//now reply the client with the same data
99+
if (sendto(sock, buf, len, 0, (struct sockaddr*) &si_other, slen) == -1) {
100+
SWSS_LOG_ERROR("BMT debug server sendto error");
101+
}
102+
// printf("Sent response of %u bytes\n", len);
103+
}
104+
close(sock);
105+
return 0;
106+
}
107+
108+
int start_server(global_config_t* cfg) {
109+
printf("Debug server thread started\n");
110+
return pthread_create(&debug_thread, NULL, debug_listener, cfg);
111+
}
112+
113+
int stop_server() {
114+
printf("Debug server thread stopped\n");
115+
return pthread_cancel(debug_thread);
116+
}
117+
118+
/**
119+
* A test UDP server connection, to pass back debug commands
120+
*/
121+
int bmt_cache_debug_init() {
122+
static int PORT = 50505;
123+
struct sockaddr_in si_me;
124+
125+
//create a UDP socket
126+
if ((sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
127+
SWSS_LOG_ERROR("BMT debug server socket error");
128+
}
129+
130+
// zero out the structure
131+
memset((char *) &si_me, 0, sizeof(si_me));
132+
133+
si_me.sin_family = AF_INET;
134+
si_me.sin_port = htons(PORT);
135+
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
136+
137+
//bind socket to port
138+
if( bind(sock , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1) {
139+
SWSS_LOG_ERROR("BMT debug server bind error");
140+
}
141+
printf("Debug server initialized to port %i\n", PORT);
142+
143+
start_server(&g);
144+
return 0;
145+
}
146+
147+
int bmt_cache_debug_deinit() {
148+
shutdown(sock, 2);
149+
close(sock);
150+
stop_server();
151+
return 0;
152+
}
153+

orchagent/bmt_cache_debug.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
#ifndef __BMT_CACHE_DEBUG_H_
4+
#define __BMT_CACHE_DEBUG_H_
5+
6+
int bmt_cache_debug_init();
7+
int bmt_cache_debug_deinit();
8+
9+
#endif /** __BMT_CACHE_DEBUG_H_ */

orchagent/bmt_cache_evacuator.cpp

Whitespace-only changes.

orchagent/bmt_cache_evacuator.h

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)