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
19package service
20
21import (
22	"time"
23
24	"github.com/golang/protobuf/ptypes"
25	durpb "github.com/golang/protobuf/ptypes/duration"
26	channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1"
27	"google.golang.org/grpc/internal/channelz"
28	"google.golang.org/grpc/internal/testutils"
29)
30
31func convertToPtypesDuration(sec int64, usec int64) *durpb.Duration {
32	return ptypes.DurationProto(time.Duration(sec*1e9 + usec*1e3))
33}
34
35func sockoptToProto(skopts *channelz.SocketOptionData) []*channelzpb.SocketOption {
36	var opts []*channelzpb.SocketOption
37	if skopts.Linger != nil {
38		opts = append(opts, &channelzpb.SocketOption{
39			Name: "SO_LINGER",
40			Additional: testutils.MarshalAny(&channelzpb.SocketOptionLinger{
41				Active:   skopts.Linger.Onoff != 0,
42				Duration: convertToPtypesDuration(int64(skopts.Linger.Linger), 0),
43			}),
44		})
45	}
46	if skopts.RecvTimeout != nil {
47		opts = append(opts, &channelzpb.SocketOption{
48			Name: "SO_RCVTIMEO",
49			Additional: testutils.MarshalAny(&channelzpb.SocketOptionTimeout{
50				Duration: convertToPtypesDuration(int64(skopts.RecvTimeout.Sec), int64(skopts.RecvTimeout.Usec)),
51			}),
52		})
53	}
54	if skopts.SendTimeout != nil {
55		opts = append(opts, &channelzpb.SocketOption{
56			Name: "SO_SNDTIMEO",
57			Additional: testutils.MarshalAny(&channelzpb.SocketOptionTimeout{
58				Duration: convertToPtypesDuration(int64(skopts.SendTimeout.Sec), int64(skopts.SendTimeout.Usec)),
59			}),
60		})
61	}
62	if skopts.TCPInfo != nil {
63		additional := testutils.MarshalAny(&channelzpb.SocketOptionTcpInfo{
64			TcpiState:       uint32(skopts.TCPInfo.State),
65			TcpiCaState:     uint32(skopts.TCPInfo.Ca_state),
66			TcpiRetransmits: uint32(skopts.TCPInfo.Retransmits),
67			TcpiProbes:      uint32(skopts.TCPInfo.Probes),
68			TcpiBackoff:     uint32(skopts.TCPInfo.Backoff),
69			TcpiOptions:     uint32(skopts.TCPInfo.Options),
70			// https://golang.org/pkg/syscall/#TCPInfo
71			// TCPInfo struct does not contain info about TcpiSndWscale and TcpiRcvWscale.
72			TcpiRto:          skopts.TCPInfo.Rto,
73			TcpiAto:          skopts.TCPInfo.Ato,
74			TcpiSndMss:       skopts.TCPInfo.Snd_mss,
75			TcpiRcvMss:       skopts.TCPInfo.Rcv_mss,
76			TcpiUnacked:      skopts.TCPInfo.Unacked,
77			TcpiSacked:       skopts.TCPInfo.Sacked,
78			TcpiLost:         skopts.TCPInfo.Lost,
79			TcpiRetrans:      skopts.TCPInfo.Retrans,
80			TcpiFackets:      skopts.TCPInfo.Fackets,
81			TcpiLastDataSent: skopts.TCPInfo.Last_data_sent,
82			TcpiLastAckSent:  skopts.TCPInfo.Last_ack_sent,
83			TcpiLastDataRecv: skopts.TCPInfo.Last_data_recv,
84			TcpiLastAckRecv:  skopts.TCPInfo.Last_ack_recv,
85			TcpiPmtu:         skopts.TCPInfo.Pmtu,
86			TcpiRcvSsthresh:  skopts.TCPInfo.Rcv_ssthresh,
87			TcpiRtt:          skopts.TCPInfo.Rtt,
88			TcpiRttvar:       skopts.TCPInfo.Rttvar,
89			TcpiSndSsthresh:  skopts.TCPInfo.Snd_ssthresh,
90			TcpiSndCwnd:      skopts.TCPInfo.Snd_cwnd,
91			TcpiAdvmss:       skopts.TCPInfo.Advmss,
92			TcpiReordering:   skopts.TCPInfo.Reordering,
93		})
94		opts = append(opts, &channelzpb.SocketOption{
95			Name:       "TCP_INFO",
96			Additional: additional,
97		})
98	}
99	return opts
100}
101