You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1218 lines
36 KiB

  1. /*
  2. *
  3. * Copyright 2014 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 transport
  19. import (
  20. "bytes"
  21. "context"
  22. "errors"
  23. "fmt"
  24. "io"
  25. "math"
  26. "net"
  27. "strconv"
  28. "sync"
  29. "sync/atomic"
  30. "time"
  31. "github.com/golang/protobuf/proto"
  32. "golang.org/x/net/http2"
  33. "golang.org/x/net/http2/hpack"
  34. "google.golang.org/grpc/codes"
  35. "google.golang.org/grpc/credentials"
  36. "google.golang.org/grpc/grpclog"
  37. "google.golang.org/grpc/internal/channelz"
  38. "google.golang.org/grpc/internal/grpcrand"
  39. "google.golang.org/grpc/keepalive"
  40. "google.golang.org/grpc/metadata"
  41. "google.golang.org/grpc/peer"
  42. "google.golang.org/grpc/stats"
  43. "google.golang.org/grpc/status"
  44. "google.golang.org/grpc/tap"
  45. )
  46. var (
  47. // ErrIllegalHeaderWrite indicates that setting header is illegal because of
  48. // the stream's state.
  49. ErrIllegalHeaderWrite = errors.New("transport: the stream is done or WriteHeader was already called")
  50. // ErrHeaderListSizeLimitViolation indicates that the header list size is larger
  51. // than the limit set by peer.
  52. ErrHeaderListSizeLimitViolation = errors.New("transport: trying to send header list size larger than the limit set by peer")
  53. )
  54. // http2Server implements the ServerTransport interface with HTTP2.
  55. type http2Server struct {
  56. ctx context.Context
  57. ctxDone <-chan struct{} // Cache the context.Done() chan
  58. cancel context.CancelFunc
  59. conn net.Conn
  60. loopy *loopyWriter
  61. readerDone chan struct{} // sync point to enable testing.
  62. writerDone chan struct{} // sync point to enable testing.
  63. remoteAddr net.Addr
  64. localAddr net.Addr
  65. maxStreamID uint32 // max stream ID ever seen
  66. authInfo credentials.AuthInfo // auth info about the connection
  67. inTapHandle tap.ServerInHandle
  68. framer *framer
  69. // The max number of concurrent streams.
  70. maxStreams uint32
  71. // controlBuf delivers all the control related tasks (e.g., window
  72. // updates, reset streams, and various settings) to the controller.
  73. controlBuf *controlBuffer
  74. fc *trInFlow
  75. stats stats.Handler
  76. // Flag to keep track of reading activity on transport.
  77. // 1 is true and 0 is false.
  78. activity uint32 // Accessed atomically.
  79. // Keepalive and max-age parameters for the server.
  80. kp keepalive.ServerParameters
  81. // Keepalive enforcement policy.
  82. kep keepalive.EnforcementPolicy
  83. // The time instance last ping was received.
  84. lastPingAt time.Time
  85. // Number of times the client has violated keepalive ping policy so far.
  86. pingStrikes uint8
  87. // Flag to signify that number of ping strikes should be reset to 0.
  88. // This is set whenever data or header frames are sent.
  89. // 1 means yes.
  90. resetPingStrikes uint32 // Accessed atomically.
  91. initialWindowSize int32
  92. bdpEst *bdpEstimator
  93. maxSendHeaderListSize *uint32
  94. mu sync.Mutex // guard the following
  95. // drainChan is initialized when drain(...) is called the first time.
  96. // After which the server writes out the first GoAway(with ID 2^31-1) frame.
  97. // Then an independent goroutine will be launched to later send the second GoAway.
  98. // During this time we don't want to write another first GoAway(with ID 2^31 -1) frame.
  99. // Thus call to drain(...) will be a no-op if drainChan is already initialized since draining is
  100. // already underway.
  101. drainChan chan struct{}
  102. state transportState
  103. activeStreams map[uint32]*Stream
  104. // idle is the time instant when the connection went idle.
  105. // This is either the beginning of the connection or when the number of
  106. // RPCs go down to 0.
  107. // When the connection is busy, this value is set to 0.
  108. idle time.Time
  109. // Fields below are for channelz metric collection.
  110. channelzID int64 // channelz unique identification number
  111. czData *channelzData
  112. }
  113. // newHTTP2Server constructs a ServerTransport based on HTTP2. ConnectionError is
  114. // returned if something goes wrong.
  115. func newHTTP2Server(conn net.Conn, config *ServerConfig) (_ ServerTransport, err error) {
  116. writeBufSize := config.WriteBufferSize
  117. readBufSize := config.ReadBufferSize
  118. maxHeaderListSize := defaultServerMaxHeaderListSize
  119. if config.MaxHeaderListSize != nil {
  120. maxHeaderListSize = *config.MaxHeaderListSize
  121. }
  122. framer := newFramer(conn, writeBufSize, readBufSize, maxHeaderListSize)
  123. // Send initial settings as connection preface to client.
  124. var isettings []http2.Setting
  125. // TODO(zhaoq): Have a better way to signal "no limit" because 0 is
  126. // permitted in the HTTP2 spec.
  127. maxStreams := config.MaxStreams
  128. if maxStreams == 0 {
  129. maxStreams = math.MaxUint32
  130. } else {
  131. isettings = append(isettings, http2.Setting{
  132. ID: http2.SettingMaxConcurrentStreams,
  133. Val: maxStreams,
  134. })
  135. }
  136. dynamicWindow := true
  137. iwz := int32(initialWindowSize)
  138. if config.InitialWindowSize >= defaultWindowSize {
  139. iwz = config.InitialWindowSize
  140. dynamicWindow = false
  141. }
  142. icwz := int32(initialWindowSize)
  143. if config.InitialConnWindowSize >= defaultWindowSize {
  144. icwz = config.InitialConnWindowSize
  145. dynamicWindow = false
  146. }
  147. if iwz != defaultWindowSize {
  148. isettings = append(isettings, http2.Setting{
  149. ID: http2.SettingInitialWindowSize,
  150. Val: uint32(iwz)})
  151. }
  152. if config.MaxHeaderListSize != nil {
  153. isettings = append(isettings, http2.Setting{
  154. ID: http2.SettingMaxHeaderListSize,
  155. Val: *config.MaxHeaderListSize,
  156. })
  157. }
  158. if err := framer.fr.WriteSettings(isettings...); err != nil {
  159. return nil, connectionErrorf(false, err, "transport: %v", err)
  160. }
  161. // Adjust the connection flow control window if needed.
  162. if delta := uint32(icwz - defaultWindowSize); delta > 0 {
  163. if err := framer.fr.WriteWindowUpdate(0, delta); err != nil {
  164. return nil, connectionErrorf(false, err, "transport: %v", err)
  165. }
  166. }
  167. kp := config.KeepaliveParams
  168. if kp.MaxConnectionIdle == 0 {
  169. kp.MaxConnectionIdle = defaultMaxConnectionIdle
  170. }
  171. if kp.MaxConnectionAge == 0 {
  172. kp.MaxConnectionAge = defaultMaxConnectionAge
  173. }
  174. // Add a jitter to MaxConnectionAge.
  175. kp.MaxConnectionAge += getJitter(kp.MaxConnectionAge)
  176. if kp.MaxConnectionAgeGrace == 0 {
  177. kp.MaxConnectionAgeGrace = defaultMaxConnectionAgeGrace
  178. }
  179. if kp.Time == 0 {
  180. kp.Time = defaultServerKeepaliveTime
  181. }
  182. if kp.Timeout == 0 {
  183. kp.Timeout = defaultServerKeepaliveTimeout
  184. }
  185. kep := config.KeepalivePolicy
  186. if kep.MinTime == 0 {
  187. kep.MinTime = defaultKeepalivePolicyMinTime
  188. }
  189. ctx, cancel := context.WithCancel(context.Background())
  190. t := &http2Server{
  191. ctx: ctx,
  192. cancel: cancel,
  193. ctxDone: ctx.Done(),
  194. conn: conn,
  195. remoteAddr: conn.RemoteAddr(),
  196. localAddr: conn.LocalAddr(),
  197. authInfo: config.AuthInfo,
  198. framer: framer,
  199. readerDone: make(chan struct{}),
  200. writerDone: make(chan struct{}),
  201. maxStreams: maxStreams,
  202. inTapHandle: config.InTapHandle,
  203. fc: &trInFlow{limit: uint32(icwz)},
  204. state: reachable,
  205. activeStreams: make(map[uint32]*Stream),
  206. stats: config.StatsHandler,
  207. kp: kp,
  208. idle: time.Now(),
  209. kep: kep,
  210. initialWindowSize: iwz,
  211. czData: new(channelzData),
  212. }
  213. t.controlBuf = newControlBuffer(t.ctxDone)
  214. if dynamicWindow {
  215. t.bdpEst = &bdpEstimator{
  216. bdp: initialWindowSize,
  217. updateFlowControl: t.updateFlowControl,
  218. }
  219. }
  220. if t.stats != nil {
  221. t.ctx = t.stats.TagConn(t.ctx, &stats.ConnTagInfo{
  222. RemoteAddr: t.remoteAddr,
  223. LocalAddr: t.localAddr,
  224. })
  225. connBegin := &stats.ConnBegin{}
  226. t.stats.HandleConn(t.ctx, connBegin)
  227. }
  228. if channelz.IsOn() {
  229. t.channelzID = channelz.RegisterNormalSocket(t, config.ChannelzParentID, fmt.Sprintf("%s -> %s", t.remoteAddr, t.localAddr))
  230. }
  231. t.framer.writer.Flush()
  232. defer func() {
  233. if err != nil {
  234. t.Close()
  235. }
  236. }()
  237. // Check the validity of client preface.
  238. preface := make([]byte, len(clientPreface))
  239. if _, err := io.ReadFull(t.conn, preface); err != nil {
  240. return nil, connectionErrorf(false, err, "transport: http2Server.HandleStreams failed to receive the preface from client: %v", err)
  241. }
  242. if !bytes.Equal(preface, clientPreface) {
  243. return nil, connectionErrorf(false, nil, "transport: http2Server.HandleStreams received bogus greeting from client: %q", preface)
  244. }
  245. frame, err := t.framer.fr.ReadFrame()
  246. if err == io.EOF || err == io.ErrUnexpectedEOF {
  247. return nil, err
  248. }
  249. if err != nil {
  250. return nil, connectionErrorf(false, err, "transport: http2Server.HandleStreams failed to read initial settings frame: %v", err)
  251. }
  252. atomic.StoreUint32(&t.activity, 1)
  253. sf, ok := frame.(*http2.SettingsFrame)
  254. if !ok {
  255. return nil, connectionErrorf(false, nil, "transport: http2Server.HandleStreams saw invalid preface type %T from client", frame)
  256. }
  257. t.handleSettings(sf)
  258. go func() {
  259. t.loopy = newLoopyWriter(serverSide, t.framer, t.controlBuf, t.bdpEst)
  260. t.loopy.ssGoAwayHandler = t.outgoingGoAwayHandler
  261. if err := t.loopy.run(); err != nil {
  262. errorf("transport: loopyWriter.run returning. Err: %v", err)
  263. }
  264. t.conn.Close()
  265. close(t.writerDone)
  266. }()
  267. go t.keepalive()
  268. return t, nil
  269. }
  270. // operateHeader takes action on the decoded headers.
  271. func (t *http2Server) operateHeaders(frame *http2.MetaHeadersFrame, handle func(*Stream), traceCtx func(context.Context, string) context.Context) (fatal bool) {
  272. streamID := frame.Header().StreamID
  273. state := &decodeState{
  274. serverSide: true,
  275. ignoreContentType: false,
  276. }
  277. if err := state.decodeHeader(frame); err != nil {
  278. if se, ok := status.FromError(err); ok {
  279. t.controlBuf.put(&cleanupStream{
  280. streamID: streamID,
  281. rst: true,
  282. rstCode: statusCodeConvTab[se.Code()],
  283. onWrite: func() {},
  284. })
  285. }
  286. return false
  287. }
  288. buf := newRecvBuffer()
  289. s := &Stream{
  290. id: streamID,
  291. st: t,
  292. buf: buf,
  293. fc: &inFlow{limit: uint32(t.initialWindowSize)},
  294. recvCompress: state.data.encoding,
  295. method: state.data.method,
  296. contentSubtype: state.data.contentSubtype,
  297. }
  298. if frame.StreamEnded() {
  299. // s is just created by the caller. No lock needed.
  300. s.state = streamReadDone
  301. }
  302. if state.data.timeoutSet {
  303. s.ctx, s.cancel = context.WithTimeout(t.ctx, state.data.timeout)
  304. } else {
  305. s.ctx, s.cancel = context.WithCancel(t.ctx)
  306. }
  307. pr := &peer.Peer{
  308. Addr: t.remoteAddr,
  309. }
  310. // Attach Auth info if there is any.
  311. if t.authInfo != nil {
  312. pr.AuthInfo = t.authInfo
  313. }
  314. s.ctx = peer.NewContext(s.ctx, pr)
  315. // Attach the received metadata to the context.
  316. if len(state.data.mdata) > 0 {
  317. s.ctx = metadata.NewIncomingContext(s.ctx, state.data.mdata)
  318. }
  319. if state.data.statsTags != nil {
  320. s.ctx = stats.SetIncomingTags(s.ctx, state.data.statsTags)
  321. }
  322. if state.data.statsTrace != nil {
  323. s.ctx = stats.SetIncomingTrace(s.ctx, state.data.statsTrace)
  324. }
  325. if t.inTapHandle != nil {
  326. var err error
  327. info := &tap.Info{
  328. FullMethodName: state.data.method,
  329. }
  330. s.ctx, err = t.inTapHandle(s.ctx, info)
  331. if err != nil {
  332. warningf("transport: http2Server.operateHeaders got an error from InTapHandle: %v", err)
  333. t.controlBuf.put(&cleanupStream{
  334. streamID: s.id,
  335. rst: true,
  336. rstCode: http2.ErrCodeRefusedStream,
  337. onWrite: func() {},
  338. })
  339. return false
  340. }
  341. }
  342. t.mu.Lock()
  343. if t.state != reachable {
  344. t.mu.Unlock()
  345. return false
  346. }
  347. if uint32(len(t.activeStreams)) >= t.maxStreams {
  348. t.mu.Unlock()
  349. t.controlBuf.put(&cleanupStream{
  350. streamID: streamID,
  351. rst: true,
  352. rstCode: http2.ErrCodeRefusedStream,
  353. onWrite: func() {},
  354. })
  355. return false
  356. }
  357. if streamID%2 != 1 || streamID <= t.maxStreamID {
  358. t.mu.Unlock()
  359. // illegal gRPC stream id.
  360. errorf("transport: http2Server.HandleStreams received an illegal stream id: %v", streamID)
  361. return true
  362. }
  363. t.maxStreamID = streamID
  364. t.activeStreams[streamID] = s
  365. if len(t.activeStreams) == 1 {
  366. t.idle = time.Time{}
  367. }
  368. t.mu.Unlock()
  369. if channelz.IsOn() {
  370. atomic.AddInt64(&t.czData.streamsStarted, 1)
  371. atomic.StoreInt64(&t.czData.lastStreamCreatedTime, time.Now().UnixNano())
  372. }
  373. s.requestRead = func(n int) {
  374. t.adjustWindow(s, uint32(n))
  375. }
  376. s.ctx = traceCtx(s.ctx, s.method)
  377. if t.stats != nil {
  378. s.ctx = t.stats.TagRPC(s.ctx, &stats.RPCTagInfo{FullMethodName: s.method})
  379. inHeader := &stats.InHeader{
  380. FullMethod: s.method,
  381. RemoteAddr: t.remoteAddr,
  382. LocalAddr: t.localAddr,
  383. Compression: s.recvCompress,
  384. WireLength: int(frame.Header().Length),
  385. }
  386. t.stats.HandleRPC(s.ctx, inHeader)
  387. }
  388. s.ctxDone = s.ctx.Done()
  389. s.wq = newWriteQuota(defaultWriteQuota, s.ctxDone)
  390. s.trReader = &transportReader{
  391. reader: &recvBufferReader{
  392. ctx: s.ctx,
  393. ctxDone: s.ctxDone,
  394. recv: s.buf,
  395. },
  396. windowHandler: func(n int) {
  397. t.updateWindow(s, uint32(n))
  398. },
  399. }
  400. // Register the stream with loopy.
  401. t.controlBuf.put(&registerStream{
  402. streamID: s.id,
  403. wq: s.wq,
  404. })
  405. handle(s)
  406. return false
  407. }
  408. // HandleStreams receives incoming streams using the given handler. This is
  409. // typically run in a separate goroutine.
  410. // traceCtx attaches trace to ctx and returns the new context.
  411. func (t *http2Server) HandleStreams(handle func(*Stream), traceCtx func(context.Context, string) context.Context) {
  412. defer close(t.readerDone)
  413. for {
  414. frame, err := t.framer.fr.ReadFrame()
  415. atomic.StoreUint32(&t.activity, 1)
  416. if err != nil {
  417. if se, ok := err.(http2.StreamError); ok {
  418. warningf("transport: http2Server.HandleStreams encountered http2.StreamError: %v", se)
  419. t.mu.Lock()
  420. s := t.activeStreams[se.StreamID]
  421. t.mu.Unlock()
  422. if s != nil {
  423. t.closeStream(s, true, se.Code, nil, false)
  424. } else {
  425. t.controlBuf.put(&cleanupStream{
  426. streamID: se.StreamID,
  427. rst: true,
  428. rstCode: se.Code,
  429. onWrite: func() {},
  430. })
  431. }
  432. continue
  433. }
  434. if err == io.EOF || err == io.ErrUnexpectedEOF {
  435. t.Close()
  436. return
  437. }
  438. warningf("transport: http2Server.HandleStreams failed to read frame: %v", err)
  439. t.Close()
  440. return
  441. }
  442. switch frame := frame.(type) {
  443. case *http2.MetaHeadersFrame:
  444. if t.operateHeaders(frame, handle, traceCtx) {
  445. t.Close()
  446. break
  447. }
  448. case *http2.DataFrame:
  449. t.handleData(frame)
  450. case *http2.RSTStreamFrame:
  451. t.handleRSTStream(frame)
  452. case *http2.SettingsFrame:
  453. t.handleSettings(frame)
  454. case *http2.PingFrame:
  455. t.handlePing(frame)
  456. case *http2.WindowUpdateFrame:
  457. t.handleWindowUpdate(frame)
  458. case *http2.GoAwayFrame:
  459. // TODO: Handle GoAway from the client appropriately.
  460. default:
  461. errorf("transport: http2Server.HandleStreams found unhandled frame type %v.", frame)
  462. }
  463. }
  464. }
  465. func (t *http2Server) getStream(f http2.Frame) (*Stream, bool) {
  466. t.mu.Lock()
  467. defer t.mu.Unlock()
  468. if t.activeStreams == nil {
  469. // The transport is closing.
  470. return nil, false
  471. }
  472. s, ok := t.activeStreams[f.Header().StreamID]
  473. if !ok {
  474. // The stream is already done.
  475. return nil, false
  476. }
  477. return s, true
  478. }
  479. // adjustWindow sends out extra window update over the initial window size
  480. // of stream if the application is requesting data larger in size than
  481. // the window.
  482. func (t *http2Server) adjustWindow(s *Stream, n uint32) {
  483. if w := s.fc.maybeAdjust(n); w > 0 {
  484. t.controlBuf.put(&outgoingWindowUpdate{streamID: s.id, increment: w})
  485. }
  486. }
  487. // updateWindow adjusts the inbound quota for the stream and the transport.
  488. // Window updates will deliver to the controller for sending when
  489. // the cumulative quota exceeds the corresponding threshold.
  490. func (t *http2Server) updateWindow(s *Stream, n uint32) {
  491. if w := s.fc.onRead(n); w > 0 {
  492. t.controlBuf.put(&outgoingWindowUpdate{streamID: s.id,
  493. increment: w,
  494. })
  495. }
  496. }
  497. // updateFlowControl updates the incoming flow control windows
  498. // for the transport and the stream based on the current bdp
  499. // estimation.
  500. func (t *http2Server) updateFlowControl(n uint32) {
  501. t.mu.Lock()
  502. for _, s := range t.activeStreams {
  503. s.fc.newLimit(n)
  504. }
  505. t.initialWindowSize = int32(n)
  506. t.mu.Unlock()
  507. t.controlBuf.put(&outgoingWindowUpdate{
  508. streamID: 0,
  509. increment: t.fc.newLimit(n),
  510. })
  511. t.controlBuf.put(&outgoingSettings{
  512. ss: []http2.Setting{
  513. {
  514. ID: http2.SettingInitialWindowSize,
  515. Val: n,
  516. },
  517. },
  518. })
  519. }
  520. func (t *http2Server) handleData(f *http2.DataFrame) {
  521. size := f.Header().Length
  522. var sendBDPPing bool
  523. if t.bdpEst != nil {
  524. sendBDPPing = t.bdpEst.add(size)
  525. }
  526. // Decouple connection's flow control from application's read.
  527. // An update on connection's flow control should not depend on
  528. // whether user application has read the data or not. Such a
  529. // restriction is already imposed on the stream's flow control,
  530. // and therefore the sender will be blocked anyways.
  531. // Decoupling the connection flow control will prevent other
  532. // active(fast) streams from starving in presence of slow or
  533. // inactive streams.
  534. if w := t.fc.onData(size); w > 0 {
  535. t.controlBuf.put(&outgoingWindowUpdate{
  536. streamID: 0,
  537. increment: w,
  538. })
  539. }
  540. if sendBDPPing {
  541. // Avoid excessive ping detection (e.g. in an L7 proxy)
  542. // by sending a window update prior to the BDP ping.
  543. if w := t.fc.reset(); w > 0 {
  544. t.controlBuf.put(&outgoingWindowUpdate{
  545. streamID: 0,
  546. increment: w,
  547. })
  548. }
  549. t.controlBuf.put(bdpPing)
  550. }
  551. // Select the right stream to dispatch.
  552. s, ok := t.getStream(f)
  553. if !ok {
  554. return
  555. }
  556. if size > 0 {
  557. if err := s.fc.onData(size); err != nil {
  558. t.closeStream(s, true, http2.ErrCodeFlowControl, nil, false)
  559. return
  560. }
  561. if f.Header().Flags.Has(http2.FlagDataPadded) {
  562. if w := s.fc.onRead(size - uint32(len(f.Data()))); w > 0 {
  563. t.controlBuf.put(&outgoingWindowUpdate{s.id, w})
  564. }
  565. }
  566. // TODO(bradfitz, zhaoq): A copy is required here because there is no
  567. // guarantee f.Data() is consumed before the arrival of next frame.
  568. // Can this copy be eliminated?
  569. if len(f.Data()) > 0 {
  570. data := make([]byte, len(f.Data()))
  571. copy(data, f.Data())
  572. s.write(recvMsg{data: data})
  573. }
  574. }
  575. if f.Header().Flags.Has(http2.FlagDataEndStream) {
  576. // Received the end of stream from the client.
  577. s.compareAndSwapState(streamActive, streamReadDone)
  578. s.write(recvMsg{err: io.EOF})
  579. }
  580. }
  581. func (t *http2Server) handleRSTStream(f *http2.RSTStreamFrame) {
  582. s, ok := t.getStream(f)
  583. if !ok {
  584. return
  585. }
  586. t.closeStream(s, false, 0, nil, false)
  587. }
  588. func (t *http2Server) handleSettings(f *http2.SettingsFrame) {
  589. if f.IsAck() {
  590. return
  591. }
  592. var ss []http2.Setting
  593. var updateFuncs []func()
  594. f.ForeachSetting(func(s http2.Setting) error {
  595. switch s.ID {
  596. case http2.SettingMaxHeaderListSize:
  597. updateFuncs = append(updateFuncs, func() {
  598. t.maxSendHeaderListSize = new(uint32)
  599. *t.maxSendHeaderListSize = s.Val
  600. })
  601. default:
  602. ss = append(ss, s)
  603. }
  604. return nil
  605. })
  606. t.controlBuf.executeAndPut(func(interface{}) bool {
  607. for _, f := range updateFuncs {
  608. f()
  609. }
  610. return true
  611. }, &incomingSettings{
  612. ss: ss,
  613. })
  614. }
  615. const (
  616. maxPingStrikes = 2
  617. defaultPingTimeout = 2 * time.Hour
  618. )
  619. func (t *http2Server) handlePing(f *http2.PingFrame) {
  620. if f.IsAck() {
  621. if f.Data == goAwayPing.data && t.drainChan != nil {
  622. close(t.drainChan)
  623. return
  624. }
  625. // Maybe it's a BDP ping.
  626. if t.bdpEst != nil {
  627. t.bdpEst.calculate(f.Data)
  628. }
  629. return
  630. }
  631. pingAck := &ping{ack: true}
  632. copy(pingAck.data[:], f.Data[:])
  633. t.controlBuf.put(pingAck)
  634. now := time.Now()
  635. defer func() {
  636. t.lastPingAt = now
  637. }()
  638. // A reset ping strikes means that we don't need to check for policy
  639. // violation for this ping and the pingStrikes counter should be set
  640. // to 0.
  641. if atomic.CompareAndSwapUint32(&t.resetPingStrikes, 1, 0) {
  642. t.pingStrikes = 0
  643. return
  644. }
  645. t.mu.Lock()
  646. ns := len(t.activeStreams)
  647. t.mu.Unlock()
  648. if ns < 1 && !t.kep.PermitWithoutStream {
  649. // Keepalive shouldn't be active thus, this new ping should
  650. // have come after at least defaultPingTimeout.
  651. if t.lastPingAt.Add(defaultPingTimeout).After(now) {
  652. t.pingStrikes++
  653. }
  654. } else {
  655. // Check if keepalive policy is respected.
  656. if t.lastPingAt.Add(t.kep.MinTime).After(now) {
  657. t.pingStrikes++
  658. }
  659. }
  660. if t.pingStrikes > maxPingStrikes {
  661. // Send goaway and close the connection.
  662. errorf("transport: Got too many pings from the client, closing the connection.")
  663. t.controlBuf.put(&goAway{code: http2.ErrCodeEnhanceYourCalm, debugData: []byte("too_many_pings"), closeConn: true})
  664. }
  665. }
  666. func (t *http2Server) handleWindowUpdate(f *http2.WindowUpdateFrame) {
  667. t.controlBuf.put(&incomingWindowUpdate{
  668. streamID: f.Header().StreamID,
  669. increment: f.Increment,
  670. })
  671. }
  672. func appendHeaderFieldsFromMD(headerFields []hpack.HeaderField, md metadata.MD) []hpack.HeaderField {
  673. for k, vv := range md {
  674. if isReservedHeader(k) {
  675. // Clients don't tolerate reading restricted headers after some non restricted ones were sent.
  676. continue
  677. }
  678. for _, v := range vv {
  679. headerFields = append(headerFields, hpack.HeaderField{Name: k, Value: encodeMetadataHeader(k, v)})
  680. }
  681. }
  682. return headerFields
  683. }
  684. func (t *http2Server) checkForHeaderListSize(it interface{}) bool {
  685. if t.maxSendHeaderListSize == nil {
  686. return true
  687. }
  688. hdrFrame := it.(*headerFrame)
  689. var sz int64
  690. for _, f := range hdrFrame.hf {
  691. if sz += int64(f.Size()); sz > int64(*t.maxSendHeaderListSize) {
  692. errorf("header list size to send violates the maximum size (%d bytes) set by client", *t.maxSendHeaderListSize)
  693. return false
  694. }
  695. }
  696. return true
  697. }
  698. // WriteHeader sends the header metedata md back to the client.
  699. func (t *http2Server) WriteHeader(s *Stream, md metadata.MD) error {
  700. if s.updateHeaderSent() || s.getState() == streamDone {
  701. return ErrIllegalHeaderWrite
  702. }
  703. s.hdrMu.Lock()
  704. if md.Len() > 0 {
  705. if s.header.Len() > 0 {
  706. s.header = metadata.Join(s.header, md)
  707. } else {
  708. s.header = md
  709. }
  710. }
  711. if err := t.writeHeaderLocked(s); err != nil {
  712. s.hdrMu.Unlock()
  713. return err
  714. }
  715. s.hdrMu.Unlock()
  716. return nil
  717. }
  718. func (t *http2Server) writeHeaderLocked(s *Stream) error {
  719. // TODO(mmukhi): Benchmark if the performance gets better if count the metadata and other header fields
  720. // first and create a slice of that exact size.
  721. headerFields := make([]hpack.HeaderField, 0, 2) // at least :status, content-type will be there if none else.
  722. headerFields = append(headerFields, hpack.HeaderField{Name: ":status", Value: "200"})
  723. headerFields = append(headerFields, hpack.HeaderField{Name: "content-type", Value: contentType(s.contentSubtype)})
  724. if s.sendCompress != "" {
  725. headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-encoding", Value: s.sendCompress})
  726. }
  727. headerFields = appendHeaderFieldsFromMD(headerFields, s.header)
  728. success, err := t.controlBuf.executeAndPut(t.checkForHeaderListSize, &headerFrame{
  729. streamID: s.id,
  730. hf: headerFields,
  731. endStream: false,
  732. onWrite: func() {
  733. atomic.StoreUint32(&t.resetPingStrikes, 1)
  734. },
  735. })
  736. if !success {
  737. if err != nil {
  738. return err
  739. }
  740. t.closeStream(s, true, http2.ErrCodeInternal, nil, false)
  741. return ErrHeaderListSizeLimitViolation
  742. }
  743. if t.stats != nil {
  744. // Note: WireLength is not set in outHeader.
  745. // TODO(mmukhi): Revisit this later, if needed.
  746. outHeader := &stats.OutHeader{}
  747. t.stats.HandleRPC(s.Context(), outHeader)
  748. }
  749. return nil
  750. }
  751. // WriteStatus sends stream status to the client and terminates the stream.
  752. // There is no further I/O operations being able to perform on this stream.
  753. // TODO(zhaoq): Now it indicates the end of entire stream. Revisit if early
  754. // OK is adopted.
  755. func (t *http2Server) WriteStatus(s *Stream, st *status.Status) error {
  756. if s.getState() == streamDone {
  757. return nil
  758. }
  759. s.hdrMu.Lock()
  760. // TODO(mmukhi): Benchmark if the performance gets better if count the metadata and other header fields
  761. // first and create a slice of that exact size.
  762. headerFields := make([]hpack.HeaderField, 0, 2) // grpc-status and grpc-message will be there if none else.
  763. if !s.updateHeaderSent() { // No headers have been sent.
  764. if len(s.header) > 0 { // Send a separate header frame.
  765. if err := t.writeHeaderLocked(s); err != nil {
  766. s.hdrMu.Unlock()
  767. return err
  768. }
  769. } else { // Send a trailer only response.
  770. headerFields = append(headerFields, hpack.HeaderField{Name: ":status", Value: "200"})
  771. headerFields = append(headerFields, hpack.HeaderField{Name: "content-type", Value: contentType(s.contentSubtype)})
  772. }
  773. }
  774. headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-status", Value: strconv.Itoa(int(st.Code()))})
  775. headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-message", Value: encodeGrpcMessage(st.Message())})
  776. if p := st.Proto(); p != nil && len(p.Details) > 0 {
  777. stBytes, err := proto.Marshal(p)
  778. if err != nil {
  779. // TODO: return error instead, when callers are able to handle it.
  780. grpclog.Errorf("transport: failed to marshal rpc status: %v, error: %v", p, err)
  781. } else {
  782. headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-status-details-bin", Value: encodeBinHeader(stBytes)})
  783. }
  784. }
  785. // Attach the trailer metadata.
  786. headerFields = appendHeaderFieldsFromMD(headerFields, s.trailer)
  787. trailingHeader := &headerFrame{
  788. streamID: s.id,
  789. hf: headerFields,
  790. endStream: true,
  791. onWrite: func() {
  792. atomic.StoreUint32(&t.resetPingStrikes, 1)
  793. },
  794. }
  795. s.hdrMu.Unlock()
  796. success, err := t.controlBuf.execute(t.checkForHeaderListSize, trailingHeader)
  797. if !success {
  798. if err != nil {
  799. return err
  800. }
  801. t.closeStream(s, true, http2.ErrCodeInternal, nil, false)
  802. return ErrHeaderListSizeLimitViolation
  803. }
  804. // Send a RST_STREAM after the trailers if the client has not already half-closed.
  805. rst := s.getState() == streamActive
  806. t.closeStream(s, rst, http2.ErrCodeNo, trailingHeader, true)
  807. if t.stats != nil {
  808. t.stats.HandleRPC(s.Context(), &stats.OutTrailer{})
  809. }
  810. return nil
  811. }
  812. // Write converts the data into HTTP2 data frame and sends it out. Non-nil error
  813. // is returns if it fails (e.g., framing error, transport error).
  814. func (t *http2Server) Write(s *Stream, hdr []byte, data []byte, opts *Options) error {
  815. if !s.isHeaderSent() { // Headers haven't been written yet.
  816. if err := t.WriteHeader(s, nil); err != nil {
  817. if _, ok := err.(ConnectionError); ok {
  818. return err
  819. }
  820. // TODO(mmukhi, dfawley): Make sure this is the right code to return.
  821. return status.Errorf(codes.Internal, "transport: %v", err)
  822. }
  823. } else {
  824. // Writing headers checks for this condition.
  825. if s.getState() == streamDone {
  826. // TODO(mmukhi, dfawley): Should the server write also return io.EOF?
  827. s.cancel()
  828. select {
  829. case <-t.ctx.Done():
  830. return ErrConnClosing
  831. default:
  832. }
  833. return ContextErr(s.ctx.Err())
  834. }
  835. }
  836. // Add some data to header frame so that we can equally distribute bytes across frames.
  837. emptyLen := http2MaxFrameLen - len(hdr)
  838. if emptyLen > len(data) {
  839. emptyLen = len(data)
  840. }
  841. hdr = append(hdr, data[:emptyLen]...)
  842. data = data[emptyLen:]
  843. df := &dataFrame{
  844. streamID: s.id,
  845. h: hdr,
  846. d: data,
  847. onEachWrite: func() {
  848. atomic.StoreUint32(&t.resetPingStrikes, 1)
  849. },
  850. }
  851. if err := s.wq.get(int32(len(hdr) + len(data))); err != nil {
  852. select {
  853. case <-t.ctx.Done():
  854. return ErrConnClosing
  855. default:
  856. }
  857. return ContextErr(s.ctx.Err())
  858. }
  859. return t.controlBuf.put(df)
  860. }
  861. // keepalive running in a separate goroutine does the following:
  862. // 1. Gracefully closes an idle connection after a duration of keepalive.MaxConnectionIdle.
  863. // 2. Gracefully closes any connection after a duration of keepalive.MaxConnectionAge.
  864. // 3. Forcibly closes a connection after an additive period of keepalive.MaxConnectionAgeGrace over keepalive.MaxConnectionAge.
  865. // 4. Makes sure a connection is alive by sending pings with a frequency of keepalive.Time and closes a non-responsive connection
  866. // after an additional duration of keepalive.Timeout.
  867. func (t *http2Server) keepalive() {
  868. p := &ping{}
  869. var pingSent bool
  870. maxIdle := time.NewTimer(t.kp.MaxConnectionIdle)
  871. maxAge := time.NewTimer(t.kp.MaxConnectionAge)
  872. keepalive := time.NewTimer(t.kp.Time)
  873. // NOTE: All exit paths of this function should reset their
  874. // respective timers. A failure to do so will cause the
  875. // following clean-up to deadlock and eventually leak.
  876. defer func() {
  877. if !maxIdle.Stop() {
  878. <-maxIdle.C
  879. }
  880. if !maxAge.Stop() {
  881. <-maxAge.C
  882. }
  883. if !keepalive.Stop() {
  884. <-keepalive.C
  885. }
  886. }()
  887. for {
  888. select {
  889. case <-maxIdle.C:
  890. t.mu.Lock()
  891. idle := t.idle
  892. if idle.IsZero() { // The connection is non-idle.
  893. t.mu.Unlock()
  894. maxIdle.Reset(t.kp.MaxConnectionIdle)
  895. continue
  896. }
  897. val := t.kp.MaxConnectionIdle - time.Since(idle)
  898. t.mu.Unlock()
  899. if val <= 0 {
  900. // The connection has been idle for a duration of keepalive.MaxConnectionIdle or more.
  901. // Gracefully close the connection.
  902. t.drain(http2.ErrCodeNo, []byte{})
  903. // Resetting the timer so that the clean-up doesn't deadlock.
  904. maxIdle.Reset(infinity)
  905. return
  906. }
  907. maxIdle.Reset(val)
  908. case <-maxAge.C:
  909. t.drain(http2.ErrCodeNo, []byte{})
  910. maxAge.Reset(t.kp.MaxConnectionAgeGrace)
  911. select {
  912. case <-maxAge.C:
  913. // Close the connection after grace period.
  914. t.Close()
  915. // Resetting the timer so that the clean-up doesn't deadlock.
  916. maxAge.Reset(infinity)
  917. case <-t.ctx.Done():
  918. }
  919. return
  920. case <-keepalive.C:
  921. if atomic.CompareAndSwapUint32(&t.activity, 1, 0) {
  922. pingSent = false
  923. keepalive.Reset(t.kp.Time)
  924. continue
  925. }
  926. if pingSent {
  927. t.Close()
  928. // Resetting the timer so that the clean-up doesn't deadlock.
  929. keepalive.Reset(infinity)
  930. return
  931. }
  932. pingSent = true
  933. if channelz.IsOn() {
  934. atomic.AddInt64(&t.czData.kpCount, 1)
  935. }
  936. t.controlBuf.put(p)
  937. keepalive.Reset(t.kp.Timeout)
  938. case <-t.ctx.Done():
  939. return
  940. }
  941. }
  942. }
  943. // Close starts shutting down the http2Server transport.
  944. // TODO(zhaoq): Now the destruction is not blocked on any pending streams. This
  945. // could cause some resource issue. Revisit this later.
  946. func (t *http2Server) Close() error {
  947. t.mu.Lock()
  948. if t.state == closing {
  949. t.mu.Unlock()
  950. return errors.New("transport: Close() was already called")
  951. }
  952. t.state = closing
  953. streams := t.activeStreams
  954. t.activeStreams = nil
  955. t.mu.Unlock()
  956. t.controlBuf.finish()
  957. t.cancel()
  958. err := t.conn.Close()
  959. if channelz.IsOn() {
  960. channelz.RemoveEntry(t.channelzID)
  961. }
  962. // Cancel all active streams.
  963. for _, s := range streams {
  964. s.cancel()
  965. }
  966. if t.stats != nil {
  967. connEnd := &stats.ConnEnd{}
  968. t.stats.HandleConn(t.ctx, connEnd)
  969. }
  970. return err
  971. }
  972. // deleteStream deletes the stream s from transport's active streams.
  973. func (t *http2Server) deleteStream(s *Stream, eosReceived bool) {
  974. t.mu.Lock()
  975. if _, ok := t.activeStreams[s.id]; !ok {
  976. t.mu.Unlock()
  977. return
  978. }
  979. delete(t.activeStreams, s.id)
  980. if len(t.activeStreams) == 0 {
  981. t.idle = time.Now()
  982. }
  983. t.mu.Unlock()
  984. if channelz.IsOn() {
  985. if eosReceived {
  986. atomic.AddInt64(&t.czData.streamsSucceeded, 1)
  987. } else {
  988. atomic.AddInt64(&t.czData.streamsFailed, 1)
  989. }
  990. }
  991. }
  992. // closeStream clears the footprint of a stream when the stream is not needed
  993. // any more.
  994. func (t *http2Server) closeStream(s *Stream, rst bool, rstCode http2.ErrCode, hdr *headerFrame, eosReceived bool) {
  995. // Mark the stream as done
  996. oldState := s.swapState(streamDone)
  997. // In case stream sending and receiving are invoked in separate
  998. // goroutines (e.g., bi-directional streaming), cancel needs to be
  999. // called to interrupt the potential blocking on other goroutines.
  1000. s.cancel()
  1001. // Deletes the stream from active streams
  1002. t.deleteStream(s, eosReceived)
  1003. cleanup := &cleanupStream{
  1004. streamID: s.id,
  1005. rst: rst,
  1006. rstCode: rstCode,
  1007. onWrite: func() {},
  1008. }
  1009. // No trailer. Puts cleanupFrame into transport's control buffer.
  1010. if hdr == nil {
  1011. t.controlBuf.put(cleanup)
  1012. return
  1013. }
  1014. // We do the check here, because of the following scenario:
  1015. // 1. closeStream is called first with a trailer. A trailer item with a piggybacked cleanup item
  1016. // is put to control buffer.
  1017. // 2. Loopy writer is waiting on a stream quota. It will never get it because client errored at
  1018. // some point. So loopy can't act on trailer
  1019. // 3. Client sends a RST_STREAM due to the error. Then closeStream is called without a trailer as
  1020. // the result of the received RST_STREAM.
  1021. // If we do this check at the beginning of the closeStream, then we won't put a cleanup item in
  1022. // response to received RST_STREAM into the control buffer and outStream in loopy writer will
  1023. // never get cleaned up.
  1024. // If the stream is already done, don't send the trailer.
  1025. if oldState == streamDone {
  1026. return
  1027. }
  1028. hdr.cleanup = cleanup
  1029. t.controlBuf.put(hdr)
  1030. }
  1031. func (t *http2Server) RemoteAddr() net.Addr {
  1032. return t.remoteAddr
  1033. }
  1034. func (t *http2Server) Drain() {
  1035. t.drain(http2.ErrCodeNo, []byte{})
  1036. }
  1037. func (t *http2Server) drain(code http2.ErrCode, debugData []byte) {
  1038. t.mu.Lock()
  1039. defer t.mu.Unlock()
  1040. if t.drainChan != nil {
  1041. return
  1042. }
  1043. t.drainChan = make(chan struct{})
  1044. t.controlBuf.put(&goAway{code: code, debugData: debugData, headsUp: true})
  1045. }
  1046. var goAwayPing = &ping{data: [8]byte{1, 6, 1, 8, 0, 3, 3, 9}}
  1047. // Handles outgoing GoAway and returns true if loopy needs to put itself
  1048. // in draining mode.
  1049. func (t *http2Server) outgoingGoAwayHandler(g *goAway) (bool, error) {
  1050. t.mu.Lock()
  1051. if t.state == closing { // TODO(mmukhi): This seems unnecessary.
  1052. t.mu.Unlock()
  1053. // The transport is closing.
  1054. return false, ErrConnClosing
  1055. }
  1056. sid := t.maxStreamID
  1057. if !g.headsUp {
  1058. // Stop accepting more streams now.
  1059. t.state = draining
  1060. if len(t.activeStreams) == 0 {
  1061. g.closeConn = true
  1062. }
  1063. t.mu.Unlock()
  1064. if err := t.framer.fr.WriteGoAway(sid, g.code, g.debugData); err != nil {
  1065. return false, err
  1066. }
  1067. if g.closeConn {
  1068. // Abruptly close the connection following the GoAway (via
  1069. // loopywriter). But flush out what's inside the buffer first.
  1070. t.framer.writer.Flush()
  1071. return false, fmt.Errorf("transport: Connection closing")
  1072. }
  1073. return true, nil
  1074. }
  1075. t.mu.Unlock()
  1076. // For a graceful close, send out a GoAway with stream ID of MaxUInt32,
  1077. // Follow that with a ping and wait for the ack to come back or a timer
  1078. // to expire. During this time accept new streams since they might have
  1079. // originated before the GoAway reaches the client.
  1080. // After getting the ack or timer expiration send out another GoAway this
  1081. // time with an ID of the max stream server intends to process.
  1082. if err := t.framer.fr.WriteGoAway(math.MaxUint32, http2.ErrCodeNo, []byte{}); err != nil {
  1083. return false, err
  1084. }
  1085. if err := t.framer.fr.WritePing(false, goAwayPing.data); err != nil {
  1086. return false, err
  1087. }
  1088. go func() {
  1089. timer := time.NewTimer(time.Minute)
  1090. defer timer.Stop()
  1091. select {
  1092. case <-t.drainChan:
  1093. case <-timer.C:
  1094. case <-t.ctx.Done():
  1095. return
  1096. }
  1097. t.controlBuf.put(&goAway{code: g.code, debugData: g.debugData})
  1098. }()
  1099. return false, nil
  1100. }
  1101. func (t *http2Server) ChannelzMetric() *channelz.SocketInternalMetric {
  1102. s := channelz.SocketInternalMetric{
  1103. StreamsStarted: atomic.LoadInt64(&t.czData.streamsStarted),
  1104. StreamsSucceeded: atomic.LoadInt64(&t.czData.streamsSucceeded),
  1105. StreamsFailed: atomic.LoadInt64(&t.czData.streamsFailed),
  1106. MessagesSent: atomic.LoadInt64(&t.czData.msgSent),
  1107. MessagesReceived: atomic.LoadInt64(&t.czData.msgRecv),
  1108. KeepAlivesSent: atomic.LoadInt64(&t.czData.kpCount),
  1109. LastRemoteStreamCreatedTimestamp: time.Unix(0, atomic.LoadInt64(&t.czData.lastStreamCreatedTime)),
  1110. LastMessageSentTimestamp: time.Unix(0, atomic.LoadInt64(&t.czData.lastMsgSentTime)),
  1111. LastMessageReceivedTimestamp: time.Unix(0, atomic.LoadInt64(&t.czData.lastMsgRecvTime)),
  1112. LocalFlowControlWindow: int64(t.fc.getSize()),
  1113. SocketOptions: channelz.GetSocketOption(t.conn),
  1114. LocalAddr: t.localAddr,
  1115. RemoteAddr: t.remoteAddr,
  1116. // RemoteName :
  1117. }
  1118. if au, ok := t.authInfo.(credentials.ChannelzSecurityInfo); ok {
  1119. s.Security = au.GetSecurityValue()
  1120. }
  1121. s.RemoteFlowControlWindow = t.getOutFlowWindow()
  1122. return &s
  1123. }
  1124. func (t *http2Server) IncrMsgSent() {
  1125. atomic.AddInt64(&t.czData.msgSent, 1)
  1126. atomic.StoreInt64(&t.czData.lastMsgSentTime, time.Now().UnixNano())
  1127. }
  1128. func (t *http2Server) IncrMsgRecv() {
  1129. atomic.AddInt64(&t.czData.msgRecv, 1)
  1130. atomic.StoreInt64(&t.czData.lastMsgRecvTime, time.Now().UnixNano())
  1131. }
  1132. func (t *http2Server) getOutFlowWindow() int64 {
  1133. resp := make(chan uint32, 1)
  1134. timer := time.NewTimer(time.Second)
  1135. defer timer.Stop()
  1136. t.controlBuf.put(&outFlowControlSizeRequest{resp})
  1137. select {
  1138. case sz := <-resp:
  1139. return int64(sz)
  1140. case <-t.ctxDone:
  1141. return -1
  1142. case <-timer.C:
  1143. return -2
  1144. }
  1145. }
  1146. func getJitter(v time.Duration) time.Duration {
  1147. if v == infinity {
  1148. return 0
  1149. }
  1150. // Generate a jitter between +/- 10% of the value.
  1151. r := int64(v / 10)
  1152. j := grpcrand.Int63n(2*r) - r
  1153. return time.Duration(j)
  1154. }