1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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  */
18 
19 /* This file contains constants defined in <linux/errqueue.h> and
20  * <linux/net_tstamp.h> so as to allow collecting network timestamps in the
21  * kernel. This file allows tcp_posix.cc to compile on platforms that do not
22  * have <linux/errqueue.h> and <linux/net_tstamp.h>.
23  */
24 
25 #ifndef GRPC_CORE_LIB_IOMGR_INTERNAL_ERRQUEUE_H
26 #define GRPC_CORE_LIB_IOMGR_INTERNAL_ERRQUEUE_H
27 
28 #include <grpc/support/port_platform.h>
29 
30 #include "src/core/lib/iomgr/port.h"
31 
32 #ifdef GRPC_POSIX_SOCKET_TCP
33 
34 #include <sys/types.h>
35 #include <time.h>
36 
37 #ifdef GRPC_LINUX_ERRQUEUE
38 #include <linux/errqueue.h>
39 #include <linux/net_tstamp.h>
40 #include <sys/socket.h>
41 #endif /* GRPC_LINUX_ERRQUEUE */
42 
43 namespace grpc_core {
44 
45 #ifdef GRPC_LINUX_ERRQUEUE
46 
47 /* Redefining scm_timestamping in the same way that <linux/errqueue.h> defines
48  * it, so that code compiles on systems that don't have it. */
49 struct scm_timestamping {
50   struct timespec ts[3];
51 };
52 /* Also redefine timestamp types */
53 /* The timestamp type for when the driver passed skb to NIC, or HW. */
54 constexpr int SCM_TSTAMP_SND = 0;
55 /* The timestamp type for when data entered the packet scheduler. */
56 constexpr int SCM_TSTAMP_SCHED = 1;
57 /* The timestamp type for when data acknowledged by peer. */
58 constexpr int SCM_TSTAMP_ACK = 2;
59 /* Redefine required constants from <linux/net_tstamp.h> */
60 constexpr uint32_t SOF_TIMESTAMPING_TX_SOFTWARE = 1u << 1;
61 constexpr uint32_t SOF_TIMESTAMPING_SOFTWARE = 1u << 4;
62 constexpr uint32_t SOF_TIMESTAMPING_OPT_ID = 1u << 7;
63 constexpr uint32_t SOF_TIMESTAMPING_TX_SCHED = 1u << 8;
64 constexpr uint32_t SOF_TIMESTAMPING_TX_ACK = 1u << 9;
65 constexpr uint32_t SOF_TIMESTAMPING_OPT_TSONLY = 1u << 11;
66 
67 constexpr uint32_t kTimestampingSocketOptions = SOF_TIMESTAMPING_SOFTWARE |
68                                                 SOF_TIMESTAMPING_OPT_ID |
69                                                 SOF_TIMESTAMPING_OPT_TSONLY;
70 constexpr uint32_t kTimestampingRecordingOptions =
71     SOF_TIMESTAMPING_TX_SCHED | SOF_TIMESTAMPING_TX_SOFTWARE |
72     SOF_TIMESTAMPING_TX_ACK;
73 #endif /* GRPC_LINUX_ERRQUEUE */
74 
75 /* Returns true if kernel is capable of supporting errqueue and timestamping.
76  * Currently allowing only linux kernels above 4.0.0
77  */
78 bool kernel_supports_errqueue();
79 }  // namespace grpc_core
80 
81 #endif /* GRPC_POSIX_SOCKET_TCP */
82 
83 #endif /* GRPC_CORE_LIB_IOMGR_INTERNAL_ERRQUEUE_H */
84