Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
| 17 | package com.android.server; |
| 18 | |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 19 | import android.os.StrictMode; |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 20 | import android.os.SystemProperties; |
| 21 | import android.util.Log; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 22 | import android.util.Slog; |
| 23 | |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 24 | import dalvik.system.SocketTagger; |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 25 | |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 26 | import java.io.FileDescriptor; |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 27 | import java.net.SocketException; |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Assigns tags to sockets for traffic stats. |
| 31 | */ |
| 32 | public final class NetworkManagementSocketTagger extends SocketTagger { |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 33 | private static final String TAG = "NetworkManagementSocketTagger"; |
| 34 | private static final boolean LOGD = false; |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 35 | |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 36 | /** |
| 37 | * {@link SystemProperties} key that indicates if {@code qtaguid} bandwidth |
| 38 | * controls have been enabled. |
| 39 | */ |
| 40 | // TODO: remove when always enabled, or once socket tagging silently fails. |
| 41 | public static final String PROP_QTAGUID_ENABLED = "net.qtaguid_enabled"; |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 42 | |
| 43 | private static ThreadLocal<SocketTags> threadSocketTags = new ThreadLocal<SocketTags>() { |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 44 | @Override |
| 45 | protected SocketTags initialValue() { |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 46 | return new SocketTags(); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | public static void install() { |
| 51 | SocketTagger.set(new NetworkManagementSocketTagger()); |
| 52 | } |
| 53 | |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 54 | public static int setThreadSocketStatsTag(int tag) { |
| 55 | final int old = threadSocketTags.get().statsTag; |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 56 | threadSocketTags.get().statsTag = tag; |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 57 | return old; |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Alon Albert | eaef351 | 2011-07-19 11:16:09 +0300 | [diff] [blame] | 60 | public static int getThreadSocketStatsTag() { |
| 61 | return threadSocketTags.get().statsTag; |
| 62 | } |
| 63 | |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 64 | public static int setThreadSocketStatsUid(int uid) { |
| 65 | final int old = threadSocketTags.get().statsUid; |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 66 | threadSocketTags.get().statsUid = uid; |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 67 | return old; |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Jeff Sharkey | 121d565 | 2018-03-26 13:11:33 -0600 | [diff] [blame] | 70 | public static int getThreadSocketStatsUid() { |
| 71 | return threadSocketTags.get().statsUid; |
| 72 | } |
| 73 | |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 74 | @Override |
| 75 | public void tag(FileDescriptor fd) throws SocketException { |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 76 | final SocketTags options = threadSocketTags.get(); |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 77 | if (LOGD) { |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 78 | Log.d(TAG, "tagSocket(" + fd.getInt$() + ") with statsTag=0x" |
| 79 | + Integer.toHexString(options.statsTag) + ", statsUid=" + options.statsUid); |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 80 | } |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 81 | if (options.statsTag == -1 && StrictMode.vmUntaggedSocketEnabled()) { |
| 82 | StrictMode.onUntaggedSocket(); |
| 83 | } |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 84 | // TODO: skip tagging when options would be no-op |
| 85 | tagSocketFd(fd, options.statsTag, options.statsUid); |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 86 | } |
| 87 | |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 88 | private void tagSocketFd(FileDescriptor fd, int tag, int uid) { |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 89 | if (tag == -1 && uid == -1) return; |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 90 | |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 91 | if (SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) { |
| 92 | final int errno = native_tagSocketFd(fd, tag, uid); |
| 93 | if (errno < 0) { |
| 94 | Log.i(TAG, "tagSocketFd(" + fd.getInt$() + ", " |
| 95 | + tag + ", " + |
| 96 | + uid + ") failed with errno" + errno); |
| 97 | } |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 98 | } |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 101 | @Override |
| 102 | public void untag(FileDescriptor fd) throws SocketException { |
| 103 | if (LOGD) { |
| 104 | Log.i(TAG, "untagSocket(" + fd.getInt$() + ")"); |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 105 | } |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 106 | unTagSocketFd(fd); |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 107 | } |
| 108 | |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 109 | private void unTagSocketFd(FileDescriptor fd) { |
JP Abgrall | 36bd984 | 2011-08-17 12:06:56 -0700 | [diff] [blame] | 110 | final SocketTags options = threadSocketTags.get(); |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 111 | if (options.statsTag == -1 && options.statsUid == -1) return; |
| 112 | |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 113 | if (SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) { |
| 114 | final int errno = native_untagSocketFd(fd); |
| 115 | if (errno < 0) { |
| 116 | Log.w(TAG, "untagSocket(" + fd.getInt$() + ") failed with errno " + errno); |
| 117 | } |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 118 | } |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | public static class SocketTags { |
| 122 | public int statsTag = -1; |
| 123 | public int statsUid = -1; |
| 124 | } |
| 125 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 126 | public static void setKernelCounterSet(int uid, int counterSet) { |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 127 | if (SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) { |
| 128 | final int errno = native_setCounterSet(counterSet, uid); |
| 129 | if (errno < 0) { |
| 130 | Log.w(TAG, "setKernelCountSet(" + uid + ", " + counterSet + ") failed with errno " |
| 131 | + errno); |
| 132 | } |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | public static void resetKernelUidStats(int uid) { |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 137 | if (SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) { |
| 138 | int errno = native_deleteTagData(0, uid); |
| 139 | if (errno < 0) { |
| 140 | Slog.w(TAG, "problem clearing counters for uid " + uid + " : errno " + errno); |
| 141 | } |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 145 | /** |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 146 | * Convert {@code /proc/} tag format to {@link Integer}. Assumes incoming |
| 147 | * format like {@code 0x7fffffff00000000}. |
| 148 | */ |
| 149 | public static int kernelToTag(String string) { |
Jeff Sharkey | 2d6c580 | 2012-05-02 16:00:52 -0700 | [diff] [blame] | 150 | int length = string.length(); |
| 151 | if (length > 10) { |
| 152 | return Long.decode(string.substring(0, length - 8)).intValue(); |
| 153 | } else { |
| 154 | return 0; |
| 155 | } |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 156 | } |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 157 | |
| 158 | private static native int native_tagSocketFd(FileDescriptor fd, int tag, int uid); |
| 159 | private static native int native_untagSocketFd(FileDescriptor fd); |
| 160 | private static native int native_setCounterSet(int uid, int counterSetNum); |
| 161 | private static native int native_deleteTagData(int tag, int uid); |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 162 | } |