google.golang.org/grpc@v1.62.1/channelz/service/func_linux.go (about)

     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  package service
    20  
    21  import (
    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/protobuf/types/known/anypb"
    29  )
    30  
    31  func convertToPtypesDuration(sec int64, usec int64) *durpb.Duration {
    32  	return ptypes.DurationProto(time.Duration(sec*1e9 + usec*1e3))
    33  }
    34  
    35  func sockoptToProto(skopts *channelz.SocketOptionData) []*channelzpb.SocketOption {
    36  	var opts []*channelzpb.SocketOption
    37  	if skopts.Linger != nil {
    38  		additional, err := anypb.New(&channelzpb.SocketOptionLinger{
    39  			Active:   skopts.Linger.Onoff != 0,
    40  			Duration: convertToPtypesDuration(int64(skopts.Linger.Linger), 0),
    41  		})
    42  		if err == nil {
    43  			opts = append(opts, &channelzpb.SocketOption{
    44  				Name:       "SO_LINGER",
    45  				Additional: additional,
    46  			})
    47  		} else {
    48  			logger.Warningf("Failed to marshal socket options linger %+v: %v", skopts.Linger, err)
    49  		}
    50  	}
    51  	if skopts.RecvTimeout != nil {
    52  		additional, err := anypb.New(&channelzpb.SocketOptionTimeout{
    53  			Duration: convertToPtypesDuration(int64(skopts.RecvTimeout.Sec), int64(skopts.RecvTimeout.Usec)),
    54  		})
    55  		if err == nil {
    56  			opts = append(opts, &channelzpb.SocketOption{
    57  				Name:       "SO_RCVTIMEO",
    58  				Additional: additional,
    59  			})
    60  		} else {
    61  			logger.Warningf("Failed to marshal socket options receive timeout %+v: %v", skopts.RecvTimeout, err)
    62  		}
    63  	}
    64  	if skopts.SendTimeout != nil {
    65  		additional, err := anypb.New(&channelzpb.SocketOptionTimeout{
    66  			Duration: convertToPtypesDuration(int64(skopts.SendTimeout.Sec), int64(skopts.SendTimeout.Usec)),
    67  		})
    68  		if err == nil {
    69  			opts = append(opts, &channelzpb.SocketOption{
    70  				Name:       "SO_SNDTIMEO",
    71  				Additional: additional,
    72  			})
    73  		} else {
    74  			logger.Warningf("Failed to marshal socket options send timeout %+v: %v", skopts.SendTimeout, err)
    75  		}
    76  	}
    77  	if skopts.TCPInfo != nil {
    78  		additional, err := anypb.New(&channelzpb.SocketOptionTcpInfo{
    79  			TcpiState:       uint32(skopts.TCPInfo.State),
    80  			TcpiCaState:     uint32(skopts.TCPInfo.Ca_state),
    81  			TcpiRetransmits: uint32(skopts.TCPInfo.Retransmits),
    82  			TcpiProbes:      uint32(skopts.TCPInfo.Probes),
    83  			TcpiBackoff:     uint32(skopts.TCPInfo.Backoff),
    84  			TcpiOptions:     uint32(skopts.TCPInfo.Options),
    85  			// https://golang.org/pkg/syscall/#TCPInfo
    86  			// TCPInfo struct does not contain info about TcpiSndWscale and TcpiRcvWscale.
    87  			TcpiRto:          skopts.TCPInfo.Rto,
    88  			TcpiAto:          skopts.TCPInfo.Ato,
    89  			TcpiSndMss:       skopts.TCPInfo.Snd_mss,
    90  			TcpiRcvMss:       skopts.TCPInfo.Rcv_mss,
    91  			TcpiUnacked:      skopts.TCPInfo.Unacked,
    92  			TcpiSacked:       skopts.TCPInfo.Sacked,
    93  			TcpiLost:         skopts.TCPInfo.Lost,
    94  			TcpiRetrans:      skopts.TCPInfo.Retrans,
    95  			TcpiFackets:      skopts.TCPInfo.Fackets,
    96  			TcpiLastDataSent: skopts.TCPInfo.Last_data_sent,
    97  			TcpiLastAckSent:  skopts.TCPInfo.Last_ack_sent,
    98  			TcpiLastDataRecv: skopts.TCPInfo.Last_data_recv,
    99  			TcpiLastAckRecv:  skopts.TCPInfo.Last_ack_recv,
   100  			TcpiPmtu:         skopts.TCPInfo.Pmtu,
   101  			TcpiRcvSsthresh:  skopts.TCPInfo.Rcv_ssthresh,
   102  			TcpiRtt:          skopts.TCPInfo.Rtt,
   103  			TcpiRttvar:       skopts.TCPInfo.Rttvar,
   104  			TcpiSndSsthresh:  skopts.TCPInfo.Snd_ssthresh,
   105  			TcpiSndCwnd:      skopts.TCPInfo.Snd_cwnd,
   106  			TcpiAdvmss:       skopts.TCPInfo.Advmss,
   107  			TcpiReordering:   skopts.TCPInfo.Reordering,
   108  		})
   109  		if err == nil {
   110  			opts = append(opts, &channelzpb.SocketOption{
   111  				Name:       "TCP_INFO",
   112  				Additional: additional,
   113  			})
   114  		} else {
   115  			logger.Warningf("Failed to marshal socket options TCP info %+v: %v", skopts.TCPInfo, err)
   116  		}
   117  	}
   118  	return opts
   119  }