Parsers

linuxnet.qos uses a number of parser classes to parse the output of the tc(8) command:

The following classes are also used in parsing:


Queuing discipline parsing

The QDiscParser class is responsible for parsing the output of tc -s qdisc ls dev <interface>.

The QDiscParser.register_qdisc() method must be invoked to register queueing discipline classes.

class QDiscParser(allow_parsing_errors: bool)[source]

Helper class that creates QDisc objects from the output of the tc(8) command.

classmethod register_qdisc(ident: str, klass) None[source]

Register the given class (which should be a subclass of the QDisc class).

This method is intended to be used for adding support for new queuing disciplines.

Parameters:
  • ident – the qdisc name that appears in the tc -s qdisc ls output.

  • klass – the Python class for this queuing discipline

class QDiscOutput(line_group: List[str])[source]

Helper class used for parsing tc qdisc ls output for a single qdisc

Parameters:

line_group – list of lines, guaranteed not to be empty

get_handle() Handle[source]

Returns the (parsed) Handle of the queuing discipline.

get_parent_handle() Optional[Handle][source]

Returns the (parsed) Handle of the parent of this queueing discipline, or None if this is a root qdisc

get_refcnt() Optional[int][source]

Returns reference count of qdisc (None for non-root qdiscs, and for some root qdiscs like mq)

get_qdisc_line() str[source]

Returns the tc(8) output qdisc line

get_field_iter() Iterator[str][source]

Return an iterator for the (remaining) fields of the qdisc line


Queuing class parsing

The QClassParser class is responsible for parsing the output of tc -s class ls dev <interface>.

The QClassParser.register_qclass() method must be invoked to register Python classes providing support for classes of classful queueing disciplines.

class QClassParser(allow_parsing_errors: bool)[source]

Helper class that creates QClass objects from the output of the tc(8) command.

classmethod register_qclass(ident: str, klass) None[source]

Register the given class (which should be a subclass of the QClass class).

This method is intended to be used for adding support for new queuing discipline classes.

Parameters:
  • ident – the queuing class name that appears in the tc -s class ls output.

  • klass – the Python class for this queuing class

class QClassOutput(line_group: List[str])[source]

Helper class used for parsing tc class ls output for a single qclass

Parameters:

line_group – list of lines, guaranteed not to be empty

get_handle() Handle[source]

Returns the (parsed) Handle of the queuing class.

get_parent_handle() Handle[source]

Returns the (parsed) Handle of the parent of the queuing class.

get_qdisc_handle() Optional[Handle][source]

Returns the Handle of a (leaf) qdisc

get_class_line() str[source]

Returns the tc(8) output class line

get_field_iter() Iterator[str][source]

Return an iterator for the (remaining) fields of the class line


Traffic filter parsing

The TrafficFilterParser class is responsible for parsing the output of tc filter ls dev <interface>.

The TrafficFilterParser.register_filter() method must be invoked to register classes traffic filter classes.

class TrafficFilterParser(allow_parsing_errors: bool)[source]

Helper class that creates TrafficFilter objects from the output of the tc(8) command.

classmethod register_filter(*, filter_type: str, protocol: str, klass) None[source]

Register the given class (which should be a subclass of the TrafficFilter class).

This method is intended to be used for adding support for new traffic filter types.

Parameters:
  • filter_type – a tc(8) filter type, e.g. u32

  • protocol – a tc(8) protocol name, e.g. ip

  • klass – the Python class for this (filter_type, protocol)

classmethod register_action(*, action_name: str, klass) None[source]

Register the given class (which should be a subclass of the TrafficAction class).

This method is intended to be used for adding support for new traffic actions.

Parameters:
  • action_nametc(8) action, e.g. police

  • klass – the Python class for this action

classmethod parse_action(fields: List[str], nfl_iter: Iterator[str]) TrafficAction[source]

Parse the tc(8) output for a traffic action.

Parameters:
  • fields – fields of the output line identifying the action and its arguments; fields[0] is the action type

  • nfl_iter – iterator returning the lines after the lines that start with ‘filter’

Return type:

a TrafficAction instance

Raises a TcParsingError if unable to parse the action


class FilterOutput(proto: str, prio: int, filter_type: str, owner: QNode)[source]

An instance of this class contains the tc(8) output for a single filter.

Parameters:
  • proto – filter protocol

  • prio – filter priority

  • filter_type – filter type

  • ownerQDisc/QClass that owns the filter

get_prio() int[source]

Returns the priority value

get_proto() str[source]

Returns the protocol value

get_filter_type() str[source]

Returns the filter type

get_filter_owner() QNode[source]

Returns the QDisc/QClass that owns the filter

get_first_line() str[source]

Returns the first line from the tc(8) output for this filter

has_nonfilter_lines() bool[source]

Returns True if the filter output has any lines not starting with the word filter

filter_lines_iter() Iterator[FilterOutputLine][source]

Returns an iterator that returns FilterOutputLine instances.

nonfilter_lines_iter() Iterator[str][source]

Returns an iterator that returns lines (strings)


class FilterOutputLine(line: str, fields: List[str])[source]

A class that holds a line of filter output.

One can iterate over the fields of the line:

def parse_fields(fline: FilterOutputLine):
    for field in fline:
        if field == 'xxx':
            ...

The entire line can be returned by using the str() builtin function.

Parameters:
  • line – the complete tc(8) filter line

  • fields – list of fields of the line after the filter type