Apache Mesos
handle.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __LINUX_ROUTING_HANDLE_HPP__
18 #define __LINUX_ROUTING_HANDLE_HPP__
19 
20 #include <stdint.h>
21 
22 #include <iosfwd>
23 
24 #include <netlink/route/tc.h>
25 
26 #include <stout/try.hpp>
27 
28 namespace routing {
29 
30 // The Linux kernel Traffic Control (TC) uses handles to uniqely
31 // identify the queueing disciplines (qdiscs), classes and filters
32 // attached to a network interface. The most common type of handle is
33 // identified by primary and secondary device numbers (sometimes
34 // called major and minor numbers) and written as primary:secondary.
35 // Handles provide the mechanism by which TC classes, qdiscs and
36 // filters can be connected together to create complex network traffic
37 // policing policies.
38 class Handle
39 {
40 public:
41  explicit constexpr Handle(uint32_t _handle) : handle(_handle) {}
42 
43  constexpr Handle(uint16_t primary, uint16_t secondary)
44  : handle((((uint32_t) primary) << 16) + secondary) {}
45 
46  // NOTE: This is used to construct a classid. The higher 16 bits of
47  // the given 'parent' will be the primary and the lower 16 bits is
48  // specified by the given 'id'.
49  constexpr Handle(const Handle& parent, uint16_t id)
50  : handle((((uint32_t) parent.primary()) << 16) + id) {}
51 
52  constexpr bool operator==(const Handle& that) const
53  {
54  return handle == that.handle;
55  }
56 
57  constexpr bool operator!=(const Handle& that) const
58  {
59  return handle != that.handle;
60  }
61 
62  static Try<Handle> parse(const std::string& str);
63 
64  constexpr uint16_t primary() const { return handle >> 16; }
65  constexpr uint16_t secondary() const { return handle & 0x0000ffff; }
66  constexpr uint32_t get() const { return handle; }
67 
68 protected:
69  uint32_t handle;
70 };
71 
72 
73 std::ostream& operator<<(std::ostream& stream, const Handle& handle);
74 
75 
76 // Packets flowing from the device driver to the network stack are
77 // called ingress traffic, and packets flowing from the network stack
78 // to the device driver are called egress traffic (shown below).
79 //
80 // +---------+
81 // | Network |
82 // | Stack |
83 // |---------|
84 // | eth0 |
85 // +---------+
86 // ^ |
87 // Ingress | | Egress
88 // | |
89 // -------+ +------>
90 //
91 // The root handles for both ingress and egress are immutable.
92 constexpr Handle EGRESS_ROOT = Handle(TC_H_ROOT);
93 constexpr Handle INGRESS_ROOT = Handle(TC_H_INGRESS);
94 
95 } // namespace routing {
96 
97 #endif // __LINUX_ROUTING_HANDLE_HPP__
constexpr Handle(uint32_t _handle)
Definition: handle.hpp:41
uint32_t handle
Definition: handle.hpp:69
Definition: try.hpp:34
constexpr Handle EGRESS_ROOT
Definition: handle.hpp:92
Definition: handle.hpp:38
std::ostream & operator<<(std::ostream &stream, const Handle &handle)
constexpr uint16_t primary() const
Definition: handle.hpp:64
constexpr bool operator!=(const Handle &that) const
Definition: handle.hpp:57
static Try< Handle > parse(const std::string &str)
constexpr uint16_t secondary() const
Definition: handle.hpp:65
constexpr Handle(uint16_t primary, uint16_t secondary)
Definition: handle.hpp:43
constexpr bool operator==(const Handle &that) const
Definition: handle.hpp:52
constexpr Handle INGRESS_ROOT
Definition: handle.hpp:93
constexpr Handle(const Handle &parent, uint16_t id)
Definition: handle.hpp:49