-
Notifications
You must be signed in to change notification settings - Fork 0
/
tna_operations.p4
145 lines (114 loc) · 3.38 KB
/
tna_operations.p4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* -*- P4_16 -*- */
/*******************************************************************************
* BAREFOOT NETWORKS CONFIDENTIAL & PROPRIETARY
*
* Copyright (c) Intel Corporation
* SPDX-License-Identifier: CC-BY-ND-4.0
*/
#include <core.p4>
#if __TARGET_TOFINO__ == 2
#include <t2na.p4>
#else
#include <tna.p4>
#endif
#include "common/headers.p4"
#include "common/util.p4"
struct metadata_t {}
// ---------------------------------------------------------------------------
// Ingress parser
// ---------------------------------------------------------------------------
parser SwitchIngressParser(
packet_in pkt,
out header_t hdr,
out metadata_t ig_md,
out ingress_intrinsic_metadata_t ig_intr_md) {
TofinoIngressParser() tofino_parser;
state start {
tofino_parser.apply(pkt, ig_intr_md);
transition parse_ethernet;
}
state parse_ethernet {
pkt.extract(hdr.ethernet);
transition select (hdr.ethernet.ether_type) {
ETHERTYPE_IPV4 : parse_ipv4;
default : reject;
}
}
state parse_ipv4 {
pkt.extract(hdr.ipv4);
transition accept;
}
}
// ---------------------------------------------------------------------------
// Ingress Deparser
// ---------------------------------------------------------------------------
control SwitchIngressDeparser(
packet_out pkt,
inout header_t hdr,
in metadata_t ig_md,
in ingress_intrinsic_metadata_for_deparser_t ig_intr_dprsr_md) {
apply {
pkt.emit(hdr);
}
}
control SwitchIngress(
inout header_t hdr,
inout metadata_t ig_md,
in ingress_intrinsic_metadata_t ig_intr_md,
in ingress_intrinsic_metadata_from_parser_t ig_intr_prsr_md,
inout ingress_intrinsic_metadata_for_deparser_t ig_intr_dprsr_md,
inout ingress_intrinsic_metadata_for_tm_t ig_intr_tm_md) {
Counter<bit<32>, PortId_t>(
512, CounterType_t.PACKETS_AND_BYTES) indirect_counter;
DirectCounter<bit<32>>(CounterType_t.PACKETS_AND_BYTES) direct_counter;
action hit(PortId_t port) {
direct_counter.count();
ig_intr_tm_md.ucast_egress_port = port;
}
action hit_dst(PortId_t port) {
indirect_counter.count(port);
ig_intr_tm_md.ucast_egress_port = port;
ig_intr_dprsr_md.drop_ctl = 0x0; // clear drop packet
}
action miss() {
ig_intr_dprsr_md.drop_ctl = 0x1; // Drop packet.
}
action nop() {
}
table forward {
key = {
hdr.ethernet.src_addr : ternary;
}
actions = {
hit;
@defaultonly miss;
}
const default_action = miss;
size = 1024;
counters = direct_counter;
}
table forward_dst {
key = {
hdr.ethernet.dst_addr : exact;
}
actions = {
hit_dst;
@defaultonly nop;
}
const default_action = nop;
size = 1024;
}
apply {
forward.apply();
forward_dst.apply();
// No need for egress processing, skip it and use empty controls for egress.
ig_intr_tm_md.bypass_egress = 1w1;
}
}
Pipeline(SwitchIngressParser(),
SwitchIngress(),
SwitchIngressDeparser(),
EmptyEgressParser(),
EmptyEgress(),
EmptyEgressDeparser()) pipe;
Switch(pipe) main;