Skip to content

Commit

Permalink
mod_sonic config option sonic{unixsock=on} triggers use of unix domai…
Browse files Browse the repository at this point in the history
…n socket if learned from JSON config
  • Loading branch information
sflow committed Jan 27, 2021
1 parent be8f404 commit 4bbb6c2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/Linux/hsflowconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ extern "C" {
break;
case HSPTOKEN_OS10:
if((tok = expectToken(sp, tok, HSPTOKEN_STARTOBJ)) == NULL) return NO;
sp->opx.opx = YES;
sp->opx.opx = YES; // os10 now maps to opx internally
level[++depth] = HSPOBJ_OS10;
break;
case HSPTOKEN_OPX:
Expand Down Expand Up @@ -1769,8 +1769,11 @@ extern "C" {
{
switch(tok->stok) {
case HSPTOKEN_SWITCHPORT:
if((tok = expectRegex(sp, tok, &sp->opx.swp_regex)) == NULL) return NO;
sp->opx.swp_regex_str = my_strdup(tok->str);
if((tok = expectRegex(sp, tok, &sp->sonic.swp_regex)) == NULL) return NO;
sp->sonic.swp_regex_str = my_strdup(tok->str);
break;
case HSPTOKEN_UNIXSOCK:
if((tok = expectONOFF(sp, tok, &sp->sonic.unixsock)) == NULL) return NO;
break;
default:
unexpectedToken(sp, tok, level[depth]);
Expand Down
1 change: 1 addition & 0 deletions src/Linux/hsflowd.h
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ extern "C" {
bool sonic;
char *swp_regex_str;
regex_t *swp_regex;
bool unixsock;
} sonic;
struct {
bool nvml;
Expand Down
1 change: 1 addition & 0 deletions src/Linux/hsflowtokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ HSPTOKEN_DATA( HSPTOKEN_SW, "sw", HSPTOKENTYPE_ATTRIB, NULL)
HSPTOKEN_DATA( HSPTOKEN_HW, "hw", HSPTOKENTYPE_ATTRIB, NULL)
HSPTOKEN_DATA( HSPTOKEN_TUNNEL, "tunnel", HSPTOKENTYPE_ATTRIB, NULL)
HSPTOKEN_DATA( HSPTOKEN_MAX, "max", HSPTOKENTYPE_ATTRIB, NULL)
HSPTOKEN_DATA( HSPTOKEN_UNIXSOCK, "unixsock", HSPTOKENTYPE_ATTRIB, NULL)
31 changes: 23 additions & 8 deletions src/Linux/mod_sonic.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ extern "C" {
int dbNo;
EVMod *mod;
char *dbInstance;
// connect via TCP
char *hostname;
int port;
// or via unix domain socket
char *unixSocketPath;
EVSocket *sock;
bool connected;
uint32_t reads;
Expand Down Expand Up @@ -431,9 +434,9 @@ extern "C" {
return UTHashGet(mdata->dbInstances, &search);
}

static HSPSonicDBClient *addDB(EVMod *mod, char *dbInstance, char *hostname, int port) {
static HSPSonicDBClient *addDB(EVMod *mod, char *dbInstance, char *hostname, int port, char *unixSocketPath) {
HSP_mod_SONIC *mdata = (HSP_mod_SONIC *)mod->data;
myDebug(1, "addDB: %s hostname=%s, port=%d", dbInstance, hostname, port);
myDebug(1, "addDB: %s hostname=%s, port=%d, unixSocketPath=%s", dbInstance, hostname, port, unixSocketPath);
HSPSonicDBClient *db = getDB(mod, dbInstance);
if(db == NULL) {
db = (HSPSonicDBClient *)my_calloc(sizeof(HSPSonicDBClient));
Expand All @@ -442,6 +445,7 @@ extern "C" {
db->mod = mod;
db->hostname = my_strdup(hostname);
db->port = port;
db->unixSocketPath = my_strdup(unixSocketPath);
UTHashAdd(mdata->dbInstances, db);
// the socket will be opened later
}
Expand All @@ -453,6 +457,7 @@ extern "C" {
my_free(db->dbInstance);
UTStrBuf_free(db->replyBuf);
my_free(db->hostname);
my_free(db->unixSocketPath);
my_free(db);
}
#endif
Expand Down Expand Up @@ -536,9 +541,9 @@ extern "C" {
for(cJSON *inst = instances->child; inst; inst = inst->next) {
cJSON *hostname = cJSON_GetObjectItem(inst, "hostname");
cJSON *port = cJSON_GetObjectItem(inst, "port");
// cJSON *unixSockPath = cJSON_GetObjectItem(inst, "unix_socket_path");
cJSON *unixSock = cJSON_GetObjectItem(inst, "unix_socket_path");
// cJSON *persist = cJSON_GetObjectItem(inst, "persistence_for_warm_boot");
addDB(mod, inst->string, hostname->valuestring, port->valueint);
addDB(mod, inst->string, hostname->valuestring, port->valueint, unixSock->valuestring);
}
for(cJSON *dbTab = databases->child; dbTab; dbTab = dbTab->next) {
cJSON *id = cJSON_GetObjectItem(dbTab, "id");
Expand Down Expand Up @@ -569,7 +574,8 @@ extern "C" {
&& configTab->dbClient) {
configTab->evtClient = addDB(mod, HSP_SONIC_DB_CONFIG_NAME HSP_SONIC_DB_EVENT_SUFFIX,
configTab->dbClient->hostname,
configTab->dbClient->port);
configTab->dbClient->port,
configTab->dbClient->unixSocketPath);
}
}

Expand Down Expand Up @@ -649,15 +655,24 @@ extern "C" {
}

static bool db_connectClient(EVMod *mod, HSPSonicDBClient *db) {
HSP *sp = (HSP *)EVROOTDATA(mod);
HSP_mod_SONIC *mdata = (HSP_mod_SONIC *)mod->data;
myDebug(1, "sonic db_connectClient %s = %s:%d", db->dbInstance, db->hostname, db->port);
redisAsyncContext *ctx = db->ctx = redisAsyncConnect(db->hostname, db->port);
redisAsyncContext *ctx = NULL;
if(sp->sonic.unixsock
&& db->unixSocketPath) {
myDebug(1, "sonic db_connectClient %s = %s", db->dbInstance, db->unixSocketPath);
ctx = db->ctx = redisAsyncConnectUnix(db->unixSocketPath);
}
else {
myDebug(1, "sonic db_connectClient %s = %s:%d", db->dbInstance, db->hostname, db->port);
ctx = db->ctx = redisAsyncConnect(db->hostname, db->port);
}
if(ctx) {
redisAsyncSetConnectCallback(ctx, db_connectCB);
redisAsyncSetDisconnectCallback(ctx, db_disconnectCB);
int fd = ctx->c.fd;
myDebug(1, "sonic redis fd == %d", fd);
if(fd > 0) {
myDebug(1, "sonic redis fd == %d", fd);
db->sock = EVBusAddSocket(mod, mdata->pollBus, fd, db_readCB, db /* magic */);
// db->ev.addRead = db_addReadCB; // EVBus always ready to read
// db->ev.delRead = db_delReadCB; // no-op
Expand Down

0 comments on commit 4bbb6c2

Please sign in to comment.