pvtcp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. * Linux 2.6.32 and later Kernel module for VMware MVP PVTCP Server
  3. *
  4. * Copyright (C) 2010-2013 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; see the file COPYING. If not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #line 5
  20. /**
  21. * @file
  22. *
  23. * @brief Pvtcp common code.
  24. */
  25. #include "pvtcp.h"
  26. /*
  27. * Operation table.
  28. */
  29. CommOperationFunc pvtcpOperations[] = {
  30. [PVTCP_OP_FLOW] = PvtcpFlowOp,
  31. [PVTCP_OP_IO] = PvtcpIoOp,
  32. [PVTCP_OP_CREATE] = PvtcpCreateOp,
  33. [PVTCP_OP_RELEASE] = PvtcpReleaseOp,
  34. [PVTCP_OP_BIND] = PvtcpBindOp,
  35. [PVTCP_OP_LISTEN] = PvtcpListenOp,
  36. [PVTCP_OP_ACCEPT] = PvtcpAcceptOp,
  37. [PVTCP_OP_CONNECT] = PvtcpConnectOp,
  38. [PVTCP_OP_SHUTDOWN] = PvtcpShutdownOp,
  39. [PVTCP_OP_SETSOCKOPT] = PvtcpSetSockOptOp,
  40. [PVTCP_OP_GETSOCKOPT] = PvtcpGetSockOptOp,
  41. [PVTCP_OP_IOCTL] = PvtcpIoctlOp,
  42. [PVTCP_OP_INVALID] = NULL
  43. };
  44. /*
  45. * Implementation block.
  46. */
  47. CommImpl pvtcpImpl = {
  48. .owner = NULL,
  49. .checkArgs = PvtcpCheckArgs,
  50. .stateCtor = PvtcpStateAlloc,
  51. .stateDtor = PvtcpStateFree,
  52. .dataAlloc = PvtcpBufAlloc,
  53. .dataFree = PvtcpBufFree,
  54. .operations = pvtcpOperations,
  55. .closeNtf = PvtcpCloseNtf,
  56. .closeNtfData = &pvtcpImpl,
  57. .ntfCenterID = {
  58. {
  59. .d32[0] = 2U, /* x86 host context (vmci, only). */
  60. .d32[1] = 10000 /* Default, not yet reserved, resource (vmci, only). */
  61. }
  62. }
  63. };
  64. /*
  65. * Version array.
  66. */
  67. const char *pvtcpVersions[] = {
  68. [PVTCP_VERS_1_1] = PVTCP_COMM_IMPL_VERS_1_1,
  69. [PVTCP_VERS_1_0] = PVTCP_COMM_IMPL_VERS_1_0
  70. };
  71. const unsigned int pvtcpVersionsSize =
  72. (sizeof(pvtcpVersions) / sizeof(pvtcpVersions[0]));
  73. /*
  74. * Client (pv) channel to offload side. We choose to define it here, although
  75. * it's only applicable to the pv implementation. The reason is that we can
  76. * share a common close notification function which does the right thing
  77. * depending on the channel configuration.
  78. */
  79. CommChannel pvtcpClientChannel;
  80. /*
  81. * Built-in state interfaces.
  82. */
  83. static PvtcpIfConf ifUnbound = {
  84. .family = PVTCP_PF_UNBOUND
  85. };
  86. const PvtcpIfConf *pvtcpIfUnbound = &ifUnbound;
  87. static PvtcpIfConf ifDeathRow = {
  88. .family = PVTCP_PF_DEATH_ROW
  89. };
  90. const PvtcpIfConf *pvtcpIfDeathRow = &ifDeathRow;
  91. static PvtcpIfConf ifLoopbackInet4 = {
  92. .family = PVTCP_PF_LOOPBACK_INET4
  93. };
  94. const PvtcpIfConf *pvtcpIfLoopbackInet4 = &ifLoopbackInet4;
  95. /* Functions */
  96. /**
  97. * @brief Checks if the IF configuration has reasonable values.
  98. * @param conf configuration to check
  99. * @return zero if successful, -1 otherwise
  100. */
  101. static int
  102. IfCheck(const PvtcpIfConf *conf)
  103. {
  104. if (!conf ||
  105. ((conf->family != PF_INET) &&
  106. (conf->family != PF_INET6) &&
  107. (conf->family != PVTCP_PF_UNBOUND) &&
  108. (conf->family != PVTCP_PF_DEATH_ROW) &&
  109. (conf->family != PVTCP_PF_LOOPBACK_INET4))) {
  110. return -1;
  111. }
  112. /** @todo Need more checks for IP/netmask format validity. */
  113. return 0;
  114. }
  115. /**
  116. * @brief Checks if the IF has reasonable values, but restricts types to
  117. * AF_INET and AF_INET6
  118. * @param conf IF to check
  119. * @return zero if successful, -1 otherwise
  120. */
  121. static int
  122. IfRestrictedCheck(const PvtcpIfConf *conf)
  123. {
  124. if (IfCheck(conf) ||
  125. ((conf->family != PF_INET) &&
  126. (conf->family != PF_INET6))) {
  127. return -1;
  128. }
  129. return 0;
  130. }
  131. /**
  132. * @brief Finds a netif given a state and a configuration. The configuration
  133. * must have already been checked. This function doesn't lock, so it
  134. * should not be called when the state, or the netif for the passed
  135. * configuration may be deleted.
  136. * @param state state to look for.
  137. * @param conf configuration to look for.
  138. * @return netif matching configuration, or NULL.
  139. */
  140. PvtcpIf *
  141. PvtcpStateFindIf(PvtcpState *state,
  142. const PvtcpIfConf *conf)
  143. {
  144. PvtcpIf *netif;
  145. if (!state) {
  146. return NULL;
  147. }
  148. if (conf->family == PVTCP_PF_UNBOUND) {
  149. return &state->ifUnbound;
  150. }
  151. if (conf->family == PVTCP_PF_DEATH_ROW) {
  152. return &state->ifDeathRow;
  153. }
  154. if (conf->family == PVTCP_PF_LOOPBACK_INET4) {
  155. return &state->ifLoopbackInet4;
  156. }
  157. CommOS_ListForEach(&state->ifList, netif, stateLink) {
  158. if (netif->conf.family == conf->family) {
  159. if ((conf->family == PF_INET &&
  160. !memcmp(&netif->conf.addr.in, &conf->addr.in,
  161. sizeof(conf->addr.in))) ||
  162. (conf->family == PF_INET6 &&
  163. !memcmp(&netif->conf.addr.in6, &conf->addr.in6,
  164. sizeof(conf->addr.in6)))) {
  165. return netif;
  166. }
  167. }
  168. }
  169. return NULL;
  170. }
  171. /**
  172. * @brief Creates and initializes a new netif for a given channel and with
  173. * the specified configuration. Death row and unbound netifs may not
  174. * be added using this function.
  175. * @param[in,out] channel channel to make a new netif in
  176. * @param conf configuration to set netif to
  177. * @return 0 if successful, -1 otherwise
  178. * @sideeffect May allocate memory
  179. */
  180. int
  181. PvtcpStateAddIf(CommChannel channel,
  182. const PvtcpIfConf *conf)
  183. {
  184. int rc = -1;
  185. PvtcpState *state;
  186. PvtcpIf *netif;
  187. if (!channel || IfRestrictedCheck(conf)) {
  188. return rc;
  189. }
  190. if (CommSvc_Lock(channel)) {
  191. return rc; /* channel isn't active. */
  192. }
  193. state = CommSvc_GetState(channel);
  194. if (!state) {
  195. goto out;
  196. }
  197. if (PvtcpStateFindIf(state, conf)) {
  198. goto out; /* Already configured. */
  199. }
  200. netif = CommOS_Kmalloc(sizeof(*netif));
  201. if (!netif) {
  202. goto out;
  203. }
  204. INIT_LIST_HEAD(&netif->stateLink);
  205. INIT_LIST_HEAD(&netif->sockList);
  206. netif->state = state;
  207. netif->conf = *conf;
  208. CommOS_ListAddTail(&state->ifList, &netif->stateLink);
  209. rc = 0;
  210. out:
  211. CommSvc_Unlock(channel);
  212. return rc;
  213. }
  214. /**
  215. * @brief Removes all sockets associated with the given netif.
  216. * @param[in,out] netif interface to remove the socket from.
  217. * @sideeffect Closes sockets.
  218. */
  219. static void
  220. IfReleaseSockets(PvtcpIf *netif)
  221. {
  222. PvtcpSock *pvsk;
  223. PvtcpSock *tmp;
  224. if (netif) {
  225. CommOS_ListForEachSafe(&netif->sockList, pvsk, tmp, ifLink) {
  226. CommOS_ListDel(&pvsk->ifLink);
  227. PvtcpReleaseSocket(pvsk);
  228. }
  229. }
  230. }
  231. /**
  232. * @brief Deallocates the given net interface.
  233. * @param[in,out] netif netif to deallocate
  234. * @sideeffect Deallocates memory.
  235. */
  236. static void
  237. IfFree(PvtcpIf *netif)
  238. {
  239. if (netif) {
  240. CommOS_ListDel(&netif->stateLink);
  241. CommOS_Kfree(netif);
  242. }
  243. }
  244. /**
  245. * @brief Closes all sockets associated with, and deallocates the netif
  246. * in the given channel and with the specified configuration.
  247. * Death row and unbound netifs may not be removed using this function.
  248. * @param[in,out] channel channel to remove from
  249. * @param conf configuration specified
  250. * @return zero if successful, error code otherwise
  251. * @sideeffect Closes sockets, deallocates memory
  252. */
  253. void
  254. PvtcpStateRemoveIf(CommChannel channel,
  255. const PvtcpIfConf *conf)
  256. {
  257. PvtcpState *state;
  258. PvtcpIf *netif;
  259. if (!channel || IfRestrictedCheck(conf)) {
  260. return;
  261. }
  262. if (CommSvc_Lock(channel)) {
  263. return; /* channel isn't active. */
  264. }
  265. state = CommSvc_GetState(channel);
  266. if (state) {
  267. netif = PvtcpStateFindIf(state, conf);
  268. if (netif && netif->state == state) {
  269. IfReleaseSockets(netif);
  270. if ((netif->conf.family != PVTCP_PF_UNBOUND) &&
  271. (netif->conf.family != PVTCP_PF_DEATH_ROW) &&
  272. (netif->conf.family != PVTCP_PF_LOOPBACK_INET4)) {
  273. IfFree(netif);
  274. }
  275. }
  276. }
  277. CommSvc_Unlock(channel);
  278. }
  279. /**
  280. * @brief Adds a socket to an existing netif. If the socket is already on a
  281. * different netif, it is removed from that netif.
  282. * It locks the must-be-active channel. We use that lock to guard
  283. * against concurrent removal of the netif.
  284. * @param[in,out] channel channel to add to
  285. * @param conf specified configuration
  286. * @param[in,out] sock socket to add
  287. * @return zero if successful, -1 otherwise
  288. */
  289. int
  290. PvtcpStateAddSocket(CommChannel channel,
  291. const PvtcpIfConf *conf,
  292. PvtcpSock *sock)
  293. {
  294. int rc = -1;
  295. PvtcpState *state;
  296. PvtcpIf *netif;
  297. if (!channel || !sock || (sock->channel != channel) || IfCheck(conf)) {
  298. return rc;
  299. }
  300. if (CommSvc_Lock(channel)) {
  301. return rc; /* channel isn't active. */
  302. }
  303. state = CommSvc_GetState(channel);
  304. if (!state) {
  305. goto out;
  306. }
  307. netif = PvtcpStateFindIf(state, conf);
  308. if (!netif) {
  309. goto out;
  310. }
  311. CommOS_ListDel(&sock->ifLink);
  312. sock->netif = netif;
  313. CommOS_ListAddTail(&netif->sockList, &sock->ifLink);
  314. rc = 0;
  315. out:
  316. CommSvc_Unlock(channel);
  317. return rc;
  318. }
  319. /**
  320. * @brief Removes a socket from its netif.
  321. * It locks the must-be-active channel. We use that lock to guard
  322. * against concurrent removal of the netif.
  323. * @param[in,out] channel channel to remove from
  324. * @param[in,out] sock socket to remove
  325. * @return zero if successful, -1 otherwise
  326. */
  327. int
  328. PvtcpStateRemoveSocket(CommChannel channel,
  329. PvtcpSock *sock)
  330. {
  331. if (!channel || !sock ||
  332. (sock->channel && (sock->channel != channel))) {
  333. return -1;
  334. }
  335. if (CommSvc_Lock(channel)) {
  336. return -1; /* channel isn't active. */
  337. }
  338. CommOS_ListDel(&sock->ifLink);
  339. CommSvc_Unlock(channel);
  340. return 0;
  341. }
  342. /**
  343. * @brief State constructor called when a channel is created. The netifs
  344. * 'death row' and 'unbound' are always initialized.
  345. * @param[in,out] channel channel to initialize
  346. * @return pointer to a new state structure or NULL
  347. * @sideeffect Allocates memory
  348. */
  349. void *
  350. PvtcpStateAlloc(CommChannel channel)
  351. {
  352. PvtcpState *state;
  353. state = CommOS_Kmalloc(sizeof(*state));
  354. if (state) {
  355. state->channel = channel;
  356. INIT_LIST_HEAD(&state->ifList);
  357. /* Initialize always-present netifs. */
  358. INIT_LIST_HEAD(&state->ifDeathRow.stateLink); /* Irrelevant */
  359. INIT_LIST_HEAD(&state->ifDeathRow.sockList);
  360. state->ifDeathRow.state = state;
  361. state->ifDeathRow.conf.family = PVTCP_PF_DEATH_ROW;
  362. INIT_LIST_HEAD(&state->ifUnbound.stateLink); /* Irrelevant */
  363. INIT_LIST_HEAD(&state->ifUnbound.sockList);
  364. state->ifUnbound.state = state;
  365. state->ifUnbound.conf.family = PVTCP_PF_UNBOUND;
  366. INIT_LIST_HEAD(&state->ifLoopbackInet4.stateLink); /* Irrelevant */
  367. INIT_LIST_HEAD(&state->ifLoopbackInet4.sockList);
  368. state->ifLoopbackInet4.state = state;
  369. state->ifLoopbackInet4.conf.family = PVTCP_PF_LOOPBACK_INET4;
  370. state->namespace = NULL;
  371. state->mask = ((unsigned int)channel << 4) ^ (unsigned int)state;
  372. #if defined(__linux__)
  373. state->id = ((unsigned long long)random32() << 32) |
  374. (unsigned long long)random32();
  375. #else
  376. state->id = (unsigned long long)state;
  377. #endif
  378. }
  379. return state;
  380. }
  381. /**
  382. * @brief State destructor called when a channel is closed.
  383. * The caller (Comm) guarantees proper locking.
  384. * @param arg pointer to state structure
  385. * @sideeffect Destroys all netifs and their sockets, deallocates memory
  386. */
  387. void
  388. PvtcpStateFree(void *arg)
  389. {
  390. PvtcpState *state = arg;
  391. PvtcpIf *netif;
  392. PvtcpIf *tmp;
  393. if (state) {
  394. CommOS_ListForEachSafe(&state->ifList, netif, tmp, stateLink) {
  395. IfReleaseSockets(netif);
  396. IfFree(netif);
  397. }
  398. IfReleaseSockets(&state->ifLoopbackInet4);
  399. IfReleaseSockets(&state->ifUnbound);
  400. IfReleaseSockets(&state->ifDeathRow);
  401. CommOS_Kfree(state);
  402. }
  403. }
  404. /**
  405. * @brief Checks transport arguments.
  406. * @param transpArgs transport arguments.
  407. * @return zero if successful, < 0 otherwise.
  408. */
  409. int
  410. PvtcpCheckArgs(CommTranspInitArgs *transpArgs)
  411. {
  412. int rc = -1;
  413. const unsigned int minCapacity =
  414. (PVTCP_SOCK_BUF_SIZE + sizeof(CommPacket)) * 2;
  415. unsigned int versionIndex = pvtcpVersionsSize;
  416. if (transpArgs->capacity < minCapacity) {
  417. return rc;
  418. }
  419. while (versionIndex--) {
  420. if (transpArgs->type == CommTransp_GetType(pvtcpVersions[versionIndex])) {
  421. /* If a match, overwrite the hash with the actual version (index). */
  422. transpArgs->type = versionIndex;
  423. rc = 0;
  424. break;
  425. }
  426. }
  427. return rc;
  428. }
  429. /**
  430. * @brief Called after a channel is freed.
  431. * @param ntfData callback data from implementation block.
  432. * @param transpArgs transport arguments of closed channel.
  433. * @param inBH whether called in bottom half.
  434. */
  435. void
  436. PvtcpCloseNtf(void *ntfData,
  437. const CommTranspInitArgs *transpArgs,
  438. int inBH)
  439. {
  440. CommImpl *impl = (CommImpl *)ntfData;
  441. pvtcpClientChannel = NULL;
  442. CommOS_Log(("%s: Channel was reset!\n", __func__));
  443. /*
  444. * If the impl. block owner is NULL, we're pv client: we attempt to
  445. * reopen the channel in a few seconds.
  446. */
  447. if (impl && !impl->owner && !inBH) {
  448. CommOS_Log(("%s: Attempting to re-initialize channel.\n", __func__));
  449. impl->openAtMillis = CommOS_GetCurrentMillis();
  450. impl->openTimeoutAtMillis =
  451. CommOS_GetCurrentMillis() + PVTCP_CHANNEL_OPEN_TIMEOUT;
  452. if (CommSvc_Alloc(transpArgs, impl, inBH, &pvtcpClientChannel)) {
  453. CommOS_Log(("%s: Failed to initialize channel!\n", __func__));
  454. }
  455. }
  456. }
  457. /**
  458. * @brief Initializes the Pvtcp socket common fields.
  459. * @param pvsk pvtcp socket.
  460. * @param channel Comm channel this socket is associated with.
  461. * @return 0 if successful, -1 otherwise.
  462. */
  463. int
  464. PvtcpSockInit(PvtcpSock *pvsk,
  465. CommChannel channel)
  466. {
  467. PvtcpState *state;
  468. int rc = -1;
  469. if (pvsk && channel) {
  470. state = CommSvc_GetState(channel);
  471. if (state) {
  472. /* Must _not_ zero out pvsk! */
  473. CommOS_MutexInit(&pvsk->inLock);
  474. CommOS_MutexInit(&pvsk->outLock);
  475. CommOS_SpinlockInit(&pvsk->stateLock);
  476. CommOS_ListInit(&pvsk->ifLink);
  477. CommOS_InitWork(&pvsk->work, PvtcpProcessAIO);
  478. pvsk->netif = NULL;
  479. pvsk->state = state;
  480. pvsk->stateID = state->id;
  481. pvsk->channel = channel;
  482. pvsk->peerSock = PVTCP_PEER_SOCK_NULL;
  483. pvsk->peerSockSet = 0;
  484. CommOS_WriteAtomic(&pvsk->deltaAckSize,
  485. (1 << PVTCP_SOCK_SMALL_ACK_ORDER));
  486. CommOS_WriteAtomic(&pvsk->rcvdSize, 0);
  487. CommOS_WriteAtomic(&pvsk->sentSize, 0);
  488. CommOS_WriteAtomic(&pvsk->queueSize, 0);
  489. CommOS_ListInit(&pvsk->queue);
  490. pvsk->rpcReply = NULL;
  491. pvsk->rpcStatus = 0;
  492. pvsk->err = 0;
  493. rc = 0;
  494. }
  495. }
  496. return rc;
  497. }