guestnetwork.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package compute
  15. import (
  16. "reflect"
  17. "time"
  18. "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/jsonutils"
  20. billing_api "yunion.io/x/onecloud/pkg/apis/billing"
  21. "yunion.io/x/pkg/gotypes"
  22. "yunion.io/x/onecloud/pkg/apis"
  23. )
  24. type GuestnetworkDetails struct {
  25. GuestJointResourceDetails
  26. SGuestnetwork
  27. // IP子网名称
  28. Network string `json:"network"`
  29. // 所属Wire
  30. WireId string `json:"wire_id"`
  31. // EipAddr associate with this guestnetwork
  32. EipAddr string `json:"eip_addr"`
  33. NetworkAddresses []NetworkAddrConf `json:"network_addresses"`
  34. GuestIpMask int8 `json:"guest_ip_mask"`
  35. // 网关地址
  36. GuestGateway string `json:"guest_gateway"`
  37. GuestIp6Mask uint8 `json:"guest_ip6_mask"`
  38. // 网关地址
  39. GuestGateway6 string `json:"guest_gateway6"`
  40. }
  41. type GuestnetworkShortDesc struct {
  42. // IP地址
  43. IpAddr string `json:"ip_addr"`
  44. // 是否为外网网卡
  45. IsExit bool `json:"is_exit"`
  46. // 网卡类型
  47. // 可能值:exit | internal | unused
  48. NicType string `json:"nic_type"`
  49. // IPv6地址
  50. Ip6Addr string `json:"ip6_addr"`
  51. // Mac地址
  52. Mac string `json:"mac"`
  53. // Bonding的配对网卡MAC
  54. TeamWith string `json:"team_with"`
  55. // 所属Vpc
  56. VpcId string `json:"vpc_id"`
  57. // 所属主机
  58. GuestId string `json:"guest_id"`
  59. // 所属Network
  60. NetworkId string `json:"network_id"`
  61. // 附属IP
  62. SubIps string `json:"sub_ips"`
  63. // 端口映射
  64. PortMappings GuestPortMappings `json:"port_mappings"`
  65. // Bandwidth
  66. BwLimitMbps int `json:"bw_limit_mbps"`
  67. // 网卡名称
  68. Ifname string `json:"ifname"`
  69. // 是否为缺省路由网关
  70. IsDefault bool `json:"is_default"`
  71. // 线路类型
  72. BgpType string `json:"bgp_type"`
  73. // 计费模式
  74. BillingType billing_api.TBillingType `json:"billing_type"`
  75. // 计量模式
  76. ChargeType billing_api.TNetChargeType `json:"charge_type"`
  77. // 网卡序号
  78. Index int `json:"index"`
  79. }
  80. type GuestnetworkSecgroupShortDesc struct {
  81. NetworkIndex int `json:"network_index"`
  82. Secgroups []apis.StandaloneShortDesc `json:"secgroups"`
  83. Mac string `json:"mac"`
  84. }
  85. type GuestnetworkListInput struct {
  86. GuestJointsListInput
  87. NetworkFilterListInput
  88. MacAddr []string `json:"mac_addr"`
  89. IpAddr []string `json:"ip_addr"`
  90. Ip6Addr []string `json:"ip6_addr"`
  91. Driver []string `json:"driver"`
  92. Ifname []string `json:"ifname"`
  93. TeamWith []string `json:"team_with"`
  94. }
  95. type GuestnetworkUpdateInput struct {
  96. GuestJointBaseUpdateInput
  97. Driver string `json:"driver"`
  98. BwLimit *int `json:"bw_limit"`
  99. Index *int8 `json:"index"`
  100. IsDefault *bool `json:"is_default"`
  101. PortMappings GuestPortMappings `json:"port_mappings"`
  102. ChargeType string `json:"charge_type"`
  103. }
  104. type GuestnetworkBaseDesc struct {
  105. Net string `json:"net"`
  106. NetId string `json:"net_id"`
  107. Mac string `json:"mac"`
  108. Virtual bool `json:"virtual"`
  109. Ip string `json:"ip"`
  110. Gateway string `json:"gateway"`
  111. Dns string `json:"dns"`
  112. Domain string `json:"domain"`
  113. Ntp string `json:"ntp"`
  114. Routes jsonutils.JSONObject `json:"routes"`
  115. Ifname string `json:"ifname"`
  116. Masklen int8 `json:"masklen"`
  117. Vlan int `json:"vlan"`
  118. Bw int `json:"bw"`
  119. Mtu int16 `json:"mtu"`
  120. Index int `json:"index"`
  121. RxTrafficLimit int64 `json:"rx_traffic_limit"`
  122. TxTrafficLimit int64 `json:"tx_traffic_limit"`
  123. NicType compute.TNicType `json:"nic_type"`
  124. BillingType billing_api.TBillingType `json:"billing_type"`
  125. ChargeType billing_api.TNetChargeType `json:"charge_type"`
  126. Ip6 string `json:"ip6"`
  127. Gateway6 string `json:"gateway6"`
  128. Masklen6 uint8 `json:"masklen6"`
  129. // 是否为缺省路由网关
  130. IsDefault bool `json:"is_default"`
  131. Bridge string `json:"bridge"`
  132. WireId string `json:"wire_id"`
  133. Interface string `json:"interface"`
  134. Secgroups []*SecgroupJsonDesc `json:"secgroups"`
  135. SecurityRules string `json:"security_rules"`
  136. Vpc struct {
  137. Id string `json:"id"`
  138. Provider string `json:"provider"`
  139. MappedIpAddr string `json:"mapped_ip_addr"`
  140. MappedIp6Addr string `json:"mapped_ip6_addr"`
  141. } `json:"vpc"`
  142. Networkaddresses jsonutils.JSONObject `json:"networkaddresses"`
  143. VirtualIps []string `json:"virtual_ips"`
  144. PortMappings GuestPortMappings `json:"port_mappings"`
  145. }
  146. type GuestnetworkJsonDesc struct {
  147. GuestnetworkBaseDesc
  148. Driver string `json:"driver"`
  149. NumQueues int `json:"num_queues"`
  150. Vectors *int `json:"vectors"`
  151. ExternalId string `json:"external_id"`
  152. TeamWith string `json:"team_with"`
  153. Manual *bool `json:"manual"`
  154. UpscriptPath string `json:"upscript_path"`
  155. DownscriptPath string `json:"downscript_path"`
  156. // baremetal
  157. Rate int `json:"rate"`
  158. BaremetalId string `json:"baremetal_id"`
  159. LinkUp bool `json:"link_up"`
  160. }
  161. type GuestnetworkSecgroupDesc struct {
  162. Secgroups []*SecgroupJsonDesc `json:"secgroups"`
  163. SecurityRules string `json:"security_rules"`
  164. Index int `json:"index"`
  165. Mac string `json:"mac"`
  166. }
  167. type SNicTrafficRecord struct {
  168. RxTraffic int64 `json:"rx_traffic"`
  169. TxTraffic int64 `json:"tx_traffic"`
  170. HasBeenSetDown bool `json:"has_been_set_down"`
  171. }
  172. type GuestNicTrafficSyncInput struct {
  173. // 同步时间
  174. SyncAt time.Time
  175. // 是否重置计数器
  176. IsReset bool `json:"is_reset"`
  177. // first key: guest_id
  178. // second key: nic_mac
  179. Traffic map[string]map[string]*SNicTrafficRecord
  180. }
  181. func NewGuestNicTrafficSyncInput(syncAt time.Time, isReset bool) *GuestNicTrafficSyncInput {
  182. return &GuestNicTrafficSyncInput{
  183. SyncAt: syncAt,
  184. Traffic: make(map[string]map[string]*SNicTrafficRecord),
  185. IsReset: isReset,
  186. }
  187. }
  188. type GuestPortMappingProtocol string
  189. const (
  190. GuestPortMappingProtocolTCP GuestPortMappingProtocol = "tcp"
  191. GuestPortMappingProtocolUDP GuestPortMappingProtocol = "udp"
  192. )
  193. const (
  194. GUEST_PORT_MAPPING_RANGE_START = 20000
  195. GUEST_PORT_MAPPING_RANGE_END = 25000
  196. )
  197. type GuestPortMappingPortRange struct {
  198. Start int `json:"start"`
  199. End int `json:"end"`
  200. }
  201. type GuestPortMappingEnvValueFrom string
  202. const (
  203. GuestPortMappingEnvValueFromPort GuestPortMappingEnvValueFrom = "port"
  204. GuestPortMappingEnvValueFromHostPort GuestPortMappingEnvValueFrom = "host_port"
  205. )
  206. type GuestPortMappingEnv struct {
  207. Key string `json:"key"`
  208. ValueFrom GuestPortMappingEnvValueFrom `json:"value_from"`
  209. }
  210. type GuestPortMapping struct {
  211. Protocol GuestPortMappingProtocol `json:"protocol"`
  212. // 容器内部 Port 端口范围 1-65535,-1表示由宿主机自动分配和 HostPort 相同的端口
  213. Port int `json:"port"`
  214. HostPort *int `json:"host_port,omitempty"`
  215. HostIp string `json:"host_ip"`
  216. HostPortRange *GuestPortMappingPortRange `json:"host_port_range,omitempty"`
  217. // whitelist for remote ips
  218. RemoteIps []string `json:"remote_ips"`
  219. Rule *GuestPortMappingRule `json:"rule,omitempty"`
  220. Envs []GuestPortMappingEnv `json:"envs,omitempty"`
  221. }
  222. type GuestPortMappingRule struct {
  223. FirstPortOffset *int `json:"first_port_offset"`
  224. }
  225. type GuestPortMappings []*GuestPortMapping
  226. func (g GuestPortMappings) String() string {
  227. return jsonutils.Marshal(g).String()
  228. }
  229. func (g GuestPortMappings) IsZero() bool {
  230. return len(g) == 0
  231. }
  232. func init() {
  233. gotypes.RegisterSerializable(reflect.TypeOf(&GuestPortMappings{}), func() gotypes.ISerializable {
  234. return &GuestPortMappings{}
  235. })
  236. }