-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathudp_sender.p4
133 lines (104 loc) · 3.94 KB
/
udp_sender.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
/*
Copyright 2021 Intel-KAUST-Microsoft
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef _UDP_SENDER_
#define _UDP_SENDER_
#define UDP_LENGTH (hdr.udp.minSizeInBytes() + hdr.switchml.minSizeInBytes() + hdr.exponents.minSizeInBytes())
#define IPV4_LENGTH (hdr.ipv4.minSizeInBytes() + UDP_LENGTH);
control UDPSender(
inout egress_metadata_t eg_md,
in egress_intrinsic_metadata_t eg_intr_md,
inout header_t hdr) {
DirectCounter<counter_t>(CounterType_t.PACKETS_AND_BYTES) send_counter;
// Read switch MAC and IP from table to form output packets
action set_switch_mac_and_ip(mac_addr_t switch_mac, ipv4_addr_t switch_ip) {
// Set switch addresses
hdr.ethernet.src_addr = switch_mac;
hdr.ipv4.src_addr = switch_ip;
hdr.udp.src_port = eg_md.switchml_md.src_port;
hdr.ethernet.ether_type = ETHERTYPE_IPV4;
hdr.ipv4.version = 4;
hdr.ipv4.ihl = 5;
hdr.ipv4.diffserv = 0x00;
hdr.ipv4.total_len = IPV4_LENGTH;
hdr.ipv4.identification = 0x0000;
hdr.ipv4.flags = 0b000;
hdr.ipv4.frag_offset = 0;
hdr.ipv4.ttl = 64;
hdr.ipv4.protocol = ip_protocol_t.UDP;
hdr.ipv4.hdr_checksum = 0; // To be filled in by deparser
hdr.ipv4.src_addr = switch_ip;
eg_md.update_ipv4_checksum = true;
hdr.udp.length = UDP_LENGTH;
hdr.switchml.setValid();
hdr.switchml.msg_type = 1;
hdr.switchml.unused = 0;
hdr.switchml.size = eg_md.switchml_md.packet_size;
hdr.switchml.job_number = eg_md.switchml_md.job_number;
hdr.switchml.tsi = eg_md.switchml_md.tsi;
// Rearrange pool index
hdr.switchml.pool_index[13:0] = eg_md.switchml_md.pool_index[14:1];
}
table switch_mac_and_ip {
actions = { @defaultonly set_switch_mac_and_ip; }
size = 1;
}
action set_dst_addr(
mac_addr_t eth_dst_addr,
ipv4_addr_t ip_dst_addr) {
// Set to destination node
hdr.ethernet.dst_addr = eth_dst_addr;
hdr.ipv4.dst_addr = ip_dst_addr;
hdr.udp.dst_port = eg_md.switchml_md.dst_port;
// Disable UDP checksum for now
hdr.udp.checksum = 0;
// Update IPv4 checksum
eg_md.update_ipv4_checksum = true;
// Pool set bit
hdr.switchml.pool_index[15:15] = eg_md.switchml_md.pool_index[0:0];
// Exponents
hdr.exponents.setValid();
hdr.exponents.e0 = eg_md.switchml_md.e0;
hdr.exponents.e1 = eg_md.switchml_md.e1;
// Count send
send_counter.count();
}
table dst_addr {
key = {
eg_md.switchml_md.worker_id : exact;
}
actions = {
set_dst_addr;
}
size = max_num_workers;
counters = send_counter;
}
apply {
hdr.ethernet.setValid();
hdr.ipv4.setValid();
hdr.udp.setValid();
hdr.switchml.setValid();
hdr.switchml.pool_index = 16w0;
switch_mac_and_ip.apply();
dst_addr.apply();
// Add payload size
if (eg_md.switchml_md.packet_size == packet_size_t.IBV_MTU_256) {
hdr.ipv4.total_len = hdr.ipv4.total_len + 256;
hdr.udp.length = hdr.udp.length + 256;
}
else if (eg_md.switchml_md.packet_size == packet_size_t.IBV_MTU_1024) {
hdr.ipv4.total_len = hdr.ipv4.total_len + 1024;
hdr.udp.length = hdr.udp.length + 1024;
}
}
}
#endif /* _UDP_SENDER_ */