messages.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Package ipc provides communication between Go sidecar and Rust RDMA engine
  2. package ipc
  3. import "time"
  4. // IpcMessage represents the tagged union of all IPC messages
  5. // This matches the Rust enum: #[serde(tag = "type", content = "data")]
  6. type IpcMessage struct {
  7. Type string `msgpack:"type"`
  8. Data interface{} `msgpack:"data"`
  9. }
  10. // Request message types
  11. const (
  12. MsgStartRead = "StartRead"
  13. MsgCompleteRead = "CompleteRead"
  14. MsgGetCapabilities = "GetCapabilities"
  15. MsgPing = "Ping"
  16. )
  17. // Response message types
  18. const (
  19. MsgStartReadResponse = "StartReadResponse"
  20. MsgCompleteReadResponse = "CompleteReadResponse"
  21. MsgGetCapabilitiesResponse = "GetCapabilitiesResponse"
  22. MsgPong = "Pong"
  23. MsgError = "Error"
  24. )
  25. // StartReadRequest corresponds to Rust StartReadRequest
  26. type StartReadRequest struct {
  27. VolumeID uint32 `msgpack:"volume_id"`
  28. NeedleID uint64 `msgpack:"needle_id"`
  29. Cookie uint32 `msgpack:"cookie"`
  30. Offset uint64 `msgpack:"offset"`
  31. Size uint64 `msgpack:"size"`
  32. RemoteAddr uint64 `msgpack:"remote_addr"`
  33. RemoteKey uint32 `msgpack:"remote_key"`
  34. TimeoutSecs uint64 `msgpack:"timeout_secs"`
  35. AuthToken *string `msgpack:"auth_token,omitempty"`
  36. }
  37. // StartReadResponse corresponds to Rust StartReadResponse
  38. type StartReadResponse struct {
  39. SessionID string `msgpack:"session_id"`
  40. LocalAddr uint64 `msgpack:"local_addr"`
  41. LocalKey uint32 `msgpack:"local_key"`
  42. TransferSize uint64 `msgpack:"transfer_size"`
  43. ExpectedCrc uint32 `msgpack:"expected_crc"`
  44. ExpiresAtNs uint64 `msgpack:"expires_at_ns"`
  45. }
  46. // CompleteReadRequest corresponds to Rust CompleteReadRequest
  47. type CompleteReadRequest struct {
  48. SessionID string `msgpack:"session_id"`
  49. Success bool `msgpack:"success"`
  50. BytesTransferred uint64 `msgpack:"bytes_transferred"`
  51. ClientCrc *uint32 `msgpack:"client_crc,omitempty"`
  52. ErrorMessage *string `msgpack:"error_message,omitempty"`
  53. }
  54. // CompleteReadResponse corresponds to Rust CompleteReadResponse
  55. type CompleteReadResponse struct {
  56. Success bool `msgpack:"success"`
  57. ServerCrc *uint32 `msgpack:"server_crc,omitempty"`
  58. Message *string `msgpack:"message,omitempty"`
  59. }
  60. // GetCapabilitiesRequest corresponds to Rust GetCapabilitiesRequest
  61. type GetCapabilitiesRequest struct {
  62. ClientID *string `msgpack:"client_id,omitempty"`
  63. }
  64. // GetCapabilitiesResponse corresponds to Rust GetCapabilitiesResponse
  65. type GetCapabilitiesResponse struct {
  66. DeviceName string `msgpack:"device_name"`
  67. VendorId uint32 `msgpack:"vendor_id"`
  68. MaxTransferSize uint64 `msgpack:"max_transfer_size"`
  69. MaxSessions usize `msgpack:"max_sessions"`
  70. ActiveSessions usize `msgpack:"active_sessions"`
  71. PortGid string `msgpack:"port_gid"`
  72. PortLid uint16 `msgpack:"port_lid"`
  73. SupportedAuth []string `msgpack:"supported_auth"`
  74. Version string `msgpack:"version"`
  75. RealRdma bool `msgpack:"real_rdma"`
  76. }
  77. // usize corresponds to Rust's usize type (platform dependent, but typically uint64 on 64-bit systems)
  78. type usize uint64
  79. // PingRequest corresponds to Rust PingRequest
  80. type PingRequest struct {
  81. TimestampNs uint64 `msgpack:"timestamp_ns"`
  82. ClientID *string `msgpack:"client_id,omitempty"`
  83. }
  84. // PongResponse corresponds to Rust PongResponse
  85. type PongResponse struct {
  86. ClientTimestampNs uint64 `msgpack:"client_timestamp_ns"`
  87. ServerTimestampNs uint64 `msgpack:"server_timestamp_ns"`
  88. ServerRttNs uint64 `msgpack:"server_rtt_ns"`
  89. }
  90. // ErrorResponse corresponds to Rust ErrorResponse
  91. type ErrorResponse struct {
  92. Code string `msgpack:"code"`
  93. Message string `msgpack:"message"`
  94. Details *string `msgpack:"details,omitempty"`
  95. }
  96. // Helper functions for creating messages
  97. func NewStartReadMessage(req *StartReadRequest) *IpcMessage {
  98. return &IpcMessage{
  99. Type: MsgStartRead,
  100. Data: req,
  101. }
  102. }
  103. func NewCompleteReadMessage(sessionID string, success bool, bytesTransferred uint64, clientCrc *uint32, errorMessage *string) *IpcMessage {
  104. return &IpcMessage{
  105. Type: MsgCompleteRead,
  106. Data: &CompleteReadRequest{
  107. SessionID: sessionID,
  108. Success: success,
  109. BytesTransferred: bytesTransferred,
  110. ClientCrc: clientCrc,
  111. ErrorMessage: errorMessage,
  112. },
  113. }
  114. }
  115. func NewGetCapabilitiesMessage(clientID *string) *IpcMessage {
  116. return &IpcMessage{
  117. Type: MsgGetCapabilities,
  118. Data: &GetCapabilitiesRequest{
  119. ClientID: clientID,
  120. },
  121. }
  122. }
  123. func NewPingMessage(clientID *string) *IpcMessage {
  124. return &IpcMessage{
  125. Type: MsgPing,
  126. Data: &PingRequest{
  127. TimestampNs: uint64(time.Now().UnixNano()),
  128. ClientID: clientID,
  129. },
  130. }
  131. }
  132. func NewErrorMessage(code, message string, details *string) *IpcMessage {
  133. return &IpcMessage{
  134. Type: MsgError,
  135. Data: &ErrorResponse{
  136. Code: code,
  137. Message: message,
  138. Details: details,
  139. },
  140. }
  141. }