NetlinkListener.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C) 2017 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. #ifndef NETLINK_LISTENER_H
  17. #define NETLINK_LISTENER_H
  18. #include <functional>
  19. #include <map>
  20. #include <mutex>
  21. #include <thread>
  22. #include <android-base/thread_annotations.h>
  23. #include <netdutils/Netlink.h>
  24. #include <netdutils/Slice.h>
  25. #include <netdutils/Status.h>
  26. #include <netdutils/UniqueFd.h>
  27. namespace android {
  28. namespace net {
  29. class NetlinkListenerInterface {
  30. public:
  31. using DispatchFn = std::function<void(const nlmsghdr& nlmsg, const netdutils::Slice msg)>;
  32. using SkErrorHandler = std::function<void(const int fd, const int err)>;
  33. virtual ~NetlinkListenerInterface() = default;
  34. // Send message to the kernel using the underlying netlink socket
  35. virtual netdutils::Status send(const netdutils::Slice msg) = 0;
  36. // Deliver future messages with nlmsghdr.nlmsg_type == type to fn.
  37. //
  38. // Threadsafe.
  39. // All dispatch functions invoked on a single service thread.
  40. // subscribe() and join() must not be called from the stack of fn().
  41. virtual netdutils::Status subscribe(uint16_t type, const DispatchFn& fn) = 0;
  42. // Halt delivery of future messages with nlmsghdr.nlmsg_type == type.
  43. // Threadsafe.
  44. virtual netdutils::Status unsubscribe(uint16_t type) = 0;
  45. virtual void registerSkErrorHandler(const SkErrorHandler& handler) = 0;
  46. };
  47. // NetlinkListener manages a netlink socket and associated blocking
  48. // service thread.
  49. //
  50. // This class is written in a generic way to allow multiple different
  51. // netlink subsystems to share this common infrastructure. If multiple
  52. // subsystems share the same message delivery requirements (drops ok,
  53. // no drops) they may share a single listener by calling subscribe()
  54. // with multiple types.
  55. //
  56. // This class is suitable for moderate performance message
  57. // processing. In particular it avoids extra copies of received
  58. // message data and allows client code to control which message
  59. // attributes are processed.
  60. //
  61. // Note that NetlinkListener is capable of processing multiple batched
  62. // netlink messages in a single system call. This is useful to
  63. // netfilter extensions that allow batching of events like NFLOG.
  64. class NetlinkListener : public NetlinkListenerInterface {
  65. public:
  66. NetlinkListener(netdutils::UniqueFd event, netdutils::UniqueFd sock, const std::string& name);
  67. ~NetlinkListener() override;
  68. netdutils::Status send(const netdutils::Slice msg) override;
  69. netdutils::Status subscribe(uint16_t type, const DispatchFn& fn) override EXCLUDES(mMutex);
  70. netdutils::Status unsubscribe(uint16_t type) override EXCLUDES(mMutex);
  71. void registerSkErrorHandler(const SkErrorHandler& handler) override;
  72. private:
  73. netdutils::Status run();
  74. const netdutils::UniqueFd mEvent;
  75. const netdutils::UniqueFd mSock;
  76. const std::string mThreadName;
  77. std::mutex mMutex;
  78. std::map<uint16_t, DispatchFn> mDispatchMap GUARDED_BY(mMutex);
  79. std::thread mWorker;
  80. SkErrorHandler mErrorHandler;
  81. };
  82. } // namespace net
  83. } // namespace android
  84. #endif /* NETLINK_LISTENER_H */