blob: 34ef4d18c027d309a80cd3f89e443b4f464f3d6b [file] [log] [blame]
Carmelo Casconeb5324e72018-11-25 02:26:32 -08001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <core.p4>
18#include <v1model.p4>
19
20#include "../define.p4"
21#include "../header.p4"
22
23control Acl (inout parsed_headers_t hdr,
24 inout fabric_metadata_t fabric_metadata,
25 inout standard_metadata_t standard_metadata) {
26
Wailok Shum4f51bde2021-06-11 22:48:41 +080027 ipv4_addr_t ipv4_src = 0;
28 ipv4_addr_t ipv4_dst = 0;
29 bit<8> ip_proto = 0;
30 l4_port_t l4_sport = 0;
31 l4_port_t l4_dport = 0;
32
Carmelo Casconeb5324e72018-11-25 02:26:32 -080033 /*
34 * ACL Table.
35 */
36 direct_counter(CounterType.packets_and_bytes) acl_counter;
37
38 action set_next_id_acl(next_id_t next_id) {
39 fabric_metadata.next_id = next_id;
40 acl_counter.count();
41 }
42
43 // Send immendiatelly to CPU - skip the rest of ingress.
44 action punt_to_cpu() {
45 standard_metadata.egress_spec = CPU_PORT;
46 fabric_metadata.skip_next = _TRUE;
47 acl_counter.count();
48 }
49
Daniele Moro01ca2ab2019-06-25 11:48:48 -070050 // Set clone session id for a I2E clone session
51 action set_clone_session_id(bit<32> clone_id) {
Daniele Moro3b7a21b2019-06-25 17:33:40 -070052 clone3(CloneType.I2E, clone_id, {standard_metadata.ingress_port});
Carmelo Casconeb5324e72018-11-25 02:26:32 -080053 acl_counter.count();
54 }
55
56 action drop() {
Carmelo Cascone9b607da2019-05-08 14:03:01 -070057 mark_to_drop(standard_metadata);
Carmelo Casconeb5324e72018-11-25 02:26:32 -080058 fabric_metadata.skip_next = _TRUE;
59 acl_counter.count();
60 }
61
62 action nop_acl() {
63 acl_counter.count();
64 }
65
66 table acl {
67 key = {
Wailok Shum4f51bde2021-06-11 22:48:41 +080068 standard_metadata.ingress_port : ternary @name("ig_port"); // 9
69 hdr.ethernet.dst_addr : ternary @name("eth_dst"); // 48
70 hdr.ethernet.src_addr : ternary @name("eth_src"); // 48
71 hdr.vlan_tag.vlan_id : ternary @name("vlan_id"); // 12
72 hdr.eth_type.value : ternary @name("eth_type"); // 16
73 ipv4_src : ternary @name("ipv4_src"); // 32
74 ipv4_dst : ternary @name("ipv4_dst"); // 32
75 ip_proto : ternary @name("ip_proto"); // 8
76 hdr.icmp.icmp_type : ternary @name("icmp_type"); // 8
77 hdr.icmp.icmp_code : ternary @name("icmp_code"); // 8
78 l4_sport : ternary @name("l4_sport"); // 16
79 l4_dport : ternary @name("l4_dport"); // 16
Carmelo Casconeb5324e72018-11-25 02:26:32 -080080 }
81
82 actions = {
83 set_next_id_acl;
84 punt_to_cpu;
Daniele Moro01ca2ab2019-06-25 11:48:48 -070085 set_clone_session_id;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080086 drop;
87 nop_acl;
88 }
89
90 const default_action = nop_acl();
Carmelo Cascone70e816b2019-03-19 16:15:47 -070091 size = ACL_TABLE_SIZE;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080092 counters = acl_counter;
93 }
94
95 apply {
Wailok Shum4f51bde2021-06-11 22:48:41 +080096 if (hdr.gtpu.isValid() && hdr.inner_ipv4.isValid()) {
97 ipv4_src = hdr.inner_ipv4.src_addr;
98 ipv4_dst = hdr.inner_ipv4.dst_addr;
99 ip_proto = hdr.inner_ipv4.protocol;
100 if (hdr.inner_tcp.isValid()) {
101 l4_sport = hdr.inner_tcp.sport;
102 l4_dport = hdr.inner_tcp.dport;
103 } else if (hdr.inner_udp.isValid()) {
104 l4_sport = hdr.inner_udp.sport;
105 l4_dport = hdr.inner_udp.dport;
106 }
107 } else if (hdr.ipv4.isValid()) {
108 ipv4_src = hdr.ipv4.src_addr;
109 ipv4_dst = hdr.ipv4.dst_addr;
110 ip_proto = hdr.ipv4.protocol;
111 if (hdr.tcp.isValid()) {
112 l4_sport = hdr.tcp.sport;
113 l4_dport = hdr.tcp.dport;
114 } else if (hdr.udp.isValid()) {
115 l4_sport = hdr.udp.sport;
116 l4_dport = hdr.udp.dport;
117 }
118 }
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800119 acl.apply();
120 }
121}