...

Source file src/google.golang.org/grpc/channelz/internal/protoconv/sockopt_linux.go

Documentation: google.golang.org/grpc/channelz/internal/protoconv

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

View as plain text