Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions bessctl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,35 @@ def reset(cli):
cli.softnic.reset_all()
cli.softnic.resume_all()

@cmd('kill', 'Kill bessd instance')
def kill(cli):
if cli.interactive:
if cli.rl:
cli.rl.set_completer(cli.complete_dummy)

response = raw_input('WARNING: bessd will be killed. ' \
'Are you sure? (type "yes") ')

if cli.rl:
# don't leave response in the history
cli.rl.remove_history_item(cli.rl.get_current_history_length() - 1)

if response.strip() == 'yes':
cli.softnic.pause_all()
cli.softnic.kill()
cli.softnic.resume_all()
cli.fout.write('Reset completed.\n')
else:
cli.fout.write('Cancelled.\n')

if cli.rl:
cli.rl.set_completer(cli.complete)

else:
cli.softnic.pause_all()
cli.softnic.kill()
cli.softnic.resume_all()

@staticmethod
def _choose_arg(arg, kwargs):
if kwargs:
Expand Down
36 changes: 35 additions & 1 deletion core/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
Expand All @@ -13,6 +14,7 @@
#include "master.h"
#include "worker.h"
#include "driver.h"
#include "syslog.h"

#if 0
#include "old_modules/vport_guest_sndrv.h"
Expand Down Expand Up @@ -908,6 +910,8 @@ static struct {
uint16_t port;
} cmdline_opts;

static int daemonize = 1;

static void parse_core_list()
{
char *ptr;
Expand Down Expand Up @@ -939,6 +943,8 @@ static void print_usage(char *exec_name)
fprintf(stderr, " %-16s Specifies the TCP port on which SoftNIC listens"
"for controller connections\n",
"-p <port>");
fprintf(stderr, " %-16s Do not daemonize BESS",
"-w");

exit(2);
}
Expand All @@ -951,7 +957,7 @@ static void init_config(int argc, char **argv)

num_workers = 0;

while ((c = getopt(argc, argv, ":tc:p:")) != -1) {
while ((c = getopt(argc, argv, ":tc:p:w")) != -1) {
switch (c) {
case 't':
dump_types();
Expand All @@ -965,6 +971,9 @@ static void init_config(int argc, char **argv)
case 'p':
sscanf(optarg, "%hu", &cmdline_opts.port);
break;
case 'w':
daemonize = 0;
break;

case ':':
fprintf(stderr, "argument is required for -%c\n",
Expand All @@ -991,7 +1000,29 @@ static void init_config(int argc, char **argv)

int main(int argc, char **argv)
{
pid_t pid, sid;
init_config(argc, argv);

if (daemonize) {
pid = fork();
if (pid < 0) {
fprintf(stderr, "Could not fork damon\n");
goto fail;
}
if (pid > 0) {
exit(EXIT_SUCCESS);
}
// Reparent
sid = setsid();
if (sid < 0) {
goto fail;
}

close(STDIN_FILENO);
close(STDERR_FILENO);
close(STDOUT_FILENO);
setup_syslog();
}
init_dpdk(argv[0]);
init_mempool();
init_drivers();
Expand All @@ -1001,6 +1032,9 @@ int main(int argc, char **argv)

run_master(cmdline_opts.port);

if (daemonize)
end_syslog();
fail:
/* never executed */
rte_eal_mp_wait_lcore();
close_mempool();
Expand Down
12 changes: 12 additions & 0 deletions core/snctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>

#include <pthread.h>

Expand Down Expand Up @@ -512,6 +513,16 @@ static struct snobj *handle_disconnect_modules(struct snobj *arg)
return NULL;
}

/* Adding this mostly to provide a reasonable way to exit when daemonized */
static struct snobj *handle_kill_bess(struct snobj *arg)
{
printf("bessd kill called\n");
exit(EXIT_SUCCESS);

/* Never called */
return NULL;
}

static struct snobj *handle_not_implemented(struct snobj *q)
{
return snobj_err(ENOTSUP, "Not implemented yet");
Expand Down Expand Up @@ -548,6 +559,7 @@ static struct handler_map sn_handlers[] = {
{ "enable_tcpdump", 1, handle_enable_tcpdump },
{ "disable_tcpdump", 1, handle_disable_tcpdump },

{ "kill_bess", 1, handle_kill_bess },
{ NULL, 0, NULL }
};

Expand Down
44 changes: 44 additions & 0 deletions core/syslog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <syslog.h>
#ifndef __SYSLOG_H__
#define __SYSLOG_H__
static ssize_t stdout_writer(void *cookie, const char *data, size_t len)
{
syslog(LOG_INFO, "%.*s", (int)len, data);
return len;
}

static ssize_t stderr_writer(void *cookie, const char *data, size_t len)
{
syslog(LOG_ERR, "%.*s", (int)len, data);
return len;
}

#define BESS_ID "bessd"
static void setup_syslog()
{
cookie_io_functions_t stdout_funcs = {
.write = &stdout_writer,
.read = NULL,
.close = NULL,
.seek = NULL
};

cookie_io_functions_t stderr_funcs = {
.write = &stderr_writer,
.read = NULL,
.close = NULL,
.seek = NULL
};

openlog(BESS_ID, LOG_CONS | LOG_NDELAY, LOG_DAEMON);

setvbuf(stderr = fopencookie(NULL, "w", stderr_funcs), NULL, _IOLBF, 0);

setvbuf(stdout = fopencookie(NULL, "w", stdout_funcs), NULL, _IOLBF, 0);
}

static void end_syslog()
{
closelog();
}
#endif
4 changes: 4 additions & 0 deletions libbess-python/softnic.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,13 @@ def _request_module(self, name, cmd, arg = None):
else:
return self._request({'to': 'module', 'name': name, 'cmd': cmd})

def kill(self):
return self._request_softnic('kill_bess')

def reset_all(self):
return self._request_softnic('reset_all')


def pause_all(self):
return self._request_softnic('pause_all')

Expand Down