Skip to content

Commit 7412215

Browse files
authored
[dhcp_server] Support scenario that gatway is not configured (sonic-net#21462)
Why I did it DHCP default route shoule be an optional config to DHCP client Work item tracking Microsoft ADO (number only): 30877295 How I did it Support to do not send default route to dhcp client How to verify it UT Install new image to test
1 parent 6415e4d commit 7412215

File tree

5 files changed

+73
-19
lines changed

5 files changed

+73
-19
lines changed

dockers/docker-dhcp-server/kea-dhcp4.conf.j2

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@
6363
"always-send": {{ config["always_send"] }}
6464
},
6565
{%- endfor %}
66+
{%- if "gateway" in subnet_info -%}
6667
{
6768
"name": "routers",
68-
"data": "{{ subnet_info["gateway"] if "gateway" in subnet_info else subnet_info["server_id"] }}"
69+
"data": "{{ subnet_info["gateway"] }}"
6970
},
71+
{%- endif -%}
7072
{
7173
"name": "dhcp-server-identifier",
7274
"data": "{{ subnet_info["server_id"] }}"

src/sonic-dhcp-utilities/dhcp_utilities/dhcpservd/dhcp_cfggen.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,12 @@ def _construct_obj_for_template(self, dhcp_server_ipv4, port_ips, hostname, cust
229229
"id": MID_PLANE_BRIDGE_SUBNET_ID if smart_switch else dhcp_interface_name.replace("Vlan", ""),
230230
"subnet": str(ipaddress.ip_network(dhcp_interface_ip, strict=False)),
231231
"pools": pools,
232-
"gateway": dhcp_config["gateway"],
233232
"server_id": dhcp_interface_ip.split("/")[0],
234233
"lease_time": dhcp_config["lease_time"] if "lease_time" in dhcp_config else DEFAULT_LEASE_TIME,
235234
"customized_options": curr_options
236235
}
236+
if "gateway" in dhcp_config:
237+
subnet_obj["gateway"] = dhcp_config["gateway"]
237238
subnets.append(subnet_obj)
238239
render_obj = {
239240
"subnets": subnets,

src/sonic-dhcp-utilities/tests/test_data/kea-dhcp4.conf.j2

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@
6363
"always-send": {{ config["always_send"] }}
6464
},
6565
{%- endfor %}
66+
{%- if "gateway" in subnet_info %}
6667
{
6768
"name": "routers",
68-
"data": "{{ subnet_info["gateway"] if "gateway" in subnet_info else subnet_info["server_id"] }}"
69+
"data": "{{ subnet_info["gateway"] }}"
6970
},
71+
{%- endif %}
7072
{
7173
"name": "dhcp-server-identifier",
7274
"data": "{{ subnet_info["server_id"] }}"

src/sonic-dhcp-utilities/tests/test_data/mock_config_db.json

-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
"option60",
8484
"option223"
8585
],
86-
"gateway": "192.168.3.1",
8786
"lease_time": "900",
8887
"mode": "PORT",
8988
"netmask": "255.255.255.0",

src/sonic-dhcp-utilities/tests/test_dhcp_cfggen.py

+65-15
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@
7070
],
7171
"valid-lifetime": 900,
7272
"reservations": []
73+
},
74+
{
75+
"id": 4000,
76+
"subnet": "192.168.3.0/24",
77+
"pools": [
78+
{
79+
"pool": "192.168.3.2 - 192.168.3.3",
80+
"client-class": "sonic-host:etp11"
81+
}
82+
],
83+
"option-data": [
84+
{
85+
"name": "dhcp-server-identifier",
86+
"data": "192.168.3.1"
87+
}
88+
],
89+
"valid-lifetime": 900,
90+
"reservations": []
7391
}
7492
],
7593
"loggers": [
@@ -93,12 +111,23 @@
93111
{
94112
"name": "sonic-host:etp7",
95113
"test": "substring(relay4[1].hex, -15, 15) == 'sonic-host:etp7'"
114+
},
115+
{
116+
"name": "sonic-host:etp11",
117+
"test": "substring(relay4[1].hex, -16, 16) == 'sonic-host:etp11'"
96118
}
97119
]
98120
}
99121
}
100122
expected_dhcp_config_without_port_config = {
101123
"Dhcp4": {
124+
"option-def": [
125+
{
126+
"name": "option223",
127+
"code": 223,
128+
"type": "string"
129+
}
130+
],
102131
"hooks-libraries": [
103132
{
104133
"library": "/usr/local/lib/kea/hooks/libdhcp_run_script.so",
@@ -194,11 +223,23 @@
194223
"value": "dummy_value"
195224
}
196225
}
226+
},
227+
{
228+
"subnet": "192.168.3.0/24", 'id': '4000',
229+
"pools": [{"range": "192.168.3.2 - 192.168.3.3", "client_class": "sonic-host:etp11"}],
230+
"server_id": "192.168.3.1", "lease_time": "900",
231+
"customized_options": {
232+
"option223": {
233+
"always_send": "true",
234+
"value": "dummy_value"
235+
}
236+
}
197237
}
198238
],
199239
"client_classes": [
200240
{"name": "sonic-host:etp8", "condition": "substring(relay4[1].hex, -15, 15) == 'sonic-host:etp8'"},
201-
{"name": "sonic-host:etp7", "condition": "substring(relay4[1].hex, -15, 15) == 'sonic-host:etp7'"}
241+
{"name": "sonic-host:etp7", "condition": "substring(relay4[1].hex, -15, 15) == 'sonic-host:etp7'"},
242+
{"name": "sonic-host:etp11", "condition": "substring(relay4[1].hex, -16, 16) == 'sonic-host:etp11'"}
202243
],
203244
"lease_update_script_path": "/etc/kea/lease_update.sh",
204245
"lease_path": "/tmp/kea-lease.csv",
@@ -238,9 +279,9 @@
238279
"id": "218",
239280
"type": "string",
240281
"value": "long_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_value" +
241-
"long_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_value" +
242-
"long_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_value" +
243-
"long_valuelong_valuelong_valuelong_valuelong_value"
282+
"long_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_value" +
283+
"long_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_value" +
284+
"long_valuelong_valuelong_valuelong_valuelong_value"
244285
},
245286
"option217": {
246287
"id": "217",
@@ -359,6 +400,11 @@ def test_construct_obj_for_template(mock_swsscommon_dbconnector_init, mock_parse
359400
"etp9": []
360401
}
361402
},
403+
"Vlan4000": {
404+
"192.168.3.1/24": {
405+
"etp11": [["192.168.3.2", "192.168.3.3"]]
406+
}
407+
},
362408
"Vlan6000": {
363409
}
364410
}
@@ -374,25 +420,29 @@ def test_construct_obj_for_template(mock_swsscommon_dbconnector_init, mock_parse
374420
@pytest.mark.parametrize("with_port_config", [True, False])
375421
@pytest.mark.parametrize("with_option_config", [True, False])
376422
def test_render_config(mock_swsscommon_dbconnector_init, mock_parse_port_map_alias, with_port_config,
377-
with_option_config):
423+
with_option_config, request):
378424
dhcp_db_connector = DhcpDbConnector()
379425
dhcp_cfg_generator = DhcpServCfgGenerator(dhcp_db_connector, "/usr/local/lib/kea/hooks/libdhcp_run_script.so",
380426
kea_conf_template_path="tests/test_data/kea-dhcp4.conf.j2")
381427
render_obj = copy.deepcopy(expected_render_obj)
382-
expected_config = copy.deepcopy(expected_dhcp_config)
383428
if not with_port_config:
384429
render_obj["client_classes"] = []
385430
render_obj["subnets"] = []
386-
elif not with_option_config:
387-
render_obj["subnets"][0]["customized_options"] = {}
431+
expected_config = copy.deepcopy(expected_dhcp_config_without_port_config)
432+
else:
433+
expected_config = copy.deepcopy(expected_dhcp_config)
434+
if not with_option_config:
435+
for i in range(len(expected_config["Dhcp4"]["subnet4"])):
436+
render_obj["subnets"][i]["customized_options"] = {}
437+
else:
438+
for i in range(len(expected_config["Dhcp4"]["subnet4"])):
439+
expected_config["Dhcp4"]["subnet4"][i]["option-data"].insert(0, {
440+
"name": "option223",
441+
"data": "dummy_value",
442+
"always-send": True
443+
})
388444
config = dhcp_cfg_generator._render_config(render_obj)
389-
if with_option_config:
390-
expected_config["Dhcp4"]["subnet4"][0]["option-data"].insert(0, {
391-
"name": "option223",
392-
"data": "dummy_value",
393-
"always-send": True
394-
})
395-
assert json.loads(config) == expected_config if with_port_config else expected_config
445+
assert json.loads(config) == expected_config
396446

397447

398448
def test_parse_customized_options(mock_swsscommon_dbconnector_init, mock_get_render_template,

0 commit comments

Comments
 (0)