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
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* 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.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
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>
|
|
|
|
|
2018-02-06 21:26:12 +01:00
|
|
|
#include <villas/utils.h>
|
2016-06-14 01:17:58 +02:00
|
|
|
|
2018-02-06 21:26:12 +01:00
|
|
|
#include <villas/kernel/kernel.h>
|
|
|
|
#include <villas/kernel/if.h>
|
|
|
|
#include <villas/kernel/tc.h>
|
|
|
|
#include <villas/kernel/nl.h>
|
2014-06-25 01:53:48 +00:00
|
|
|
|
2015-09-14 18:05:03 +02:00
|
|
|
int tc_prio(struct interface *i, struct rtnl_qdisc **qd, tc_hdl_t handle, tc_hdl_t parent, int bands)
|
2014-06-25 01:53:48 +00:00
|
|
|
{
|
2017-03-12 17:06:00 -03:00
|
|
|
int ret;
|
2015-08-22 17:42:02 +02:00
|
|
|
struct nl_sock *sock = nl_init();
|
|
|
|
struct rtnl_qdisc *q = rtnl_qdisc_alloc();
|
2014-06-25 01:53:48 +00:00
|
|
|
|
2017-05-05 19:03:49 +00:00
|
|
|
ret = kernel_module_load("sch_prio");
|
|
|
|
if (ret)
|
|
|
|
error("Failed to load kernel module: sch_prio (%d)", ret);
|
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
/* This is the default priomap used by the tc-prio qdisc
|
|
|
|
* We will use the first 'bands' bands internally */
|
2015-09-14 18:05:03 +02:00
|
|
|
uint8_t map[] = QDISC_PRIO_DEFAULT_PRIOMAP;
|
2015-08-22 17:42:02 +02:00
|
|
|
for (int i = 0; i < ARRAY_LEN(map); i++)
|
|
|
|
map[i] += bands;
|
2014-06-25 01:53:48 +00:00
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
rtnl_tc_set_link(TC_CAST(q), i->nl_link);
|
2015-09-14 18:05:03 +02:00
|
|
|
rtnl_tc_set_parent(TC_CAST(q), parent);
|
2015-08-22 17:42:02 +02:00
|
|
|
rtnl_tc_set_handle(TC_CAST(q), handle);
|
2017-05-05 19:24:16 +00:00
|
|
|
rtnl_tc_set_kind(TC_CAST(q), "prio");
|
2014-06-25 01:53:48 +00:00
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
rtnl_qdisc_prio_set_bands(q, bands + 3);
|
2015-09-14 18:05:03 +02:00
|
|
|
rtnl_qdisc_prio_set_priomap(q, map, sizeof(map));
|
2014-07-04 09:47:29 +00:00
|
|
|
|
2017-03-12 17:06:00 -03:00
|
|
|
ret = rtnl_qdisc_add(sock, q, NLM_F_CREATE | NLM_F_REPLACE);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
*qd = q;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:06:00 -03:00
|
|
|
debug(LOG_TC | 3, "Added prio qdisc with %d bands to interface '%s'", bands, rtnl_link_get_name(i->nl_link));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
return ret;
|
2014-06-25 01:53:48 +00:00
|
|
|
}
|
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
int tc_mark(struct interface *i, struct rtnl_cls **cls, tc_hdl_t flowid, uint32_t mark)
|
2014-06-25 01:53:48 +00:00
|
|
|
{
|
2017-05-05 19:03:49 +00:00
|
|
|
int ret;
|
2015-08-22 17:42:02 +02:00
|
|
|
struct nl_sock *sock = nl_init();
|
|
|
|
struct rtnl_cls *c = rtnl_cls_alloc();
|
2014-06-25 01:53:48 +00:00
|
|
|
|
2017-05-05 19:03:49 +00:00
|
|
|
ret = kernel_module_load("cls_fw");
|
|
|
|
if (ret)
|
|
|
|
error("Failed to load kernel module: cls_fw");
|
|
|
|
|
2015-08-22 17:42:02 +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
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
rtnl_cls_set_protocol(c, ETH_P_ALL);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
rtnl_fw_set_classid(c, flowid);
|
|
|
|
rtnl_fw_set_mask(c, 0xFFFFFFFF);
|
2014-06-25 01:53:48 +00:00
|
|
|
|
2017-05-05 19:03:49 +00:00
|
|
|
ret = rtnl_cls_add(sock, c, NLM_F_CREATE);
|
2015-08-22 17:42:02 +02:00
|
|
|
|
|
|
|
*cls = c;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:06:00 -03:00
|
|
|
debug(LOG_TC | 3, "Added fwmark classifier with mark %d to interface '%s'", mark, rtnl_link_get_name(i->nl_link));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-08-22 17:42:02 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tc_reset(struct interface *i)
|
|
|
|
{
|
|
|
|
struct nl_sock *sock = nl_init();
|
|
|
|
|
|
|
|
/* We restore the default pfifo_fast qdisc, by deleting ours */
|
2017-05-05 19:24:16 +00:00
|
|
|
return rtnl_qdisc_delete(sock, i->tc_qdisc);
|
2014-06-25 01:53:48 +00:00
|
|
|
}
|