WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Linux · Expert · question 73 of 100

How do you set up and manage network traffic control and Quality of Service (QoS) in Linux using tools such as tc and iptables?

📕 Buy this interview preparation book: 100 Linux questions & answers — PDF + EPUB for $5

In Linux, network traffic control and Quality of Service (QoS) can be achieved through various tools and techniques. The most commonly used tools are tc (Traffic Control) and iptables.

tc is a command-line utility used for configuring network traffic control settings in Linux. It allows the user to specify bandwidth limits, priority levels, packet filtering, and other network parameters. It operates by controlling the queuing and scheduling of network traffic on a per-interface basis.

Iptables, on the other hand, is a powerful firewall utility used to filter network packets based on various criteria such as source and destination IP address, port number, and protocol. It can also be used for traffic shaping and QoS.

To set up QoS in Linux using tc, you first need to create a traffic control class hierarchy. This hierarchy consists of classes, filters, and qdiscs. Classes define the bandwidth limits and priorities for each type of traffic, while filters match packets to specific classes based on criteria such as source and destination IP address or port number. Qdiscs define the queuing discipline to be used for each class.

For example, to create a class hierarchy that prioritizes VoIP traffic, you can use the following commands:

    # Create a root qdisc
    tc qdisc add dev eth0 root handle 1: htb
    
    # Create a parent class with a guaranteed bandwidth of 1mbps
    tc class add dev eth0 parent 1: classid 1:1 htb rate 1mbps
    
    # Create a child class for VoIP traffic with a guaranteed bandwidth of 256kbps
    tc class add dev eth0 parent 1:1 classid 1:10 htb rate 256kbps prio 1
    
    # Create a filter to match VoIP traffic based on UDP port number
    tc filter add dev eth0 parent 1: protocol ip prio 1 u32 match ip protocol 17 0xffff match udp sport 5060 0xffff flowid 1:10

This creates a root qdisc on the eth0 interface with a hierarchical token bucket (htb) queuing discipline. A parent class is created with a guaranteed bandwidth of 1mbps, and a child class for VoIP traffic is created with a guaranteed bandwidth of 256kbps and a higher priority than other traffic. Finally, a filter is created to match VoIP traffic based on the UDP port number and send it to the VoIP class.

To set up QoS using iptables, you can use the MARK target to mark packets based on their source and destination IP addresses or port numbers. Then, you can use tc to classify and prioritize packets based on their marking.

For example, to prioritize traffic from a specific IP address, you can use the following commands:

    # Mark incoming traffic from a specific IP address
    iptables -t mangle -A PREROUTING -s 192.168.1.100 -j MARK --set-mark 10
    
    # Create a qdisc for the marked packets
    tc qdisc add dev eth0 root handle 1: htb default 10
    
    # Create a class for the marked packets with a higher priority
    tc class add dev eth0 parent 1: classid 1:10 htb rate 1mbps prio 1
    
    # Create a filter to match the marked packets
    tc filter add dev eth0 parent 1: protocol ip prio 1 handle 10 fw flowid 1:10

This marks incoming traffic from the IP address 192.168.1.100 with a value of 10, then creates a qdisc on the eth0 interface with a hierarchical token bucket queuing discipline and a default class of 10

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Linux interview — then scores it.
📞 Practice Linux — free 15 min
📕 Buy this interview preparation book: 100 Linux questions & answers — PDF + EPUB for $5

All 100 Linux questions · All topics