Debian Codegraph @

ansible_collections/dellemc/enterprise_sonic/plugins/modules/sonic_l2_acls.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_l2_acls
9 """
10
11 from __future__ import absolute_import, division, print_function
12 __metaclass__ = type
13
14 DOCUMENTATION = """
15 ---
16 module: sonic_l2_acls
17 version_added: '2.1.0'
18 notes:
19 - Supports C(check_mode).
20 short_description: Manage Layer 2 access control lists (ACL) configurations on SONiC
21 description:
22 - This module provides configuration management of Layer 2 access control lists (ACL)
23 in devices running SONiC.
24 author: 'Arun Saravanan Balachandran (@ArunSaravananBalachandran)'
25 options:
26 config:
27 description:
28 - Specifies Layer 2 ACL configurations.
29 type: list
30 elements: dict
31 suboptions:
32 name:
33 description:
34 - Specifies the ACL name.
35 type: str
36 required: true
37 remark:
38 description:
39 - Specifies remark for the ACL.
40 type: str
41 rules:
42 description:
43 - List of rules with the ACL.
44 - I(sequence_num), I(action), I(source) & I(destination) are required for adding a new rule.
45 - If I(state=deleted), options other than I(sequence_num) are not considered.
46 - I(ethertype) and I(vlan_tag_format) are mutually exclusive.
47 type: list
48 elements: dict
49 suboptions:
50 sequence_num:
51 description:
52 - Specifies the sequence number of the rule.
53 - The range is from 1 to 65535.
54 type: int
55 required: true
56 action:
57 description:
58 - Specifies the action taken on the matched Ethernet frame.
59 type: str
60 choices:
61 - deny
62 - discard
63 - do-not-nat
64 - permit
65 - transit
66 source:
67 description:
68 - Specifies the source of the Ethernet frame.
69 - I(address) and I(address_mask) are required together.
70 - I(any), I(host) and I(address) are mutually exclusive.
71 type: dict
72 suboptions:
73 any:
74 description:
75 - Match any source MAC address.
76 type: bool
77 host:
78 description:
79 - MAC address of a single source host.
80 type: str
81 address:
82 description:
83 - Source MAC address.
84 type: str
85 address_mask:
86 description:
87 - Source MAC address mask.
88 type: str
89 destination:
90 description:
91 - Specifies the destination of the Ethernet frame.
92 - I(address) and I(address_mask) are required together.
93 - I(any), I(host) and I(address) are mutually exclusive.
94 type: dict
95 suboptions:
96 any:
97 description:
98 - Match any destination MAC address.
99 type: bool
100 host:
101 description:
102 - MAC address of a single destination host.
103 type: str
104 address:
105 description:
106 - Destination MAC address.
107 type: str
108 address_mask:
109 description:
110 - Destination MAC address mask.
111 type: str
112 ethertype:
113 description:
114 - Specifies the EtherType of the Ethernet frame.
115 - Only one suboption can be specified for ethertype in a rule.
116 type: dict
117 suboptions:
118 value:
119 description:
120 - Specifies the EtherType value to match as a hexadecimal string.
121 - The range is from 0x600 to 0xffff.
122 type: str
123 arp:
124 description:
125 - Match Ethernet frame with ARP EtherType (0x806).
126 type: bool
127 ipv4:
128 description:
129 - Match Ethernet frame with IPv4 EtherType (0x800).
130 type: bool
131 ipv6:
132 description:
133 - Match Ethernet frame with IPv6 EtherType (0x86DD).
134 type: bool
135 vlan_id:
136 description:
137 - Match Ethernet frame with the given VLAN ID.
138 type: int
139 vlan_tag_format:
140 description:
141 - Match Ethernet frame with the given VLAN tag format.
142 type: dict
143 suboptions:
144 multi_tagged:
145 description:
146 - Match three of more VLAN tagged Ethernet frame.
147 type: bool
148 dei:
149 description:
150 - Match Ethernet frame with the given Drop Eligible Indicator (DEI) value.
151 type: int
152 choices:
153 - 0
154 - 1
155 pcp:
156 description:
157 - Match Ethernet frames using Priority Code Point (PCP) value.
158 - I(mask) is valid only when I(value) is specified.
159 - I(value) and I(traffic_type) are mutually exclusive.
160 type: dict
161 suboptions:
162 value:
163 description:
164 - Match Ethernet frame with the given PCP value.
165 - The range is from 0 to 7
166 type: int
167 mask:
168 description:
169 - Match Ethernet frame with given PCP value and mask.
170 - The range is from 0 to 7.
171 type: int
172 traffic_type:
173 description:
174 - Match Ethernet frame with PCP value for the given traffic type.
175 - C(be) - Match Ethernet frame with Best effort PCP (0).
176 - C(bk) - Match Ethernet frame with Background PCP (1).
177 - C(ee) - Match Ethernet frame with Excellent effort PCP (2).
178 - C(ca) - Match Ethernet frame with Critical applications PCP (3).
179 - C(vi) - Match Ethernet frame with Video, < 100 ms latency and jitter PCP (4).
180 - C(vo) - Match Ethernet frame with Voice, < 10 ms latency and jitter PCP (5).
181 - C(ic) - Match Ethernet frame with Internetwork control PCP (6).
182 - C(nc) - Match Ethernet frame with Network control PCP (7).
183 type: str
184 choices:
185 - be
186 - bk
187 - ee
188 - ca
189 - vi
190 - vo
191 - ic
192 - nc
193 remark:
194 description:
195 - Specifies remark for the ACL rule.
196 type: str
197 state:
198 description:
199 - The state of the configuration after module completion.
200 - C(merged) - Merges provided L2 ACL configuration with on-device configuration.
201 - C(replaced) - Replaces on-device configuration of the specified L2 ACLs with provided configuration.
202 - C(overridden) - Overrides all on-device L2 ACL configurations with the provided configuration.
203 - C(deleted) - Deletes on-device L2 ACL configuration.
204 type: str
205 choices:
206 - merged
207 - replaced
208 - overridden
209 - deleted
210 default: merged
211 """
212
213 EXAMPLES = """
214 # Using "merged" state
215 #
216 # Before state:
217 # -------------
218 #
219 # sonic# show running-configuration mac access-list
220 # !
221 # mac access-list test
222 # seq 1 permit host 22:22:22:22:22:22 any vlan 20
223 # sonic#
224
225 - name: Merge provided Layer 2 ACL configurations
226 dellemc.enterprise_sonic.sonic_l2_acls:
227 config:
228 - name: 'test'
229 rules:
230 - sequence_num: 2
231 action: 'permit'
232 source:
233 any: true
234 destination:
235 any: true
236 ethertype:
237 value: '0x88cc'
238 remark: 'LLDP'
239 - sequence_num: 3
240 action: 'permit'
241 source:
242 any: true
243 destination:
244 address: '00:00:10:00:00:00'
245 address_mask: '00:00:ff:ff:00:00'
246 pcp:
247 value: 4
248 mask: 6
249 - sequence_num: 4
250 action: 'deny'
251 source:
252 any: true
253 destination:
254 any: true
255 vlan_tag_format:
256 multi_tagged: true
257 - name: 'test1'
258 remark: 'test_mac_acl'
259 rules:
260 - sequence_num: 1
261 action: 'permit'
262 source:
263 host: '11:11:11:11:11:11'
264 destination:
265 any: true
266 - sequence_num: 2
267 action: 'permit'
268 source:
269 any: true
270 destination:
271 any: true
272 ethertype:
273 arp: true
274 vlan_id: 100
275 - sequence_num: 3
276 action: 'deny'
277 source:
278 any: true
279 destination:
280 any: true
281 dei: 0
282 state: merged
283
284 # After state:
285 # ------------
286 #
287 # sonic# show running-configuration mac access-list
288 # !
289 # mac access-list test
290 # seq 1 permit host 22:22:22:22:22:22 any vlan 20
291 # seq 2 permit any any 0x88cc remark LLDP
292 # seq 3 permit any 00:00:10:00:00:00 00:00:ff:ff:00:00 pcp vi pcp-mask 6
293 # seq 4 deny any any vlan-tag-format multi-tagged
294 # !
295 # mac access-list test1
296 # remark test_mac_acl
297 # seq 1 permit host 11:11:11:11:11:11 any
298 # seq 2 permit any any arp vlan 100
299 # seq 3 deny any any dei 0
300 # sonic#
301
302
303 # Using "replaced" state
304 #
305 # Before state:
306 # -------------
307 #
308 # sonic# show running-configuration mac access-list
309 # !
310 # mac access-list test
311 # seq 1 permit host 22:22:22:22:22:22 any vlan 20
312 # seq 2 permit any any 0x88cc remark LLDP
313 # seq 3 permit any 00:00:10:00:00:00 00:00:ff:ff:00:00 pcp vi pcp-mask 6
314 # !
315 # mac access-list test1
316 # remark test_mac_acl
317 # seq 1 permit host 11:11:11:11:11:11 any
318 # seq 2 permit any any arp vlan 100
319 # seq 3 deny any any dei 0
320 # sonic#
321
322 - name: Replace device configuration of specified Layer 2 ACLs with provided configuration
323 dellemc.enterprise_sonic.sonic_l2_acls:
324 config:
325 - name: 'test1'
326 rules:
327 - sequence_num: 1
328 action: 'permit'
329 source:
330 any: true
331 destination:
332 any: true
333 ethertype:
334 arp: true
335 vlan_id: 200
336 - sequence_num: 2
337 action: 'discard'
338 source:
339 any: true
340 destination:
341 any: true
342 - name: 'test2'
343 rules:
344 - sequence_num: 1
345 action: 'permit'
346 source:
347 host: '33:33:33:33:33:33'
348 destination:
349 host: '44:44:44:44:44:44'
350 state: replaced
351
352 # After state:
353 # ------------
354 #
355 # sonic# show running-configuration mac access-list
356 # !
357 # mac access-list test
358 # seq 1 permit host 22:22:22:22:22:22 any vlan 20
359 # seq 2 permit any any 0x88cc remark LLDP
360 # seq 3 permit any 00:00:10:00:00:00 00:00:ff:ff:00:00 pcp vi pcp-mask 6
361 # !
362 # mac access-list test1
363 # seq 1 permit any any arp vlan 200
364 # seq 2 discard any any
365 # !
366 # mac access-list test2
367 # seq 1 permit host 33:33:33:33:33:33 host 44:44:44:44:44:44
368 # sonic#
369
370
371 # Using "overridden" state
372 #
373 # Before state:
374 # -------------
375 #
376 # sonic# show running-configuration mac access-list
377 # !
378 # mac access-list test
379 # seq 1 permit host 22:22:22:22:22:22 any vlan 20
380 # seq 2 permit any any 0x88cc remark LLDP
381 # seq 3 permit any 00:00:10:00:00:00 00:00:ff:ff:00:00 pcp vi pcp-mask 6
382 # !
383 # mac access-list test1
384 # seq 1 permit any any arp vlan 200
385 # seq 2 discard any any
386 # !
387 # mac access-list test2
388 # seq 1 permit host 33:33:33:33:33:33 host 44:44:44:44:44:44
389 # sonic#
390
391 - name: Override device configuration of all Layer 2 ACLs with provided configuration
392 dellemc.enterprise_sonic.sonic_l2_acls:
393 config:
394 - name: 'test1'
395 remark: 'test_mac_acl'
396 rules:
397 - sequence_num: 1
398 action: 'permit'
399 source:
400 host: '11:11:11:11:11:11'
401 destination:
402 any: true
403 vlan_id: 100
404 - sequence_num: 2
405 action: 'permit'
406 source:
407 any: true
408 destination:
409 any: true
410 pcp:
411 traffic_type: 'ca'
412 - sequence_num: 3
413 action: 'deny'
414 source:
415 any: true
416 destination:
417 any: true
418 ethertype:
419 ipv4: true
420 state: overridden
421
422 # After state:
423 # ------------
424 #
425 # sonic# show running-configuration mac access-list
426 # !
427 # mac access-list test1
428 # remark test_mac_acl
429 # seq 1 permit host 11:11:11:11:11:11 any vlan 100
430 # seq 2 permit any any pcp ca
431 # seq 3 deny any any ip
432 # sonic#
433
434
435 # Using "deleted" state
436 #
437 # Before state:
438 # -------------
439 #
440 # sonic# show running-configuration mac access-list
441 # !
442 # mac access-list test
443 # seq 1 permit host 22:22:22:22:22:22 any vlan 20
444 # seq 2 permit any any 0x88cc remark LLDP
445 # seq 3 permit any 00:00:10:00:00:00 00:00:ff:ff:00:00 pcp vi pcp-mask 6
446 # !
447 # mac access-list test1
448 # remark test_mac_acl
449 # seq 1 permit host 11:11:11:11:11:11 any vlan 100
450 # seq 2 deny any any ip
451 # !
452 # mac access-list test2
453 # seq 1 permit host 33:33:33:33:33:33 host 44:44:44:44:44:44
454 # sonic#
455
456 - name: Delete specified Layer 2 ACLs, ACL remark and ACL rule entries
457 dellemc.enterprise_sonic.sonic_l2_acls:
458 config:
459 - name: 'test'
460 rules:
461 - sequence_num: 3
462 - name: 'test1'
463 remark: 'test_mac_acl'
464 - name: 'test2'
465 state: deleted
466
467 # After state:
468 # ------------
469 #
470 # sonic# show running-configuration mac access-list
471 # !
472 # mac access-list test
473 # seq 1 permit host 22:22:22:22:22:22 any vlan 20
474 # seq 2 permit any any 0x88cc remark LLDP
475 # !
476 # mac access-list test1
477 # seq 1 permit host 11:11:11:11:11:11 any vlan 100
478 # seq 2 deny any any ip
479 # sonic#
480
481
482 # Using "deleted" state
483 #
484 # Before state:
485 # -------------
486 #
487 # sonic# show running-configuration mac access-list
488 # !
489 # mac access-list test
490 # seq 1 permit host 22:22:22:22:22:22 any vlan 20
491 # seq 2 permit any any 0x88cc remark LLDP
492 # seq 3 permit any 00:00:10:00:00:00 00:00:ff:ff:00:00 pcp vi pcp-mask 6
493 # !
494 # mac access-list test1
495 # remark test_mac_acl
496 # seq 1 permit host 11:11:11:11:11:11 any vlan 100
497 # seq 2 deny any any ip
498 # !
499 # mac access-list test2
500 # seq 1 permit host 33:33:33:33:33:33 host 44:44:44:44:44:44
501 # sonic#
502
503 - name: Delete all Layer 2 ACL configurations
504 dellemc.enterprise_sonic.sonic_l2_acls:
505 config:
506 state: deleted
507
508 # After state:
509 # ------------
510 #
511 # sonic# show running-configuration mac access-list
512 # sonic#
513 """
514
515 RETURN = """
516 before:
517 description: The configuration prior to the module invocation.
518 returned: always
519 type: list
520 sample: >
521 The configuration returned will always be in the same format
522 as the parameters above.
523 after:
524 description: The resulting configuration module invocation.
525 returned: when changed
526 type: list
527 sample: >
528 The configuration returned will always be in the same format
529 as the parameters above.
530 after_generated:
531 description: The generated configuration module invocation.
532 returned: when C(check_mode)
533 type: list
534 sample: >
535 The configuration returned will always be in the same format
536 as the parameters above.
537 commands:
538 description: The set of commands pushed to the remote device.
539 returned: always
540 type: list
541 sample: ['command 1', 'command 2', 'command 3']
542 """
543
544
545 from ansible.module_utils.basic import AnsibleModule
546 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.argspec.l2_acls.l2_acls import L2_aclsArgs
547 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.config.l2_acls.l2_acls import L2_acls
548
549
550 def main():
551 """
552 Main entry point for module execution
553
554 :returns: the result form module invocation
555 """
556 module = AnsibleModule(argument_spec=L2_aclsArgs.argument_spec,
557 supports_check_mode=True)
558
559 result = L2_acls(module).execute_module()
560 module.exit_json(**result)
561
562
563 if __name__ == '__main__':
564 main()