Skip to content

Commit c400366

Browse files
committed
Incorporate DHCPv6 Relay Agent into dhcp-relay docker (sonic-net#8321)
(cherry picked from commit bc06c6f)
1 parent 0b2746a commit c400366

14 files changed

+83
-73
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
group:isc-dhcp-relay
1+
group:dhcp-relay

dockers/docker-dhcp-relay/dhcp-relay.programs.j2

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[group:isc-dhcp-relay]
1+
[group:dhcp-relay]
22
programs=
33
{%- set add_preceding_comma = { 'flag': False } %}
44
{% for vlan_name in VLAN_INTERFACE %}
@@ -12,6 +12,6 @@ isc-dhcpv4-relay-{{ vlan_name }}
1212
{% if VLAN and vlan_name in VLAN and 'dhcpv6_servers' in VLAN[vlan_name] and VLAN[vlan_name]['dhcpv6_servers']|length > 0 %}
1313
{% if add_preceding_comma.flag %},{% endif %}
1414
{% set _dummy = add_preceding_comma.update({'flag': True}) %}
15-
isc-dhcpv6-relay-{{ vlan_name }}
15+
dhcp6relay
1616
{%- endif %}
1717
{% endfor %}

dockers/docker-dhcp-relay/dhcpv6-relay.agents.j2

+2-17
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,8 @@
77
{% endfor %}
88
{% if relay_for_ipv6.flag %}
99
{% set _dummy = relay_for_ipv6.update({'flag': False}) %}
10-
[program:isc-dhcpv6-relay-{{ vlan_name }}]
11-
{# We treat this VLAN as a downstream interface (-l), as we only want to listen for requests #}
12-
command=/usr/sbin/dhcrelay -d -6 --name-alias-map-file /tmp/port-name-alias-map.txt -l {{ vlan_name }}
13-
{#- We treat all other interfaces as upstream interfaces (-u), as we only want to listen for replies #}
14-
{%- for dhcpv6_server in VLAN[vlan_name]['dhcpv6_servers'] %}
15-
{%- if dhcpv6_server | ipv6 %}
16-
{%- for (name, prefix) in VLAN_INTERFACE|pfx_filter %}
17-
{%- if prefix | ipv6 and name != vlan_name %} -u {{ dhcpv6_server }}%%{{ name }} {% endif -%}
18-
{% endfor %}
19-
{% for (name, prefix) in INTERFACE|pfx_filter %}
20-
{% if prefix | ipv6 %} -u {{ dhcpv6_server }}%%{{ name }} {% endif -%}
21-
{% endfor %}
22-
{% for (name, prefix) in PORTCHANNEL_INTERFACE|pfx_filter %}
23-
{% if prefix | ipv6 %} -u {{ dhcpv6_server }}%%{{ name }} {% endif -%}
24-
{% endfor %}
25-
{% endif -%}
26-
{% endfor %}
10+
[program:dhcp6relay]
11+
command=/usr/sbin/dhcp6relay
2712

2813
priority=3
2914
autostart=false

dockers/docker-dhcp-relay/dhcpv6-relay.monitors.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ stderr_logfile=syslog
7070
dependent_startup=true
7171
dependent_startup_wait_for=
7272
{%- if relay_for_ipv4.flag %}isc-dhcpv4-relay-{{ vlan_name }}:running {% endif %}
73-
{% if relay_for_ipv6.flag %}isc-dhcpv6-relay-{{ vlan_name }}:running{% endif %}
73+
{% if relay_for_ipv6.flag %}dhcp6relay:running{% endif %}
7474

7575

7676
{% set _dummy = relay_for_ipv4.update({'flag': False}) %}

rules/dhcp6relay.dep

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
SPATH := $($(SONIC_DHCP6RELAY)_SRC_PATH)
3+
DEP_FILES := $(SONIC_COMMON_FILES_LIST) rules/dhcp6relay.mk rules/dhcp6relay.dep
4+
DEP_FILES += $(SONIC_COMMON_BASE_FILES_LIST)
5+
DEP_FILES += $(shell git ls-files $(SPATH))
6+
7+
$(SONIC_DHCP6RELAY)_CACHE_MODE := GIT_CONTENT_SHA
8+
$(SONIC_DHCP6RELAY)_DEP_FLAGS := $(SONIC_COMMON_FLAGS_LIST)
9+
$(SONIC_DHCP6RELAY)_DEP_FILES := $(DEP_FILES)

rules/dhcp6relay.mk

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SONiC DHCPV6 RELAY Package
2+
3+
SONIC_DHCP6RELAY_VERSION = 1.0.0-0
4+
SONIC_DHCP6RELAY_PKG_NAME = dhcp6relay
5+
6+
SONIC_DHCP6RELAY = sonic-$(SONIC_DHCP6RELAY_PKG_NAME)_$(SONIC_DHCP6RELAY_VERSION)_$(CONFIGURED_ARCH).deb
7+
$(SONIC_DHCP6RELAY)_DEPENDS = $(LIBSWSSCOMMON) $(LIBHIREDIS) $(LIBSWSSCOMMON_DEV) $(LIBHIREDIS_DEV)
8+
$(DOCKER_DHCP_RELAY)_INSTALL_DEBS = $(LIBSWSSCOMMON) $(LIBHIREDIS) $(LIBSWSSCOMMON_DEV) $(LIBHIREDIS_DEV)
9+
$(SONIC_DHCP6RELAY)_SRC_PATH = $(SRC_PATH)/$(SONIC_DHCP6RELAY_PKG_NAME)
10+
SONIC_DPKG_DEBS += $(SONIC_DHCP6RELAY)

rules/docker-dhcp-relay.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ DOCKER_DHCP_RELAY_DBG = $(DOCKER_DHCP_RELAY_STEM)-$(DBG_IMAGE_MARK).gz
66

77
$(DOCKER_DHCP_RELAY)_PATH = $(DOCKERS_PATH)/$(DOCKER_DHCP_RELAY_STEM)
88

9-
$(DOCKER_DHCP_RELAY)_DEPENDS += $(ISC_DHCP_RELAY) $(SONIC_DHCPMON)
9+
$(DOCKER_DHCP_RELAY)_DEPENDS += $(ISC_DHCP_RELAY) $(SONIC_DHCPMON) $(SONIC_DHCP6RELAY) $(LIBSWSSCOMMON)
1010

1111
$(DOCKER_DHCP_RELAY)_DBG_DEPENDS = $($(DOCKER_CONFIG_ENGINE_BUSTER)_DBG_DEPENDS)
1212
$(DOCKER_DHCP_RELAY)_DBG_DEPENDS += $(ISC_DHCP_RELAY_DBG)

src/dhcp6relay/src/configInterface.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ swss::SubscriberStateTable ipHelpersTable(configDbPtr.get(), "DHCP_RELAY");
1313
swss::Select swssSelect;
1414

1515
/**
16-
* @code void deinitialize_swss()
16+
* @code void initialize_swss()
1717
*
1818
* @brief initialize DB tables and start SWSS listening thread
1919
*
2020
* @return none
2121
*/
22-
void initialize_swss(std::vector<relay_config> *vlans, swss::DBConnector *db)
22+
void initialize_swss(std::vector<relay_config> *vlans)
2323
{
2424
try {
2525
swssSelect.addSelectable(&ipHelpersTable);
@@ -85,7 +85,7 @@ void handleSwssNotification(std::vector<relay_config> *vlans)
8585
* @brief handles DHCPv6 relay configuration change notification
8686
*
8787
* @param ipHelpersTable DHCP table
88-
* @param context list of vlans/argument config that contains strings of server and option
88+
* @param vlans list of vlans/argument config that contains strings of server and option
8989
*
9090
* @return none
9191
*/
@@ -103,7 +103,7 @@ void handleRelayNotification(swss::SubscriberStateTable &ipHelpersTable, std::ve
103103
* @brief process DHCPv6 relay servers and options configuration change notification
104104
*
105105
* @param entries queue of std::tuple<std::string, std::string, std::vector<FieldValueTuple>> entries in DHCP table
106-
* @param context list of vlans/argument config that contains strings of server and option
106+
* @param vlans list of vlans/argument config that contains strings of server and option
107107
*
108108
* @return none
109109
*/
@@ -117,6 +117,7 @@ void processRelayNotification(std::deque<swss::KeyOpFieldsValuesTuple> &entries,
117117
std::vector<swss::FieldValueTuple> fieldValues = kfvFieldsValues(entry);
118118

119119
relay_config intf;
120+
intf.is_option_79 = true;
120121
intf.interface = vlan;
121122
for (auto &fieldValue: fieldValues) {
122123
std::string f = fvField(fieldValue);
@@ -130,11 +131,8 @@ void processRelayNotification(std::deque<swss::KeyOpFieldsValuesTuple> &entries,
130131
}
131132
syslog(LOG_DEBUG, "key: %s, Operation: %s, f: %s, v: %s", vlan.c_str(), operation.c_str(), f.c_str(), v.c_str());
132133
}
133-
if(f == "dhcpv6_option|rfc6939_support") {
134-
if(v == "true")
135-
intf.is_option_79 = true;
136-
else if(v == "false")
137-
intf.is_option_79 = false;
134+
if(f == "dhcpv6_option|rfc6939_support" && v == "false") {
135+
intf.is_option_79 = false;
138136
}
139137
}
140138
vlans->push_back(intf);

src/dhcp6relay/src/configInterface.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#include "relay.h"
55

66
/**
7-
* @code void deinitialize_swss()
7+
* @code void initialize_swss()
88
*
99
* @brief initialize DB tables and start SWSS listening thread
1010
*
1111
* @return none
1212
*/
13-
void initialize_swss(std::vector<relay_config> *vlans, swss::DBConnector *db);
13+
void initialize_swss(std::vector<relay_config> *vlans);
1414

1515
/**
1616
* @code void deinitialize_swss()
@@ -35,7 +35,7 @@ void get_dhcp(std::vector<relay_config> *vlans);
3535
*
3636
* @brief main thread for handling SWSS notification
3737
*
38-
* @param context list of vlans/argument config that contains strings of server and option
38+
* @param vlans list of vlans/argument config that contains strings of server and option
3939
*
4040
* @return none
4141
*/
@@ -47,7 +47,7 @@ void handleSwssNotification(std::vector<relay_config> *vlans);
4747
* @brief handles DHCPv6 relay configuration change notification
4848
*
4949
* @param ipHelpersTable DHCP table
50-
* @param context list of vlans/argument config that contains strings of server and option
50+
* @param vlans list of vlans/argument config that contains strings of server and option
5151
*
5252
* @return none
5353
*/

src/dhcp6relay/src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ int main(int argc, char *argv[]) {
66
try {
77
std::vector<relay_config> vlans;
88
swss::DBConnector state_db("STATE_DB", 0);
9-
initialize_swss(&vlans, &state_db);
9+
initialize_swss(&vlans);
1010
loop_relay(&vlans, &state_db);
1111
}
1212
catch (std::exception &e)

src/dhcp6relay/src/relay.cpp

+30-21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ struct event *server_listen_event;
1818
struct event_base *base;
1919
struct event *ev_sigint;
2020
struct event *ev_sigterm;
21+
static std::string counter_table = "DHCPv6_COUNTER_TABLE|";
2122

2223
/* DHCPv6 filter */
2324
/* sudo tcpdump -dd "ip6 dst ff02::1:2 && udp dst port 547" */
@@ -325,12 +326,12 @@ int sock_open(int ifindex, const struct sock_fprog *fprog)
325326
*
326327
* @return none
327328
*/
328-
void prepare_relay_config(relay_config *interface_config, int local_sock, int filter) {
329+
void prepare_relay_config(relay_config *interface_config, int *local_sock, int filter) {
329330
struct ifaddrs *ifa, *ifa_tmp;
330331
sockaddr_in6 non_link_local;
331332
sockaddr_in6 link_local;
332333

333-
interface_config->local_sock = local_sock;
334+
interface_config->local_sock = *local_sock;
334335
interface_config->filter = filter;
335336

336337
for(auto server: interface_config->servers) {
@@ -376,22 +377,20 @@ void prepare_relay_config(relay_config *interface_config, int local_sock, int fi
376377
}
377378

378379
/**
379-
* @code prepare_socket(int *local_sock, relay_config *config);
380+
* @code prepare_socket(int *local_sock);
380381
*
381382
* @brief prepare L3 socket for sending
382383
*
383384
* @param local_sock pointer to socket to be prepared
384-
* @param config relay config that contains strings of server and interface addresses
385385
*
386386
* @return none
387387
*/
388-
void prepare_socket(int *local_sock, relay_config *config, int index) {
388+
void prepare_socket(int *local_sock) {
389389
int flag = 1;
390390
sockaddr_in6 addr;
391391
memset(&addr, 0, sizeof(addr));
392392
addr.sin6_family = AF_INET6;
393393
addr.sin6_addr = in6addr_any;
394-
addr.sin6_scope_id = index;
395394
addr.sin6_port = htons(RELAY_PORT);
396395

397396
if ((*local_sock = socket(AF_INET6, SOCK_DGRAM, 0)) == -1) {
@@ -434,11 +433,6 @@ void relay_client(int sock, const uint8_t *msg, int32_t len, const ip6_hdr *ip_h
434433
memcpy(current_buffer_position, &new_message, sizeof(dhcpv6_relay_msg));
435434
current_buffer_position += sizeof(dhcpv6_relay_msg);
436435

437-
438-
auto dhcp_message_length = len;
439-
relay_forward(current_buffer_position, parse_dhcpv6_hdr(msg), dhcp_message_length);
440-
current_buffer_position += dhcp_message_length + sizeof(dhcpv6_option);
441-
442436
if(config->is_option_79) {
443437
linklayer_addr_option option79;
444438
option79.link_layer_type = htons(1);
@@ -447,10 +441,14 @@ void relay_client(int sock, const uint8_t *msg, int32_t len, const ip6_hdr *ip_h
447441

448442
memcpy(current_buffer_position, &option79, sizeof(linklayer_addr_option));
449443
current_buffer_position += sizeof(linklayer_addr_option);
444+
445+
memcpy(current_buffer_position, &ether_hdr->ether_shost, sizeof(ether_hdr->ether_shost));
446+
current_buffer_position += sizeof(ether_hdr->ether_shost);
450447
}
451448

452-
memcpy(current_buffer_position, &ether_hdr->ether_shost, sizeof(ether_hdr->ether_shost));
453-
current_buffer_position += sizeof(ether_hdr->ether_shost);
449+
auto dhcp_message_length = len;
450+
relay_forward(current_buffer_position, parse_dhcpv6_hdr(msg), dhcp_message_length);
451+
current_buffer_position += dhcp_message_length + sizeof(dhcpv6_option);
454452

455453
for(auto server: config->servers_sock) {
456454
send_udp(sock, buffer, server, current_buffer_position - buffer);
@@ -546,11 +544,23 @@ void callback(evutil_socket_t fd, short event, void *arg) {
546544

547545
auto msg = parse_dhcpv6_hdr(current_position);
548546
counters[msg->msg_type]++;
549-
update_counter(config->db, config->counterVlan, msg->msg_type);
547+
std::string counterVlan = counter_table;
548+
update_counter(config->db, counterVlan.append(config->interface), msg->msg_type);
550549

551550
relay_client(config->local_sock, current_position, ntohs(udp_header->len) - sizeof(udphdr), ip_header, ether_header, config);
552551
}
553552

553+
/**
554+
* @code void server_callback(evutil_socket_t fd, short event, void *arg);
555+
*
556+
* @brief callback for libevent that is called everytime data is received at the server socket
557+
*
558+
* @param fd filter socket
559+
* @param event libevent triggered event
560+
* @param arg callback argument provided by user
561+
*
562+
* @return none
563+
*/
554564
void server_callback(evutil_socket_t fd, short event, void *arg) {
555565
struct relay_config *config = (struct relay_config *)arg;
556566
sockaddr_in6 from;
@@ -563,9 +573,9 @@ void server_callback(evutil_socket_t fd, short event, void *arg) {
563573
}
564574

565575
auto msg = parse_dhcpv6_hdr(message_buffer);
566-
567576
counters[msg->msg_type]++;
568-
update_counter(config->db, config->counterVlan, msg->msg_type);
577+
std::string counterVlan = counter_table;
578+
update_counter(config->db, counterVlan.append(config->interface), msg->msg_type);
569579
if (msg->msg_type == DHCPv6_MESSAGE_TYPE_RELAY_REPL) {
570580
relay_relay_reply(config->local_sock, message_buffer, data, config);
571581
}
@@ -673,17 +683,16 @@ void loop_relay(std::vector<relay_config> *vlans, swss::DBConnector *db) {
673683
int index = if_nametoindex(ifname);
674684
config.db = db;
675685

676-
config.counterVlan = "DHCPv6_COUNTER_TABLE|";
677-
config.counterVlan.append(ifname);
678-
initialize_counter(config.db, config.counterVlan);
686+
std::string counterVlan = counter_table;
687+
initialize_counter(config.db, counterVlan.append(config.interface));
679688

680689
filter = sock_open(index, &ether_relay_fprog);
681690

682-
prepare_socket(&local_sock, &config, index);
691+
prepare_socket(&local_sock);
683692
sockets.push_back(filter);
684693
sockets.push_back(local_sock);
685694

686-
prepare_relay_config(&config, local_sock, filter);
695+
prepare_relay_config(&config, &local_sock, filter);
687696

688697
evutil_make_listen_socket_reuseable(filter);
689698
evutil_make_socket_nonblocking(filter);

src/dhcp6relay/src/relay.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ struct relay_config {
4949
std::vector<std::string> servers;
5050
std::vector<sockaddr_in6> servers_sock;
5151
bool is_option_79;
52-
std::string counterVlan;
5352
};
5453

5554

@@ -91,17 +90,15 @@ struct linklayer_addr_option {
9190
int sock_open(int ifindex, const struct sock_fprog *fprog);
9291

9392
/**
94-
* @code prepare_socket(int *local_sock, arg_config *config);
93+
* @code prepare_socket(int *local_sock);
9594
*
9695
* @brief prepare L3 socket for sending
9796
*
9897
* @param local_sock pointer to socket to be prepared
99-
* @param config argument config that contains strings of server and interface addresses
100-
* @param index interface id
10198
*
10299
* @return none
103100
*/
104-
void prepare_socket(int *local_sock, relay_config *config, int index);
101+
void prepare_socket(int *local_sock);
105102

106103
/**
107104
* @code prepare_relay_config(relay_config *interface_config, int local_sock, int filter);
@@ -114,7 +111,7 @@ void prepare_socket(int *local_sock, relay_config *config, int index);
114111
*
115112
* @return none
116113
*/
117-
void prepare_relay_config(relay_config *interface_config, int local_sock, int filter);
114+
void prepare_relay_config(relay_config *interface_config, int *local_sock, int filter);
118115

119116
/**
120117
* @code relay_forward(uint8_t *buffer, const struct dhcpv6_msg *msg, uint16_t msg_length);

src/sonic-config-engine/tests/sample_output/py2/docker-dhcp-relay.supervisord.conf

+6-5
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ stderr_logfile=syslog
3939
dependent_startup=true
4040
dependent_startup_wait_for=rsyslogd:running
4141

42-
[group:isc-dhcp-relay]
43-
programs=isc-dhcpv4-relay-Vlan1000,isc-dhcpv6-relay-Vlan1000
42+
[group:dhcp-relay]
43+
programs=isc-dhcpv4-relay-Vlan1000,dhcp6relay
4444

4545
[program:isc-dhcpv4-relay-Vlan1000]
4646
command=/usr/sbin/dhcrelay -d -m discard -a %%h:%%p %%P --name-alias-map-file /tmp/port-name-alias-map.txt -id Vlan1000 -iu Vlan2000 -iu PortChannel02 -iu PortChannel03 -iu PortChannel04 -iu PortChannel01 192.0.0.1 192.0.0.2
@@ -52,8 +52,9 @@ stderr_logfile=syslog
5252
dependent_startup=true
5353
dependent_startup_wait_for=start:exited
5454

55-
[program:isc-dhcpv6-relay-Vlan1000]
56-
command=/usr/sbin/dhcrelay -d -6 --name-alias-map-file /tmp/port-name-alias-map.txt -l Vlan1000 -u fc02:2000::1%%PortChannel04 -u fc02:2000::1%%PortChannel03 -u fc02:2000::1%%PortChannel01 -u fc02:2000::1%%PortChannel02 -u fc02:2000::2%%PortChannel04 -u fc02:2000::2%%PortChannel03 -u fc02:2000::2%%PortChannel01 -u fc02:2000::2%%PortChannel02
55+
[program:dhcp6relay]
56+
command=/usr/sbin/dhcp6relay
57+
5758
priority=3
5859
autostart=false
5960
autorestart=false
@@ -74,6 +75,6 @@ autorestart=false
7475
stdout_logfile=syslog
7576
stderr_logfile=syslog
7677
dependent_startup=true
77-
dependent_startup_wait_for=isc-dhcpv4-relay-Vlan1000:running isc-dhcpv6-relay-Vlan1000:running
78+
dependent_startup_wait_for=isc-dhcpv4-relay-Vlan1000:running dhcp6relay:running
7879

7980

0 commit comments

Comments
 (0)