func_linux.go 4.2 KB

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