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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
/*
* Copyright (C) 2013-2016 B.A.T.M.A.N. contributors:
*
* Antonio Quartulli <a@unstable.cc>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*
*/
#include "main.h"
#include "tp_meter.h"
#include <netinet/ether.h>
#include <netinet/in.h>
#include <netlink/netlink.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <net/if.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include "bat-hosts.h"
#include "batman_adv.h"
#include "functions.h"
#include "genl.h"
#include "netlink.h"
#include "packet.h"
#include "debugfs.h"
static struct ether_addr *dst_mac;
static char *tp_mesh_iface;
struct tp_result {
int error;
bool found;
uint32_t cookie;
uint8_t return_value;
uint32_t test_time;
uint64_t total_bytes;
};
struct tp_cookie {
int error;
bool found;
uint32_t cookie;
};
static int tpmeter_nl_print_error(struct sockaddr_nl *nla __maybe_unused,
struct nlmsgerr *nlerr,
void *arg)
{
struct tp_result *result = arg;
if (nlerr->error != -EOPNOTSUPP)
fprintf(stderr, "Error received: %s\n",
strerror(-nlerr->error));
result->error = nlerr->error;
return NL_STOP;
}
static int tp_meter_result_callback(struct nl_msg *msg, void *arg)
{
struct tp_result *result = arg;
struct nlmsghdr *nlh = nlmsg_hdr(msg);
struct nlattr *attrs[NUM_BATADV_ATTR];
struct genlmsghdr *ghdr;
uint32_t cookie;
if (!genlmsg_valid_hdr(nlh, 0)) {
result->error = -EINVAL;
return NL_STOP;
}
ghdr = nlmsg_data(nlh);
if (ghdr->cmd != BATADV_CMD_TP_METER)
return NL_OK;
if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0),
genlmsg_len(ghdr), batadv_netlink_policy)) {
fputs("Received invalid data from kernel.\n", stderr);
result->error = -EINVAL;
return NL_STOP;
}
if (!attrs[BATADV_ATTR_TPMETER_COOKIE]) {
result->error = -EINVAL;
return NL_STOP;
}
if (!attrs[BATADV_ATTR_TPMETER_RESULT])
return NL_OK;
cookie = nla_get_u32(attrs[BATADV_ATTR_TPMETER_COOKIE]);
if (cookie != result->cookie)
return NL_OK;
result->found = true;
result->return_value = nla_get_u8(attrs[BATADV_ATTR_TPMETER_RESULT]);
if (attrs[BATADV_ATTR_TPMETER_TEST_TIME])
result->test_time = nla_get_u32(attrs[BATADV_ATTR_TPMETER_TEST_TIME]);
if (attrs[BATADV_ATTR_TPMETER_BYTES])
result->total_bytes = nla_get_u64(attrs[BATADV_ATTR_TPMETER_BYTES]);
return NL_OK;
}
static int tp_meter_cookie_callback(struct nl_msg *msg, void *arg)
{
struct tp_cookie *cookie = arg;
struct nlmsghdr *nlh = nlmsg_hdr(msg);
struct nlattr *attrs[NUM_BATADV_ATTR];
struct genlmsghdr *ghdr;
if (!genlmsg_valid_hdr(nlh, 0)) {
cookie->error = -EINVAL;
return NL_STOP;
}
ghdr = nlmsg_data(nlh);
if (ghdr->cmd != BATADV_CMD_TP_METER) {
cookie->error = -EINVAL;
return NL_STOP;
}
if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0),
genlmsg_len(ghdr), batadv_netlink_policy)) {
fputs("Received invalid data from kernel.\n", stderr);
cookie->error = -EINVAL;
return NL_STOP;
}
if (!attrs[BATADV_ATTR_TPMETER_COOKIE]) {
cookie->error = -EINVAL;
return NL_STOP;
}
cookie->cookie = nla_get_u32(attrs[BATADV_ATTR_TPMETER_COOKIE]);
cookie->found = true;
return NL_OK;
}
static int tp_meter_start(char *mesh_iface, struct ether_addr *dst_mac,
uint32_t time, struct tp_cookie *cookie)
{
struct nl_sock *sock;
struct nl_msg *msg;
struct nl_cb *cb;
int ifindex;
int family;
int ret;
int err = 0;
sock = nl_socket_alloc();
if (!sock)
return -ENOMEM;
ret = genl_connect(sock);
if (ret < 0) {
err = -EOPNOTSUPP;
goto out;
}
family = genl_ctrl_resolve(sock, BATADV_NL_NAME);
if (family < 0) {
err = -EOPNOTSUPP;
goto out;
}
ifindex = if_nametoindex(mesh_iface);
if (!ifindex) {
fprintf(stderr, "Interface %s is unknown\n", mesh_iface);
err = -ENODEV;
goto out;
}
cb = nl_cb_alloc(NL_CB_DEFAULT);
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, tp_meter_cookie_callback,
cookie);
nl_cb_err(cb, NL_CB_CUSTOM, tpmeter_nl_print_error, cookie);
msg = nlmsg_alloc();
if (!msg) {
err = -ENOMEM;
goto out;
}
genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0,
0, BATADV_CMD_TP_METER, 1);
nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, ifindex);
nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, dst_mac);
nla_put_u32(msg, BATADV_ATTR_TPMETER_TEST_TIME, time);
nl_send_auto_complete(sock, msg);
nlmsg_free(msg);
nl_recvmsgs(sock, cb);
nl_cb_put(cb);
if (cookie->error < 0)
err = cookie->error;
else if (!cookie->found)
err= -EINVAL;
out:
nl_socket_free(sock);
return err;
}
static int no_seq_check(struct nl_msg *msg __maybe_unused,
void *arg __maybe_unused)
{
return NL_OK;
}
static int tp_recv_result(struct nl_sock *sock, struct tp_result *result)
{
int err = 0;
struct nl_cb *cb;
cb = nl_cb_alloc(NL_CB_DEFAULT);
nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, tp_meter_result_callback,
result);
nl_cb_err(cb, NL_CB_CUSTOM, tpmeter_nl_print_error, result);
while (result->error == 0 && !result->found)
nl_recvmsgs(sock, cb);
nl_cb_put(cb);
if (result->error < 0)
err = result->error;
else if (!result->found)
err= -EINVAL;
return err;
}
static int tp_meter_stop(char *mesh_iface, struct ether_addr *dst_mac)
{
struct nl_sock *sock;
struct nl_msg *msg;
int ifindex;
int family;
int ret;
int err = 0;
sock = nl_socket_alloc();
if (!sock)
return -ENOMEM;
ret = genl_connect(sock);
if (ret < 0) {
err = -EOPNOTSUPP;
goto out;
}
family = genl_ctrl_resolve(sock, BATADV_NL_NAME);
if (family < 0) {
err = -EOPNOTSUPP;
goto out;
}
ifindex = if_nametoindex(mesh_iface);
if (!ifindex) {
fprintf(stderr, "Interface %s is unknown\n", mesh_iface);
err = -ENODEV;
goto out;
}
msg = nlmsg_alloc();
if (!msg) {
err = -ENOMEM;
goto out;
}
genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0,
0, BATADV_CMD_TP_METER_CANCEL, 1);
nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, ifindex);
nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, dst_mac);
nl_send_auto_complete(sock, msg);
nlmsg_free(msg);
out:
nl_socket_free(sock);
return err;
}
static struct nl_sock *tp_prepare_listening_sock(void)
{
struct nl_sock *sock;
int family;
int ret;
int mcid;
sock = nl_socket_alloc();
if (!sock)
return NULL;
ret = genl_connect(sock);
if (ret < 0) {
fprintf(stderr, "Failed to connect to generic netlink: %d\n",
ret);
goto err;
}
family = genl_ctrl_resolve(sock, BATADV_NL_NAME);
if (family < 0) {
fprintf(stderr, "Failed to resolve batman-adv netlink: %d\n",
family);
goto err;
}
mcid = nl_get_multicast_id(sock, BATADV_NL_NAME,
BATADV_NL_MCAST_GROUP_TPMETER);
if (mcid < 0) {
fprintf(stderr, "Failed to resolve batman-adv tpmeter multicast group: %d\n",
mcid);
goto err;
}
ret = nl_socket_add_membership(sock, mcid);
if (ret) {
fprintf(stderr, "Failed to join batman-adv tpmeter multicast group: %d\n",
ret);
goto err;
}
return sock;
err:
nl_socket_free(sock);
return NULL;
}
void tp_sig_handler(int sig)
{
switch (sig) {
case SIGINT:
case SIGTERM:
fflush(stdout);
tp_meter_stop(tp_mesh_iface, dst_mac);
break;
default:
break;
}
}
static void tp_meter_usage(void)
{
fprintf(stderr, "Usage: batctl tp [parameters] <MAC>\n");
fprintf(stderr, "Parameters:\n");
fprintf(stderr, "\t -t <time> test length in milliseconds\n");
fprintf(stderr, "\t -n don't convert addresses to bat-host names\n");
}
int tp_meter(char *mesh_iface, int argc, char **argv)
{
struct bat_host *bat_host;
uint64_t throughput;
char *dst_string;
int ret = EXIT_FAILURE;
int found_args = 1, read_opt = USE_BAT_HOSTS;
uint32_t time = 0;
int optchar;
struct nl_sock *listen_sock = NULL;
struct tp_result result = {
.error = 0,
.return_value = 0,
.test_time = 0,
.total_bytes = 0,
.found = false,
};
struct tp_cookie cookie = {
.error = 0,
.cookie = 0,
.found = false,
};
while ((optchar = getopt(argc, argv, "t:n")) != -1) {
switch (optchar) {
case 't':
found_args += 2;
time = strtoul(optarg, NULL, 10);
break;
case 'n':
read_opt &= ~USE_BAT_HOSTS;
found_args += 1;
break;
default:
tp_meter_usage();
return EXIT_FAILURE;
}
}
if (argc <= found_args) {
tp_meter_usage();
return EXIT_FAILURE;
}
dst_string = argv[found_args];
bat_hosts_init(read_opt);
bat_host = bat_hosts_find_by_name(dst_string);
if (bat_host)
dst_mac = &bat_host->mac_addr;
if (!dst_mac) {
dst_mac = ether_aton(dst_string);
if (!dst_mac) {
printf("Error - the tp meter destination is not a mac address or bat-host name: %s\n",
dst_string);
goto out;
}
}
if (bat_host && (read_opt & USE_BAT_HOSTS))
dst_string = bat_host->name;
else
dst_string = ether_ntoa_long(dst_mac);
/* for sighandler */
tp_mesh_iface = mesh_iface;
signal(SIGINT, tp_sig_handler);
signal(SIGTERM, tp_sig_handler);
listen_sock = tp_prepare_listening_sock();
if (!listen_sock)
goto out;
ret = tp_meter_start(mesh_iface, dst_mac, time, &cookie);
if (ret < 0) {
printf("Failed to send tp_meter request to kernel: %d\n", ret);
goto out;
}
result.cookie = cookie.cookie;
ret = tp_recv_result(listen_sock, &result);
if (ret < 0) {
printf("Failed to recv tp_meter result from kernel: %d\n", ret);
goto out;
}
switch (result.return_value) {
case BATADV_TP_REASON_DST_UNREACHABLE:
fprintf(stderr, "Destination unreachable\n");
break;
case BATADV_TP_REASON_RESEND_LIMIT:
fprintf(stderr,
"The number of retry for the same window exceeds the limit, test aborted\n");
break;
case BATADV_TP_REASON_ALREADY_ONGOING:
fprintf(stderr,
"Cannot run two test towards the same node\n");
break;
case BATADV_TP_REASON_MEMORY_ERROR:
fprintf(stderr,
"Kernel cannot allocate memory, aborted\n");
break;
case BATADV_TP_REASON_TOO_MANY:
fprintf(stderr, "Too many ongoing sessions\n");
break;
case BATADV_TP_REASON_CANCEL:
printf("CANCEL received: test aborted\n");
/* fall through and print the partial result */
case BATADV_TP_REASON_COMPLETE:
if (result.test_time > 0) {
throughput = result.total_bytes * 1000;
throughput /= result.test_time;
} else {
throughput = UINT64_MAX;
}
printf("Test duration %ums.\n", result.test_time);
printf("Sent %" PRIu64 " Bytes.\n", result.total_bytes);
printf("Throughput: ");
if (throughput == UINT64_MAX)
printf("inf\n");
else if (throughput > (1UL<<30))
printf("%.2f GB/s (%2.f Gbps)\n",
(float)throughput / (1<<30),
(float)throughput * 8 / 1000000000);
else if (throughput > (1UL<<20))
printf("%.2f MB/s (%.2f Mbps)\n",
(float)throughput / (1<<20),
(float)throughput * 8 / 1000000);
else if (throughput > (1UL<<10))
printf("%.2f KB/s (%.2f Kbps)\n",
(float)throughput / (1<<10),
(float)throughput * 8 / 1000);
else
printf("%" PRIu64 " Bytes/s (%" PRIu64 " Bps)\n",
throughput, throughput * 8);
ret = 0;
break;
default:
printf("Unrecognized return value %d\n", result.return_value);
}
out:
nl_socket_free(listen_sock);
bat_hosts_free();
return ret;
}
|