|
| 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 | + |
0 commit comments