summaryrefslogtreecommitdiffstats
path: root/extras/vpptop/vpptop.sh
blob: 0d079429749949e5bbfbacc04fdd4f62b860093b (plain)
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
#!/bin/bash

# A simple script that installs VPPTop utility including
# all requirements. The binary API is built from the local
# vpp data. 'make install-dep' is recommended to call first.

set -eo pipefail

OPT_ARG=${1:-}

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "${SCRIPT_DIR}"/../../
VPP_DIR=$(pwd)
BUILD_DIR=${VPP_DIR}/extras/vpptop/build

GOROOT=${GOROOT:-}
GOPATH=${GOPATH:-}

[ -z "${GOROOT}" ] && GOROOT="${HOME}/.go"
[ -z "${GOPATH}" ] && GOPATH="${HOME}/go"

function dep_install() {
  echo "Installing VPPTop dependencies"
  apt-get update
  apt-get install git wget curl -y
}

# Install latest GO version
function go_install() {
  local TMP="/tmp"

  # Search for existing golang installation
  echo "Looking for pre-installed GO.."
  local installed_ver installed_ver_fmt
  if [[ -f "${GOROOT}/bin/go" ]]; then
    installed_ver=$(cd ${GOROOT}/bin && ./go version)
    installed_ver_fmt=${installed_ver#"go version go"}
    export PATH=$GOROOT/bin:$PATH
    export PATH=$GOPATH/bin:$PATH
    echo "Found installed version ${installed_ver_fmt}"
    return
  fi

  # install golang when missing
  echo ".. none was found. Installing the latest one"
  mkdir -p "${GOROOT}"
  mkdir -p "${GOPATH}/"{src,pkg,bin}

  wget "https://dl.google.com/go/$(curl https://golang.org/VERSION?m=text).linux-amd64.tar.gz" -O "${TMP}/go.tar.gz"
  tar -C "$GOROOT" --strip-components=1 -xzf "${TMP}/go.tar.gz"

  rm -f "${TMP}/go.tar.gz"

  # export for current session so the VPPTop can be installed
  export GOROOT=${GOROOT}
  export GOPATH=$GOPATH
  export PATH=$GOROOT/bin:$PATH
  export PATH=$GOPATH/bin:$PATH

  cat << EOF
Installed $(go version)
Note: following variables were exported for the current session:
GOROOT=${GOROOT}
GOPATH=${GOPATH}
Both were added to PATH
EOF
}

# Install GoVPP binary API generator. GoLang required
# to be installed in version 1.13 or higher
function install_binapi_gen() {
  echo "Installing GoVPP binary API generator"

  export GO111MODULE=on
  # master required for latest VPPTop
  if [[ ! -f "${GOROOT}/bin/go" ]]; then
    echo "GO is not installed"
    exit 1
  fi
  cd ${GOROOT}/bin && ./go get git.fd.io/govpp.git/cmd/binapi-generator@master

  local installed_ver installed_ver_fmt
  installed_ver=$(cd ${GOPATH}/bin && ./binapi-generator -version)
  installed_ver_fmt=${installed_ver#"govpp "}
  echo "Binary API generator ${installed_ver_fmt} installed"
}

# Generate binary API files in the VPPTop directory using
# the local VPP sources
function generate_binary_api() {
  # note: build-root dir is planned to be removed, update the path by then
  local api_dir=${VPP_DIR}/build-root/install-vpp-native/vpp/share/vpp/api
  local out_dir=${BUILD_DIR}/vpptop/stats/local/binapi

  if [[ ! -f "${GOPATH}/bin/binapi-generator" ]]; then
    install_binapi_gen
  fi
  if [ ! -d "${BUILD_DIR}" ]; then
    echo "VPPTop directory does not exist"
    exit 1
  elif [ ! -d "${out_dir}" ]; then
    mkdir -p "${out_dir}"
  fi
  if [ ! -d "${api_dir}" ]; then
    echo "JSON API files missing, call 'make json-api-files' first"
    exit 1
  fi

  echo "Generating API"
  cd ${GOPATH}/bin && ./binapi-generator --output-dir="${out_dir}" -input-dir="${api_dir}" \
    "${api_dir}"/plugins/dhcp.api.json \
    "${api_dir}"/core/interface.api.json \
    "${api_dir}"/core/ip.api.json \
    "${api_dir}"/core/vpe.api.json
}

# Retrieve VPPTop repository
function get_vpptop() {
  echo "Fetching VPPTop"
  if [ ! -d "${BUILD_DIR}/vpptop" ]; then
    mkdir "${BUILD_DIR}"
    cd "${BUILD_DIR}" && git clone https://github.com/PANTHEONtech/vpptop.git
  else
    echo "VPPTop directory already exists"
  fi
}

# Resolve VPPTop dependencies and install the binary
function vpptop_install() {
  get_vpptop
  generate_binary_api

  echo "Installing VPPTop"
  if [ ! -d "${BUILD_DIR}" ]; then
    echo "VPPTop directory does not exist"
    exit 1
  fi

  gopath=${GOROOT}/bin/go

  cd "${BUILD_DIR}"/vpptop && go mod download
  cd "${BUILD_DIR}"/vpptop && make install

  if [[ ! -x "$(command -v vpptop)" ]] && [[ ! -f "${GOPATH}/bin/vpptop" ]] ; then
    echo "VPPTop was not successfully installed"
    exit 1
  fi
  if [[ ! -x "$(command -v vpptop)" ]] ; then
    echo "VPPTop was installed to ${GOPATH}/bin/vpptop"
  fi

    cat << EOF
-----
$(vpptop --help)
-----

Following binaries were installed:
${GOPATH}/bin/binapi-generator
${GOPATH}/bin/vpptop
EOF
}

# Starts the vpptop binary
function vpptop_start() {
  if [[ -f "${GOPATH}/bin/vpptop"  ]] ; then
    cd ${GOPATH}/bin && ./vpptop
    return
  fi

  echo "VPPTop is not installed, use 'make vpptop-install' first"
}

# Remove VPPTop repository from extras
function cleanup() {
  echo "Cleaning up VPPTop"
  rm -rf "${BUILD_DIR}"
}

# Show available commands
function help() {
  cat <<__EOF__
  VPPTop installer

  Run 'make install-dep' before the installation

  <install>:
  Installs VPPTop requirements (latest GO, GoVPP binary API generator),
  retrieves VPPTop repository, generates binary API and installs the
  VPPTop binary.

  <cleanup>:
  Removes VPPTop repository

  <start>:
  Runs vpptop binary

__EOF__
}

# Resolve chosen option and call appropriate functions
function resolve_option() {
  local option=$1
  case ${option} in
  "start")
    vpptop_start
    ;;
  "install")
    dep_install
    go_install
    vpptop_install
    ;;
  "cleanup")
    cleanup
    ;;
  "help")
    help
    ;;
  *) echo invalid option ;;
  esac
}

if [[ -n ${OPT_ARG} ]]; then
  resolve_option "${OPT_ARG}"
else
  PS3="--> "
  options=("install" "cleanup" "help")
  select option in "${options[@]}"; do
    resolve_option "${option}"
    break
  done
fi
, next0, bi0); vlib_set_next_frame_buffer (vm, node, next1, bi1); if (next0 == next1) { vlib_put_next_frame (vm, node, next_index, n_left_to_next); next_index = next1; vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); } } } } while (n_left_from > 0 && n_left_to_next > 0) { u32 bi0; vlib_buffer_t *b0; osi_header_t *h0; u8 next0; bi0 = from[0]; to_next[0] = bi0; from += 1; to_next += 1; n_left_from -= 1; n_left_to_next -= 1; b0 = vlib_get_buffer (vm, bi0); h0 = (void *) (b0->data + b0->current_data); next0 = lm->input_next_by_protocol[h0->protocol]; b0->error = node->errors[next0 == OSI_INPUT_NEXT_DROP ? OSI_ERROR_UNKNOWN_PROTOCOL : OSI_ERROR_NONE]; /* Sent packet to wrong next? */ if (PREDICT_FALSE (next0 != next_index)) { /* Return old frame; remove incorrectly enqueued packet. */ vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1); /* Send to correct next. */ next_index = next0; vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); to_next[0] = bi0; to_next += 1; n_left_to_next -= 1; } } vlib_put_next_frame (vm, node, next_index, n_left_to_next); } return from_frame->n_vectors; } static char *osi_error_strings[] = { #define _(f,s) s, foreach_osi_error #undef _ }; /* *INDENT-OFF* */ VLIB_REGISTER_NODE (osi_input_node) = { .function = osi_input, .name = "osi-input", /* Takes a vector of packets. */ .vector_size = sizeof (u32), .n_errors = OSI_N_ERROR, .error_strings = osi_error_strings, .n_next_nodes = OSI_INPUT_N_NEXT, .next_nodes = { #define _(s,n) [OSI_INPUT_NEXT_##s] = n, foreach_osi_input_next #undef _ }, .format_buffer = format_osi_header_with_length, .format_trace = format_osi_input_trace, .unformat_buffer = unformat_osi_header, }; /* *INDENT-ON* */ static clib_error_t * osi_input_init (vlib_main_t * vm) { clib_error_t *error = 0; osi_main_t *lm = &osi_main; if ((error = vlib_call_init_function (vm, osi_init))) return error; osi_setup_node (vm, osi_input_node.index); { int i; for (i = 0; i < ARRAY_LEN (lm->input_next_by_protocol); i++) lm->input_next_by_protocol[i] = OSI_INPUT_NEXT_DROP; } ppp_register_input_protocol (vm, PPP_PROTOCOL_osi, osi_input_node.index); hdlc_register_input_protocol (vm, HDLC_PROTOCOL_osi, osi_input_node.index); llc_register_input_protocol (vm, LLC_PROTOCOL_osi_layer1, osi_input_node.index); llc_register_input_protocol (vm, LLC_PROTOCOL_osi_layer2, osi_input_node.index); llc_register_input_protocol (vm, LLC_PROTOCOL_osi_layer3, osi_input_node.index); llc_register_input_protocol (vm, LLC_PROTOCOL_osi_layer4, osi_input_node.index); llc_register_input_protocol (vm, LLC_PROTOCOL_osi_layer5, osi_input_node.index); return 0; } VLIB_INIT_FUNCTION (osi_input_init); void osi_register_input_protocol (osi_protocol_t protocol, u32 node_index) { osi_main_t *lm = &osi_main; vlib_main_t *vm = lm->vlib_main; osi_protocol_info_t *pi; { clib_error_t *error = vlib_call_init_function (vm, osi_input_init); if (error) clib_error_report (error); } pi = osi_get_protocol_info (lm, protocol); pi->node_index = node_index; pi->next_index = vlib_node_add_next (vm, osi_input_node.index, node_index); lm->input_next_by_protocol[protocol] = pi->next_index; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */