Debian Codegraph @

ansible_collections/dellemc/enterprise_sonic/plugins/modules/sonic_dhcp_snooping.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright 2025 Dell Inc. or its subsidiaries. All Rights Reserved.
4 # GNU General Public License v3.0+
5 # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 """
8 The module file for sonic_dhcp_snooping
9 """
10
11 from __future__ import absolute_import, division, print_function
12 __metaclass__ = type
13
14 DOCUMENTATION = """
15 ---
16 module: sonic_dhcp_snooping
17 version_added: 2.3.0
18 notes:
19 - Tested against Enterprise SONiC Distribution by Dell Technologies.
20 - Supports C(check_mode).
21 short_description: "Manage DHCP Snooping on SONiC"
22 description: "This module provides configuration management of DHCP snooping for devices running SONiC."
23 author: Simon Nathans (@simon-nathans), Xiao Han (@Xiao_Han2)
24 options:
25 config:
26 description: The DHCP snooping configuration.
27 type: dict
28 suboptions:
29 afis:
30 description:
31 - List of address families to configure.
32 - "There can be up to two items in this list: one where I(afi=ipv4) and one where I(afi=ipv6) to configure DHCPv4 and DHCPv6, respectively."
33 type: list
34 elements: dict
35 suboptions:
36 afi:
37 description:
38 - The address family to configure.
39 type: str
40 choices: ['ipv4', 'ipv6']
41 required: true
42 enabled:
43 description:
44 - Enable DHCP snooping for I(afi).
45 type: bool
46 vlans:
47 description:
48 - Enable DHCP snooping on a list of VLANs for I(afi).
49 - When I(state=deleted), passing an empty list will disable DHCP snooping in all VLANs
50 type: list
51 elements: str
52 verify_mac:
53 description:
54 - Enable DHCP snooping MAC verification for I(afi).
55 type: bool
56 trusted:
57 description:
58 - Mark interfaces as trusted for DHCP snooping for I(afi).
59 - When I(state=deleted), passing an empty list will delete all trusted interfaces.
60 type: list
61 elements: dict
62 suboptions:
63 intf_name:
64 description:
65 - The interface name.
66 type: str
67 required: true
68 source_bindings:
69 description:
70 - Create a static entry in the DHCP snooping binding database for I(afi).
71 - When I(state=deleted), passing an empty list will delete all source bindings.
72 type: list
73 elements: dict
74 suboptions:
75 mac_addr:
76 description:
77 - The binding's MAC address.
78 type: str
79 required: true
80 ip_addr:
81 description:
82 - The bindings's IP address.
83 type: str
84 intf_name:
85 description:
86 - The binding's interface name.
87 - Can be an Ethernet or a PortChannel interface.
88 type: str
89 vlan_id:
90 description:
91 - The binding's VLAN ID.
92 type: int
93 state:
94 description:
95 - The state of the configuration after module completion.
96 default: merged
97 choices: ['merged', 'deleted', 'overridden', 'replaced']
98 type: str
99 """
100
101 EXAMPLES = """
102 # Using "merged" state
103 #
104 # Before state:
105 # -------------
106 #
107 # sonic# show ip dhcp snooping
108 # !
109 # DHCP snooping is Disabled
110 # DHCP snooping source MAC verification is Disabled
111 # DHCP snooping is enabled on the following VLANs:
112 # DHCP snooping trusted interfaces:
113 # !
114
115 - name: Configure DHCPv4 snooping global settings
116 dellemc.enterprise_sonic.sonic_dhcp_snooping:
117 config:
118 afis:
119 - afi: 'ipv4'
120 enabled: true
121 verify_mac: true
122 vlans: ['1', '2', '3', '5']
123 trusted:
124 - intf_name: 'Ethernet8'
125 state: merged
126
127 # After state:
128 # ------------
129 #
130 # sonic# show ip dhcp snooping
131 # !
132 # DHCP snooping is Enabled
133 # DHCP snooping source MAC verification is Enabled
134 # DHCP snooping is enabled on the following VLANs: 1 2 3 5
135 # DHCP snooping trusted interfaces: Ethernet8
136 # !
137
138
139 # Using "merged" state
140 #
141 # Before state:
142 # -------------
143 #
144 # sonic# show ipv6 dhcp snooping
145 # !
146 # DHCPv6 snooping is Disabled
147 # DHCPv6 snooping source MAC verification is Disabled
148 # DHCPv6 snooping is enabled on the following VLANs:
149 # DHCPv6 snooping trusted interfaces:
150 # !
151
152 - name: Configure DHCPv6 snooping global settings
153 dellemc.enterprise_sonic.sonic_dhcp_snooping:
154 config:
155 afis:
156 - afi: 'ipv6'
157 enabled: true
158 vlans:
159 - '4'
160 trusted:
161 - intf_name: 'Ethernet2'
162 - intf_name: PortChannel1
163 state: merged
164
165 # After state:
166 # ------------
167 #
168 # sonic# show ipv6 dhcp snooping
169 # !
170 # DHCPv6 snooping is Enabled
171 # DHCPv6 snooping source MAC verification is Disabled
172 # DHCPv6 snooping is enabled on the following VLANs: 4
173 # DHCPv6 snooping trusted interfaces: PortChannel1
174 # !
175
176
177 # Using "merged" state
178 #
179 # Before state:
180 # -------------
181 #
182 # sonic# show ip dhcp snooping binding
183 # !
184 # Total number of Dynamic bindings: 0
185 # Total number of Static bindings: 0
186 # Total number of Tentative bindings: 0
187 # MAC Address IP Address VLAN Interface Type Lease (Secs)
188 # ----------------- --------------- ---- ----------- ------- -----------
189 # !
190
191 - name: Add DHCPv4 snooping bindings
192 dellemc.enterprise_sonic.sonic_dhcp_snooping:
193 config:
194 afis:
195 - afi: 'ipv4'
196 source_bindings:
197 - mac_addr: '00:b0:d0:63:c2:26'
198 ip_addr: '192.0.2.146'
199 intf_name: 'Ethernet4'
200 vlan_id: '1'
201 - mac_addr: 'aa:f7:67:fc:f4:9a'
202 ip_addr: '156.33.90.167'
203 intf_name: 'PortChannel1'
204 vlan_id: '2'
205 state: merged
206
207 # After state:
208 # ------------
209 #
210 # sonic# show ip dhcp snooping binding
211 # !
212 # Total number of Dynamic bindings: 0
213 # Total number of Static bindings: 2
214 # Total number of Tentative bindings: 0
215 # MAC Address IP Address VLAN Interface Type Lease (Secs)
216 # ----------------- --------------- ---- ----------- ------- -----------
217 # 00:b0:d0:63:c2:26 192.0.2.146 1 Ethernet4 static NA
218 # aa:f7:67:fc:f4:9a 156.33.90.167 2 PortChannel1 static NA
219 # !
220
221
222 # Using "deleted" state
223 #
224 # Before state:
225 # -------------
226 #
227 # sonic# show ip dhcp snooping
228 # !
229 # DHCP snooping is Enabled
230 # DHCP snooping source MAC verification is Enabled
231 # DHCP snooping is enabled on the following VLANs: 1 2 3 5
232 # DHCP snooping trusted interfaces: Ethernet8
233 # !
234
235 - name: Disable DHCPv4 snooping on some VLANs
236 dellemc.enterprise_sonic.sonic_dhcp_snooping:
237 config:
238 afis:
239 - afi: 'ipv4'
240 vlans:
241 - '3'
242 - '5'
243 state: deleted
244
245 # After state:
246 # ------------
247 #
248 # sonic# show ip dhcp snooping
249 # !
250 # DHCP snooping is Enabled
251 # DHCP snooping source MAC verification is Enabled
252 # DHCP snooping is enabled on the following VLANs: 1 2
253 # DHCP snooping trusted interfaces:
254 # !
255
256
257 # Using "deleted" state
258 #
259 # Before state:
260 # -------------
261 #
262 # sonic# show ipv6 dhcp snooping
263 # !
264 # DHCPv6 snooping is Enabled
265 # DHCPv6 snooping source MAC verification is Disabled
266 # DHCPv6 snooping is enabled on the following VLANs: 4
267 # DHCPv6 snooping trusted interfaces: PortChannel1 PortChannel2 PortChannel3 PortChannel4
268 # !
269
270 - name: Disable DHCPv6 snooping on all VLANs
271 dellemc.enterprise_sonic.sonic_dhcp_snooping:
272 config:
273 afis:
274 - afi: 'ipv6'
275 vlans: []
276 state: deleted
277
278 # After state:
279 # ------------
280 #
281 # sonic# show ipv6 dhcp snooping
282 # !
283 # DHCPv6 snooping is Enabled
284 # DHCPv6 snooping source MAC verification is Disabled
285 # DHCPv6 snooping is enabled on the following VLANs:
286 # DHCPv6 snooping trusted interfaces: PortChannel1 PortChannel2 PortChannel3 PortChannel4
287 # !
288
289
290 # Using "deleted" state
291 #
292 # Before state:
293 # -------------
294 #
295 # sonic# show ipv6 dhcp snooping
296 # !
297 # DHCPv6 snooping is Enabled
298 # DHCPv6 snooping source MAC verification is Disabled
299 # DHCPv6 snooping is enabled on the following VLANs: 4
300 # DHCPv6 snooping trusted interfaces: PortChannel1 PortChannel2 PortChannel3 PortChannel4
301 # !
302
303 - name: Delete all DHCPv6 configuration
304 dellemc.enterprise_sonic.sonic_dhcp_snooping:
305 config:
306 afis:
307 - afi: 'ipv6'
308 state: deleted
309
310 # After state:
311 # ------------
312 #
313 # sonic# show ipv6 dhcp snooping
314 # !
315 # DHCPv6 snooping is Disabled
316 # DHCPv6 snooping source MAC verification is Disabled
317 # DHCPv6 snooping is enabled on the following VLANs:
318 # DHCPv6 snooping trusted interfaces:
319 # !
320
321
322 # Using "deleted" state
323 #
324 # Before state:
325 # -------------
326 #
327 # sonic# show ip dhcp snooping binding
328 # !
329 # Total number of Dynamic bindings: 0
330 # Total number of Static bindings: 2
331 # Total number of Tentative bindings: 0
332 # MAC Address IP Address VLAN Interface Type Lease (Secs)
333 # ----------------- --------------- ---- ----------- ------- -----------
334 # 00:b0:d0:63:c2:26 192.0.2.146 1 Ethernet4 static NA
335 # aa:f7:67:fc:f4:9a 156.33.90.167 2 PortChannel1 static NA
336 # !
337
338 - name: Delete a DHCPv4 snooping binding
339 dellemc.enterprise_sonic.sonic_dhcp_snooping:
340 config:
341 afis:
342 - afi: 'ipv4'
343 source_bindings:
344 - mac_addr: '00:b0:d0:63:c2:26'
345 ip_addr: '192.0.2.146'
346 intf_name: 'Ethernet4'
347 vlan_id: '1'
348 state: deleted
349
350 # After state:
351 # ------------
352 #
353 # sonic# show ip dhcp snooping binding
354 # !
355 # Total number of Dynamic bindings: 0
356 # Total number of Static bindings: 2
357 # Total number of Tentative bindings: 0
358 # MAC Address IP Address VLAN Interface Type Lease (Secs)
359 # ----------------- --------------- ---- ----------- ------- -----------
360 # aa:f7:67:fc:f4:9a 156.33.90.167 2 PortChannel1 static NA
361 # !
362
363
364 # Using "overridden" state
365 #
366 # Before state:
367 # -------------
368 #
369 # sonic# show ipv4 dhcp snooping binding
370 # !
371 # MAC Address IP Address VLAN Interface Type Lease (Secs)
372 # ----------------- --------------- ---- ----------- ------- -----------
373 # 00:b0:d0:63:c2:26 192.0.2.146 1 Ethernet4 static NA
374 # 28:21:28:15:c1:1b 141.202.222.118 1 Ethernet2 static NA
375 # aa:f7:67:fc:f4:9a 156.33.90.167 2 PortChannel1 static NA
376 # !
377
378 - name: Override DHCPv4 snooping bindings
379 dellemc.enterprise_sonic.sonic_dhcp_snooping:
380 config:
381 afis:
382 - afi: 'ipv4'
383 source_bindings:
384 - mac_addr: '00:b0:d0:63:c2:26'
385 ip_addr: '192.0.2.146'
386 intf_name: 'Ethernet4'
387 vlan_id: '3'
388 state: overridden
389
390 # After state:
391 # ------------
392 #
393 # sonic# show ipv4 dhcp snooping binding
394 # !
395 # MAC Address IP Address VLAN Interface Type Lease (Secs)
396 # ----------------- --------------- ---- ----------- ------- -----------
397 # 00:b0:d0:63:c2:26 192.0.2.146 3 Ethernet4 static NA
398 # !
399
400
401 # Using "replaced" state
402 #
403 # Before state:
404 # -------------
405 #
406 # sonic# show ipv4 dhcp snooping binding
407 # !
408 # MAC Address IP Address VLAN Interface Type Lease (Secs)
409 # ----------------- --------------- ---- ----------- ------- -----------
410 # 00:b0:d0:63:c2:26 192.0.2.146 1 Ethernet4 static NA
411 # 28:21:28:15:c1:1b 141.202.222.118 1 Ethernet2 static NA
412 # aa:f7:67:fc:f4:9a 156.33.90.167 2 PortChannel1 static NA
413 # !
414
415 - name: Replace DHCPv4 snooping bindings
416 dellemc.enterprise_sonic.sonic_dhcp_snooping:
417 config:
418 afis:
419 - afi: 'ipv4'
420 source_bindings:
421 - mac_addr: '00:b0:d0:63:c2:26'
422 ip_addr: '192.0.2.146'
423 intf_name: 'Ethernet4'
424 vlan_id: '3'
425 state: replaced
426
427 # After state:
428 # ------------
429 #
430 # sonic# show ipv4 dhcp snooping binding
431 # !
432 # MAC Address IP Address VLAN Interface Type Lease (Secs)
433 # ----------------- --------------- ---- ----------- ------- -----------
434 # 00:b0:d0:63:c2:26 192.0.2.146 3 Ethernet4 static NA
435 # 28:21:28:15:c1:1b 141.202.222.118 1 Ethernet2 static NA
436 # aa:f7:67:fc:f4:9a 156.33.90.167 2 PortChannel1 static NA
437 # !
438 """
439
440 RETURN = """
441 before:
442 description: The configuration prior to the module invocation.
443 returned: always
444 type: dict
445 sample: >
446 The configuration returned will always be in the same format
447 as the parameters above.
448 after:
449 description: The resulting configuration module invocation.
450 returned: when changed
451 type: dict
452 sample: >
453 The configuration returned will always be in the same format
454 as the parameters above.
455 after_generated:
456 description: The generated configuration module invocation.
457 returned: when C(check_mode)
458 type: list
459 sample: >
460 The configuration returned will always be in the same format
461 as the parameters above.
462 commands:
463 description: The set of commands pushed to the remote device.
464 returned: always
465 type: list
466 sample: ['command 1', 'command 2', 'command 3']
467 """
468
469
470 from ansible.module_utils.basic import AnsibleModule
471 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.argspec.dhcp_snooping.dhcp_snooping import Dhcp_snoopingArgs
472 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.config.dhcp_snooping.dhcp_snooping import Dhcp_snooping
473
474
475 def main():
476 """
477 Main entry point for module execution
478
479 :returns: the result form module invocation
480 """
481 module = AnsibleModule(argument_spec=Dhcp_snoopingArgs.argument_spec,
482 supports_check_mode=True)
483
484 result = Dhcp_snooping(module).execute_module()
485 module.exit_json(**result)
486
487
488 if __name__ == '__main__':
489 main()