Home | History | Annotate | Download | only in shm
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 #pragma once
     17 
     18 #include "common/vsoc/shm/base.h"
     19 #include "common/vsoc/shm/circqueue.h"
     20 #include "common/vsoc/shm/lock.h"
     21 
     22 // Memory layout for wifi packet exchange region.
     23 namespace vsoc {
     24 namespace layout {
     25 namespace socket_forward {
     26 
     27 constexpr std::size_t kMaxPacketSize = 8192;
     28 constexpr std::size_t kNumQueues = 16;
     29 
     30 struct Queue {
     31   static constexpr size_t layout_size =
     32       CircularPacketQueue<16, kMaxPacketSize>::layout_size;
     33 
     34   CircularPacketQueue<16, kMaxPacketSize> queue;
     35 
     36   bool Recover() { return queue.Recover(); }
     37 };
     38 ASSERT_SHM_COMPATIBLE(Queue);
     39 
     40 struct QueuePair {
     41   static constexpr size_t layout_size = 2 * Queue::layout_size;
     42 
     43   // Traffic originating from host that proceeds towards guest.
     44   Queue host_to_guest;
     45   // Traffic originating from guest that proceeds towards host.
     46   Queue guest_to_host;
     47 
     48   bool Recover() {
     49     bool recovered = false;
     50     recovered = recovered || host_to_guest.Recover();
     51     recovered = recovered || guest_to_host.Recover();
     52     return recovered;
     53   }
     54 };
     55 ASSERT_SHM_COMPATIBLE(QueuePair);
     56 
     57 struct SocketForwardLayout : public RegionLayout {
     58   static constexpr size_t layout_size = QueuePair::layout_size * kNumQueues;
     59 
     60   bool Recover() {
     61     bool recovered = false;
     62     for (auto& i : queues_) {
     63       bool rval = i.Recover();
     64       recovered = recovered || rval;
     65     }
     66     return recovered;
     67   }
     68 
     69   QueuePair queues_[kNumQueues];
     70   static const char* region_name;
     71 };
     72 
     73 ASSERT_SHM_COMPATIBLE(SocketForwardLayout);
     74 
     75 }  // namespace socket_forward
     76 }  // namespace layout
     77 }  // namespace vsoc
     78