2014-07-14 11:49:44 +00:00
|
|
|
/* Traffic control (tc): setup interface queuing desciplines.
|
2014-06-28 06:40:52 +00:00
|
|
|
*
|
2016-06-08 23:21:42 +02:00
|
|
|
* VILLASnode uses these functions to setup the network emulation feature.
|
2014-06-25 01:53:48 +00:00
|
|
|
*
|
2022-03-15 09:18:01 -04:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
2022-07-04 18:20:03 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-06-02 21:53:04 +02:00
|
|
|
*/
|
2014-06-25 01:53:48 +00:00
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
#include <netlink/route/cls/fw.h>
|
|
|
|
#include <netlink/route/qdisc/prio.h>
|
|
|
|
|
|
|
|
#include <linux/if_ether.h>
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
#include <villas/exceptions.hpp>
|
2020-09-13 11:01:20 +02:00
|
|
|
#include <villas/kernel/if.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/kernel/kernel.hpp>
|
2020-09-13 11:01:20 +02:00
|
|
|
#include <villas/kernel/nl.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/kernel/tc.hpp>
|
|
|
|
#include <villas/utils.hpp>
|
2014-06-25 01:53:48 +00:00
|
|
|
|
2020-03-04 13:38:40 +01:00
|
|
|
using namespace villas;
|
2020-09-13 11:01:20 +02:00
|
|
|
using namespace villas::kernel;
|
2020-03-04 13:38:40 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int villas::kernel::tc::prio(Interface *i, struct rtnl_qdisc **qd,
|
|
|
|
tc_hdl_t handle, tc_hdl_t parent, int bands) {
|
|
|
|
int ret;
|
|
|
|
struct nl_sock *sock = nl::init();
|
|
|
|
struct rtnl_qdisc *q = rtnl_qdisc_alloc();
|
2014-06-25 01:53:48 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = kernel::loadModule("sch_prio");
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to load kernel module: sch_prio ({})", ret);
|
2017-05-05 19:03:49 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
/* This is the default priomap used by the tc-prio qdisc
|
2015-08-22 17:42:02 +02:00
|
|
|
* We will use the first 'bands' bands internally */
|
2023-09-07 11:46:39 +02:00
|
|
|
uint8_t map[] = QDISC_PRIO_DEFAULT_PRIOMAP;
|
|
|
|
for (unsigned i = 0; i < ARRAY_LEN(map); i++)
|
|
|
|
map[i] += bands;
|
2014-06-25 01:53:48 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
rtnl_tc_set_link(TC_CAST(q), i->nl_link);
|
|
|
|
rtnl_tc_set_parent(TC_CAST(q), parent);
|
|
|
|
rtnl_tc_set_handle(TC_CAST(q), handle);
|
|
|
|
rtnl_tc_set_kind(TC_CAST(q), "prio");
|
2014-06-25 01:53:48 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
rtnl_qdisc_prio_set_bands(q, bands + 3);
|
|
|
|
rtnl_qdisc_prio_set_priomap(q, map, sizeof(map));
|
2014-07-04 09:47:29 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = rtnl_qdisc_add(sock, q, NLM_F_CREATE | NLM_F_REPLACE);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
*qd = q;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
auto logger = logging.get("kernel");
|
|
|
|
logger->debug("Added prio qdisc with {} bands to interface '{}'", bands,
|
|
|
|
rtnl_link_get_name(i->nl_link));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return ret;
|
2014-06-25 01:53:48 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int villas::kernel::tc::mark(Interface *i, struct rtnl_cls **cls,
|
|
|
|
tc_hdl_t flowid, uint32_t mark) {
|
|
|
|
int ret;
|
|
|
|
struct nl_sock *sock = nl::init();
|
|
|
|
struct rtnl_cls *c = rtnl_cls_alloc();
|
2014-06-25 01:53:48 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = kernel::loadModule("cls_fw");
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to load kernel module: cls_fw");
|
2017-05-05 19:03:49 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
rtnl_tc_set_link(TC_CAST(c), i->nl_link);
|
|
|
|
rtnl_tc_set_handle(TC_CAST(c), mark);
|
|
|
|
rtnl_tc_set_kind(TC_CAST(c), "fw");
|
2014-07-04 09:47:29 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
rtnl_cls_set_protocol(c, ETH_P_ALL);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
rtnl_fw_set_classid(c, flowid);
|
|
|
|
rtnl_fw_set_mask(c, 0xFFFFFFFF);
|
2014-06-25 01:53:48 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = rtnl_cls_add(sock, c, NLM_F_CREATE);
|
2015-08-22 17:42:02 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
*cls = c;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
auto logger = logging.get("kernel");
|
|
|
|
logger->debug("Added fwmark classifier with mark {} to interface '{}'", mark,
|
|
|
|
rtnl_link_get_name(i->nl_link));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return ret;
|
2015-08-22 17:42:02 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int villas::kernel::tc::reset(Interface *i) {
|
|
|
|
struct nl_sock *sock = nl::init();
|
2015-08-22 17:42:02 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// We restore the default pfifo_fast qdisc, by deleting ours
|
|
|
|
return rtnl_qdisc_delete(sock, i->tc_qdisc);
|
2014-06-25 01:53:48 +00:00
|
|
|
}
|