Apache Mesos
diagnosis.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_DIAGNOSIS_DIAGNOSIS_HPP__
18 #define __LINUX_ROUTING_DIAGNOSIS_DIAGNOSIS_HPP__
19 
20 #include <sys/socket.h> // For protocol families, e.g., AF_INET6 is IPv6.
21 
22 #include <netinet/tcp.h> // For tcp_info.
23 
24 #include <vector>
25 
26 #include <stout/ip.hpp>
27 #include <stout/option.hpp>
28 #include <stout/try.hpp>
29 
30 namespace routing {
31 namespace diagnosis {
32 namespace socket {
33 namespace state {
34 
35 // The different connection states of a socket.
36 // TODO(chzhcn): libnl3-idiag still uses the old idiag kernel API,
37 // which only supports TCP sockets. When it moves to the newer API,
38 // consider changing this to a per-family state structure.
39 const int UNKNOWN = 0;
40 const int ESTABLISHED = 1 << 1;
41 const int SYN_SENT = 1 << 2;
42 const int SYN_RECV = 1 << 3;
43 const int FIN_WAIT1 = 1 << 4;
44 const int FIN_WAIT2 = 1 << 5;
45 const int TIME_WAIT = 1 << 6;
46 const int CLOSE = 1 << 7;
47 const int CLOSE_WAIT = 1 << 8;
48 const int LAST_ACK = 1 << 9;
49 const int LISTEN = 1 << 10;
50 const int CLOSING = 1 << 11;
51 const int MAX = 1 << 12;
52 const int ALL = MAX - 1;
53 
54 } // namespace state {
55 
56 // The diagnosis information of a socket. We only included a few
57 // members of 'struct idiagnl_msg' from libnl3-idiag, but more could
58 // be added later on.
59 struct Info
60 {
61  Info(int _family,
62  int _state,
63  uint32_t _inode,
64  const Option<uint16_t>& _sourcePort,
65  const Option<uint16_t>& _destinationPort,
66  const Option<net::IP>& _sourceIP,
67  const Option<net::IP>& _destinationIP,
68  const Option<struct tcp_info>& _tcpInfo)
69  : family(_family),
70  state(_state),
71  inode(_inode),
72  sourcePort(_sourcePort),
73  destinationPort(_destinationPort),
74  sourceIP(_sourceIP),
75  destinationIP(_destinationIP),
76  tcpInfo(_tcpInfo) {}
77 
78  int family;
79  int state;
80  uint32_t inode;
81 
82  // sourcePort, destinationPort, sourceIP and destinationIP should
83  // all be present because this version of kernel API that libnl3
84  // uses can only return TCP sockets. We leave them as optional here
85  // because future support of other families could leave them as
86  // empty values.
91 
92  // tcp_info is included in the kernel header files so we expose it
93  // directly.
95 };
96 
97 
98 // Return a list of socket information that matches the given protocol
99 // family and socket states. 'states' can accept multiple states using
100 // bitwise OR.
101 // NOTE: 'family' is actually ignored here because the older kernel
102 // idiag API libnl3 uses only supports TCP and ignores this value. We
103 // keep it here to follow libnl3-idiag's suit.
104 Try<std::vector<Info>> infos(int familiy, int states);
105 
106 } // namespace socket {
107 } // namespace diagnosis {
108 } // namespace routing {
109 
110 #endif // __LINUX_ROUTING_DIAGNOSIS_DIAGNOSIS_HPP__
Option< net::IP > destinationIP
Definition: diagnosis.hpp:90
Option< net::IP > sourceIP
Definition: diagnosis.hpp:89
Definition: check.hpp:33
const int ESTABLISHED
Definition: diagnosis.hpp:40
const int ALL
Definition: diagnosis.hpp:52
const int CLOSING
Definition: diagnosis.hpp:50
Try< ino_t > inode(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:202
const int SYN_SENT
Definition: diagnosis.hpp:41
Try< std::vector< Info > > infos(int familiy, int states)
const int LAST_ACK
Definition: diagnosis.hpp:48
Definition: diagnosis.hpp:59
const int CLOSE_WAIT
Definition: diagnosis.hpp:47
uint32_t inode
Definition: diagnosis.hpp:80
const int FIN_WAIT1
Definition: diagnosis.hpp:43
const int UNKNOWN
Definition: diagnosis.hpp:39
const int LISTEN
Definition: diagnosis.hpp:49
int family
Definition: diagnosis.hpp:78
const int TIME_WAIT
Definition: diagnosis.hpp:45
const int FIN_WAIT2
Definition: diagnosis.hpp:44
Option< uint16_t > sourcePort
Definition: diagnosis.hpp:87
Definition: diagnosis.hpp:30
Info(int _family, int _state, uint32_t _inode, const Option< uint16_t > &_sourcePort, const Option< uint16_t > &_destinationPort, const Option< net::IP > &_sourceIP, const Option< net::IP > &_destinationIP, const Option< struct tcp_info > &_tcpInfo)
Definition: diagnosis.hpp:61
const int MAX
Definition: diagnosis.hpp:51
int state
Definition: diagnosis.hpp:79
const int SYN_RECV
Definition: diagnosis.hpp:42
Try< Netlink< struct nl_sock > > socket(int protocol=NETLINK_ROUTE)
Definition: internal.hpp:91
const int CLOSE
Definition: diagnosis.hpp:46
Option< uint16_t > destinationPort
Definition: diagnosis.hpp:88
Option< struct tcp_info > tcpInfo
Definition: diagnosis.hpp:94