datapath.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475
  1. /*
  2. * Copyright (c) 2007-2014 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <net/genetlink.h>
  49. #include <net/net_namespace.h>
  50. #include <net/netns/generic.h>
  51. #include "datapath.h"
  52. #include "flow.h"
  53. #include "flow_table.h"
  54. #include "flow_netlink.h"
  55. #include "meter.h"
  56. #include "vport-internal_dev.h"
  57. #include "vport-netdev.h"
  58. unsigned int ovs_net_id __read_mostly;
  59. static struct genl_family dp_packet_genl_family;
  60. static struct genl_family dp_flow_genl_family;
  61. static struct genl_family dp_datapath_genl_family;
  62. static const struct nla_policy flow_policy[];
  63. static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
  64. .name = OVS_FLOW_MCGROUP,
  65. };
  66. static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  67. .name = OVS_DATAPATH_MCGROUP,
  68. };
  69. static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
  70. .name = OVS_VPORT_MCGROUP,
  71. };
  72. /* Check if need to build a reply message.
  73. * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
  74. static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
  75. unsigned int group)
  76. {
  77. return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
  78. genl_has_listeners(family, genl_info_net(info), group);
  79. }
  80. static void ovs_notify(struct genl_family *family,
  81. struct sk_buff *skb, struct genl_info *info)
  82. {
  83. genl_notify(family, skb, info, 0, GFP_KERNEL);
  84. }
  85. /**
  86. * DOC: Locking:
  87. *
  88. * All writes e.g. Writes to device state (add/remove datapath, port, set
  89. * operations on vports, etc.), Writes to other state (flow table
  90. * modifications, set miscellaneous datapath parameters, etc.) are protected
  91. * by ovs_lock.
  92. *
  93. * Reads are protected by RCU.
  94. *
  95. * There are a few special cases (mostly stats) that have their own
  96. * synchronization but they nest under all of above and don't interact with
  97. * each other.
  98. *
  99. * The RTNL lock nests inside ovs_mutex.
  100. */
  101. static DEFINE_MUTEX(ovs_mutex);
  102. void ovs_lock(void)
  103. {
  104. mutex_lock(&ovs_mutex);
  105. }
  106. void ovs_unlock(void)
  107. {
  108. mutex_unlock(&ovs_mutex);
  109. }
  110. #ifdef CONFIG_LOCKDEP
  111. int lockdep_ovsl_is_held(void)
  112. {
  113. if (debug_locks)
  114. return lockdep_is_held(&ovs_mutex);
  115. else
  116. return 1;
  117. }
  118. #endif
  119. static struct vport *new_vport(const struct vport_parms *);
  120. static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
  121. const struct sw_flow_key *,
  122. const struct dp_upcall_info *,
  123. uint32_t cutlen);
  124. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
  125. const struct sw_flow_key *,
  126. const struct dp_upcall_info *,
  127. uint32_t cutlen);
  128. /* Must be called with rcu_read_lock or ovs_mutex. */
  129. const char *ovs_dp_name(const struct datapath *dp)
  130. {
  131. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  132. return ovs_vport_name(vport);
  133. }
  134. static int get_dpifindex(const struct datapath *dp)
  135. {
  136. struct vport *local;
  137. int ifindex;
  138. rcu_read_lock();
  139. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  140. if (local)
  141. ifindex = local->dev->ifindex;
  142. else
  143. ifindex = 0;
  144. rcu_read_unlock();
  145. return ifindex;
  146. }
  147. static void destroy_dp_rcu(struct rcu_head *rcu)
  148. {
  149. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  150. ovs_flow_tbl_destroy(&dp->table);
  151. free_percpu(dp->stats_percpu);
  152. kfree(dp->ports);
  153. ovs_meters_exit(dp);
  154. kfree(dp);
  155. }
  156. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  157. u16 port_no)
  158. {
  159. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  160. }
  161. /* Called with ovs_mutex or RCU read lock. */
  162. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  163. {
  164. struct vport *vport;
  165. struct hlist_head *head;
  166. head = vport_hash_bucket(dp, port_no);
  167. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  168. if (vport->port_no == port_no)
  169. return vport;
  170. }
  171. return NULL;
  172. }
  173. /* Called with ovs_mutex. */
  174. static struct vport *new_vport(const struct vport_parms *parms)
  175. {
  176. struct vport *vport;
  177. vport = ovs_vport_add(parms);
  178. if (!IS_ERR(vport)) {
  179. struct datapath *dp = parms->dp;
  180. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  181. hlist_add_head_rcu(&vport->dp_hash_node, head);
  182. }
  183. return vport;
  184. }
  185. void ovs_dp_detach_port(struct vport *p)
  186. {
  187. ASSERT_OVSL();
  188. /* First drop references to device. */
  189. hlist_del_rcu(&p->dp_hash_node);
  190. /* Then destroy it. */
  191. ovs_vport_del(p);
  192. }
  193. /* Must be called with rcu_read_lock. */
  194. void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
  195. {
  196. const struct vport *p = OVS_CB(skb)->input_vport;
  197. struct datapath *dp = p->dp;
  198. struct sw_flow *flow;
  199. struct sw_flow_actions *sf_acts;
  200. struct dp_stats_percpu *stats;
  201. u64 *stats_counter;
  202. u32 n_mask_hit;
  203. stats = this_cpu_ptr(dp->stats_percpu);
  204. /* Look up flow. */
  205. flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
  206. if (unlikely(!flow)) {
  207. struct dp_upcall_info upcall;
  208. int error;
  209. memset(&upcall, 0, sizeof(upcall));
  210. upcall.cmd = OVS_PACKET_CMD_MISS;
  211. upcall.portid = ovs_vport_find_upcall_portid(p, skb);
  212. upcall.mru = OVS_CB(skb)->mru;
  213. error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
  214. if (unlikely(error))
  215. kfree_skb(skb);
  216. else
  217. consume_skb(skb);
  218. stats_counter = &stats->n_missed;
  219. goto out;
  220. }
  221. ovs_flow_stats_update(flow, key->tp.flags, skb);
  222. sf_acts = rcu_dereference(flow->sf_acts);
  223. ovs_execute_actions(dp, skb, sf_acts, key);
  224. stats_counter = &stats->n_hit;
  225. out:
  226. /* Update datapath statistics. */
  227. u64_stats_update_begin(&stats->syncp);
  228. (*stats_counter)++;
  229. stats->n_mask_hit += n_mask_hit;
  230. u64_stats_update_end(&stats->syncp);
  231. }
  232. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  233. const struct sw_flow_key *key,
  234. const struct dp_upcall_info *upcall_info,
  235. uint32_t cutlen)
  236. {
  237. struct dp_stats_percpu *stats;
  238. int err;
  239. if (upcall_info->portid == 0) {
  240. err = -ENOTCONN;
  241. goto err;
  242. }
  243. if (!skb_is_gso(skb))
  244. err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
  245. else
  246. err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
  247. if (err)
  248. goto err;
  249. return 0;
  250. err:
  251. stats = this_cpu_ptr(dp->stats_percpu);
  252. u64_stats_update_begin(&stats->syncp);
  253. stats->n_lost++;
  254. u64_stats_update_end(&stats->syncp);
  255. return err;
  256. }
  257. static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
  258. const struct sw_flow_key *key,
  259. const struct dp_upcall_info *upcall_info,
  260. uint32_t cutlen)
  261. {
  262. unsigned int gso_type = skb_shinfo(skb)->gso_type;
  263. struct sw_flow_key later_key;
  264. struct sk_buff *segs, *nskb;
  265. int err;
  266. BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_SGO_CB_OFFSET);
  267. segs = __skb_gso_segment(skb, NETIF_F_SG, false);
  268. if (IS_ERR(segs))
  269. return PTR_ERR(segs);
  270. if (segs == NULL)
  271. return -EINVAL;
  272. if (gso_type & SKB_GSO_UDP) {
  273. /* The initial flow key extracted by ovs_flow_key_extract()
  274. * in this case is for a first fragment, so we need to
  275. * properly mark later fragments.
  276. */
  277. later_key = *key;
  278. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  279. }
  280. /* Queue all of the segments. */
  281. skb = segs;
  282. do {
  283. if (gso_type & SKB_GSO_UDP && skb != segs)
  284. key = &later_key;
  285. err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
  286. if (err)
  287. break;
  288. } while ((skb = skb->next));
  289. /* Free all of the segments. */
  290. skb = segs;
  291. do {
  292. nskb = skb->next;
  293. if (err)
  294. kfree_skb(skb);
  295. else
  296. consume_skb(skb);
  297. } while ((skb = nskb));
  298. return err;
  299. }
  300. static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
  301. unsigned int hdrlen, int actions_attrlen)
  302. {
  303. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  304. + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
  305. + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
  306. + nla_total_size(sizeof(unsigned int)); /* OVS_PACKET_ATTR_LEN */
  307. /* OVS_PACKET_ATTR_USERDATA */
  308. if (upcall_info->userdata)
  309. size += NLA_ALIGN(upcall_info->userdata->nla_len);
  310. /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
  311. if (upcall_info->egress_tun_info)
  312. size += nla_total_size(ovs_tun_key_attr_size());
  313. /* OVS_PACKET_ATTR_ACTIONS */
  314. if (upcall_info->actions_len)
  315. size += nla_total_size(actions_attrlen);
  316. /* OVS_PACKET_ATTR_MRU */
  317. if (upcall_info->mru)
  318. size += nla_total_size(sizeof(upcall_info->mru));
  319. return size;
  320. }
  321. static void pad_packet(struct datapath *dp, struct sk_buff *skb)
  322. {
  323. if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
  324. size_t plen = NLA_ALIGN(skb->len) - skb->len;
  325. if (plen > 0)
  326. skb_put_zero(skb, plen);
  327. }
  328. }
  329. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
  330. const struct sw_flow_key *key,
  331. const struct dp_upcall_info *upcall_info,
  332. uint32_t cutlen)
  333. {
  334. struct ovs_header *upcall;
  335. struct sk_buff *nskb = NULL;
  336. struct sk_buff *user_skb = NULL; /* to be queued to userspace */
  337. struct nlattr *nla;
  338. size_t len;
  339. unsigned int hlen;
  340. int err, dp_ifindex;
  341. dp_ifindex = get_dpifindex(dp);
  342. if (!dp_ifindex)
  343. return -ENODEV;
  344. if (skb_vlan_tag_present(skb)) {
  345. nskb = skb_clone(skb, GFP_ATOMIC);
  346. if (!nskb)
  347. return -ENOMEM;
  348. nskb = __vlan_hwaccel_push_inside(nskb);
  349. if (!nskb)
  350. return -ENOMEM;
  351. skb = nskb;
  352. }
  353. if (nla_attr_size(skb->len) > USHRT_MAX) {
  354. err = -EFBIG;
  355. goto out;
  356. }
  357. /* Complete checksum if needed */
  358. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  359. (err = skb_csum_hwoffload_help(skb, 0)))
  360. goto out;
  361. /* Older versions of OVS user space enforce alignment of the last
  362. * Netlink attribute to NLA_ALIGNTO which would require extensive
  363. * padding logic. Only perform zerocopy if padding is not required.
  364. */
  365. if (dp->user_features & OVS_DP_F_UNALIGNED)
  366. hlen = skb_zerocopy_headlen(skb);
  367. else
  368. hlen = skb->len;
  369. len = upcall_msg_size(upcall_info, hlen - cutlen,
  370. OVS_CB(skb)->acts_origlen);
  371. user_skb = genlmsg_new(len, GFP_ATOMIC);
  372. if (!user_skb) {
  373. err = -ENOMEM;
  374. goto out;
  375. }
  376. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  377. 0, upcall_info->cmd);
  378. upcall->dp_ifindex = dp_ifindex;
  379. err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
  380. BUG_ON(err);
  381. if (upcall_info->userdata)
  382. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  383. nla_len(upcall_info->userdata),
  384. nla_data(upcall_info->userdata));
  385. if (upcall_info->egress_tun_info) {
  386. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
  387. err = ovs_nla_put_tunnel_info(user_skb,
  388. upcall_info->egress_tun_info);
  389. BUG_ON(err);
  390. nla_nest_end(user_skb, nla);
  391. }
  392. if (upcall_info->actions_len) {
  393. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS);
  394. err = ovs_nla_put_actions(upcall_info->actions,
  395. upcall_info->actions_len,
  396. user_skb);
  397. if (!err)
  398. nla_nest_end(user_skb, nla);
  399. else
  400. nla_nest_cancel(user_skb, nla);
  401. }
  402. /* Add OVS_PACKET_ATTR_MRU */
  403. if (upcall_info->mru) {
  404. if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
  405. upcall_info->mru)) {
  406. err = -ENOBUFS;
  407. goto out;
  408. }
  409. pad_packet(dp, user_skb);
  410. }
  411. /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
  412. if (cutlen > 0) {
  413. if (nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN,
  414. skb->len)) {
  415. err = -ENOBUFS;
  416. goto out;
  417. }
  418. pad_packet(dp, user_skb);
  419. }
  420. /* Only reserve room for attribute header, packet data is added
  421. * in skb_zerocopy() */
  422. if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
  423. err = -ENOBUFS;
  424. goto out;
  425. }
  426. nla->nla_len = nla_attr_size(skb->len - cutlen);
  427. err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
  428. if (err)
  429. goto out;
  430. /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
  431. pad_packet(dp, user_skb);
  432. ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
  433. err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
  434. user_skb = NULL;
  435. out:
  436. if (err)
  437. skb_tx_error(skb);
  438. kfree_skb(user_skb);
  439. kfree_skb(nskb);
  440. return err;
  441. }
  442. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  443. {
  444. struct ovs_header *ovs_header = info->userhdr;
  445. struct net *net = sock_net(skb->sk);
  446. struct nlattr **a = info->attrs;
  447. struct sw_flow_actions *acts;
  448. struct sk_buff *packet;
  449. struct sw_flow *flow;
  450. struct sw_flow_actions *sf_acts;
  451. struct datapath *dp;
  452. struct vport *input_vport;
  453. u16 mru = 0;
  454. int len;
  455. int err;
  456. bool log = !a[OVS_PACKET_ATTR_PROBE];
  457. err = -EINVAL;
  458. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  459. !a[OVS_PACKET_ATTR_ACTIONS])
  460. goto err;
  461. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  462. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  463. err = -ENOMEM;
  464. if (!packet)
  465. goto err;
  466. skb_reserve(packet, NET_IP_ALIGN);
  467. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  468. /* Set packet's mru */
  469. if (a[OVS_PACKET_ATTR_MRU]) {
  470. mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
  471. packet->ignore_df = 1;
  472. }
  473. OVS_CB(packet)->mru = mru;
  474. /* Build an sw_flow for sending this packet. */
  475. flow = ovs_flow_alloc();
  476. err = PTR_ERR(flow);
  477. if (IS_ERR(flow))
  478. goto err_kfree_skb;
  479. err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
  480. packet, &flow->key, log);
  481. if (err)
  482. goto err_flow_free;
  483. err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
  484. &flow->key, &acts, log);
  485. if (err)
  486. goto err_flow_free;
  487. rcu_assign_pointer(flow->sf_acts, acts);
  488. packet->priority = flow->key.phy.priority;
  489. packet->mark = flow->key.phy.skb_mark;
  490. rcu_read_lock();
  491. dp = get_dp_rcu(net, ovs_header->dp_ifindex);
  492. err = -ENODEV;
  493. if (!dp)
  494. goto err_unlock;
  495. input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
  496. if (!input_vport)
  497. input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
  498. if (!input_vport)
  499. goto err_unlock;
  500. packet->dev = input_vport->dev;
  501. OVS_CB(packet)->input_vport = input_vport;
  502. sf_acts = rcu_dereference(flow->sf_acts);
  503. local_bh_disable();
  504. err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
  505. local_bh_enable();
  506. rcu_read_unlock();
  507. ovs_flow_free(flow, false);
  508. return err;
  509. err_unlock:
  510. rcu_read_unlock();
  511. err_flow_free:
  512. ovs_flow_free(flow, false);
  513. err_kfree_skb:
  514. kfree_skb(packet);
  515. err:
  516. return err;
  517. }
  518. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  519. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  520. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  521. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  522. [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
  523. [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
  524. };
  525. static const struct genl_ops dp_packet_genl_ops[] = {
  526. { .cmd = OVS_PACKET_CMD_EXECUTE,
  527. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  528. .policy = packet_policy,
  529. .doit = ovs_packet_cmd_execute
  530. }
  531. };
  532. static struct genl_family dp_packet_genl_family __ro_after_init = {
  533. .hdrsize = sizeof(struct ovs_header),
  534. .name = OVS_PACKET_FAMILY,
  535. .version = OVS_PACKET_VERSION,
  536. .maxattr = OVS_PACKET_ATTR_MAX,
  537. .netnsok = true,
  538. .parallel_ops = true,
  539. .ops = dp_packet_genl_ops,
  540. .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
  541. .module = THIS_MODULE,
  542. };
  543. static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
  544. struct ovs_dp_megaflow_stats *mega_stats)
  545. {
  546. int i;
  547. memset(mega_stats, 0, sizeof(*mega_stats));
  548. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  549. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  550. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  551. for_each_possible_cpu(i) {
  552. const struct dp_stats_percpu *percpu_stats;
  553. struct dp_stats_percpu local_stats;
  554. unsigned int start;
  555. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  556. do {
  557. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  558. local_stats = *percpu_stats;
  559. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  560. stats->n_hit += local_stats.n_hit;
  561. stats->n_missed += local_stats.n_missed;
  562. stats->n_lost += local_stats.n_lost;
  563. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  564. }
  565. }
  566. static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
  567. {
  568. return ovs_identifier_is_ufid(sfid) &&
  569. !(ufid_flags & OVS_UFID_F_OMIT_KEY);
  570. }
  571. static bool should_fill_mask(uint32_t ufid_flags)
  572. {
  573. return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
  574. }
  575. static bool should_fill_actions(uint32_t ufid_flags)
  576. {
  577. return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
  578. }
  579. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
  580. const struct sw_flow_id *sfid,
  581. uint32_t ufid_flags)
  582. {
  583. size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
  584. /* OVS_FLOW_ATTR_UFID */
  585. if (sfid && ovs_identifier_is_ufid(sfid))
  586. len += nla_total_size(sfid->ufid_len);
  587. /* OVS_FLOW_ATTR_KEY */
  588. if (!sfid || should_fill_key(sfid, ufid_flags))
  589. len += nla_total_size(ovs_key_attr_size());
  590. /* OVS_FLOW_ATTR_MASK */
  591. if (should_fill_mask(ufid_flags))
  592. len += nla_total_size(ovs_key_attr_size());
  593. /* OVS_FLOW_ATTR_ACTIONS */
  594. if (should_fill_actions(ufid_flags))
  595. len += nla_total_size(acts->orig_len);
  596. return len
  597. + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  598. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  599. + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
  600. }
  601. /* Called with ovs_mutex or RCU read lock. */
  602. static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
  603. struct sk_buff *skb)
  604. {
  605. struct ovs_flow_stats stats;
  606. __be16 tcp_flags;
  607. unsigned long used;
  608. ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
  609. if (used &&
  610. nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
  611. OVS_FLOW_ATTR_PAD))
  612. return -EMSGSIZE;
  613. if (stats.n_packets &&
  614. nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
  615. sizeof(struct ovs_flow_stats), &stats,
  616. OVS_FLOW_ATTR_PAD))
  617. return -EMSGSIZE;
  618. if ((u8)ntohs(tcp_flags) &&
  619. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
  620. return -EMSGSIZE;
  621. return 0;
  622. }
  623. /* Called with ovs_mutex or RCU read lock. */
  624. static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
  625. struct sk_buff *skb, int skb_orig_len)
  626. {
  627. struct nlattr *start;
  628. int err;
  629. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  630. * this is the first flow to be dumped into 'skb'. This is unusual for
  631. * Netlink but individual action lists can be longer than
  632. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  633. * The userspace caller can always fetch the actions separately if it
  634. * really wants them. (Most userspace callers in fact don't care.)
  635. *
  636. * This can only fail for dump operations because the skb is always
  637. * properly sized for single flows.
  638. */
  639. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  640. if (start) {
  641. const struct sw_flow_actions *sf_acts;
  642. sf_acts = rcu_dereference_ovsl(flow->sf_acts);
  643. err = ovs_nla_put_actions(sf_acts->actions,
  644. sf_acts->actions_len, skb);
  645. if (!err)
  646. nla_nest_end(skb, start);
  647. else {
  648. if (skb_orig_len)
  649. return err;
  650. nla_nest_cancel(skb, start);
  651. }
  652. } else if (skb_orig_len) {
  653. return -EMSGSIZE;
  654. }
  655. return 0;
  656. }
  657. /* Called with ovs_mutex or RCU read lock. */
  658. static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
  659. struct sk_buff *skb, u32 portid,
  660. u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
  661. {
  662. const int skb_orig_len = skb->len;
  663. struct ovs_header *ovs_header;
  664. int err;
  665. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
  666. flags, cmd);
  667. if (!ovs_header)
  668. return -EMSGSIZE;
  669. ovs_header->dp_ifindex = dp_ifindex;
  670. err = ovs_nla_put_identifier(flow, skb);
  671. if (err)
  672. goto error;
  673. if (should_fill_key(&flow->id, ufid_flags)) {
  674. err = ovs_nla_put_masked_key(flow, skb);
  675. if (err)
  676. goto error;
  677. }
  678. if (should_fill_mask(ufid_flags)) {
  679. err = ovs_nla_put_mask(flow, skb);
  680. if (err)
  681. goto error;
  682. }
  683. err = ovs_flow_cmd_fill_stats(flow, skb);
  684. if (err)
  685. goto error;
  686. if (should_fill_actions(ufid_flags)) {
  687. err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
  688. if (err)
  689. goto error;
  690. }
  691. genlmsg_end(skb, ovs_header);
  692. return 0;
  693. error:
  694. genlmsg_cancel(skb, ovs_header);
  695. return err;
  696. }
  697. /* May not be called with RCU read lock. */
  698. static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
  699. const struct sw_flow_id *sfid,
  700. struct genl_info *info,
  701. bool always,
  702. uint32_t ufid_flags)
  703. {
  704. struct sk_buff *skb;
  705. size_t len;
  706. if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
  707. return NULL;
  708. len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
  709. skb = genlmsg_new(len, GFP_KERNEL);
  710. if (!skb)
  711. return ERR_PTR(-ENOMEM);
  712. return skb;
  713. }
  714. /* Called with ovs_mutex. */
  715. static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
  716. int dp_ifindex,
  717. struct genl_info *info, u8 cmd,
  718. bool always, u32 ufid_flags)
  719. {
  720. struct sk_buff *skb;
  721. int retval;
  722. skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
  723. &flow->id, info, always, ufid_flags);
  724. if (IS_ERR_OR_NULL(skb))
  725. return skb;
  726. retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
  727. info->snd_portid, info->snd_seq, 0,
  728. cmd, ufid_flags);
  729. BUG_ON(retval < 0);
  730. return skb;
  731. }
  732. static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
  733. {
  734. struct net *net = sock_net(skb->sk);
  735. struct nlattr **a = info->attrs;
  736. struct ovs_header *ovs_header = info->userhdr;
  737. struct sw_flow *flow = NULL, *new_flow;
  738. struct sw_flow_mask mask;
  739. struct sk_buff *reply;
  740. struct datapath *dp;
  741. struct sw_flow_actions *acts;
  742. struct sw_flow_match match;
  743. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  744. int error;
  745. bool log = !a[OVS_FLOW_ATTR_PROBE];
  746. /* Must have key and actions. */
  747. error = -EINVAL;
  748. if (!a[OVS_FLOW_ATTR_KEY]) {
  749. OVS_NLERR(log, "Flow key attr not present in new flow.");
  750. goto error;
  751. }
  752. if (!a[OVS_FLOW_ATTR_ACTIONS]) {
  753. OVS_NLERR(log, "Flow actions attr not present in new flow.");
  754. goto error;
  755. }
  756. /* Most of the time we need to allocate a new flow, do it before
  757. * locking.
  758. */
  759. new_flow = ovs_flow_alloc();
  760. if (IS_ERR(new_flow)) {
  761. error = PTR_ERR(new_flow);
  762. goto error;
  763. }
  764. /* Extract key. */
  765. ovs_match_init(&match, &new_flow->key, false, &mask);
  766. error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
  767. a[OVS_FLOW_ATTR_MASK], log);
  768. if (error)
  769. goto err_kfree_flow;
  770. /* Extract flow identifier. */
  771. error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
  772. &new_flow->key, log);
  773. if (error)
  774. goto err_kfree_flow;
  775. /* unmasked key is needed to match when ufid is not used. */
  776. if (ovs_identifier_is_key(&new_flow->id))
  777. match.key = new_flow->id.unmasked_key;
  778. ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask);
  779. /* Validate actions. */
  780. error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
  781. &new_flow->key, &acts, log);
  782. if (error) {
  783. OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
  784. goto err_kfree_flow;
  785. }
  786. reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
  787. ufid_flags);
  788. if (IS_ERR(reply)) {
  789. error = PTR_ERR(reply);
  790. goto err_kfree_acts;
  791. }
  792. ovs_lock();
  793. dp = get_dp(net, ovs_header->dp_ifindex);
  794. if (unlikely(!dp)) {
  795. error = -ENODEV;
  796. goto err_unlock_ovs;
  797. }
  798. /* Check if this is a duplicate flow */
  799. if (ovs_identifier_is_ufid(&new_flow->id))
  800. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
  801. if (!flow)
  802. flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key);
  803. if (likely(!flow)) {
  804. rcu_assign_pointer(new_flow->sf_acts, acts);
  805. /* Put flow in bucket. */
  806. error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
  807. if (unlikely(error)) {
  808. acts = NULL;
  809. goto err_unlock_ovs;
  810. }
  811. if (unlikely(reply)) {
  812. error = ovs_flow_cmd_fill_info(new_flow,
  813. ovs_header->dp_ifindex,
  814. reply, info->snd_portid,
  815. info->snd_seq, 0,
  816. OVS_FLOW_CMD_NEW,
  817. ufid_flags);
  818. BUG_ON(error < 0);
  819. }
  820. ovs_unlock();
  821. } else {
  822. struct sw_flow_actions *old_acts;
  823. /* Bail out if we're not allowed to modify an existing flow.
  824. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  825. * because Generic Netlink treats the latter as a dump
  826. * request. We also accept NLM_F_EXCL in case that bug ever
  827. * gets fixed.
  828. */
  829. if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
  830. | NLM_F_EXCL))) {
  831. error = -EEXIST;
  832. goto err_unlock_ovs;
  833. }
  834. /* The flow identifier has to be the same for flow updates.
  835. * Look for any overlapping flow.
  836. */
  837. if (unlikely(!ovs_flow_cmp(flow, &match))) {
  838. if (ovs_identifier_is_key(&flow->id))
  839. flow = ovs_flow_tbl_lookup_exact(&dp->table,
  840. &match);
  841. else /* UFID matches but key is different */
  842. flow = NULL;
  843. if (!flow) {
  844. error = -ENOENT;
  845. goto err_unlock_ovs;
  846. }
  847. }
  848. /* Update actions. */
  849. old_acts = ovsl_dereference(flow->sf_acts);
  850. rcu_assign_pointer(flow->sf_acts, acts);
  851. if (unlikely(reply)) {
  852. error = ovs_flow_cmd_fill_info(flow,
  853. ovs_header->dp_ifindex,
  854. reply, info->snd_portid,
  855. info->snd_seq, 0,
  856. OVS_FLOW_CMD_NEW,
  857. ufid_flags);
  858. BUG_ON(error < 0);
  859. }
  860. ovs_unlock();
  861. ovs_nla_free_flow_actions_rcu(old_acts);
  862. ovs_flow_free(new_flow, false);
  863. }
  864. if (reply)
  865. ovs_notify(&dp_flow_genl_family, reply, info);
  866. return 0;
  867. err_unlock_ovs:
  868. ovs_unlock();
  869. kfree_skb(reply);
  870. err_kfree_acts:
  871. ovs_nla_free_flow_actions(acts);
  872. err_kfree_flow:
  873. ovs_flow_free(new_flow, false);
  874. error:
  875. return error;
  876. }
  877. /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
  878. static struct sw_flow_actions *get_flow_actions(struct net *net,
  879. const struct nlattr *a,
  880. const struct sw_flow_key *key,
  881. const struct sw_flow_mask *mask,
  882. bool log)
  883. {
  884. struct sw_flow_actions *acts;
  885. struct sw_flow_key masked_key;
  886. int error;
  887. ovs_flow_mask_key(&masked_key, key, true, mask);
  888. error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
  889. if (error) {
  890. OVS_NLERR(log,
  891. "Actions may not be safe on all matching packets");
  892. return ERR_PTR(error);
  893. }
  894. return acts;
  895. }
  896. /* Factor out match-init and action-copy to avoid
  897. * "Wframe-larger-than=1024" warning. Because mask is only
  898. * used to get actions, we new a function to save some
  899. * stack space.
  900. *
  901. * If there are not key and action attrs, we return 0
  902. * directly. In the case, the caller will also not use the
  903. * match as before. If there is action attr, we try to get
  904. * actions and save them to *acts. Before returning from
  905. * the function, we reset the match->mask pointer. Because
  906. * we should not to return match object with dangling reference
  907. * to mask.
  908. * */
  909. static int ovs_nla_init_match_and_action(struct net *net,
  910. struct sw_flow_match *match,
  911. struct sw_flow_key *key,
  912. struct nlattr **a,
  913. struct sw_flow_actions **acts,
  914. bool log)
  915. {
  916. struct sw_flow_mask mask;
  917. int error = 0;
  918. if (a[OVS_FLOW_ATTR_KEY]) {
  919. ovs_match_init(match, key, true, &mask);
  920. error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
  921. a[OVS_FLOW_ATTR_MASK], log);
  922. if (error)
  923. goto error;
  924. }
  925. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  926. if (!a[OVS_FLOW_ATTR_KEY]) {
  927. OVS_NLERR(log,
  928. "Flow key attribute not present in set flow.");
  929. error = -EINVAL;
  930. goto error;
  931. }
  932. *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
  933. &mask, log);
  934. if (IS_ERR(*acts)) {
  935. error = PTR_ERR(*acts);
  936. goto error;
  937. }
  938. }
  939. /* On success, error is 0. */
  940. error:
  941. match->mask = NULL;
  942. return error;
  943. }
  944. static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
  945. {
  946. struct net *net = sock_net(skb->sk);
  947. struct nlattr **a = info->attrs;
  948. struct ovs_header *ovs_header = info->userhdr;
  949. struct sw_flow_key key;
  950. struct sw_flow *flow;
  951. struct sk_buff *reply = NULL;
  952. struct datapath *dp;
  953. struct sw_flow_actions *old_acts = NULL, *acts = NULL;
  954. struct sw_flow_match match;
  955. struct sw_flow_id sfid;
  956. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  957. int error = 0;
  958. bool log = !a[OVS_FLOW_ATTR_PROBE];
  959. bool ufid_present;
  960. ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
  961. if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
  962. OVS_NLERR(log,
  963. "Flow set message rejected, Key attribute missing.");
  964. return -EINVAL;
  965. }
  966. error = ovs_nla_init_match_and_action(net, &match, &key, a,
  967. &acts, log);
  968. if (error)
  969. goto error;
  970. if (acts) {
  971. /* Can allocate before locking if have acts. */
  972. reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
  973. ufid_flags);
  974. if (IS_ERR(reply)) {
  975. error = PTR_ERR(reply);
  976. goto err_kfree_acts;
  977. }
  978. }
  979. ovs_lock();
  980. dp = get_dp(net, ovs_header->dp_ifindex);
  981. if (unlikely(!dp)) {
  982. error = -ENODEV;
  983. goto err_unlock_ovs;
  984. }
  985. /* Check that the flow exists. */
  986. if (ufid_present)
  987. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
  988. else
  989. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  990. if (unlikely(!flow)) {
  991. error = -ENOENT;
  992. goto err_unlock_ovs;
  993. }
  994. /* Update actions, if present. */
  995. if (likely(acts)) {
  996. old_acts = ovsl_dereference(flow->sf_acts);
  997. rcu_assign_pointer(flow->sf_acts, acts);
  998. if (unlikely(reply)) {
  999. error = ovs_flow_cmd_fill_info(flow,
  1000. ovs_header->dp_ifindex,
  1001. reply, info->snd_portid,
  1002. info->snd_seq, 0,
  1003. OVS_FLOW_CMD_SET,
  1004. ufid_flags);
  1005. BUG_ON(error < 0);
  1006. }
  1007. } else {
  1008. /* Could not alloc without acts before locking. */
  1009. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
  1010. info, OVS_FLOW_CMD_SET, false,
  1011. ufid_flags);
  1012. if (IS_ERR(reply)) {
  1013. error = PTR_ERR(reply);
  1014. goto err_unlock_ovs;
  1015. }
  1016. }
  1017. /* Clear stats. */
  1018. if (a[OVS_FLOW_ATTR_CLEAR])
  1019. ovs_flow_stats_clear(flow);
  1020. ovs_unlock();
  1021. if (reply)
  1022. ovs_notify(&dp_flow_genl_family, reply, info);
  1023. if (old_acts)
  1024. ovs_nla_free_flow_actions_rcu(old_acts);
  1025. return 0;
  1026. err_unlock_ovs:
  1027. ovs_unlock();
  1028. kfree_skb(reply);
  1029. err_kfree_acts:
  1030. ovs_nla_free_flow_actions(acts);
  1031. error:
  1032. return error;
  1033. }
  1034. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1035. {
  1036. struct nlattr **a = info->attrs;
  1037. struct ovs_header *ovs_header = info->userhdr;
  1038. struct net *net = sock_net(skb->sk);
  1039. struct sw_flow_key key;
  1040. struct sk_buff *reply;
  1041. struct sw_flow *flow;
  1042. struct datapath *dp;
  1043. struct sw_flow_match match;
  1044. struct sw_flow_id ufid;
  1045. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  1046. int err = 0;
  1047. bool log = !a[OVS_FLOW_ATTR_PROBE];
  1048. bool ufid_present;
  1049. ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
  1050. if (a[OVS_FLOW_ATTR_KEY]) {
  1051. ovs_match_init(&match, &key, true, NULL);
  1052. err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
  1053. log);
  1054. } else if (!ufid_present) {
  1055. OVS_NLERR(log,
  1056. "Flow get message rejected, Key attribute missing.");
  1057. err = -EINVAL;
  1058. }
  1059. if (err)
  1060. return err;
  1061. ovs_lock();
  1062. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1063. if (!dp) {
  1064. err = -ENODEV;
  1065. goto unlock;
  1066. }
  1067. if (ufid_present)
  1068. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
  1069. else
  1070. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  1071. if (!flow) {
  1072. err = -ENOENT;
  1073. goto unlock;
  1074. }
  1075. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
  1076. OVS_FLOW_CMD_GET, true, ufid_flags);
  1077. if (IS_ERR(reply)) {
  1078. err = PTR_ERR(reply);
  1079. goto unlock;
  1080. }
  1081. ovs_unlock();
  1082. return genlmsg_reply(reply, info);
  1083. unlock:
  1084. ovs_unlock();
  1085. return err;
  1086. }
  1087. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1088. {
  1089. struct nlattr **a = info->attrs;
  1090. struct ovs_header *ovs_header = info->userhdr;
  1091. struct net *net = sock_net(skb->sk);
  1092. struct sw_flow_key key;
  1093. struct sk_buff *reply;
  1094. struct sw_flow *flow = NULL;
  1095. struct datapath *dp;
  1096. struct sw_flow_match match;
  1097. struct sw_flow_id ufid;
  1098. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  1099. int err;
  1100. bool log = !a[OVS_FLOW_ATTR_PROBE];
  1101. bool ufid_present;
  1102. ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
  1103. if (a[OVS_FLOW_ATTR_KEY]) {
  1104. ovs_match_init(&match, &key, true, NULL);
  1105. err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
  1106. NULL, log);
  1107. if (unlikely(err))
  1108. return err;
  1109. }
  1110. ovs_lock();
  1111. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1112. if (unlikely(!dp)) {
  1113. err = -ENODEV;
  1114. goto unlock;
  1115. }
  1116. if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
  1117. err = ovs_flow_tbl_flush(&dp->table);
  1118. goto unlock;
  1119. }
  1120. if (ufid_present)
  1121. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
  1122. else
  1123. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  1124. if (unlikely(!flow)) {
  1125. err = -ENOENT;
  1126. goto unlock;
  1127. }
  1128. ovs_flow_tbl_remove(&dp->table, flow);
  1129. ovs_unlock();
  1130. reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
  1131. &flow->id, info, false, ufid_flags);
  1132. if (likely(reply)) {
  1133. if (likely(!IS_ERR(reply))) {
  1134. rcu_read_lock(); /*To keep RCU checker happy. */
  1135. err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
  1136. reply, info->snd_portid,
  1137. info->snd_seq, 0,
  1138. OVS_FLOW_CMD_DEL,
  1139. ufid_flags);
  1140. rcu_read_unlock();
  1141. BUG_ON(err < 0);
  1142. ovs_notify(&dp_flow_genl_family, reply, info);
  1143. } else {
  1144. netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
  1145. }
  1146. }
  1147. ovs_flow_free(flow, true);
  1148. return 0;
  1149. unlock:
  1150. ovs_unlock();
  1151. return err;
  1152. }
  1153. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1154. {
  1155. struct nlattr *a[__OVS_FLOW_ATTR_MAX];
  1156. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1157. struct table_instance *ti;
  1158. struct datapath *dp;
  1159. u32 ufid_flags;
  1160. int err;
  1161. err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
  1162. OVS_FLOW_ATTR_MAX, flow_policy, NULL);
  1163. if (err)
  1164. return err;
  1165. ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  1166. rcu_read_lock();
  1167. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1168. if (!dp) {
  1169. rcu_read_unlock();
  1170. return -ENODEV;
  1171. }
  1172. ti = rcu_dereference(dp->table.ti);
  1173. for (;;) {
  1174. struct sw_flow *flow;
  1175. u32 bucket, obj;
  1176. bucket = cb->args[0];
  1177. obj = cb->args[1];
  1178. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  1179. if (!flow)
  1180. break;
  1181. if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
  1182. NETLINK_CB(cb->skb).portid,
  1183. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1184. OVS_FLOW_CMD_GET, ufid_flags) < 0)
  1185. break;
  1186. cb->args[0] = bucket;
  1187. cb->args[1] = obj;
  1188. }
  1189. rcu_read_unlock();
  1190. return skb->len;
  1191. }
  1192. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  1193. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  1194. [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
  1195. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  1196. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  1197. [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
  1198. [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
  1199. [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
  1200. };
  1201. static const struct genl_ops dp_flow_genl_ops[] = {
  1202. { .cmd = OVS_FLOW_CMD_NEW,
  1203. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1204. .policy = flow_policy,
  1205. .doit = ovs_flow_cmd_new
  1206. },
  1207. { .cmd = OVS_FLOW_CMD_DEL,
  1208. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1209. .policy = flow_policy,
  1210. .doit = ovs_flow_cmd_del
  1211. },
  1212. { .cmd = OVS_FLOW_CMD_GET,
  1213. .flags = 0, /* OK for unprivileged users. */
  1214. .policy = flow_policy,
  1215. .doit = ovs_flow_cmd_get,
  1216. .dumpit = ovs_flow_cmd_dump
  1217. },
  1218. { .cmd = OVS_FLOW_CMD_SET,
  1219. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1220. .policy = flow_policy,
  1221. .doit = ovs_flow_cmd_set,
  1222. },
  1223. };
  1224. static struct genl_family dp_flow_genl_family __ro_after_init = {
  1225. .hdrsize = sizeof(struct ovs_header),
  1226. .name = OVS_FLOW_FAMILY,
  1227. .version = OVS_FLOW_VERSION,
  1228. .maxattr = OVS_FLOW_ATTR_MAX,
  1229. .netnsok = true,
  1230. .parallel_ops = true,
  1231. .ops = dp_flow_genl_ops,
  1232. .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
  1233. .mcgrps = &ovs_dp_flow_multicast_group,
  1234. .n_mcgrps = 1,
  1235. .module = THIS_MODULE,
  1236. };
  1237. static size_t ovs_dp_cmd_msg_size(void)
  1238. {
  1239. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1240. msgsize += nla_total_size(IFNAMSIZ);
  1241. msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
  1242. msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
  1243. msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
  1244. return msgsize;
  1245. }
  1246. /* Called with ovs_mutex. */
  1247. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1248. u32 portid, u32 seq, u32 flags, u8 cmd)
  1249. {
  1250. struct ovs_header *ovs_header;
  1251. struct ovs_dp_stats dp_stats;
  1252. struct ovs_dp_megaflow_stats dp_megaflow_stats;
  1253. int err;
  1254. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1255. flags, cmd);
  1256. if (!ovs_header)
  1257. goto error;
  1258. ovs_header->dp_ifindex = get_dpifindex(dp);
  1259. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1260. if (err)
  1261. goto nla_put_failure;
  1262. get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
  1263. if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
  1264. &dp_stats, OVS_DP_ATTR_PAD))
  1265. goto nla_put_failure;
  1266. if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
  1267. sizeof(struct ovs_dp_megaflow_stats),
  1268. &dp_megaflow_stats, OVS_DP_ATTR_PAD))
  1269. goto nla_put_failure;
  1270. if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
  1271. goto nla_put_failure;
  1272. genlmsg_end(skb, ovs_header);
  1273. return 0;
  1274. nla_put_failure:
  1275. genlmsg_cancel(skb, ovs_header);
  1276. error:
  1277. return -EMSGSIZE;
  1278. }
  1279. static struct sk_buff *ovs_dp_cmd_alloc_info(void)
  1280. {
  1281. return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
  1282. }
  1283. /* Called with rcu_read_lock or ovs_mutex. */
  1284. static struct datapath *lookup_datapath(struct net *net,
  1285. const struct ovs_header *ovs_header,
  1286. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1287. {
  1288. struct datapath *dp;
  1289. if (!a[OVS_DP_ATTR_NAME])
  1290. dp = get_dp(net, ovs_header->dp_ifindex);
  1291. else {
  1292. struct vport *vport;
  1293. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1294. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1295. }
  1296. return dp ? dp : ERR_PTR(-ENODEV);
  1297. }
  1298. static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
  1299. {
  1300. struct datapath *dp;
  1301. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1302. if (IS_ERR(dp))
  1303. return;
  1304. WARN(dp->user_features, "Dropping previously announced user features\n");
  1305. dp->user_features = 0;
  1306. }
  1307. static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
  1308. {
  1309. if (a[OVS_DP_ATTR_USER_FEATURES])
  1310. dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
  1311. }
  1312. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1313. {
  1314. struct nlattr **a = info->attrs;
  1315. struct vport_parms parms;
  1316. struct sk_buff *reply;
  1317. struct datapath *dp;
  1318. struct vport *vport;
  1319. struct ovs_net *ovs_net;
  1320. int err, i;
  1321. err = -EINVAL;
  1322. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1323. goto err;
  1324. reply = ovs_dp_cmd_alloc_info();
  1325. if (!reply)
  1326. return -ENOMEM;
  1327. err = -ENOMEM;
  1328. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1329. if (dp == NULL)
  1330. goto err_free_reply;
  1331. ovs_dp_set_net(dp, sock_net(skb->sk));
  1332. /* Allocate table. */
  1333. err = ovs_flow_tbl_init(&dp->table);
  1334. if (err)
  1335. goto err_free_dp;
  1336. dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
  1337. if (!dp->stats_percpu) {
  1338. err = -ENOMEM;
  1339. goto err_destroy_table;
  1340. }
  1341. dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS,
  1342. sizeof(struct hlist_head),
  1343. GFP_KERNEL);
  1344. if (!dp->ports) {
  1345. err = -ENOMEM;
  1346. goto err_destroy_percpu;
  1347. }
  1348. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1349. INIT_HLIST_HEAD(&dp->ports[i]);
  1350. err = ovs_meters_init(dp);
  1351. if (err)
  1352. goto err_destroy_ports_array;
  1353. /* Set up our datapath device. */
  1354. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1355. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1356. parms.options = NULL;
  1357. parms.dp = dp;
  1358. parms.port_no = OVSP_LOCAL;
  1359. parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
  1360. ovs_dp_change(dp, a);
  1361. /* So far only local changes have been made, now need the lock. */
  1362. ovs_lock();
  1363. vport = new_vport(&parms);
  1364. if (IS_ERR(vport)) {
  1365. err = PTR_ERR(vport);
  1366. if (err == -EBUSY)
  1367. err = -EEXIST;
  1368. if (err == -EEXIST) {
  1369. /* An outdated user space instance that does not understand
  1370. * the concept of user_features has attempted to create a new
  1371. * datapath and is likely to reuse it. Drop all user features.
  1372. */
  1373. if (info->genlhdr->version < OVS_DP_VER_FEATURES)
  1374. ovs_dp_reset_user_features(skb, info);
  1375. }
  1376. goto err_destroy_meters;
  1377. }
  1378. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1379. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1380. BUG_ON(err < 0);
  1381. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1382. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1383. ovs_unlock();
  1384. ovs_notify(&dp_datapath_genl_family, reply, info);
  1385. return 0;
  1386. err_destroy_meters:
  1387. ovs_unlock();
  1388. ovs_meters_exit(dp);
  1389. err_destroy_ports_array:
  1390. kfree(dp->ports);
  1391. err_destroy_percpu:
  1392. free_percpu(dp->stats_percpu);
  1393. err_destroy_table:
  1394. ovs_flow_tbl_destroy(&dp->table);
  1395. err_free_dp:
  1396. kfree(dp);
  1397. err_free_reply:
  1398. kfree_skb(reply);
  1399. err:
  1400. return err;
  1401. }
  1402. /* Called with ovs_mutex. */
  1403. static void __dp_destroy(struct datapath *dp)
  1404. {
  1405. int i;
  1406. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1407. struct vport *vport;
  1408. struct hlist_node *n;
  1409. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1410. if (vport->port_no != OVSP_LOCAL)
  1411. ovs_dp_detach_port(vport);
  1412. }
  1413. list_del_rcu(&dp->list_node);
  1414. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1415. * all ports in datapath are destroyed first before freeing datapath.
  1416. */
  1417. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1418. /* RCU destroy the flow table */
  1419. call_rcu(&dp->rcu, destroy_dp_rcu);
  1420. }
  1421. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1422. {
  1423. struct sk_buff *reply;
  1424. struct datapath *dp;
  1425. int err;
  1426. reply = ovs_dp_cmd_alloc_info();
  1427. if (!reply)
  1428. return -ENOMEM;
  1429. ovs_lock();
  1430. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1431. err = PTR_ERR(dp);
  1432. if (IS_ERR(dp))
  1433. goto err_unlock_free;
  1434. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1435. info->snd_seq, 0, OVS_DP_CMD_DEL);
  1436. BUG_ON(err < 0);
  1437. __dp_destroy(dp);
  1438. ovs_unlock();
  1439. ovs_notify(&dp_datapath_genl_family, reply, info);
  1440. return 0;
  1441. err_unlock_free:
  1442. ovs_unlock();
  1443. kfree_skb(reply);
  1444. return err;
  1445. }
  1446. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1447. {
  1448. struct sk_buff *reply;
  1449. struct datapath *dp;
  1450. int err;
  1451. reply = ovs_dp_cmd_alloc_info();
  1452. if (!reply)
  1453. return -ENOMEM;
  1454. ovs_lock();
  1455. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1456. err = PTR_ERR(dp);
  1457. if (IS_ERR(dp))
  1458. goto err_unlock_free;
  1459. ovs_dp_change(dp, info->attrs);
  1460. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1461. info->snd_seq, 0, OVS_DP_CMD_SET);
  1462. BUG_ON(err < 0);
  1463. ovs_unlock();
  1464. ovs_notify(&dp_datapath_genl_family, reply, info);
  1465. return 0;
  1466. err_unlock_free:
  1467. ovs_unlock();
  1468. kfree_skb(reply);
  1469. return err;
  1470. }
  1471. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1472. {
  1473. struct sk_buff *reply;
  1474. struct datapath *dp;
  1475. int err;
  1476. reply = ovs_dp_cmd_alloc_info();
  1477. if (!reply)
  1478. return -ENOMEM;
  1479. ovs_lock();
  1480. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1481. if (IS_ERR(dp)) {
  1482. err = PTR_ERR(dp);
  1483. goto err_unlock_free;
  1484. }
  1485. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1486. info->snd_seq, 0, OVS_DP_CMD_GET);
  1487. BUG_ON(err < 0);
  1488. ovs_unlock();
  1489. return genlmsg_reply(reply, info);
  1490. err_unlock_free:
  1491. ovs_unlock();
  1492. kfree_skb(reply);
  1493. return err;
  1494. }
  1495. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1496. {
  1497. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1498. struct datapath *dp;
  1499. int skip = cb->args[0];
  1500. int i = 0;
  1501. ovs_lock();
  1502. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1503. if (i >= skip &&
  1504. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1505. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1506. OVS_DP_CMD_GET) < 0)
  1507. break;
  1508. i++;
  1509. }
  1510. ovs_unlock();
  1511. cb->args[0] = i;
  1512. return skb->len;
  1513. }
  1514. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1515. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1516. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1517. [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
  1518. };
  1519. static const struct genl_ops dp_datapath_genl_ops[] = {
  1520. { .cmd = OVS_DP_CMD_NEW,
  1521. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1522. .policy = datapath_policy,
  1523. .doit = ovs_dp_cmd_new
  1524. },
  1525. { .cmd = OVS_DP_CMD_DEL,
  1526. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1527. .policy = datapath_policy,
  1528. .doit = ovs_dp_cmd_del
  1529. },
  1530. { .cmd = OVS_DP_CMD_GET,
  1531. .flags = 0, /* OK for unprivileged users. */
  1532. .policy = datapath_policy,
  1533. .doit = ovs_dp_cmd_get,
  1534. .dumpit = ovs_dp_cmd_dump
  1535. },
  1536. { .cmd = OVS_DP_CMD_SET,
  1537. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1538. .policy = datapath_policy,
  1539. .doit = ovs_dp_cmd_set,
  1540. },
  1541. };
  1542. static struct genl_family dp_datapath_genl_family __ro_after_init = {
  1543. .hdrsize = sizeof(struct ovs_header),
  1544. .name = OVS_DATAPATH_FAMILY,
  1545. .version = OVS_DATAPATH_VERSION,
  1546. .maxattr = OVS_DP_ATTR_MAX,
  1547. .netnsok = true,
  1548. .parallel_ops = true,
  1549. .ops = dp_datapath_genl_ops,
  1550. .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
  1551. .mcgrps = &ovs_dp_datapath_multicast_group,
  1552. .n_mcgrps = 1,
  1553. .module = THIS_MODULE,
  1554. };
  1555. /* Called with ovs_mutex or RCU read lock. */
  1556. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1557. struct net *net, u32 portid, u32 seq,
  1558. u32 flags, u8 cmd)
  1559. {
  1560. struct ovs_header *ovs_header;
  1561. struct ovs_vport_stats vport_stats;
  1562. int err;
  1563. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1564. flags, cmd);
  1565. if (!ovs_header)
  1566. return -EMSGSIZE;
  1567. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1568. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1569. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1570. nla_put_string(skb, OVS_VPORT_ATTR_NAME,
  1571. ovs_vport_name(vport)) ||
  1572. nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
  1573. goto nla_put_failure;
  1574. if (!net_eq(net, dev_net(vport->dev))) {
  1575. int id = peernet2id_alloc(net, dev_net(vport->dev));
  1576. if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
  1577. goto nla_put_failure;
  1578. }
  1579. ovs_vport_get_stats(vport, &vport_stats);
  1580. if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
  1581. sizeof(struct ovs_vport_stats), &vport_stats,
  1582. OVS_VPORT_ATTR_PAD))
  1583. goto nla_put_failure;
  1584. if (ovs_vport_get_upcall_portids(vport, skb))
  1585. goto nla_put_failure;
  1586. err = ovs_vport_get_options(vport, skb);
  1587. if (err == -EMSGSIZE)
  1588. goto error;
  1589. genlmsg_end(skb, ovs_header);
  1590. return 0;
  1591. nla_put_failure:
  1592. err = -EMSGSIZE;
  1593. error:
  1594. genlmsg_cancel(skb, ovs_header);
  1595. return err;
  1596. }
  1597. static struct sk_buff *ovs_vport_cmd_alloc_info(void)
  1598. {
  1599. return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1600. }
  1601. /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
  1602. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
  1603. u32 portid, u32 seq, u8 cmd)
  1604. {
  1605. struct sk_buff *skb;
  1606. int retval;
  1607. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1608. if (!skb)
  1609. return ERR_PTR(-ENOMEM);
  1610. retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd);
  1611. BUG_ON(retval < 0);
  1612. return skb;
  1613. }
  1614. /* Called with ovs_mutex or RCU read lock. */
  1615. static struct vport *lookup_vport(struct net *net,
  1616. const struct ovs_header *ovs_header,
  1617. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1618. {
  1619. struct datapath *dp;
  1620. struct vport *vport;
  1621. if (a[OVS_VPORT_ATTR_IFINDEX])
  1622. return ERR_PTR(-EOPNOTSUPP);
  1623. if (a[OVS_VPORT_ATTR_NAME]) {
  1624. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1625. if (!vport)
  1626. return ERR_PTR(-ENODEV);
  1627. if (ovs_header->dp_ifindex &&
  1628. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1629. return ERR_PTR(-ENODEV);
  1630. return vport;
  1631. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1632. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1633. if (port_no >= DP_MAX_PORTS)
  1634. return ERR_PTR(-EFBIG);
  1635. dp = get_dp(net, ovs_header->dp_ifindex);
  1636. if (!dp)
  1637. return ERR_PTR(-ENODEV);
  1638. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1639. if (!vport)
  1640. return ERR_PTR(-ENODEV);
  1641. return vport;
  1642. } else
  1643. return ERR_PTR(-EINVAL);
  1644. }
  1645. /* Called with ovs_mutex */
  1646. static void update_headroom(struct datapath *dp)
  1647. {
  1648. unsigned dev_headroom, max_headroom = 0;
  1649. struct net_device *dev;
  1650. struct vport *vport;
  1651. int i;
  1652. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1653. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1654. dev = vport->dev;
  1655. dev_headroom = netdev_get_fwd_headroom(dev);
  1656. if (dev_headroom > max_headroom)
  1657. max_headroom = dev_headroom;
  1658. }
  1659. }
  1660. dp->max_headroom = max_headroom;
  1661. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1662. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
  1663. netdev_set_rx_headroom(vport->dev, max_headroom);
  1664. }
  1665. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1666. {
  1667. struct nlattr **a = info->attrs;
  1668. struct ovs_header *ovs_header = info->userhdr;
  1669. struct vport_parms parms;
  1670. struct sk_buff *reply;
  1671. struct vport *vport;
  1672. struct datapath *dp;
  1673. u32 port_no;
  1674. int err;
  1675. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1676. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1677. return -EINVAL;
  1678. if (a[OVS_VPORT_ATTR_IFINDEX])
  1679. return -EOPNOTSUPP;
  1680. port_no = a[OVS_VPORT_ATTR_PORT_NO]
  1681. ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
  1682. if (port_no >= DP_MAX_PORTS)
  1683. return -EFBIG;
  1684. reply = ovs_vport_cmd_alloc_info();
  1685. if (!reply)
  1686. return -ENOMEM;
  1687. ovs_lock();
  1688. restart:
  1689. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1690. err = -ENODEV;
  1691. if (!dp)
  1692. goto exit_unlock_free;
  1693. if (port_no) {
  1694. vport = ovs_vport_ovsl(dp, port_no);
  1695. err = -EBUSY;
  1696. if (vport)
  1697. goto exit_unlock_free;
  1698. } else {
  1699. for (port_no = 1; ; port_no++) {
  1700. if (port_no >= DP_MAX_PORTS) {
  1701. err = -EFBIG;
  1702. goto exit_unlock_free;
  1703. }
  1704. vport = ovs_vport_ovsl(dp, port_no);
  1705. if (!vport)
  1706. break;
  1707. }
  1708. }
  1709. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1710. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1711. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1712. parms.dp = dp;
  1713. parms.port_no = port_no;
  1714. parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1715. vport = new_vport(&parms);
  1716. err = PTR_ERR(vport);
  1717. if (IS_ERR(vport)) {
  1718. if (err == -EAGAIN)
  1719. goto restart;
  1720. goto exit_unlock_free;
  1721. }
  1722. err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
  1723. info->snd_portid, info->snd_seq, 0,
  1724. OVS_VPORT_CMD_NEW);
  1725. if (netdev_get_fwd_headroom(vport->dev) > dp->max_headroom)
  1726. update_headroom(dp);
  1727. else
  1728. netdev_set_rx_headroom(vport->dev, dp->max_headroom);
  1729. BUG_ON(err < 0);
  1730. ovs_unlock();
  1731. ovs_notify(&dp_vport_genl_family, reply, info);
  1732. return 0;
  1733. exit_unlock_free:
  1734. ovs_unlock();
  1735. kfree_skb(reply);
  1736. return err;
  1737. }
  1738. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1739. {
  1740. struct nlattr **a = info->attrs;
  1741. struct sk_buff *reply;
  1742. struct vport *vport;
  1743. int err;
  1744. reply = ovs_vport_cmd_alloc_info();
  1745. if (!reply)
  1746. return -ENOMEM;
  1747. ovs_lock();
  1748. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1749. err = PTR_ERR(vport);
  1750. if (IS_ERR(vport))
  1751. goto exit_unlock_free;
  1752. if (a[OVS_VPORT_ATTR_TYPE] &&
  1753. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1754. err = -EINVAL;
  1755. goto exit_unlock_free;
  1756. }
  1757. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1758. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1759. if (err)
  1760. goto exit_unlock_free;
  1761. }
  1762. if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
  1763. struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1764. err = ovs_vport_set_upcall_portids(vport, ids);
  1765. if (err)
  1766. goto exit_unlock_free;
  1767. }
  1768. err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
  1769. info->snd_portid, info->snd_seq, 0,
  1770. OVS_VPORT_CMD_SET);
  1771. BUG_ON(err < 0);
  1772. ovs_unlock();
  1773. ovs_notify(&dp_vport_genl_family, reply, info);
  1774. return 0;
  1775. exit_unlock_free:
  1776. ovs_unlock();
  1777. kfree_skb(reply);
  1778. return err;
  1779. }
  1780. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1781. {
  1782. bool must_update_headroom = false;
  1783. struct nlattr **a = info->attrs;
  1784. struct sk_buff *reply;
  1785. struct datapath *dp;
  1786. struct vport *vport;
  1787. int err;
  1788. reply = ovs_vport_cmd_alloc_info();
  1789. if (!reply)
  1790. return -ENOMEM;
  1791. ovs_lock();
  1792. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1793. err = PTR_ERR(vport);
  1794. if (IS_ERR(vport))
  1795. goto exit_unlock_free;
  1796. if (vport->port_no == OVSP_LOCAL) {
  1797. err = -EINVAL;
  1798. goto exit_unlock_free;
  1799. }
  1800. err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
  1801. info->snd_portid, info->snd_seq, 0,
  1802. OVS_VPORT_CMD_DEL);
  1803. BUG_ON(err < 0);
  1804. /* the vport deletion may trigger dp headroom update */
  1805. dp = vport->dp;
  1806. if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
  1807. must_update_headroom = true;
  1808. netdev_reset_rx_headroom(vport->dev);
  1809. ovs_dp_detach_port(vport);
  1810. if (must_update_headroom)
  1811. update_headroom(dp);
  1812. ovs_unlock();
  1813. ovs_notify(&dp_vport_genl_family, reply, info);
  1814. return 0;
  1815. exit_unlock_free:
  1816. ovs_unlock();
  1817. kfree_skb(reply);
  1818. return err;
  1819. }
  1820. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1821. {
  1822. struct nlattr **a = info->attrs;
  1823. struct ovs_header *ovs_header = info->userhdr;
  1824. struct sk_buff *reply;
  1825. struct vport *vport;
  1826. int err;
  1827. reply = ovs_vport_cmd_alloc_info();
  1828. if (!reply)
  1829. return -ENOMEM;
  1830. rcu_read_lock();
  1831. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1832. err = PTR_ERR(vport);
  1833. if (IS_ERR(vport))
  1834. goto exit_unlock_free;
  1835. err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
  1836. info->snd_portid, info->snd_seq, 0,
  1837. OVS_VPORT_CMD_GET);
  1838. BUG_ON(err < 0);
  1839. rcu_read_unlock();
  1840. return genlmsg_reply(reply, info);
  1841. exit_unlock_free:
  1842. rcu_read_unlock();
  1843. kfree_skb(reply);
  1844. return err;
  1845. }
  1846. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1847. {
  1848. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1849. struct datapath *dp;
  1850. int bucket = cb->args[0], skip = cb->args[1];
  1851. int i, j = 0;
  1852. rcu_read_lock();
  1853. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1854. if (!dp) {
  1855. rcu_read_unlock();
  1856. return -ENODEV;
  1857. }
  1858. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1859. struct vport *vport;
  1860. j = 0;
  1861. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1862. if (j >= skip &&
  1863. ovs_vport_cmd_fill_info(vport, skb,
  1864. sock_net(skb->sk),
  1865. NETLINK_CB(cb->skb).portid,
  1866. cb->nlh->nlmsg_seq,
  1867. NLM_F_MULTI,
  1868. OVS_VPORT_CMD_GET) < 0)
  1869. goto out;
  1870. j++;
  1871. }
  1872. skip = 0;
  1873. }
  1874. out:
  1875. rcu_read_unlock();
  1876. cb->args[0] = i;
  1877. cb->args[1] = j;
  1878. return skb->len;
  1879. }
  1880. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1881. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1882. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1883. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1884. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1885. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1886. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1887. [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
  1888. [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
  1889. };
  1890. static const struct genl_ops dp_vport_genl_ops[] = {
  1891. { .cmd = OVS_VPORT_CMD_NEW,
  1892. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1893. .policy = vport_policy,
  1894. .doit = ovs_vport_cmd_new
  1895. },
  1896. { .cmd = OVS_VPORT_CMD_DEL,
  1897. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1898. .policy = vport_policy,
  1899. .doit = ovs_vport_cmd_del
  1900. },
  1901. { .cmd = OVS_VPORT_CMD_GET,
  1902. .flags = 0, /* OK for unprivileged users. */
  1903. .policy = vport_policy,
  1904. .doit = ovs_vport_cmd_get,
  1905. .dumpit = ovs_vport_cmd_dump
  1906. },
  1907. { .cmd = OVS_VPORT_CMD_SET,
  1908. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1909. .policy = vport_policy,
  1910. .doit = ovs_vport_cmd_set,
  1911. },
  1912. };
  1913. struct genl_family dp_vport_genl_family __ro_after_init = {
  1914. .hdrsize = sizeof(struct ovs_header),
  1915. .name = OVS_VPORT_FAMILY,
  1916. .version = OVS_VPORT_VERSION,
  1917. .maxattr = OVS_VPORT_ATTR_MAX,
  1918. .netnsok = true,
  1919. .parallel_ops = true,
  1920. .ops = dp_vport_genl_ops,
  1921. .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
  1922. .mcgrps = &ovs_dp_vport_multicast_group,
  1923. .n_mcgrps = 1,
  1924. .module = THIS_MODULE,
  1925. };
  1926. static struct genl_family * const dp_genl_families[] = {
  1927. &dp_datapath_genl_family,
  1928. &dp_vport_genl_family,
  1929. &dp_flow_genl_family,
  1930. &dp_packet_genl_family,
  1931. &dp_meter_genl_family,
  1932. #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
  1933. &dp_ct_limit_genl_family,
  1934. #endif
  1935. };
  1936. static void dp_unregister_genl(int n_families)
  1937. {
  1938. int i;
  1939. for (i = 0; i < n_families; i++)
  1940. genl_unregister_family(dp_genl_families[i]);
  1941. }
  1942. static int __init dp_register_genl(void)
  1943. {
  1944. int err;
  1945. int i;
  1946. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1947. err = genl_register_family(dp_genl_families[i]);
  1948. if (err)
  1949. goto error;
  1950. }
  1951. return 0;
  1952. error:
  1953. dp_unregister_genl(i);
  1954. return err;
  1955. }
  1956. static int __net_init ovs_init_net(struct net *net)
  1957. {
  1958. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1959. INIT_LIST_HEAD(&ovs_net->dps);
  1960. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1961. return ovs_ct_init(net);
  1962. }
  1963. static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
  1964. struct list_head *head)
  1965. {
  1966. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1967. struct datapath *dp;
  1968. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1969. int i;
  1970. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1971. struct vport *vport;
  1972. hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
  1973. if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
  1974. continue;
  1975. if (dev_net(vport->dev) == dnet)
  1976. list_add(&vport->detach_list, head);
  1977. }
  1978. }
  1979. }
  1980. }
  1981. static void __net_exit ovs_exit_net(struct net *dnet)
  1982. {
  1983. struct datapath *dp, *dp_next;
  1984. struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
  1985. struct vport *vport, *vport_next;
  1986. struct net *net;
  1987. LIST_HEAD(head);
  1988. ovs_ct_exit(dnet);
  1989. ovs_lock();
  1990. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1991. __dp_destroy(dp);
  1992. down_read(&net_rwsem);
  1993. for_each_net(net)
  1994. list_vports_from_net(net, dnet, &head);
  1995. up_read(&net_rwsem);
  1996. /* Detach all vports from given namespace. */
  1997. list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
  1998. list_del(&vport->detach_list);
  1999. ovs_dp_detach_port(vport);
  2000. }
  2001. ovs_unlock();
  2002. cancel_work_sync(&ovs_net->dp_notify_work);
  2003. }
  2004. static struct pernet_operations ovs_net_ops = {
  2005. .init = ovs_init_net,
  2006. .exit = ovs_exit_net,
  2007. .id = &ovs_net_id,
  2008. .size = sizeof(struct ovs_net),
  2009. };
  2010. static int __init dp_init(void)
  2011. {
  2012. int err;
  2013. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  2014. pr_info("Open vSwitch switching datapath\n");
  2015. err = action_fifos_init();
  2016. if (err)
  2017. goto error;
  2018. err = ovs_internal_dev_rtnl_link_register();
  2019. if (err)
  2020. goto error_action_fifos_exit;
  2021. err = ovs_flow_init();
  2022. if (err)
  2023. goto error_unreg_rtnl_link;
  2024. err = ovs_vport_init();
  2025. if (err)
  2026. goto error_flow_exit;
  2027. err = register_pernet_device(&ovs_net_ops);
  2028. if (err)
  2029. goto error_vport_exit;
  2030. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  2031. if (err)
  2032. goto error_netns_exit;
  2033. err = ovs_netdev_init();
  2034. if (err)
  2035. goto error_unreg_notifier;
  2036. err = dp_register_genl();
  2037. if (err < 0)
  2038. goto error_unreg_netdev;
  2039. return 0;
  2040. error_unreg_netdev:
  2041. ovs_netdev_exit();
  2042. error_unreg_notifier:
  2043. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  2044. error_netns_exit:
  2045. unregister_pernet_device(&ovs_net_ops);
  2046. error_vport_exit:
  2047. ovs_vport_exit();
  2048. error_flow_exit:
  2049. ovs_flow_exit();
  2050. error_unreg_rtnl_link:
  2051. ovs_internal_dev_rtnl_link_unregister();
  2052. error_action_fifos_exit:
  2053. action_fifos_exit();
  2054. error:
  2055. return err;
  2056. }
  2057. static void dp_cleanup(void)
  2058. {
  2059. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  2060. ovs_netdev_exit();
  2061. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  2062. unregister_pernet_device(&ovs_net_ops);
  2063. rcu_barrier();
  2064. ovs_vport_exit();
  2065. ovs_flow_exit();
  2066. ovs_internal_dev_rtnl_link_unregister();
  2067. action_fifos_exit();
  2068. }
  2069. module_init(dp_init);
  2070. module_exit(dp_cleanup);
  2071. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  2072. MODULE_LICENSE("GPL");
  2073. MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
  2074. MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
  2075. MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
  2076. MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
  2077. MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);
  2078. MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY);