<commit_before>---
layout: post
title:  "Introduce to net_cls subsystem"
date:   2017-10-22 19:00:00 +0800
categories: linux
---
***Network classifier cgroup (net_cls)***





## What is net_cls

The Network classifier cgroup provides an interface to
tag network packets with a class identifier (classid).

The Traffic Controller (tc) can be used to assign
different priorities to packets from different cgroups.
Also, Netfilter (iptables) can use this tag to perform
actions on such packets.

Creating a net_cls cgroups instance creates a net_cls.classid file.
This net_cls.classid value is initialized to 0.


## Sample with tc and iptables

You can write hexadecimal values to net_cls.classid; the format for these
values is 0xAAAABBBB; AAAA is the major handle number and BBBB<commit_msg>update: add control scp net speed<commit_after>---
layout: post
title:  "Introduce to net_cls subsystem"
date:   2017-10-22 19:00:00 +0800
categories: linux
---
***Network classifier cgroup (net_cls)***





## What is net_cls

The Network classifier cgroup provides an interface to
tag network packets with a class identifier (classid).

The Traffic Controller (tc) can be used to assign
different priorities to packets from different cgroups.
Also, Netfilter (iptables) can use this tag to perform
actions on such packets.

Creating a net_cls cgroups instance creates a net_cls.classid file.
This net_cls.classid value is initialized to 0.

## Control scp netwoerk speed

Enbale tc on eth1, and set some limit

```
tc -s qdisc ls dev eth1
tc qdisc add dev eth1 root handle 3:0 htb default 3
tc class add dev eth1 parent 3:0 classid 3:0 htb rate 700kbps ceil 800kbps prio 0
tc class add dev eth1 parent 3:0 classid 3:1 htb rate 100kbps ceil 200kbps prio 0
tc class add dev eth1 parent 3:0 classid 3:2 htb rate 200kbps ceil 300kbps prio 0
tc class add dev eth1 parent 3:0 classid 3:3 htb rate 300kbps ceil 400kbps prio 0
```

Make it works with cgroup

```
mkdir /sys/fs/cgroup/net_cls,net_prio/foo
tc filter add dev eth1 protocol ip parent 3:0 prio 1 handle 3:3 cgroup
```


Test scp time

```
time scp file user@another.server:/your_path
```

default is 3:3, so you get around 300kbps speed

Switch to group and check speed

get current bash pid
```
ps
```

set cgroup, and let cgroup to 3:1

```
echo 0x00030001 > /sys/fs/cgroup/net_cls,net_prio/foo/net_cls.classid
echo ${pid} > /sys/fs/cgroup/net_cls,net_prio/foo/tasks
```

check scp speed

```
time scp file user@another.<EMAIL>:/your_path
```

you get speed arount 100kbps


## Sample with tc and iptables

You can write hexadecimal values to net_cls.classid; the format for these
values is 0xAAAABBBB; AAAA is the major handle number and BBBB
{'fraction_non_alphanumeric': 0.07055328629780913, 'fraction_numerical': 0.044188637207575196, 'mean_word_length': 4.303149606299213, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3512878', 'n_tokens_mistral': 996, 'n_tokens_neox': 856, 'n_words': 388}