-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathset_sde.bash
402 lines (367 loc) · 11.1 KB
/
set_sde.bash
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#
# Basic usage check
#
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
echo ERROR: This script is supposed to be sourced, not executed
exit 1
fi
# set_sde.sh
# SDE selection script
#
# (c) Barefoot Networks, 2017
# All Rights Reserved
#
# Usage:
# ------
#
# Source this file in BASH to select the desired version of Barefoot SDE
# For example:
# . ~/tools/set_sde.bash
# or
# source ~/tools/set_sde.bash
#
# NOTE: Because this file is supposed to be SOURCED and not EXECUTED
# do NOT make it executable. This will prevent unnecessary errors and
# frustrations :) We now have a check in place as well.
#
# Quick Manual:
# =============
# You can source this file from any directory. It will try to search
# for SDE directory as follows:
# 1) In the current directory. If this directory contains SDE, it
# will be chosen (so the behavior is like before)
# **** This is the main method that always works ****
#
# 2) In the directories ~/bf-sde-*.*.* in reverse chronological
# order (i.e. starting from the newest).
#
# Note: if you keep your SDE trees somewhere else, e.g. in
# /opt/src then you can simply replace '~' with /opt/src
# **** This is a convenience method that might require customization
#
# It also prints the chosen SDE version (or an error if nothing is
# found). This reduces the potential for errors.
#
# CAVEAT: If you try to source this file from an SDE subdirectory (e.g.
# ~/bf-sde-x.y.z.w/pkgsrc, the script might or might not choose
# bf-sde-x.y.z.w, based on whether this is a latest SDE or not.
#
#
# Additional commands
# -------------------
#
# The script also defines additional commands that you might find helpful in
# your day-to-day work with the SDE.
#
# set_sde
# Sets the environment as described above; This is especially useful
# if you decide to incorporate this script in your .bashrc
#
# sde
# Changes directory to $SDE. No parameters.
#
# A useful paradigm is:
# . ~/tools/set_sde.bash; sde
#
# Easy to type on the terminal and then copy to others in one piece
#
#
# build_dir <program_name or the path to the main program .p4 file> [suffix]
# Prints the full pathname of the directory that is created for
# the given program by p4-build.sh script.
#
# Typical usage:
# cd `build_dir myprog` or cd `build_dir p4src/myprog.p4`
# cd `build_dir myprog .p4c` (with suffix ".p4c")
#
# Note: this command is aware the typical build_dir for standard
# programs switch.p4 and diag.p4 are different
#
# Suffix is the optional parameter and corresponds to the suffix,
# used by p4_build.sh script
#
#
# tofino_dir <program_name or the path to the main program .p4 file> [suffix]
# Prints the full pathname of the directory, containing the
# p4c-tofino compiper output. Essentially it is equivalent to
# `build_dir prog>`/tofino/prog
#
#
# target_dir <program_name or the path to main .p4> [suffix] [target]
# Similar to tofino_dir but also accepts tofino2
# Acceptable target names (case insensitive):
# tofino, tf, t
# tofino2, tf2, t2
#
# manifest.json <program name or the main .p4 file> [suffix] [target]
# Prints the full pathname of the manifest.json file in the target
# directory
#
# The rest of the commands rely on some info from manifest.json that is
# extracted from it using the script $SDE_INSTALL/bin/p4c-manifest-config
#
# pipes <program name or the main .p4 file> [suffix] [target]
# Returns the list of pipes. for P4_14 program thePipe really means "."
#
# logs_dir <program_name or the path to the main program .p4 file> [suffix]
# Prints the full name of the directory, containing the log files
# produced by p4c-tofino
#
# Typical usage:
# vi `logs_dir myprog`/pa.results.log
#
#
# vis_dir <program_name or the path to the main program .p4 file> [suffix]
# Prints the full name of the directory, containing the visualizations
# produced by p4c-tofino
#
# Typical usage:
# firefox `vis_dir myprog`
#
#
# graphs_dir <program_name or the path to the main program .p4 file> [suffix]
# Prints the full name of the directory, containing the visualizations
# produced by p4c-tofino
#
# Typical usage:
# mkdir -p `graphs_dir myprog`
# ~/tools/p4-graphs-tofino myprog.p4 --gen-dir `graphs_dir myprog`
#
# pd.h <program_name or the path to the main program .p4 file> [suffix]
# Prints the full name of the pd.h, generated for the given program
#
# Typical usage:
# emacs `pd.h myprog` &
#
# context.json <program_name or the path to the main program .p4 file> [suffix]
# Prints the full name of the context.json, generated for the given
# program
#
# Typical usage:
# kate `context.json myprog` &
#
# cpu_pcie
# This command prints the name of the Linux networking interface that
# is associated with the CPU PCIe port, when it is exposed by bf_knet.
# If bf_knet is not running then prints nothing.
############################################################################
function set_sde() {
local sdes=~ # Directory, where you keep SDE trees, $HOME, /opt/sde, etc.
for sde_dir in `pwd` `/bin/ls -dt ${sdes}/bf-sde-*.*.* 2> /dev/null`; do
manifest=`/bin/ls $sde_dir/*.manifest 2> /dev/null | head -n 1`
if [ ! -z $manifest ]; then
export SDE=$sde_dir
export SDE_INSTALL=$SDE/install
export PATH=$SDE_INSTALL/bin:$PATH
# Add SDE site-packages directories only for the versions of Python
# that exist in the system. This might be more complicated if
# Python, shipped with the SDE has the same version or if the system
# uses its own installation of Python outside of /usr/bin
for p in /usr/bin/python[23].*[0-9]; do
PYTHON=`basename $p`
SP=$SDE_INSTALL/lib/${PYTHON}/site-packages
if [ -d $SP ]; then
export PYTHONPATH=$SP:$PYTHONPATH
fi
if [ -d $SP/tofino ]; then
export PYTHONPATH=$SP/tofino:$PYTHONPATH
fi
if [ -d $SP/tofino/bfrt_grpc ]; then
export PYTHONPATH=$SP/tofino/bfrt_grpc:$PYTHONPATH
fi
done
export PKTPY=false
echo Using `basename $manifest .manifest` in $SDE
break
fi
done
if [ -z $manifest ]; then
echo "ERROR: No suitable SDE directory found"
echo " Please, source this file from the root of your SDE directory"
fi
}
set_sde
function sde() {
cd $SDE
}
function build_dir() {
prog=$1; shift
suffix=$1; shift
target=`target $1`; shift
SDE_BUILD=$SDE/build
# Remove occasional directory and extension to get the name
prog=`basename $prog .p4`
if [ ! -z $prog ]; then
if [ $prog = switch ]; then
if [ -d $SDE_BUILD/$prog$suffix/p4-build ]; then
echo $SDE_BUILD/$prog$suffix/p4-build
return 0
fi
fi
if [ $prog = switch-p4-16 ]; then
if [ -d $SDE_BUILD/$prog$suffix/p4src ]; then
echo $SDE_BUILD/$prog$suffix/p4src
return 0
fi
fi
if [ $prog = diag ]; then
if [ -d $SDE_BUILD/$prog$suffix/p4-build ]; then
echo $SDE_BUILD/$prog$suffix/p4-build
return 0
fi
fi
if [ -d $SDE_BUILD/examples/$prog$suffix ]; then
echo $SDE_BUILD/examples/$prog$suffix
return 0
fi
if [ -d $SDE_BUILD/p4-examples/${prog}_${target}$suffix ]; then
echo $SDE_BUILD/p4-examples/${prog}_${target}$suffix
return 0
fi
if [ -d $SDE_BUILD/p4-examples/$prog$suffix ]; then
echo $SDE_BUILD/p4-examples/$prog$suffix
return 0
fi
if [ -d $SDE_BUILD/p4-build/${target}/${prog}${suffix} ]; then
echo $SDE_BUILD/p4-build/${target}/${prog}${suffix}
return 0
fi
fi
}
function target() {
if [ -z $1 ]; then
if [ ! -z $P4_TARGET ]; then
tgt=$P4_TARGET
else
tgt=tofino
fi
else
tgt=`echo $1 | tr A-Z a-z`
fi
case $tgt in
tofino|tf|t) echo tofino ;;
tofino2|tf2|t2) echo tofino2 ;;
esac
}
function target_dir() {
prog=$1
if [ ! -z $prog ]; then
if [ $prog = switch-p4-16 ]; then
echo `build_dir $1 $2`/switch-`target $3`/switch
else
echo `build_dir $1 $2`/`basename $1 .p4`/`target $3`
fi
fi
}
function tofino_dir() {
if [ ! -z $1 ]; then
echo `target_dir $* tofino`
fi
}
function tofino2_dir() {
prog=$1
suffix=$2
if [ ! -z $prog ]; then
echo `target_dir $* tofino2`
fi
}
function manifest.json() {
prog=$1
suffix=$2
target=$3
if [ ! -z $prog ]; then
echo `target_dir $*`/manifest.json
fi
}
function pipes() {
prog=$1
suffix=$2
target=$3
if [ ! -z $1 ]; then
p4c-manifest-config --pipe `manifest.json $*` | sed -e 's/thePipe/./'
fi
}
function vis_dir() {
if [ ! -z $1 ]; then
for p in `pipes $*`; do
echo `target_dir $*`/$p/visualization
done
fi
}
function logs_dir() {
if [ ! -z $1 ]; then
for p in `pipes $*`; do
echo `target_dir $*`/$p/logs
done
fi
}
function graphs_dir() {
if [ ! -z $1 ]; then
for p in `pipes $*`; do
echo `target_dir $*`/$p/graphs
done
fi
}
function pd.h() {
if [ ! -z $1 ]; then
for p in `pipes $*`; do
echo `target_dir $*`/$p/pd/pd.h
done
fi
}
function context.json() {
if [ ! -z $1 ]; then
for p in `pipes $*`; do
if [ -f `target_dir $*`/$p/context/context.json ]; then
echo `target_dir $*`/$p/context/context.json
else
echo `target_dir $*`/$p/context.json
fi
done
fi
}
function bfa() {
if [ ! -z $1 ]; then
for p in `pipes $*`; do
if [ -f `target_dir $*`/$p/context/context.json ]; then
echo `target_dir $*`/$p/context/context.json
else
echo `target_dir $*`/$p/$1.bfa
fi
done
fi
}
function show_vis() {
for d in `vis_dir $*`; do
xdg-open $d
done
}
function show_graphs() {
for d in `graphs_dir $*`; do
xdg-open $d
done
}
function show_p4i() {
if [ ! -z $1 ]; then
p4i -o `manifest.json $*`
fi
}
function show_logs() {
for d in `log_dir $*`; do
xdg-open $d
done
}
function p4_select() {
echo -n Selecting $* as $1 " ... "
if make -C `build_dir $*` install 2>&1 > /dev/null; then
echo DONE
else
echo FAILED
fi
}
function cpu_pcie() {
basename /sys/module/bf_kpkt/drivers/pci\:bf/*/net/*
}
function clear_all() {
$SDE/run_bfshell.sh -b ~/tools/bfrt_clear_all.py
}