service_sktopt_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //go:build linux && (386 || amd64)
  2. // +build linux
  3. // +build 386 amd64
  4. /*
  5. *
  6. * Copyright 2018 gRPC authors.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. */
  21. // SocketOptions is only supported on linux system. The functions defined in
  22. // this file are to parse the socket option field and the test is specifically
  23. // to verify the behavior of socket option parsing.
  24. package service
  25. import (
  26. "context"
  27. "strconv"
  28. "testing"
  29. "github.com/golang/protobuf/ptypes"
  30. "github.com/google/go-cmp/cmp"
  31. "golang.org/x/sys/unix"
  32. "google.golang.org/grpc/internal/channelz"
  33. "google.golang.org/protobuf/testing/protocmp"
  34. durpb "github.com/golang/protobuf/ptypes/duration"
  35. channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1"
  36. )
  37. func init() {
  38. // Assign protoToSocketOption to protoToSocketOpt in order to enable socket option
  39. // data conversion from proto message to channelz defined struct.
  40. protoToSocketOpt = protoToSocketOption
  41. }
  42. func convertToDuration(d *durpb.Duration) (sec int64, usec int64) {
  43. if d != nil {
  44. if dur, err := ptypes.Duration(d); err == nil {
  45. sec = int64(int64(dur) / 1e9)
  46. usec = (int64(dur) - sec*1e9) / 1e3
  47. }
  48. }
  49. return
  50. }
  51. func protoToLinger(protoLinger *channelzpb.SocketOptionLinger) *unix.Linger {
  52. linger := &unix.Linger{}
  53. if protoLinger.GetActive() {
  54. linger.Onoff = 1
  55. }
  56. lv, _ := convertToDuration(protoLinger.GetDuration())
  57. linger.Linger = int32(lv)
  58. return linger
  59. }
  60. func protoToSocketOption(skopts []*channelzpb.SocketOption) *channelz.SocketOptionData {
  61. skdata := &channelz.SocketOptionData{}
  62. for _, opt := range skopts {
  63. switch opt.GetName() {
  64. case "SO_LINGER":
  65. protoLinger := &channelzpb.SocketOptionLinger{}
  66. err := ptypes.UnmarshalAny(opt.GetAdditional(), protoLinger)
  67. if err == nil {
  68. skdata.Linger = protoToLinger(protoLinger)
  69. }
  70. case "SO_RCVTIMEO":
  71. protoTimeout := &channelzpb.SocketOptionTimeout{}
  72. err := ptypes.UnmarshalAny(opt.GetAdditional(), protoTimeout)
  73. if err == nil {
  74. skdata.RecvTimeout = protoToTime(protoTimeout)
  75. }
  76. case "SO_SNDTIMEO":
  77. protoTimeout := &channelzpb.SocketOptionTimeout{}
  78. err := ptypes.UnmarshalAny(opt.GetAdditional(), protoTimeout)
  79. if err == nil {
  80. skdata.SendTimeout = protoToTime(protoTimeout)
  81. }
  82. case "TCP_INFO":
  83. tcpi := &channelzpb.SocketOptionTcpInfo{}
  84. err := ptypes.UnmarshalAny(opt.GetAdditional(), tcpi)
  85. if err == nil {
  86. skdata.TCPInfo = &unix.TCPInfo{
  87. State: uint8(tcpi.TcpiState),
  88. Ca_state: uint8(tcpi.TcpiCaState),
  89. Retransmits: uint8(tcpi.TcpiRetransmits),
  90. Probes: uint8(tcpi.TcpiProbes),
  91. Backoff: uint8(tcpi.TcpiBackoff),
  92. Options: uint8(tcpi.TcpiOptions),
  93. Rto: tcpi.TcpiRto,
  94. Ato: tcpi.TcpiAto,
  95. Snd_mss: tcpi.TcpiSndMss,
  96. Rcv_mss: tcpi.TcpiRcvMss,
  97. Unacked: tcpi.TcpiUnacked,
  98. Sacked: tcpi.TcpiSacked,
  99. Lost: tcpi.TcpiLost,
  100. Retrans: tcpi.TcpiRetrans,
  101. Fackets: tcpi.TcpiFackets,
  102. Last_data_sent: tcpi.TcpiLastDataSent,
  103. Last_ack_sent: tcpi.TcpiLastAckSent,
  104. Last_data_recv: tcpi.TcpiLastDataRecv,
  105. Last_ack_recv: tcpi.TcpiLastAckRecv,
  106. Pmtu: tcpi.TcpiPmtu,
  107. Rcv_ssthresh: tcpi.TcpiRcvSsthresh,
  108. Rtt: tcpi.TcpiRtt,
  109. Rttvar: tcpi.TcpiRttvar,
  110. Snd_ssthresh: tcpi.TcpiSndSsthresh,
  111. Snd_cwnd: tcpi.TcpiSndCwnd,
  112. Advmss: tcpi.TcpiAdvmss,
  113. Reordering: tcpi.TcpiReordering}
  114. }
  115. }
  116. }
  117. return skdata
  118. }
  119. func (s) TestGetSocketOptions(t *testing.T) {
  120. czCleanup := channelz.NewChannelzStorageForTesting()
  121. defer cleanupWrapper(czCleanup, t)
  122. ss := []*dummySocket{
  123. {
  124. socketOptions: &channelz.SocketOptionData{
  125. Linger: &unix.Linger{Onoff: 1, Linger: 2},
  126. RecvTimeout: &unix.Timeval{Sec: 10, Usec: 1},
  127. SendTimeout: &unix.Timeval{},
  128. TCPInfo: &unix.TCPInfo{State: 1},
  129. },
  130. },
  131. }
  132. svr := newCZServer()
  133. ids := make([]*channelz.Identifier, len(ss))
  134. svrID := channelz.RegisterServer(&dummyServer{}, "")
  135. defer channelz.RemoveEntry(svrID)
  136. for i, s := range ss {
  137. ids[i], _ = channelz.RegisterNormalSocket(s, svrID, strconv.Itoa(i))
  138. defer channelz.RemoveEntry(ids[i])
  139. }
  140. ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
  141. defer cancel()
  142. for i, s := range ss {
  143. resp, _ := svr.GetSocket(ctx, &channelzpb.GetSocketRequest{SocketId: ids[i].Int()})
  144. got, want := resp.GetSocket().GetRef(), &channelzpb.SocketRef{SocketId: ids[i].Int(), Name: strconv.Itoa(i)}
  145. if !cmp.Equal(got, want, protocmp.Transform()) {
  146. t.Fatalf("resp.GetSocket() returned metrics.GetRef() = %#v, want %#v", got, want)
  147. }
  148. socket, err := socketProtoToStruct(resp.GetSocket())
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. if diff := cmp.Diff(s, socket, protocmp.Transform(), cmp.AllowUnexported(dummySocket{})); diff != "" {
  153. t.Fatalf("unexpected socket, diff (-want +got):\n%s", diff)
  154. }
  155. }
  156. }