|
| 1 | +/* |
| 2 | + * Copyright (C) Jan 2013 Mellanox Technologies Ltd. All rights reserved. |
| 3 | + * |
| 4 | + * This software is available to you under a choice of one of two |
| 5 | + * licenses. You may choose to be licensed under the terms of the GNU |
| 6 | + * General Public License (GPL) Version 2, available from the file |
| 7 | + * COPYING in the main directory of this source tree, or the |
| 8 | + * OpenIB.org BSD license below: |
| 9 | + * |
| 10 | + * Redistribution and use in source and binary forms, with or |
| 11 | + * without modification, are permitted provided that the following |
| 12 | + * conditions are met: |
| 13 | + * |
| 14 | + * - Redistributions of source code must retain the above |
| 15 | + * copyright notice, this list of conditions and the following |
| 16 | + * disclaimer. |
| 17 | + * |
| 18 | + * - Redistributions in binary form must reproduce the above |
| 19 | + * copyright notice, this list of conditions and the following |
| 20 | + * disclaimer in the documentation and/or other materials |
| 21 | + * provided with the distribution. |
| 22 | + * |
| 23 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 | + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 | + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 | + * SOFTWARE. |
| 31 | + * |
| 32 | + */ |
| 33 | + |
| 34 | +#include "congestion.h" |
| 35 | +#include "reg_access/reg_access.h" |
| 36 | +#include "common/tools_version.h" |
| 37 | +#include <stdlib.h> |
| 38 | +#include <iostream> |
| 39 | + |
| 40 | +using namespace std; |
| 41 | + |
| 42 | +enum { |
| 43 | + BIT_TX_LOSSY_OPER = 1, |
| 44 | + BIT_TX_SENSE = 1 << 3, |
| 45 | + BIT_MARK_TX_CQE = 1 << 4, |
| 46 | + BIT_MARK_TX_CNP = 1 << 5, |
| 47 | +}; |
| 48 | + |
| 49 | +int main(int argc, char **argv) |
| 50 | +{ |
| 51 | + CongestionUI congestObject; |
| 52 | + if (!congestObject.run(argc, argv)) { |
| 53 | + cout << "-E- Failed to perform the operation: " << congestObject.getError() << endl; |
| 54 | + return 1; |
| 55 | + } else { |
| 56 | + if (congestObject.getSuccess() != "") { |
| 57 | + cout << "-I- " << congestObject.getSuccess() << endl; |
| 58 | + } |
| 59 | + } |
| 60 | + return 0; |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +CongestionUI::CongestionUI() : |
| 65 | + CommandLineRequester("mstcongestion [OPTIONS]"), |
| 66 | + _cmdParser("mstcongestion") |
| 67 | +{ |
| 68 | + _mf = NULL; |
| 69 | + _action = ACTION_NA; |
| 70 | + _mode = MODE_NA; |
| 71 | + _query = false; |
| 72 | + _ops = 0; |
| 73 | + _dynamicSupp = 0; |
| 74 | + _markCqeSupp = 0; |
| 75 | + _markCnpSupp = 0; |
| 76 | + initCmdParser(); |
| 77 | +} |
| 78 | + |
| 79 | +CongestionUI::~CongestionUI() |
| 80 | +{ |
| 81 | + if (_mf) { |
| 82 | + mclose(_mf); |
| 83 | + _mf = NULL; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +void CongestionUI::initCmdParser() |
| 88 | +{ |
| 89 | + |
| 90 | + AddOptions("device", 'd', "<PCI DEVICE>", "Mellanox PCI device address"); |
| 91 | + AddOptions("mode", ' ', "<MODE>", "Set Mode, options are: [aggressive | dynamic]"); |
| 92 | + AddOptions("action", ' ', "<ACTION>", "Set Action, options are: [disabled | drop | mark]"); |
| 93 | + AddOptions("query", 'q', "", "Query congestion"); |
| 94 | + AddOptions("help", 'h', "", "Show help message and exit"); |
| 95 | + AddOptions("version", 'v', "", "Show version and exit"); |
| 96 | + AddDescription("mstcongestion is a tool to set Congestion mode and action."); |
| 97 | + _cmdParser.AddRequester(this); |
| 98 | +} |
| 99 | + |
| 100 | +ParseStatus CongestionUI::HandleOption(string name, string value) |
| 101 | +{ |
| 102 | + if (name == "device") { |
| 103 | + _devname = value; |
| 104 | + } else if (name == "mode" ) { |
| 105 | + _ops++; |
| 106 | + if (value == "aggressive") { |
| 107 | + _mode = MODE_AGGRESSIVE; |
| 108 | + } else if (value == "dynamic") { |
| 109 | + _mode = MODE_DYNAMIC; |
| 110 | + } else { |
| 111 | + _errorMsg = "Invalid mode, options are: [aggressive | dynamic]"; |
| 112 | + return PARSE_ERROR; |
| 113 | + } |
| 114 | + } else if (name == "action") { |
| 115 | + _ops++; |
| 116 | + if (value == "disabled") { |
| 117 | + _action = ACTION_DISABLED; |
| 118 | + } else if (value == "drop") { |
| 119 | + _action = ACTION_DROP; |
| 120 | + } else if (value == "mark") { |
| 121 | + _action = ACTION_MARK; |
| 122 | + } else { |
| 123 | + _errorMsg = "Invalid action, options are: [disabled | drop | mark]"; |
| 124 | + return PARSE_ERROR; |
| 125 | + } |
| 126 | + } else if (name == "help") { |
| 127 | + cout << _cmdParser.GetUsage() << endl; |
| 128 | + return PARSE_OK_WITH_EXIT; |
| 129 | + } else if (name == "version") { |
| 130 | + print_version_string("mstcongestion", NULL); |
| 131 | + return PARSE_OK_WITH_EXIT; |
| 132 | + } else if (name == "query") { |
| 133 | + _ops++; |
| 134 | + _query = true; |
| 135 | + } else { |
| 136 | + _errorMsg = "Invalid option: " + name; |
| 137 | + return PARSE_ERROR; |
| 138 | + } |
| 139 | + return PARSE_OK; |
| 140 | +} |
| 141 | + |
| 142 | +bool CongestionUI::run(int argc, char** argv) |
| 143 | +{ |
| 144 | + ParseStatus rc = _cmdParser.ParseOptions(argc, argv); |
| 145 | + if (rc == PARSE_OK_WITH_EXIT) { |
| 146 | + return true; |
| 147 | + } else if (rc == PARSE_ERROR) { |
| 148 | + if (_errorMsg == "") { |
| 149 | + _errorMsg = "Failed while parsing the arguments."; |
| 150 | + } |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + if (_devname == "") { |
| 155 | + _errorMsg = "Missing device."; |
| 156 | + return false; |
| 157 | + } |
| 158 | + _mf = mopen(_devname.c_str()); |
| 159 | + if (!_mf) { |
| 160 | + _errorMsg = "Failed to open device: " + _devname; |
| 161 | + return false; |
| 162 | + } |
| 163 | + struct tools_open_mcam mcam; |
| 164 | + memset(&mcam, 0, sizeof(mcam)); |
| 165 | + reg_access_status_t status = reg_access_mcam(_mf, REG_ACCESS_METHOD_GET, &mcam); |
| 166 | + if (status) { |
| 167 | + _errorMsg = "Failed to get device capabilities!"; |
| 168 | + return false; |
| 169 | + } |
| 170 | + u_int8_t caps = mcam.mng_feature_cap_mask[14]; |
| 171 | + _dynamicSupp = caps & 0x2; |
| 172 | + _markCqeSupp = caps & 0x4; |
| 173 | + _markCnpSupp = caps & 0x8; |
| 174 | + if (_ops != 1) { |
| 175 | + _errorMsg = "Please choose one operation."; |
| 176 | + return false; |
| 177 | + } |
| 178 | + if (_mode != MODE_NA) { |
| 179 | + return setMode(_mode); |
| 180 | + } else if (_action != ACTION_NA) { |
| 181 | + return setAction(_action); |
| 182 | + } else if (_query) { |
| 183 | + return query(); |
| 184 | + } |
| 185 | + return true; |
| 186 | +} |
| 187 | + |
| 188 | + |
| 189 | +bool CongestionUI::setMode(cong_mode_t mode) |
| 190 | +{ |
| 191 | + if (!_dynamicSupp) { |
| 192 | + _errorMsg = "Dynamic overflow is not supported."; |
| 193 | + return false; |
| 194 | + } |
| 195 | + _successMsg = "Mode was set successfully."; |
| 196 | + struct reg_access_hca_mpegc_reg mpegc; |
| 197 | + memset(&mpegc, 0, sizeof (mpegc)); |
| 198 | + mpegc.tx_overflow_sense = (int)mode; |
| 199 | + mpegc.field_select |= BIT_TX_SENSE; |
| 200 | + reg_access_status_t status = reg_access_mpegc(_mf, REG_ACCESS_METHOD_SET, &mpegc); |
| 201 | + if (status) { |
| 202 | + _errorMsg = string(reg_access_err2str(status)); |
| 203 | + return false; |
| 204 | + } |
| 205 | + return true; |
| 206 | +} |
| 207 | + |
| 208 | +bool CongestionUI::setAction(cong_action_t action) |
| 209 | +{ |
| 210 | + _successMsg = "Action was set successfully."; |
| 211 | + struct reg_access_hca_mpegc_reg mpegc; |
| 212 | + memset(&mpegc, 0, sizeof (mpegc)); |
| 213 | + mpegc.tx_lossy_overflow_oper = (int)action; |
| 214 | + mpegc.field_select |= BIT_TX_LOSSY_OPER; |
| 215 | + if (action == ACTION_MARK) { |
| 216 | + if (_markCqeSupp) { |
| 217 | + mpegc.field_select |= BIT_MARK_TX_CQE; |
| 218 | + mpegc.mark_cqe = 1; |
| 219 | + } |
| 220 | + if (_markCnpSupp) { |
| 221 | + mpegc.field_select |= BIT_MARK_TX_CNP; |
| 222 | + mpegc.mark_cnp = 1; |
| 223 | + } |
| 224 | + } |
| 225 | + reg_access_status_t status = reg_access_mpegc(_mf, REG_ACCESS_METHOD_SET, &mpegc); |
| 226 | + if (status) { |
| 227 | + _errorMsg = string(reg_access_err2str(status)); |
| 228 | + return false; |
| 229 | + } |
| 230 | + return true; |
| 231 | +} |
| 232 | + |
| 233 | +bool CongestionUI::query() |
| 234 | +{ |
| 235 | + struct reg_access_hca_mpegc_reg mpegc; |
| 236 | + memset(&mpegc, 0, sizeof (mpegc)); |
| 237 | + reg_access_status_t status = reg_access_mpegc(_mf, REG_ACCESS_METHOD_GET, &mpegc); |
| 238 | + if (status) { |
| 239 | + _errorMsg = string(reg_access_err2str(status)); |
| 240 | + return false; |
| 241 | + } |
| 242 | + printf("Action: %s\nMode: %s\n", getActionString((cong_action_t)mpegc.tx_lossy_overflow_oper).c_str(), |
| 243 | + getModeString((cong_mode_t)mpegc.tx_overflow_sense).c_str()); |
| 244 | + return true; |
| 245 | +} |
| 246 | + |
| 247 | + |
| 248 | +string CongestionUI::getModeString(cong_mode_t mode) |
| 249 | +{ |
| 250 | + switch (mode) { |
| 251 | + case MODE_AGGRESSIVE: |
| 252 | + return "Aggressive"; |
| 253 | + case MODE_DYNAMIC: |
| 254 | + return "Dynamic"; |
| 255 | + default: |
| 256 | + return "N/A"; |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +string CongestionUI::getActionString(cong_action_t action) |
| 261 | +{ |
| 262 | + switch (action) { |
| 263 | + case ACTION_DISABLED: |
| 264 | + return "Disabled"; |
| 265 | + case ACTION_DROP: |
| 266 | + return "Drop"; |
| 267 | + case ACTION_MARK: |
| 268 | + return "Mark"; |
| 269 | + default: |
| 270 | + return "N/A"; |
| 271 | + } |
| 272 | +} |
0 commit comments