This file is indexed.

/usr/src/open-vm-tools-10.0.7/vsock/linux/notify.c is in open-vm-tools-dkms 2:10.0.7-3227872-2ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
/*********************************************************
 * Copyright (C) 2009-2014 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation version 2 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 *********************************************************/

/*
 * notify.c --
 *
 *      Linux control notifications for the VMCI Stream Sockets protocol.
 */


#include "driver-config.h"

#include <linux/socket.h>

#include "compat_sock.h"

#include "notify.h"
#include "af_vsock.h"

#define PKT_FIELD(vsk, fieldName) \
   (vsk)->notify.pkt.fieldName

#define VSOCK_MAX_DGRAM_RESENDS       10


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifyWaitingWrite --
 *
 *      Determines if the conditions have been met to notify a waiting writer.
 *
 * Results:
 *      TRUE if a notification should be sent, FALSE otherwise.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static Bool
VSockVmciNotifyWaitingWrite(VSockVmciSock *vsk)    // IN
{
#if defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
   Bool retval;
   uint64 notifyLimit;

   if (!PKT_FIELD(vsk, peerWaitingWrite)) {
      return FALSE;
   }

#ifdef VSOCK_OPTIMIZATION_FLOW_CONTROL
   /*
    * When the sender blocks, we take that as a sign that the sender
    * is faster than the receiver. To reduce the transmit rate of the
    * sender, we delay the sending of the read notification by
    * decreasing the writeNotifyWindow. The notification is delayed
    * until the number of bytes used in the queue drops below the
    * writeNotifyWindow.
    */

   if (!PKT_FIELD(vsk, peerWaitingWriteDetected)) {
      PKT_FIELD(vsk, peerWaitingWriteDetected) = TRUE;
      if (PKT_FIELD(vsk, writeNotifyWindow) < PAGE_SIZE) {
         PKT_FIELD(vsk, writeNotifyWindow) =
            PKT_FIELD(vsk, writeNotifyMinWindow);
      } else {
         PKT_FIELD(vsk, writeNotifyWindow) -= PAGE_SIZE;
         if (PKT_FIELD(vsk, writeNotifyWindow) <
             PKT_FIELD(vsk, writeNotifyMinWindow)) {
            PKT_FIELD(vsk, writeNotifyWindow) =
               PKT_FIELD(vsk, writeNotifyMinWindow);
         }
      }
   }
   notifyLimit = vsk->consumeSize - PKT_FIELD(vsk, writeNotifyWindow);
#else
   notifyLimit = 0;
#endif // VSOCK_OPTIMIZATION_FLOW_CONTROL

   /*
    * For now we ignore the wait information and just see if the free
    * space exceeds the notify limit.  Note that improving this
    * function to be more intelligent will not require a protocol
    * change and will retain compatibility between endpoints with
    * mixed versions of this function.
    *
    * The notifyLimit is used to delay notifications in the case where
    * flow control is enabled. Below the test is expressed in terms of
    * free space in the queue:
    *   if freeSpace > ConsumeSize - writeNotifyWindow then notify
    * An alternate way of expressing this is to rewrite the expression
    * to use the data ready in the receive queue:
    *   if writeNotifyWindow > bufferReady then notify
    * as freeSpace == ConsumeSize - bufferReady.
    */
   retval = vmci_qpair_consume_free_space(vsk->qpair) > notifyLimit;
#ifdef VSOCK_OPTIMIZATION_FLOW_CONTROL
   if (retval) {
      /*
       * Once we notify the peer, we reset the detected flag so the
       * next wait will again cause a decrease in the window size.
       */

      PKT_FIELD(vsk, peerWaitingWriteDetected) = FALSE;
   }
#endif // VSOCK_OPTIMIZATION_FLOW_CONTROL
   return retval;
#else
   return TRUE;
#endif
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifyWaitingRead --
 *
v *      Determines if the conditions have been met to notify a waiting reader.
 *
 * Results:
 *      TRUE if a notification should be sent, FALSE otherwise.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static Bool
VSockVmciNotifyWaitingRead(VSockVmciSock *vsk)  // IN
{
#if defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
   if (!PKT_FIELD(vsk, peerWaitingRead)) {
      return FALSE;
   }

   /*
    * For now we ignore the wait information and just see if there is any data
    * for our peer to read.  Note that improving this function to be more intelligent will
    * not require a protocol change and will retain compatibility between
    * endpoints with mixed versions of this function.
    */
   return vmci_qpair_produce_buf_ready(vsk->qpair) > 0;
#else
   return TRUE;
#endif
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciHandleWaitingRead --
 *
 *      Handles an incoming waiting read message.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      May send a notification to the peer, may update socket's wait info
 *      structure.
 *
 *----------------------------------------------------------------------------
 */

static void
VSockVmciHandleWaitingRead(struct sock *sk,             // IN
                           VSockPacket *pkt,            // IN
                           Bool bottomHalf,             // IN
                           struct sockaddr_vm *dst,     // IN
                           struct sockaddr_vm *src)     // IN
{
#if defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
   VSockVmciSock *vsk;

   vsk = vsock_sk(sk);

   PKT_FIELD(vsk, peerWaitingRead) = TRUE;
   memcpy(&PKT_FIELD(vsk, peerWaitingReadInfo), &pkt->u.wait,
          sizeof PKT_FIELD(vsk, peerWaitingReadInfo));

   if (VSockVmciNotifyWaitingRead(vsk)) {
      Bool sent;

      if (bottomHalf) {
         sent = VSOCK_SEND_WROTE_BH(dst, src) > 0;
      } else {
         sent = VSOCK_SEND_WROTE(sk) > 0;
      }

      if (sent) {
         PKT_FIELD(vsk, peerWaitingRead) = FALSE;
      }
   }
#endif
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciHandleWaitingWrite --
 *
 *      Handles an incoming waiting write message.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      May send a notification to the peer, may update socket's wait info
 *      structure.
 *
 *----------------------------------------------------------------------------
 */

static void
VSockVmciHandleWaitingWrite(struct sock *sk,            // IN
                            VSockPacket *pkt,           // IN
                            Bool bottomHalf,            // IN
                            struct sockaddr_vm *dst,    // IN
                            struct sockaddr_vm *src)    // IN
{
#if defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
   VSockVmciSock *vsk;

   vsk = vsock_sk(sk);

   PKT_FIELD(vsk, peerWaitingWrite) = TRUE;
   memcpy(&PKT_FIELD(vsk, peerWaitingWriteInfo), &pkt->u.wait,
          sizeof PKT_FIELD(vsk,peerWaitingWriteInfo));

   if (VSockVmciNotifyWaitingWrite(vsk)) {
      Bool sent;

      if (bottomHalf) {
         sent = VSOCK_SEND_READ_BH(dst, src) > 0;
      } else {
         sent = VSOCK_SEND_READ(sk) > 0;
      }

      if (sent) {
         PKT_FIELD(vsk, peerWaitingWrite) = FALSE;
      }
   }
#endif
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciHandleRead --
 *
 *      Handles an incoming read message.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static void
VSockVmciHandleRead(struct sock *sk,            // IN
                    VSockPacket *pkt,           // IN: unused
                    Bool bottomHalf,            // IN: unused
                    struct sockaddr_vm *dst,    // IN: unused
                    struct sockaddr_vm *src)    // IN: unused
{
#if defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
   VSockVmciSock *vsk;

   vsk = vsock_sk(sk);
   PKT_FIELD(vsk, sentWaitingWrite) = FALSE;
#endif

   sk->sk_write_space(sk);
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciSendWaitingRead --
 *
 *      Sends a waiting read notification to this socket's peer.
 *
 * Results:
 *      TRUE if the datagram is sent successfully, FALSE otherwise.
 *
 * Side effects:
 *      Our peer will notify us when there is data to read from our consume
 *      queue.
 *
 *----------------------------------------------------------------------------
 */

static Bool
VSockVmciSendWaitingRead(struct sock *sk,    // IN
                         uint64 roomNeeded)  // IN
{
#if defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
   VSockVmciSock *vsk;
   VSockWaitingInfo waitingInfo;
   uint64 tail;
   uint64 head;
   uint64 roomLeft;
   Bool ret;

   ASSERT(sk);

   vsk = vsock_sk(sk);

   if (PKT_FIELD(vsk, sentWaitingRead)) {
      return TRUE;
   }

   if (PKT_FIELD(vsk, writeNotifyWindow) < vsk->consumeSize) {
      PKT_FIELD(vsk, writeNotifyWindow) =
         MIN(PKT_FIELD(vsk, writeNotifyWindow) + PAGE_SIZE,
             vsk->consumeSize);
   }

   vmci_qpair_get_consume_indexes(vsk->qpair, &tail, &head);
   roomLeft = vsk->consumeSize - head;
   if (roomNeeded >= roomLeft) {
      waitingInfo.offset = roomNeeded - roomLeft;
      waitingInfo.generation = PKT_FIELD(vsk, consumeQGeneration) + 1;
   } else {
      waitingInfo.offset = head + roomNeeded;
      waitingInfo.generation = PKT_FIELD(vsk, consumeQGeneration);
   }

   ret = VSOCK_SEND_WAITING_READ(sk, &waitingInfo) > 0;
   if (ret) {
      PKT_FIELD(vsk, sentWaitingRead) = TRUE;
   }
   return ret;
#else
   return TRUE;
#endif
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciSendWaitingWrite --
 *
 *      Sends a waiting write notification to this socket's peer.
 *
 * Results:
 *      TRUE if the datagram is sent successfully or does not need to be sent.
 *      FALSE otherwise.
 *
 * Side effects:
 *      Our peer will notify us when there is room to write in to our produce
 *      queue.
 *
 *----------------------------------------------------------------------------
 */

static Bool
VSockVmciSendWaitingWrite(struct sock *sk,   // IN
                          uint64 roomNeeded) // IN
{
#if defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
   VSockVmciSock *vsk;
   VSockWaitingInfo waitingInfo;
   uint64 tail;
   uint64 head;
   uint64 roomLeft;
   Bool ret;

   ASSERT(sk);

   vsk = vsock_sk(sk);

   if (PKT_FIELD(vsk, sentWaitingWrite)) {
      return TRUE;
   }

   vmci_qpair_get_produce_indexes(vsk->qpair, &tail, &head);
   roomLeft = vsk->produceSize - tail;
   if (roomNeeded + 1 >= roomLeft) {
      /* Wraps around to current generation. */
      waitingInfo.offset = roomNeeded + 1 - roomLeft;
      waitingInfo.generation = PKT_FIELD(vsk, produceQGeneration);
   } else {
      waitingInfo.offset = tail + roomNeeded + 1;
      waitingInfo.generation = PKT_FIELD(vsk, produceQGeneration) - 1;
   }

   ret = VSOCK_SEND_WAITING_WRITE(sk, &waitingInfo) > 0;
   if (ret) {
      PKT_FIELD(vsk, sentWaitingWrite) = TRUE;
   }
   return ret;
#else
   return TRUE;
#endif
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciSendReadNotification --
 *
 *      Sends a read notification to this socket's peer.
 *
 * Results:
 *      >= 0 if the datagram is sent successfully, negative error value
 *      otherwise.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static int
VSockVmciSendReadNotification(struct sock *sk)  // IN
{
   VSockVmciSock *vsk;
   Bool sentRead;
   unsigned int retries;
   int err;

   ASSERT(sk);

   vsk = vsock_sk(sk);
   sentRead = FALSE;
   retries = 0;
   err = 0;

   if (VSockVmciNotifyWaitingWrite(vsk)) {
      /*
       * Notify the peer that we have read, retrying the send on failure up to our
       * maximum value.  XXX For now we just log the failure, but later we should
       * schedule a work item to handle the resend until it succeeds.  That would
       * require keeping track of work items in the vsk and cleaning them up upon
       * socket close.
       */
      while (!(vsk->peerShutdown & RCV_SHUTDOWN) &&
             !sentRead &&
             retries < VSOCK_MAX_DGRAM_RESENDS) {
         err = VSOCK_SEND_READ(sk);
         if (err >= 0) {
            sentRead = TRUE;
         }

         retries++;
      }

      if (retries >= VSOCK_MAX_DGRAM_RESENDS) {
         Warning("unable to send read notification to peer for socket %p.\n", sk);
      } else {
#if defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
         PKT_FIELD(vsk, peerWaitingWrite) = FALSE;
#endif
      }
   }
   return err;
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciHandleWrote --
 *
 *      Handles an incoming wrote message.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static void
VSockVmciHandleWrote(struct sock *sk,            // IN
                     VSockPacket *pkt,           // IN: unused
                     Bool bottomHalf,            // IN: unused
                     struct sockaddr_vm *dst,    // IN: unused
                     struct sockaddr_vm *src)    // IN: unused
{
#if defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
   VSockVmciSock *vsk;

   vsk = vsock_sk(sk);
   PKT_FIELD(vsk, sentWaitingRead) = FALSE;
#endif

   sk->sk_data_ready(sk, 0);
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifyPktSocketInit --
 *
 *      Function that is called after a socket is created and before any
 *      notify ops are used.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static void
VSockVmciNotifyPktSocketInit(struct sock *sk) // IN
{
   VSockVmciSock *vsk;
   vsk = vsock_sk(sk);

   PKT_FIELD(vsk, writeNotifyWindow) = PAGE_SIZE;
   PKT_FIELD(vsk, writeNotifyMinWindow) = PAGE_SIZE;
   PKT_FIELD(vsk, peerWaitingRead) = FALSE;
   PKT_FIELD(vsk, peerWaitingWrite) = FALSE;
   PKT_FIELD(vsk, peerWaitingWriteDetected) = FALSE;
   PKT_FIELD(vsk, sentWaitingRead) = FALSE;
   PKT_FIELD(vsk, sentWaitingWrite) = FALSE;
   PKT_FIELD(vsk, produceQGeneration) = 0;
   PKT_FIELD(vsk, consumeQGeneration) = 0;

   memset(&PKT_FIELD(vsk, peerWaitingReadInfo), 0,
          sizeof PKT_FIELD(vsk, peerWaitingReadInfo));
   memset(&PKT_FIELD(vsk, peerWaitingWriteInfo), 0,
          sizeof PKT_FIELD(vsk, peerWaitingWriteInfo));
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifyPktSocketDestruct --
 *
 *      Function that is called when the socket is being released.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static void
VSockVmciNotifyPktSocketDestruct(struct sock *sk) // IN
{
   return;
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifyPktPollIn --
 *
 *      Called by the poll function to figure out if there is data to read
 *      and to setup future notifications if needed. Only called on sockets
 *      that aren't shutdown for recv.
 *
 * Results:
 *      0 on success. Negative error on failure.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static int32
VSockVmciNotifyPktPollIn(struct sock *sk,    // IN
                         size_t target,      // IN
                         Bool *dataReadyNow) // IN
{
   VSockVmciSock *vsk;

   ASSERT(sk);
   ASSERT(dataReadyNow);

   vsk = vsock_sk(sk);

   if (VSockVmciStreamHasData(vsk)) {
      *dataReadyNow = TRUE;
   } else {
      /*
       * We can't read right now because there is nothing in the queue.
       * Ask for notifications when there is something to read.
       */
      if (sk->sk_state == SS_CONNECTED) {
         if (!VSockVmciSendWaitingRead(sk, 1)) {
            return -1;
         }
      }
      *dataReadyNow = FALSE;
   }

   return 0;
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifyPktPollOut
 *
 *      Called by the poll function to figure out if there is space to write
 *      and to setup future notifications if needed. Only called on a
 *      connected socket that isn't shutdown for send.
 *
 * Results:
 *      0 on success. Negative error on failure.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static int32
VSockVmciNotifyPktPollOut(struct sock *sk,     // IN
                          size_t target,       // IN
                          Bool *spaceAvailNow) // IN
{
   int64 produceQFreeSpace;
   VSockVmciSock *vsk;

   ASSERT(sk);
   ASSERT(spaceAvailNow);

   vsk = vsock_sk(sk);

   produceQFreeSpace =
      VSockVmciStreamHasSpace(vsk);
   if (produceQFreeSpace > 0) {
      *spaceAvailNow = TRUE;
      return 0;
   } else if (produceQFreeSpace == 0) {
      /*
       * This is a connected socket but we can't currently send data. Notify
       * the peer that we are waiting if the queue is full.
       * We only send a waiting write if the queue is full because otherwise
       * we end up in an infinite WAITING_WRITE, READ, WAITING_WRITE, READ, etc.
       * loop. Treat failing to send the notification as a socket error, passing
       * that back through the mask.
       */
      if (!VSockVmciSendWaitingWrite(sk, 1)) {
         return -1;
      }
      *spaceAvailNow = FALSE;
   }

   return 0;
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifyPktRecvInit --
 *
 *      Called at the start of a stream recv call with the socket lock held.
 *
 * Results:
 *      0 on success. Negative error on failure.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static int32
VSockVmciNotifyPktRecvInit(struct sock *sk,               // IN
                           size_t target,                 // IN
                           VSockVmciRecvNotifyData *data) // IN
{
   VSockVmciSock *vsk;

   ASSERT(sk);
   ASSERT(data);

   vsk = vsock_sk(sk);

#ifdef VSOCK_OPTIMIZATION_WAITING_NOTIFY
   data->consumeHead = 0;
   data->produceTail = 0;
#ifdef VSOCK_OPTIMIZATION_FLOW_CONTROL
   data->notifyOnBlock = FALSE;

   if (PKT_FIELD(vsk, writeNotifyMinWindow) < target + 1) {
      ASSERT(target < vsk->consumeSize);
      PKT_FIELD(vsk, writeNotifyMinWindow) = target + 1;
      if (PKT_FIELD(vsk, writeNotifyWindow) <
          PKT_FIELD(vsk, writeNotifyMinWindow)) {
         /*
          * If the current window is smaller than the new minimal
          * window size, we need to reevaluate whether we need to
          * notify the sender. If the number of ready bytes are
          * smaller than the new window, we need to send a
          * notification to the sender before we block.
          */

         PKT_FIELD(vsk, writeNotifyWindow) =
            PKT_FIELD(vsk, writeNotifyMinWindow);
         data->notifyOnBlock = TRUE;
      }
   }
#endif
#endif

   return 0;
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifyPktRecvPreBlock --
 *
 *      Called right before a socket is about to block with the socket lock
 *      held. The socket lock may have been released between the entry
 *      function and the preblock call.
 *
 *      Note: This function may be called multiple times before the post
 *      block function is called.
 *
 * Results:
 *      0 on success. Negative error on failure.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static int32
VSockVmciNotifyPktRecvPreBlock(struct sock *sk,               // IN
                               size_t target,                 // IN
                               VSockVmciRecvNotifyData *data) // IN
{
   int err;

   ASSERT(sk);
   ASSERT(data);

   err = 0;

   /* Notify our peer that we are waiting for data to read. */
   if (!VSockVmciSendWaitingRead(sk, target)) {
      err = -EHOSTUNREACH;
      return err;
   }

#ifdef VSOCK_OPTIMIZATION_FLOW_CONTROL
   if (data->notifyOnBlock) {
      err = VSockVmciSendReadNotification(sk);
      if (err < 0) {
         return err;
      }
      data->notifyOnBlock = FALSE;
   }
#endif

   return err;
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifyPktRecvPreDequeue --
 *
 *      Called right before we dequeue / peek data from a socket.
 *
 * Results:
 *      0 on success. Negative error on failure.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static int32
VSockVmciNotifyPktRecvPreDequeue(struct sock *sk,               // IN
                                 size_t target,                 // IN
                                 VSockVmciRecvNotifyData *data) // IN
{
   VSockVmciSock *vsk;

   ASSERT(sk);
   ASSERT(data);

   vsk = vsock_sk(sk);

   /*
    * Now consume up to len bytes from the queue.  Note that since we have the
    * socket locked we should copy at least ready bytes.
    */
#if defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
   vmci_qpair_get_consume_indexes(vsk->qpair,
                                  &data->produceTail,
                                  &data->consumeHead);
#endif

   return 0;
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifyPktRecvPostDequeue --
 *
 *      Called right after we dequeue / peek data from a socket.
 *
 * Results:
 *      0 on success. Negative error on failure.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static int32
VSockVmciNotifyPktRecvPostDequeue(struct sock *sk,               // IN
                                  size_t target,                 // IN
                                  ssize_t copied,                // IN
                                  Bool dataRead,                 // IN
                                  VSockVmciRecvNotifyData *data) // IN
{
   VSockVmciSock *vsk;
   int err;

   ASSERT(sk);
   ASSERT(data);

   vsk = vsock_sk(sk);
   err = 0;

   if (dataRead) {
#if defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
      /*
       * Detect a wrap-around to maintain queue generation.  Note that this is
       * safe since we hold the socket lock across the two queue pair
       * operations.
       */
      if (copied >= vsk->consumeSize - data->consumeHead) {
         PKT_FIELD(vsk, consumeQGeneration)++;
      }
#endif

      err = VSockVmciSendReadNotification(sk);
      if (err < 0) {
         return err;
      }

   }
   return err;
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifyPktSendInit --
 *
 *      Called at the start of a stream send call with the socket lock held.
 *
 * Results:
 *      0 on success. A negative error code on failure.
 *
 * Side effects:
 *
 *----------------------------------------------------------------------------
 */

static int32
VSockVmciNotifyPktSendInit(struct sock *sk,               // IN
                           VSockVmciSendNotifyData *data) // IN
{
   ASSERT(sk);
   ASSERT(data);

#ifdef VSOCK_OPTIMIZATION_WAITING_NOTIFY
   data->consumeHead = 0;
   data->produceTail = 0;
#endif

   return 0;
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifyPktSendPreBlock --
 *
 *      Called right before a socket is about to block with the socket lock
 *      held. The socket lock may have been released between the entry
 *      function and the preblock call.
 *
 *      Note: This function may be called multiple times before the post
 *      block function is called.
 *
 * Results.
 *      0 on success. A negative error code on failure.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static int32
VSockVmciNotifyPktSendPreBlock(struct sock *sk,               // IN
                               VSockVmciSendNotifyData *data) // IN
{
   ASSERT(sk);
   ASSERT(data);

   /* Notify our peer that we are waiting for room to write. */
   if (!VSockVmciSendWaitingWrite(sk, 1)) {
      return -EHOSTUNREACH;
   }

   return 0;
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifySendPreEnqueue --
 *
 *      Called right before we Enqueue to a socket.
 *
 * Results:
 *      0 on success. Negative error on failure.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static int32
VSockVmciNotifyPktSendPreEnqueue(struct sock *sk,               // IN
                                 VSockVmciSendNotifyData *data) // IN
{
   VSockVmciSock *vsk;

   ASSERT(sk);
   ASSERT(data);

   vsk = vsock_sk(sk);

#if defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
      vmci_qpair_get_produce_indexes(vsk->qpair,
                                     &data->produceTail,
                                     &data->consumeHead);
#endif

   return 0;;
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifySendPostEnqueue --
 *
 *      Called right after we enqueue data to a socket.
 *
 * Results:
 *      0 on success. Negative error on failure.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static int32
VSockVmciNotifyPktSendPostEnqueue(struct sock *sk,               // IN
                                  ssize_t written,               // IN
                                  VSockVmciSendNotifyData *data) // IN
{
   int err = 0;
   VSockVmciSock *vsk;
   Bool sentWrote = FALSE;
   int retries = 0;

   ASSERT(sk);
   ASSERT(data);

   vsk = vsock_sk(sk);

#if defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
      /*
       * Detect a wrap-around to maintain queue generation.  Note that this is
       * safe since we hold the socket lock across the two queue pair
       * operations.
       */
   if (written >= vsk->produceSize - data->produceTail) {
      PKT_FIELD(vsk, produceQGeneration)++;
   }
#endif

   if (VSockVmciNotifyWaitingRead(vsk)) {
      /*
       * Notify the peer that we have written, retrying the send on failure up to
       * our maximum value. See the XXX comment for the corresponding piece of
       * code in StreamRecvmsg() for potential improvements.
       */
      while (!(vsk->peerShutdown & RCV_SHUTDOWN) &&
             !sentWrote &&
             retries < VSOCK_MAX_DGRAM_RESENDS) {
         err = VSOCK_SEND_WROTE(sk);
         if (err >= 0) {
            sentWrote = TRUE;
         }

         retries++;
      }

      if (retries >= VSOCK_MAX_DGRAM_RESENDS) {
         Warning("unable to send wrote notification to peer for socket %p.\n", sk);
         return err;
      } else {
#if defined(VSOCK_OPTIMIZATION_WAITING_NOTIFY)
         PKT_FIELD(vsk, peerWaitingRead) = FALSE;
#endif
      }
   }
   return err;
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifyPktHandlePkt
 *
 *      Called when a notify packet is recieved for a socket in the connected
 *      state. Note this might be called from a bottom half.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static void
VSockVmciNotifyPktHandlePkt(struct sock *sk,         // IN
                            VSockPacket *pkt,        // IN
                            Bool bottomHalf,         // IN
                            struct sockaddr_vm *dst, // IN
                            struct sockaddr_vm *src, // IN
                            Bool *pktProcessed)      // In
{
   Bool processed = FALSE;

   ASSERT(sk);
   ASSERT(pkt);

   switch (pkt->type) {
   case VSOCK_PACKET_TYPE_WROTE:
      VSockVmciHandleWrote(sk, pkt, bottomHalf, dst, src);
      processed = TRUE;
      break;
   case VSOCK_PACKET_TYPE_READ:
      VSockVmciHandleRead(sk, pkt, bottomHalf, dst, src);
      processed = TRUE;
      break;
   case VSOCK_PACKET_TYPE_WAITING_WRITE:
      VSockVmciHandleWaitingWrite(sk, pkt, bottomHalf, dst, src);
      processed = TRUE;
      break;

   case VSOCK_PACKET_TYPE_WAITING_READ:
      VSockVmciHandleWaitingRead(sk, pkt, bottomHalf, dst, src);
      processed = TRUE;
      break;
   }

   if (pktProcessed) {
      *pktProcessed = processed;
   }
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifyPktProcessRequest
 *
 *      Called near the end of process request.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static void
VSockVmciNotifyPktProcessRequest(struct sock *sk) // IN
{
   VSockVmciSock *vsk;

   ASSERT(sk);

   vsk = vsock_sk(sk);

   PKT_FIELD(vsk, writeNotifyWindow) = vsk->consumeSize;
   if (vsk->consumeSize < PKT_FIELD(vsk, writeNotifyMinWindow)) {
      PKT_FIELD(vsk, writeNotifyMinWindow) = vsk->consumeSize;
   }
}


/*
 *----------------------------------------------------------------------------
 *
 * VSockVmciNotifyPktProcessNegotiate
 *
 *      Called near the end of process negotiate.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

static void
VSockVmciNotifyPktProcessNegotiate(struct sock *sk) // IN
{
   VSockVmciSock *vsk;

   ASSERT(sk);

   vsk = vsock_sk(sk);

   PKT_FIELD(vsk, writeNotifyWindow) = vsk->consumeSize;
   if (vsk->consumeSize < PKT_FIELD(vsk, writeNotifyMinWindow)) {
      PKT_FIELD(vsk, writeNotifyMinWindow) = vsk->consumeSize;
   }
}


/* Socket control packet based operations. */
VSockVmciNotifyOps vSockVmciNotifyPktOps = {
   VSockVmciNotifyPktSocketInit,
   VSockVmciNotifyPktSocketDestruct,
   VSockVmciNotifyPktPollIn,
   VSockVmciNotifyPktPollOut,
   VSockVmciNotifyPktHandlePkt,
   VSockVmciNotifyPktRecvInit,
   VSockVmciNotifyPktRecvPreBlock,
   VSockVmciNotifyPktRecvPreDequeue,
   VSockVmciNotifyPktRecvPostDequeue,
   VSockVmciNotifyPktSendInit,
   VSockVmciNotifyPktSendPreBlock,
   VSockVmciNotifyPktSendPreEnqueue,
   VSockVmciNotifyPktSendPostEnqueue,
   VSockVmciNotifyPktProcessRequest,
   VSockVmciNotifyPktProcessNegotiate,
};