Linux中的QoS分为入口(Ingress)部分和出口(Egress)部分,入口部分主要用于进行入口流量限速(policing),出口部分主要用于队列调度(queuing scheduling)。大多数排队规则(qdisc)都是用于输出方向的,输入方向只有一个排队规则,即ingress qdisc。ingress qdisc本身的功能很有限,如下;

Ingress qdisc 
The ingress qdisc itself does not require any parameters. It differs from other qdiscs in that it does not occupy the root of a device. Attach it like this:
# tc qdisc add dev eth0 ingress
This allows you to have other, sending qdiscs on your device besides the ingress qdisc.  About the ingress qdisc
Ingress qdisc (known as ffff:) can't have any children classes. (hence the existence of IMQ)
The only thing you can do with the ingress qdisc is attach filters.  About filtering on the ingress qdisc
Since there are no classes to which to direct the packets, the only reasonable option is to drop the packets.
With clever use of filtering, you can limit particular traffic signatures to particular uses of your bandwidth.

具体使用如下命令,进行限速:

tc qdisc add dev vnet1 handle ffff: ingress
tc filter add dev vnet1 parent ffff: protocol all prio 49 basic police rate 10mbit burst 1mb mtu 65535 drop

根据tc相关文档描述,使用tc ingress限速,功能有限,似乎只能选择丢弃,并且也不支持分类。实际应用中,我们可以将业务流重定向到ifb设备上,业务流从这个ifb设备中出去,再又相应的端口接收,那我们就可以像正常使用tc对egress限速一样,来对ifb设备进行egress限速,就可以达到对接收方向的限速了。具体原理可以参考最下面列出的文档。

ifb模块需要手动加载。
# modprobe ifb 启用虚拟设备ifb0
# ip link set dev ifb0 up

接下来配置ifb0的过滤规则

tc qdisc add dev ens3f3 handle ffff: ingress
tc filter add dev ens3f3 parent ffff: protocol ip u32 match u32 0 0 action mirred egress redirect dev ifb0
tc qdisc add dev ifb0 root handle 1: htb default 10
tc class add dev ifb0 parent 1: classid 1:1 htb rate 10000mbit
tc class add dev ifb0 parent 1:1 classid 1:10 htb rate 1000mbit ceil 1000mbit

注:上述配置可以针对端口来限速配置,但是不能与cgroup配合完成对某些进程组限速。

本来是想用cgroup,像限速egress侧一样,限速ingress侧,但是实践后发现不生效,查过了相关的网络资料,很多人讲在ingress侧,是无法达到像cgroup一样限制带宽的,具体是与cgroup标记的先后有关系。

be aware that if you use iptable to mark your packet and then filters them, you can't use ifb since all ingress trafic will be forwarded BEFORE any marking. so you will se your class stay at 0 and all forwarded to the default. IMQ seem the rigth solution for iptables users.

来自 <https://serverfault.com/questions/350023/tc-ingress-policing-and-ifb-mirroring

但可以通过报文中的源ip进行限速控制,如下配置:

tc qdisc add dev ifb0 root handle 1: htb default 20
tc class add dev ifb0 parent 1: classid 1:1 htb rate 10000mbit
tc class add dev ifb0 parent 1:1 classid 1:10 htb rate 2000mbit
tc class add dev ifb0 parent 1:1 classid 1:20 htb rate 1000mbit
tc class add dev ifb0 parent 1:1 classid 1:30 htb rate 500mbit
tc filter add dev ifb0 protocol ip parent 1:0 prio 1 u32 match ip src 129.9.123.85 flowid 1:10
tc filter add dev ifb0 protocol ip parent 1:0 prio 1 u32 match ip src 129.9.123.89 flowid 1:20
tc filter add dev ifb0 protocol ip parent 1:0 prio 1 u32 match ip src 129.9.123.88 flowid 1:20

当然我们也可以通过源端口和目的端口来限速,如下配置:

tc filter add dev ifb0 protocol ip parent 1:0 prio 1 u32 match ip dport 50051 0xffff flowid 1:30
tc filter add dev ifb0 protocol ip parent 1:0 prio 1 u32 match ip sport 45678 0xffff flowid 1:30

参考文档:

https://blog.csdn.net/zhangskd/article/details/8240290 
https://www.cnblogs.com/CasonChan/p/4919921.html

使用tc ingress来限速接收方向的更多相关文章

  1. 使用iptables和tc对端口限速

    首先,我们来看一下tc,TC(Traffic Control)命令,是linux自带的告警流控命令.Linux操作系统中的流量控制器TC(Traffic Control)用于Linux内核的流量控制, ...

  2. tc令牌桶限速心得

    一.实验拓扑与实验现象 实验拓扑如图所示,在①号机上发送数据,③号机上接受数据,同时在④号机的eth1与eth2网口限制速率为115200kbps,命令如下 tc qdisc add dev eth1 ...

  3. 使用tc配置后端设备,来限制虚拟机网卡带宽

    如果通过tc来限制虚拟机网卡接收方向带宽呢,实际上使用tc对接收方向限制的不够好,使用tc ingress可以限制接收,但是功能不够多,而且会形成丢包问题.一般是采用将流量重定向到一个虚拟设备ifb上 ...

  4. 使用tc对linux中某ip段限速

    TC 无需安装,Linux 内核自带例:将IP地址段192.168.1.0/24 上传下载限速为 5M将以下内容添加到/etc/ppp/ip-up文件exit 0上面. down=5Mbituploa ...

  5. ios的位置和方向(来自苹果官方文档,仅供简单参考)

    取得用户的当前位置 Core Location框架使您可以定位设备的当前位置,并将这个信息应用到程序中.该框架利用设备内置的硬件,在已有信号的基础上通过三角测量得到固定位置,然后将它报告给您的代码.在 ...

  6. iOS使用位置和方向服务(来自苹果apple官方)

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   本文章来自苹果官方文档,特此声明--------禚 Core Location框架为定位用户当前位置和方向(Headin ...

  7. 全面介绍eBPF-概念

    全面介绍eBPF-概念 前面介绍了BCC可观测性和BCC网络,但对底层使用的eBPF的介绍相对较少,且官方欠缺对网络方面的介绍.下面对eBPF进行全面介绍. 目录 全面介绍eBPF-概念 BPF概述 ...

  8. opencontrail-vrouter命令

    vif命令 vrouter需要vrouter接口(vif)来转发流量.使用vif命令查看vrouter已知的接口. 注意: 仅在OS(Linux)中使用接口不足以进行转发.相关接口必须添加到vrout ...

  9. iptables 安全

    注解:来自某位大神的详解,做个笔记. iptables防火墙简介 Netfilter/Iptables(以下简称Iptables)是unix/linux自带的一款优秀且开放源代码的安全自由的**基于包 ...

随机推荐

  1. Spring系列11:@ComponentScan批量注册bean

    回顾 在前面的章节,我们介绍了@Comfiguration和@Bean结合AnnotationConfigApplicationContext零xml配置文件使用Spring容器的方式,也介绍了通过& ...

  2. 有个姑娘叫history

    文章目录 常用参数 history的一些用法 修改history命令默认保存的数量 来给history穿衣服 让我们重新认识一下history history命令用于显示用户以前执行过的历史命令,并且 ...

  3. spring boot全局配置文件优先级

    前两篇介绍的application配置文件,即为spring boot全局配置文件.那么spring boot加载配置文件的时候,怎么确定加载哪个目录下哪个文件呢? spring boot默认的配置文 ...

  4. RPC 技术及其框架 Sekiro 在爬虫逆向中的应用,加密数据一把梭!

    什么是 RPC RPC,英文 RangPaCong,中文让爬虫,旨在为爬虫开路,秒杀一切,让爬虫畅通无阻! 开个玩笑,实际上 RPC 为远程过程调用,全称 Remote Procedure Call, ...

  5. 使用Hot Chocolate和.NET 6构建GraphQL应用(9) —— 实现Mutate更新数据

    系列导航 使用Hot Chocolate和.NET 6构建GraphQL应用文章索引 需求 在上一篇文章中,我们演示了如何使用Hot Chocolate进行GraphQL的Mutate新增数据,这篇文 ...

  6. Centos安装Sublime text

    Sublime text官方给出方法如下: 1.终端执行以下命令: 安装 GPG 密匙 sudo rpm -v --import https://download.sublimetext.com/su ...

  7. 微信小程序wifi控制开发

    小程序wifi控制开发方案 ----- 由小程序通过公众号给wifi控制板进行配网,后台服务器自动生成设备名并注册在数据库中,wifi控制板自动保存生成的设备名,小程序可读取WIFI控制板的数据,控制 ...

  8. CSRF POST型

    实验目的 掌握POST型CSRF基础知识 实验原理 管理员点击恶意链接,密码被修改 实验内容 POST型CSRF漏洞 实验环境描述 带有CSRF漏洞的注册登录系统 实验步骤 登录密码 用户名为 roo ...

  9. 李宏毅机器学习笔记——新手感悟——PM2.5作业

    python也不会,计算机也不会,啥都不会,只带了个脑子考了计算机研究生.研究生选了人工智能方向.看来注定是漫长的学习之旅. PM2.5作业,我是一个字都看不懂.所以我采用了直接看答案的方案.把答案看 ...

  10. 自助BI工具:Tableau和Smartbi的对比(上)

    商业智能(BI)和分析平台长期以来一直是商业的重要组成部分,但由于自助BI工具的兴起,在数据科学家和数据库管理员的支持下,分析的责任已经从IT转移到了商业分析师身上. 因此,BI已经从生成月度报告的记 ...