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
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ jobs:
libibverbs-dev libasan8 libcmocka-dev libedit-dev libarchive-dev \
libevent-dev libsmartcols-dev libmnl-dev libnuma-dev python3-pyelftools \
socat tcpdump traceroute graphviz iproute2 iputils-ping ndisc6 jq \
dnsmasq \
"linux-modules-extra-$(uname -r)"
if echo $MESON_EXTRA_OPTS | grep -q frr=enabled ; then
sudo apt-get install -qy --no-install-recommends \
Expand Down
964 changes: 494 additions & 470 deletions docs/graph.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions modules/dhcp/api/gr_dhcp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2025 Anthony Harivel

#pragma once

#include <gr_api.h>
#include <gr_infra.h>
#include <gr_net_types.h>

#include <stdint.h>

typedef enum dhcp_state : uint8_t {
DHCP_STATE_INIT = 0,
DHCP_STATE_SELECTING,
DHCP_STATE_REQUESTING,
DHCP_STATE_BOUND,
DHCP_STATE_RENEWING,
DHCP_STATE_REBINDING,
} dhcp_state_t;

struct gr_dhcp_status {
uint16_t iface_id;
dhcp_state_t state;
ip4_addr_t server_ip;
ip4_addr_t assigned_ip;
uint32_t lease_time;
uint32_t renewal_time; // T1
uint32_t rebind_time; // T2
Comment on lines +26 to +28
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using a more specific type, such as clock_t ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clock_t is for measuring CPU time (clock()), NOT calendar time. Using it for lease timestamps would be incorrect.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So time_t may be more appropriate ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

time_t is for absolute timestamps (calendar time), not durations. uint32_t max = 4,294,967,295 seconds ≈ 136 years (way more than any DHCP lease lol). I use time_t for timestamps (lease_start) but uint32_t for durations (lease_time, renewal_time, rebind_time)

};

#define GR_DHCP_MODULE 0xd4c9

// list ////////////////////////////////////////////////////////////////////////

#define GR_DHCP_LIST REQUEST_TYPE(GR_DHCP_MODULE, 0x01)

// struct gr_dhcp_list_req { };

// STREAM(struct gr_dhcp_status);

// start ///////////////////////////////////////////////////////////////////////

#define GR_DHCP_START REQUEST_TYPE(GR_DHCP_MODULE, 0x02)

struct gr_dhcp_start_req {
uint16_t iface_id;
};

// struct gr_dhcp_start_resp { };

// stop ////////////////////////////////////////////////////////////////////////

#define GR_DHCP_STOP REQUEST_TYPE(GR_DHCP_MODULE, 0x03)

struct gr_dhcp_stop_req {
uint16_t iface_id;
};

// struct gr_dhcp_stop_resp { };
5 changes: 5 additions & 0 deletions modules/dhcp/api/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2025 Anthony Harivel

api_headers += files('gr_dhcp.h')
api_inc += include_directories('.')
177 changes: 177 additions & 0 deletions modules/dhcp/cli/dhcp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2025 Anthony Harivel

#include <gr_cli.h>
#include <gr_cli_iface.h>
#include <gr_dhcp.h>
#include <gr_net_types.h>
#include <gr_table.h>

#include <ecoli.h>
#include <libsmartcols.h>

#include <errno.h>
#include <stdio.h>
#include <string.h>

static cmd_status_t dhcp_enable_cmd(struct gr_api_client *c, const struct ec_pnode *p) {
const char *iface_name = arg_str(p, "IFACE");
struct gr_dhcp_start_req req;
struct gr_iface *iface;

iface = iface_from_name(c, iface_name);
if (iface == NULL)
return CMD_ERROR;

req.iface_id = iface->id;
free(iface);

if (gr_api_client_send_recv(c, GR_DHCP_START, sizeof(req), &req, NULL) < 0)
return CMD_ERROR;

return CMD_SUCCESS;
}

static cmd_status_t dhcp_disable_cmd(struct gr_api_client *c, const struct ec_pnode *p) {
const char *iface_name = arg_str(p, "IFACE");
struct gr_dhcp_stop_req req;
struct gr_iface *iface;

iface = iface_from_name(c, iface_name);
if (iface == NULL)
return CMD_ERROR;

req.iface_id = iface->id;
free(iface);

if (gr_api_client_send_recv(c, GR_DHCP_STOP, sizeof(req), &req, NULL) < 0)
return CMD_ERROR;

return CMD_SUCCESS;
}

static const char *dhcp_state_str(enum dhcp_state state) {
switch (state) {
case DHCP_STATE_INIT:
return "INIT";
case DHCP_STATE_SELECTING:
return "SELECTING";
case DHCP_STATE_REQUESTING:
return "REQUESTING";
case DHCP_STATE_BOUND:
return "BOUND";
case DHCP_STATE_RENEWING:
return "RENEWING";
case DHCP_STATE_REBINDING:
return "REBINDING";
default:
return "UNKNOWN";
}
}

static cmd_status_t dhcp_show_cmd(struct gr_api_client *c, const struct ec_pnode *) {
const struct gr_dhcp_status *status;
struct libscols_table *table;
int ret;

table = scols_new_table();
if (table == NULL)
return CMD_ERROR;

scols_table_new_column(table, "INTERFACE", 0, 0);
scols_table_new_column(table, "STATE", 0, 0);
scols_table_new_column(table, "ADDRESS", 0, 0);
scols_table_new_column(table, "SERVER", 0, 0);
scols_table_new_column(table, "LEASE", 0, SCOLS_FL_RIGHT);

gr_api_client_stream_foreach (status, ret, c, GR_DHCP_LIST, 0, NULL) {
struct libscols_line *line = scols_table_new_line(table, NULL);
struct gr_iface *iface = iface_from_id(c, status->iface_id);

if (iface != NULL) {
scols_line_sprintf(line, 0, "%s", iface->name);
free(iface);
} else {
scols_line_sprintf(line, 0, "%u", status->iface_id);
}

scols_line_sprintf(line, 1, "%s", dhcp_state_str(status->state));

if (status->assigned_ip != 0) {
scols_line_sprintf(line, 2, IP4_F, &status->assigned_ip);
} else {
scols_line_sprintf(line, 2, "-");
}

if (status->server_ip != 0) {
scols_line_sprintf(line, 3, IP4_F, &status->server_ip);
} else {
scols_line_sprintf(line, 3, "-");
}

if (status->lease_time != 0)
scols_line_sprintf(line, 4, "%us", status->lease_time);
else
scols_line_sprintf(line, 4, "-");
}

if (ret < 0) {
scols_unref_table(table);
return CMD_ERROR;
}

scols_print_table(table);
scols_unref_table(table);

return CMD_SUCCESS;
}

static int ctx_init(struct ec_node *root) {
int ret;

ret = CLI_COMMAND(
CLI_CONTEXT(root, CTX_ARG("dhcp", "DHCP client.")),
"enable IFACE",
dhcp_enable_cmd,
"Enable DHCP on interface.",
with_help(
"Interface name.",
ec_node_dyn("IFACE", complete_iface_names, INT2PTR(GR_IFACE_TYPE_UNDEF))
)
);
if (ret < 0)
return ret;

ret = CLI_COMMAND(
CLI_CONTEXT(root, CTX_ARG("dhcp", "DHCP client.")),
"disable IFACE",
dhcp_disable_cmd,
"Disable DHCP on interface.",
with_help(
"Interface name.",
ec_node_dyn("IFACE", complete_iface_names, INT2PTR(GR_IFACE_TYPE_UNDEF))
)
);
if (ret < 0)
return ret;

ret = CLI_COMMAND(
CLI_CONTEXT(root, CTX_ARG("dhcp", "DHCP client.")),
"show",
dhcp_show_cmd,
"Show DHCP client status."
);
if (ret < 0)
return ret;

return 0;
}

static struct cli_context ctx = {
.name = "dhcp",
.init = ctx_init,
};

static void __attribute__((constructor, used)) init(void) {
cli_context_register(&ctx);
}
6 changes: 6 additions & 0 deletions modules/dhcp/cli/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2025 Anthony Harivel

cli_src += files(
'dhcp.c',
)
Loading